Request for help on toggle button with alert

 

Hello everyone,

I made an indicator that counts the number of pips and the profit from entry. I found code to make a toggle button, and I incorporated that into the indicator to toggle the alert On/Off.

When I place the indicator on a chart, the alert is off, and that is actually the case, so that is good. When I click the button, the button changes color and the alert is switched to On, both are correct, but when I click the button again, the button changes back the color, but the alert stays on.

Can anyone tell me how to make this work correctly, please?

Files:
 
ReactoFX:

Hello everyone,

I made an indicator that counts the number of pips and the profit from entry. I found code to make a toggle button, and I incorporated that into the indicator to toggle the alert On/Off.

When I place the indicator on a chart, the alert is off, and that is actually the case, so that is good. When I click the button, the button changes color and the alert is switched to On, both are correct, but when I click the button again, the button changes back the color, but the alert stays on.

Can anyone tell me how to make this work correctly, please?

There seems to be several issues with this... I don't have the time to do a full debug, but I can tell right away that you're attempting to create a new button every-time Toggle() is called. You only need to create the button once in OnInit.

 

The easiest way to manage event-handling with use of a button is to encapsulate your custom criteria within a button derived from the standard library. You don't have to use this method, but to better understand what you should be doing I'd recommend setting a break-point at OnInit and step through the process in the debugger. 

#property indicator_chart_window
//+------------------------------------------------------------------+
#include <ChartObjects\ChartObjectsTxtControls.mqh>

class AlertButton : public CChartObjectButton
{
public:
   bool Create()
   {
      if(!Create(0,"__alerts__",0,0,0,75,30))
         return false;
      Corner(CORNER_LEFT_LOWER);
      X_Distance(1);
      Y_Distance(31);
      Description("ALERTS");
      return true;
   }
   void OnChartEvent(int id,string sparam)
   {
      if(sparam == m_name)
         if(State())
            BackColor(clrGreen);
         else
            BackColor(clrLightGray);
   }
};

//+------------------------------------------------------------------+
AlertButton button;
//+------------------------------------------------------------------+
int OnInit()
{
   if(!button.Create())
      return INIT_FAILED;
  
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnTimer()
{
   
}
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   button.OnChartEvent(id,sparam);
}
//+------------------------------------------------------------------+
int start()
{ 
   if(true && button.State()) //alert conditions
      Alert("Alert conditions == true && button.State() == true");
   return 0; 
}
 
nicholishen:

There seems to be several issues with this... I don't have the time to do a full debug, but I can tell right away that you're attempting to create a new button every-time Toggle() is called. You only need to create the button once in OnInit.

 

The easiest way to manage event-handling with use of a button is to encapsulate your custom criteria within a button derived from the standard library. You don't have to use this method, but to better understand what you should be doing I'd recommend setting a break-point at OnInit and step through the process in the debugger. 

Hi Nicholishen,


Thanks very much for your help, I'm going to try your reply, but I understand that solving this is more difficult than changing some lines of code. I will see how far I can take it.


Have a nice day

 
nicholishen:

There seems to be several issues with this... I don't have the time to do a full debug, but I can tell right away that you're attempting to create a new button every-time Toggle() is called. You only need to create the button once in OnInit.

 

The easiest way to manage event-handling with use of a button is to encapsulate your custom criteria within a button derived from the standard library. You don't have to use this method, but to better understand what you should be doing I'd recommend setting a break-point at OnInit and step through the process in the debugger. 

Hi Nicholishen,


I have tried several times to make the button work correctly with the code you suggested, but every time I end up with multiple remarks about implicit conversions. I think I don't understand the concept correctly.

Would you please be so kind to have a go at debugging the code if and when you can find the time?

Have a nice day,


ReactoFX

Reason: