On-chart buttons question.

 
 

Dear experienced coders, I'm new in MQL and I need some guidance. And sorry for my English it's not my language.

The thing is I'm making a line of on-chart buttons that will launch specific indicators. So far so good the button loads an indicator 'X' when pressed and deletes it when the button is unpressed, but I would like to add another function that will detect indicator 'X' on chart an automatically set the button to pressed state in case I will drag and drop the 'X' indicator from indicator list.
I was trying to do that using "ObjectFind" but no luck.
Can someone please show me the proper way how to do it?
Thank you.

//+------------------------------------------------------------------+
#property indicator_chart_window
string sButtonName="Support_Resistance";
color  UnloadedColor=clrGray;
color  LoadedColor=clrBlue;
color  currentColor;
#import "user32.dll"
void keybd_event(int bVk,int bScan,int dwFlags,int dwExtraInfo);
#define  VK_Q 0x51
#define  VK_MENU 0x12
#define  KEYEVENTF_KEYUP 0x0002
//+------------------------------------------------------------------+
int OnInit()
  {   int buttonwindow=ObjectFind(sButtonName);
   if(buttonwindow<0)
      CreateButton(sButtonName,UnloadedColor,"Arial",0);
   return(INIT_SUCCEEDED); }
//+------------------------------------------------------------------+
void CreateButton(string sName,color sColor,string sFont="Arial",string sText="")
  {
   ObjectCreate(0,sName,OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,sName,OBJPROP_XDISTANCE,238);
   ObjectSetInteger(0,sName,OBJPROP_YDISTANCE,0);
   ObjectSetInteger(0,sName,OBJPROP_XSIZE,25);
   ObjectSetInteger(0,sName,OBJPROP_YSIZE,25);
   ObjectSetInteger(0,sName,OBJPROP_CORNER,4);
   ObjectSetString(0,sName,OBJPROP_TEXT,"S/R");
   ObjectSetInteger(0,sName,OBJPROP_COLOR,sColor);
   ObjectSetString(0,sName,OBJPROP_FONT,sFont);
   ObjectSetInteger(0,sName,OBJPROP_FONTSIZE,9);
  }
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)

  {   if(id==CHARTEVENT_OBJECT_CLICK && sparam==sButtonName){
      currentColor=ObjectGetInteger(0,sButtonName,OBJPROP_COLOR);
      if(currentColor==UnloadedColor)
        {
         keybd_event(VK_MENU,0,0,0);
         keybd_event(VK_Q,0,0,0);
         keybd_event(VK_Q,0,KEYEVENTF_KEYUP,0);
         keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0);
         ObjectSetInteger(0,sButtonName,OBJPROP_COLOR,LoadedColor);
         return;}
      else
         if(currentColor==LoadedColor) {
            ChartIndicatorDelete(0,0,"Support_Resistance");
            ObjectSetInteger(0,sButtonName,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);}
//+------------------------------------------------------------------+
 
Use ChartIndicatorsTotal() and ChartIndicatorName().
 
Alain Verleyen:
Use ChartIndicatorsTotal() and ChartIndicatorName().


Thank you Alain. It seems to work, but could you tell me if I did it the right way ?



//+------------------------------------------------------------------+
#property indicator_chart_window

string sButtonName1="Support_Resistance";
string IndicatorName1="Support_Resistance";

color  UnloadedColor=clrGray;
color  LoadedColor=clrBlue;
color  currentColor;

#import "user32.dll"

void keybd_event(int bVk,int bScan,int dwFlags,int dwExtraInfo);

#define  VK_Q 0x51
#define  VK_MENU 0x12
#define  KEYEVENTF_KEYUP 0x0002
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   int buttonwindow=ObjectFind(sButtonName1);
   if(buttonwindow<0)
      CreateButton(sButtonName1,UnloadedColor,"Arial",0);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CreateButton(string sName,color sColor,string sFont="Arial",string sText="")
  {
   ObjectCreate(0,sName,OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,sName,OBJPROP_XDISTANCE,238);
   ObjectSetInteger(0,sName,OBJPROP_YDISTANCE,0);
   ObjectSetInteger(0,sName,OBJPROP_XSIZE,25);
   ObjectSetInteger(0,sName,OBJPROP_YSIZE,25);
   ObjectSetInteger(0,sName,OBJPROP_CORNER,4);
   ObjectSetString(0,sName,OBJPROP_TEXT,"S/R");
   ObjectSetInteger(0,sName,OBJPROP_COLOR,sColor);
   ObjectSetString(0,sName,OBJPROP_FONT,sFont);
   ObjectSetInteger(0,sName,OBJPROP_FONTSIZE,9);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   if(id==CHARTEVENT_OBJECT_CLICK && sparam==sButtonName1)
     {
      currentColor=ObjectGetInteger(0,sButtonName1,OBJPROP_COLOR);
      if(currentColor==UnloadedColor)
        {
         PlaySound("Click ON.wav");
         keybd_event(VK_MENU,0,0,0);
         keybd_event(VK_Q,0,0,0);

         keybd_event(VK_Q,0,KEYEVENTF_KEYUP,0);
         keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0);

         ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,LoadedColor);
         return;
        }
      else
         if(currentColor==LoadedColor)
           {
            PlaySound("Click OFF.wav");
            ChartIndicatorDelete(0,0,IndicatorName1);
            ObjectSetInteger(0,sButtonName1,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[])
  {
//-------------------------------------------------------------------
   int indicators_total = ChartIndicatorsTotal(0,0);
   for(int i = 0; i < indicators_total; i++)

      if(ChartIndicatorName(0,0,i)==IndicatorName1)
        {
         Print("INDICATOR found");  // TEST
         ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,LoadedColor);
         ObjectSetInteger(0,sButtonName1,OBJPROP_STATE,true);
        }
      else
        {
         Print("INDICATOR NOT found");  // TEST
         ObjectSetInteger(0,sButtonName1,OBJPROP_STATE,false);
         ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,UnloadedColor);
        }
//+------------------------------------------------------------------+
   return(rates_total);
  }
//+------------------------------------------------------------------+
Reason: