Control a button's behaviour from a second indicator?

 

Hi guys,

I made a similar posting a couple of months ago but this one is simplified and right to the point.

I have two indicators. One creates a button and if the button is clicked an alert is shown. The second indicator comes into play when a horizontal line is drawn. It just changes the state of the button. 

But I don't want only the button's state to be changed. I also want that the alert is shown when drawing a horizontal line. Is that somehow possible? Can I activate the button's function (alert) from a second indicator?

Indicator 1:

#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

   string objName = "testbutton";
   ObjectCreate(0, objName, OBJ_BUTTON, 0, 0, 0);
   ObjectSetInteger(0, objName, OBJPROP_CORNER, CORNER_LEFT_UPPER);
   ObjectSetInteger(0, objName, OBJPROP_XDISTANCE, 200);
   ObjectSetInteger(0, objName, OBJPROP_YDISTANCE, 200);
   ObjectSetInteger(0, objName, OBJPROP_XSIZE, 200);
   ObjectSetInteger(0, objName, OBJPROP_YSIZE, 100);
   ObjectSetString(0, objName, OBJPROP_TEXT, "TEST");
   ObjectSetInteger(0, objName, OBJPROP_COLOR, ChartGetInteger(0, CHART_COLOR_FOREGROUND, 0));
   ObjectSetInteger(0, objName, OBJPROP_BGCOLOR, ChartGetInteger(0, CHART_COLOR_BACKGROUND, 0));
   ObjectSetInteger(0, objName, OBJPROP_BORDER_TYPE, BORDER_FLAT);
   ObjectSetInteger(0, objName, OBJPROP_STATE, false);
   ObjectSetString(0, objName, OBJPROP_FONT, "Tahoma");
   ObjectSetInteger(0, objName, OBJPROP_FONTSIZE, 18);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if (id==CHARTEVENT_OBJECT_CLICK) {
      if (sparam=="testbutton") {
         Alert("Button clicked");     
      }
   }
  }
//+------------------------------------------------------------------+

Indicator 2:

#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   ChartSetInteger(0, CHART_EVENT_OBJECT_CREATE, true);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if (id==CHARTEVENT_OBJECT_CREATE) {
      if (ObjectType(sparam)==OBJ_HLINE) {
         if (ObjectGetInteger(0, "testbutton", OBJPROP_STATE)==true) ObjectSetInteger(0, "testbutton", OBJPROP_STATE, false);
         else ObjectSetInteger(0, "testbutton", OBJPROP_STATE, true);
      }
   }
  }
//+------------------------------------------------------------------+
 

You can try using EventChartCustom(). This post gives you an idea how you use it.

Basically have your first indicator listening for the custom chart event, and create an alert when received.

The second indicator sends the custom chart event when the horizontal line is created.

 
honest_knave:

You can try using EventChartCustom(). This post gives you an idea how you use it.

Basically have your first indicator listening for the custom chart event, and create an alert when received.

The second indicator sends the custom chart event when the horizontal line is created.


Awesome explanation in your post! Works perfect. Thank you very much!