If I change timeframe button status changed auto

 
Hi,
I'm new to mql4 programing I try my best to learn it.
I did some modification to an alerting to have it as a buttons in the chart window.

The problem that I face is when " I active the alert button and change timeframe the status of the alert button changed to false".


#property indicator_chart_window

      extern int Button_CORNER = 1;
      extern int Button_X = 165;
      extern int Button_Y = 278;
      int Button_XSIZE = 80;
      int Button_YSIZE = 18;
      extern int Button_Text_FontSize =7;
      string Sto_Button   = "Button_toggle";
      bool Alert_On   = True;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
       Toggle();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//---

    double M_0 = iStochastic(NULL,0,15,5,5,MODE_SMA,0,MODE_MAIN,0);
    double M_1 = iStochastic(NULL,0,15,5,5,MODE_SMA,0,MODE_MAIN,1);
    double S_0 = iStochastic(NULL,0,15,5,5,MODE_SMA,0,MODE_SIGNAL,0);
    double S_1 = iStochastic(NULL,0,15,5,5,MODE_SMA,0,MODE_SIGNAL,1);
//--------------------------------------------------------------------
      double BuyIF  = M_1 < S_1 && M_0 >= S_0;
      double SellIF = M_1 > S_1 && M_0 <= S_0;
//--------------------------------------------------------------------------------------------

   if( BuyIF && Alert_On == true  && Volume[0]==1)
      {
       Alert("Sto Cross Up    "+ Symbol(),"   ", TimeToStr(CurTime(),TIME_DATE | TIME_MINUTES),"");
      }
   if( SellIF && Alert_On == true  && Volume[0]==1)
      {
       Alert("Sto Cross Dn    "+ Symbol(),"   ", TimeToStr(CurTime(),TIME_DATE | TIME_MINUTES),"");
      }

//--- return value of prev_calculated for next call
   return(0);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---

   if(sparam==Sto_Button) Toggle();

//---
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

void Toggle()
   {
      uchar Button = OBJ_BUTTON;
   
      CreateButton(Sto_Button,CharToStr(Button));
      Alert_On = !Alert_On;
   }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

void CreateButton(string Button,string sText = "")
{
   if(ObjectFind(Button)< 0)
     {
      ObjectCreate(0,Button,OBJ_BUTTON,0,0,0);
     }
      ObjectSetString (0,Button,OBJPROP_TEXT,sText);
      ObjectSetInteger(0,Button,OBJPROP_XDISTANCE,Button_X);
      ObjectSetInteger(0,Button,OBJPROP_YDISTANCE,Button_Y);
      ObjectSetInteger(0,Button,OBJPROP_XSIZE,Button_XSIZE);
      ObjectSetInteger(0,Button,OBJPROP_YSIZE,Button_YSIZE);
      ObjectSetInteger(0,Button,OBJPROP_CORNER,Button_CORNER);
      ObjectSetInteger(0,Button,OBJPROP_COLOR, clrAliceBlue);
      ObjectSetInteger(0,Button,OBJPROP_FONTSIZE,Button_Text_FontSize);
      ObjectSetInteger(0,Button,OBJPROP_BORDER_COLOR,Red);
      ObjectSetInteger(0,Button,OBJPROP_BORDER_TYPE,BORDER_FLAT);
   long result;
      ChartGetInteger(0,CHART_COLOR_BACKGROUND,0,result);
   color cBack = (color) result;
      ObjectSetInteger(0,Button,OBJPROP_BGCOLOR, cBack);
      ObjectSetInteger(0,Button,OBJPROP_HIDDEN, false);
      ObjectSetInteger(0,Button,OBJPROP_STATE,false);
   if(Alert_On ==0 )
      ObjectSetString(0,Button,OBJPROP_TEXT,"Sto Alert ON");
   else
      ObjectSetString(0,Button,OBJPROP_TEXT,"Sto Alert OFF");
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int deinit()
{
 
   ObjectDelete("Button_toggle");
 
   return(0);
}
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(ObjectFind(Sto_Button)< 0)
       Toggle();
//---
   return(INIT_SUCCEEDED);
  }

.

 

Hi GumRai,

Thank you for your reply. I test it but it still if i change timeframe the button change from "Sto Aleert ON" to "Sto Aleert ON""Sto Aleert OFF"?


   if(Alert_On ==0 )
      ObjectSetString(0,Button,OBJPROP_TEXT,"Sto Alert ON");
   else
      ObjectSetString(0,Button,OBJPROP_TEXT,"Sto Alert OFF");
}
 

The problem that I face is when " I active the alert button and change timeframe the status of the alert button changed to false".

I thought that you were referring to the button state. ie. pressed or not pressed
 

Yes Plese see the attacged photo



 

Try these changes

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
     if(ObjectFind(Sto_Button)< 0)
       Toggle();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---

   //if(sparam==Sto_Button) Toggle();
   if(ObjectGetInteger(0,Sto_Button,OBJPROP_STATE))
      {
      Alert_On = true;
      ObjectSetString(0,Sto_Button,OBJPROP_TEXT,"Sto Alert ON");
      }
   else
      {
      Alert_On = false;
      ObjectSetString(0,Sto_Button,OBJPROP_TEXT,"Sto Alert OFF");
      }

//---
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

void OnDeinit(const int reason)
  {
  
  if(reason==REASON_REMOVE || reason==REASON_RECOMPILE)
    ObjectDelete("Button_toggle");
  return;
  }

OnDeinit to replace

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int deinit()
{
 
   ObjectDelete("Button_toggle");
 
   return(0);
}
 
thank you very much