Mql4 How to set toggle button ON OFF state?

Mql4 How to set toggle button ON OFF state?

Manage alerts

Loading saved threads...

RobertFT · External communityPost link
External question — Stack Overflow Stack Exchange Author: RobertFT Original post: https://stackoverflow.com/questions/63079656 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I'm making a line of on-chart buttons that will launch specific functions and indicators. So far so good the buttons load an indicator when pressed and delete it when the button is unpressed. But I would like to add two more functions to the button but I'm doing something wrong and it doesn't work properly and I need some help. 1st - I would like the button do be able to detect indicator on chart an automatically set the button to "ON" state in case I will drag and drop the indicator from indicator list. 2nd - when I press the button it will launch an indicator popup window and we have 2 options; 1 - "OK" which will launch the indicator on the chart and the button will stay in "ON" state. 2 - "CANCEL" which will abort lunching the indicator but it will leave the button in "ON" state. I would like the button to go back to the "OFF" state just after I press "CANCEL". Here is a peace of the code for testing. #property indicator_chart_window string sButtonName1 ="B1"; string sButtonName2 ="B2"; const string IndiName1 ="X"; const string IndiName2 ="Y"; //-------------------------------------------------------------- color UnloadedColor=clrGray; color LoadedColor=clrBlue; color currentColor; //-------------------------------------------------------------- #import "user32.dll" void keybd_event(int bVk,int bScan,int dwFlags,int dwExtraInfo); #define VK_L 0x4C #define VK_M 0x4D #define VK_MENU 0x12 #define KEYEVENTF_KEYUP 0x0002 #import //+------------------------------------------------------------------+ int OnInit() { int buttonwindow=ObjectFind(sButtonName1); int buttonwindow1=ObjectFind(sButtonName2); { if(buttonwindow<0) CreateButton1(sButtonName1,UnloadedColor,"Arial",0); if(buttonwindow1<0) CreateButton2(sButtonName2,UnloadedColor,"Arial",0); } return(INIT_SUCCEEDED); } //+----------------------------------------------------------------------------------+ //|------------------------CreateButton 1--------------------------------------------| //+----------------------------------------------------------------------------------+ void CreateButton1(string sName1,color sColor,string sFont="Arial",string sText="") { ObjectCreate(0,sName1,OBJ_BUTTON,0,0,0); ObjectSetInteger(0,sName1,OBJPROP_XDISTANCE,725); ObjectSetInteger(0,sName1,OBJPROP_YDISTANCE,0); ObjectSetInteger(0,sName1,OBJPROP_XSIZE,50); ObjectSetInteger(0,sName1,OBJPROP_YSIZE,50); ObjectSetInteger(0,sName1,OBJPROP_CORNER,4); ObjectSetString(0,sName1,OBJPROP_TEXT,"X"); ObjectSetInteger(0,sName1,OBJPROP_COLOR,sColor); ObjectSetString(0,sName1,OBJPROP_FONT,sFont); ObjectSetInteger(0,sName1,OBJPROP_FONTSIZE,26); ObjectSetInteger(0,sName1,OBJPROP_STATE,false); } //+----------------------------------------------------------------------------------+ //|------------------------CreateButton 2--------------------------------------------| //+----------------------------------------------------------------------------------+ void CreateButton2(string sName2,color sColor,string sFont="Arial",string sText="") { ObjectCreate(0,sName2,OBJ_BUTTON,0,0,0); ObjectSetInteger(0,sName2,OBJPROP_XDISTANCE,775); ObjectSetInteger(0,sName2,OBJPROP_YDISTANCE,0); ObjectSetInteger(0,sName2,OBJPROP_XSIZE,50); ObjectSetInteger(0,sName2,OBJPROP_YSIZE,50); ObjectSetInteger(0,sName2,OBJPROP_CORNER,4); ObjectSetString(0,sName2,OBJPROP_TEXT,"Y"); ObjectSetInteger(0,sName2,OBJPROP_COLOR,sColor); ObjectSetString(0,sName2,OBJPROP_FONT,sFont); ObjectSetInteger(0,sName2,OBJPROP_FONTSIZE,26); ObjectSetInteger(0,sName2,OBJPROP_STATE,false); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { //+----------------------------------------------------------------------------------+ //+--------------------------------------- BUTTON 1 ---------------------------------+ //+----------------------------------------------------------------------------------+ { { if(id==CHARTEVENT_OBJECT_CLICK && sparam==sButtonName1) { currentColor=ObjectGetInteger(0,sButtonName1,OBJPROP_COLOR); if(currentColor==UnloadedColor) { keybd_event(VK_MENU,0,0,0); keybd_event(VK_L,0,0,0); keybd_event(VK_L,0,KEYEVENTF_KEYUP,0); keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0); ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,LoadedColor); return; } else if(currentColor==LoadedColor) { ChartIndicatorDelete(0,0,IndiName1); ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,UnloadedColor); } } } //+----------------------------------------------------------------------------------+ //+---------------------------------------BUTTON 2-----------------------------------+ //+----------------------------------------------------------------------------------+ { if(id==CHARTEVENT_OBJECT_CLICK && sparam==sButtonName2) { currentColor=ObjectGetInteger(0,sButtonName2,OBJPROP_COLOR); if(currentColor==UnloadedColor) { keybd_event(VK_MENU,0,0,0); keybd_event(VK_M,0,0,0); keybd_event(VK_M,0,KEYEVENTF_KEYUP,0); keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0); ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,LoadedColor); return; } else if(currentColor==LoadedColor) { ChartIndicatorDelete(0,0,IndiName2); ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,UnloadedColor); } } } } /* //+-------------------------------------NOT WORKING-----------------------------------+ //------------------------------ DETECT INDICATOR ON THE CHART------------------------------- { int indicators_total = ChartIndicatorsTotal(0,0); for(int i = 0; indicators_total > i; i++) //+------------------------- INDICATOR 1 DETECTED ----------------------------------- { if(ChartIndicatorName(0,0,i)==IndiName1) { currentColor=ObjectGetInteger(0,sButtonName1,OBJPROP_COLOR); if(currentColor==UnloadedColor) { ObjectSetInteger(0,sButtonName1,OBJPROP_STATE,true); ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,LoadedColor); } } //+------------------------- INDICATOR 1 NOT DETECTED ----------------------------------- if(!(ChartIndicatorName(0,0,i)==IndiName1)) { currentColor=ObjectGetInteger(0,sButtonName1,OBJPROP_COLOR); if(currentColor==LoadedColor) { ObjectSetInteger(0,sButtonName1,OBJPROP_STATE,false); ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,UnloadedColor); } } //+------------------------- INDICATOR 2 DETECTED----------------------------------- if(ChartIndicatorName(0,0,i)==IndiName2) { currentColor=ObjectGetInteger(0,sButtonName2,OBJPROP_COLOR); if(currentColor==UnloadedColor) { ObjectSetInteger(0,sButtonName2,OBJPROP_STATE,true); ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,LoadedColor); } } //+------------------------- INDICATOR 2 NOT DETECTED ----------------------------------- if(!(ChartIndicatorName(0,0,i)==IndiName2)) { currentColor=ObjectGetInteger(0,sButtonName2,OBJPROP_COLOR); if(currentColor==LoadedColor) { ObjectSetInteger(0,sButtonName2,OBJPROP_STATE,false); ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,UnloadedColor); } } } } //+------------------------------------------------------------------------------------- */ } //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { return(rates_total); } //+------------------------------------------------------------------+
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: RobertFT Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/questions/63079656 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I'm making a line of on-chart buttons that will launch specific functions and indicators. So far so good the buttons load an indicator when pressed and delete it when the button is unpressed. But I would like to add two more functions to the button but I'm doing something wrong and it doesn't work properly and I need some help. 1st - I would like the button do be able to detect indicator on chart an automatically set the button to "ON" state in case I will drag and drop the indicator from indicator list. 2nd - when I press the button it will launch an indicator popup window and we have 2 options; 1 - "OK" which will launch the indicator on the chart and the button will stay in "ON" state. 2 - "CANCEL" which will abort lunching the indicator but it will leave the button in "ON" state. I would like the button to go back to the "OFF" state just after I press "CANCEL". Here is a peace of the code for testing. #property indicator_chart_window string sButtonName1 ="B1"; string sButtonName2 ="B2"; const string IndiName1 ="X"; const string IndiName2 ="Y"; //-------------------------------------------------------------- color UnloadedColor=clrGray; color LoadedColor=clrBlue; color currentColor; //-------------------------------------------------------------- #import "user32.dll" void keybd_event(int bVk,int bScan,int dwFlags,int dwExtraInfo); #define VK_L 0x4C #define VK_M 0x4D #define VK_MENU 0x12 #define KEYEVENTF_KEYUP 0x0002 #import //+------------------------------------------------------------------+ int OnInit() { int buttonwindow=ObjectFind(sButtonName1); int buttonwindow1=ObjectFind(sButtonName2); { if(buttonwindow<0) CreateButton1(sButtonName1,UnloadedColor,"Arial",0); if(buttonwindow1<0) CreateButton2(sButtonName2,UnloadedColor,"Arial",0); } return(INIT_SUCCEEDED); } //+----------------------------------------------------------------------------------+ //|------------------------CreateButton 1--------------------------------------------| //+----------------------------------------------------------------------------------+ void CreateButton1(string sName1,color sColor,string sFont="Arial",string sText="") { ObjectCreate(0,sName1,OBJ_BUTTON,0,0,0); ObjectSetInteger(0,sName1,OBJPROP_XDISTANCE,725); ObjectSetInteger(0,sName1,OBJPROP_YDISTANCE,0); ObjectSetInteger(0,sName1,OBJPROP_XSIZE,50); ObjectSetInteger(0,sName1,OBJPROP_YSIZE,50); ObjectSetInteger(0,sName1,OBJPROP_CORNER,4); ObjectSetString(0,sName1,OBJPROP_TEXT,"X"); ObjectSetInteger(0,sName1,OBJPROP_COLOR,sColor); ObjectSetString(0,sName1,OBJPROP_FONT,sFont); ObjectSetInteger(0,sName1,OBJPROP_FONTSIZE,26); ObjectSetInteger(0,sName1,OBJPROP_STATE,false); } //+----------------------------------------------------------------------------------+ //|------------------------CreateButton 2--------------------------------------------| //+----------------------------------------------------------------------------------+ void CreateButton2(string sName2,color sColor,string sFont="Arial",string sText="") { ObjectCreate(0,sName2,OBJ_BUTTON,0,0,0); ObjectSetInteger(0,sName2,OBJPROP_XDISTANCE,775); ObjectSetInteger(0,sName2,OBJPROP_YDISTANCE,0); ObjectSetInteger(0,sName2,OBJPROP_XSIZE,50); ObjectSetInteger(0,sName2,OBJPROP_YSIZE,50); ObjectSetInteger(0,sName2,OBJPROP_CORNER,4); ObjectSetString(0,sName2,OBJPROP_TEXT,"Y"); ObjectSetInteger(0,sName2,OBJPROP_COLOR,sColor); ObjectSetString(0,sName2,OBJPROP_FONT,sFont); ObjectSetInteger(0,sName2,OBJPROP_FONTSIZE,26); ObjectSetInteger(0,sName2,OBJPROP_STATE,false); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { //+----------------------------------------------------------------------------------+ //+--------------------------------------- BUTTON 1 ---------------------------------+ //+----------------------------------------------------------------------------------+ { { if(id==CHARTEVENT_OBJECT_CLICK && sparam==sButtonName1) { currentColor=ObjectGetInteger(0,sButtonName1,OBJPROP_COLOR); if(currentColor==UnloadedColor) { keybd_event(VK_MENU,0,0,0); keybd_event(VK_L,0,0,0); keybd_event(VK_L,0,KEYEVENTF_KEYUP,0); keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0); ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,LoadedColor); return; } else if(currentColor==LoadedColor) { ChartIndicatorDelete(0,0,IndiName1); ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,UnloadedColor); } } } //+----------------------------------------------------------------------------------+ //+---------------------------------------BUTTON 2-----------------------------------+ //+----------------------------------------------------------------------------------+ { if(id==CHARTEVENT_OBJECT_CLICK && sparam==sButtonName2) { currentColor=ObjectGetInteger(0,sButtonName2,OBJPROP_COLOR); if(currentColor==UnloadedColor) { keybd_event(VK_MENU,0,0,0); keybd_event(VK_M,0,0,0); keybd_event(VK_M,0,KEYEVENTF_KEYUP,0); keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0); ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,LoadedColor); return; } else if(currentColor==LoadedColor) { ChartIndicatorDelete(0,0,IndiName2); ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,UnloadedColor); } } } } /* //+-------------------------------------NOT WORKING-----------------------------------+ //------------------------------ DETECT INDICATOR ON THE CHART------------------------------- { int indicators_total = ChartIndicatorsTotal(0,0); for(int i = 0; indicators_total > i; i++) //+------------------------- INDICATOR 1 DETECTED ----------------------------------- { if(ChartIndicatorName(0,0,i)==IndiName1) { currentColor=ObjectGetInteger(0,sButtonName1,OBJPROP_COLOR); if(currentColor==UnloadedColor) { ObjectSetInteger(0,sButtonName1,OBJPROP_STATE,true); ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,LoadedColor); } } //+------------------------- INDICATOR 1 NOT DETECTED ----------------------------------- if(!(ChartIndicatorName(0,0,i)==IndiName1)) { currentColor=ObjectGetInteger(0,sButtonName1,OBJPROP_COLOR); if(currentColor==LoadedColor) { ObjectSetInteger(0,sButtonName1,OBJPROP_STATE,false); ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,UnloadedColor); } } //+------------------------- INDICATOR 2 DETECTED----------------------------------- if(ChartIndicatorName(0,0,i)==IndiName2) { currentColor=ObjectGetInteger(0,sButtonName2,OBJPROP_COLOR); if(currentColor==UnloadedColor) { ObjectSetInteger(0,sButtonName2,OBJPROP_STATE,true); ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,LoadedColor); } } //+------------------------- INDICATOR 2 NOT DETECTED ----------------------------------- if(!(ChartIndicatorName(0,0,i)==IndiName2)) { currentColor=ObjectGetInteger(0,sButtonName2,OBJPROP_COLOR); if(currentColor==LoadedColor) { ObjectSetInteger(0,sButtonName2,OBJPROP_STATE,false); ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,UnloadedColor); } } } } //+------------------------------------------------------------------------------------- */ } //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { return(rates_total); } //+------------------------------------------------------------------+

Cancel quote

Checking account access…
Mql4 How to set toggle button ON OFF state? | Forex.com.bd