Keep data when time frame change.

 

Good day, I need some help please. I want the button to stay the way it is when the time frame is changed.

#property indicator_chart_window

#include <Controls\Button.mqh>
CButton TrendButton;


int OnInit()
  {    
   
    TrendButton.Create (0,"TrendButton",0,1460,62,1540 ,82);     
    TrendButton.Text("BULL");
    TrendButton.ColorBorder (clrLime); 
    TrendButton.Color  (clrLimeGreen);                                                        

   return(INIT_SUCCEEDED);
  }
 
 
 
void OnDeinit (const int reason) 
  {

  }  

int start()
{            
   return(0);
}     



void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
  
       
    color currentColor = (color) ChartGetInteger(0,CHART_COLOR_BACKGROUND);
    
    if (id == CHARTEVENT_OBJECT_CLICK && sparam == "TrendButton" )
     
       if (currentColor != clrDarkSlateGray )
          {
           ChartSetInteger (0,CHART_COLOR_BACKGROUND, clrDarkSlateGray);
           TrendButton.Text("BEAR");
           TrendButton.ColorBorder (clrRed);
           TrendButton.Color  (clrRed);                     
          }
       else
          {
           ChartSetInteger (0,CHART_COLOR_BACKGROUND, C'47,78,79' ); 
           TrendButton.Text("BULL");
           TrendButton.ColorBorder (clrLime);
           TrendButton.Color  (clrLimeGreen);                    
          }   
          
       if(id == CHARTEVENT_CHART_CHANGE)
          {         
           int rightEdge = (int)ChartGetInteger (0,CHART_WIDTH_IN_PIXELS);
           TrendButton.Move (rightEdge - 94, TrendButton.Top());
          }         
              
  } 
//____________________________________________________________________________________________________________________


 
  1. int start(){ return(0); }     

    Don't mix the event handlers. You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

  2. Indicators go through a deinit/unload/load/init cycle. Check the UninitializeReason In deinit, save what you want (in a GV). In init, check and restore what you want.
  3. EAs don't unload; just save in a variable.