Note taking indicator - Keep button color exist across different timeframe

 

Hi, I'm trying to write an indicator to be a quick note on the chart, which do the following

  - Clickable buttons which change color after clicking

  - The latest selected or deselected color will continue across all timeframe, and then clicking changing timeframe on the chart

  - The button color will be reset after every 4 hours.

  - Default color or deselected is GRAY, Selected colors are GREEN and RED, Reset period color is BLACK



My problem now are

   1. The latest selected or deselected color always reset to BLACK every time I click changing timeframe, not every 4 hours

   2. The latest selected or deselected color always change to default color GRAY every time I click changing timeframe


Here is my code

#property strict
#property indicator_chart_window

input color Buy_Color=clrLimeGreen;
input color Sell_Color=clrRed;
input color DeselectColor=clrGray;
input color NewPeriodReset=clrBlack;

double leftTime;


int OnInit()
  {

//--- H4 Buy
   ObjectCreate(0,"H4_Buy",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"H4_Buy",OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(0,"H4_Buy",OBJPROP_BGCOLOR,clrGray);
   ObjectSetInteger(0,"H4_Buy",OBJPROP_XDISTANCE,100);
   ObjectSetInteger(0,"H4_Buy",OBJPROP_YDISTANCE,100);
   ObjectSetInteger(0,"H4_Buy",OBJPROP_XSIZE,50);
   ObjectSetInteger(0,"H4_Buy",OBJPROP_YSIZE,30);
   ObjectSetString(0,"H4_Buy",OBJPROP_FONT,"Arial");
   ObjectSetString(0,"H4_Buy",OBJPROP_TEXT,"Buy");
   ObjectSetInteger(0,"H4_Buy",OBJPROP_FONTSIZE,10);

//--- H4 Sell
   ObjectCreate(0,"H4_Sell",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"H4_Sell",OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(0,"H4_Sell",OBJPROP_BGCOLOR,clrGray);
   ObjectSetInteger(0,"H4_Sell",OBJPROP_XDISTANCE,160);
   ObjectSetInteger(0,"H4_Sell",OBJPROP_YDISTANCE,100);
   ObjectSetInteger(0,"H4_Sell",OBJPROP_XSIZE,50);
   ObjectSetInteger(0,"H4_Sell",OBJPROP_YSIZE,30);
   ObjectSetString(0,"H4_Sell",OBJPROP_FONT,"Arial");
   ObjectSetString(0,"H4_Sell",OBJPROP_TEXT,"Sell");
   ObjectSetInteger(0,"H4_Sell",OBJPROP_FONTSIZE,10);
   
//--- H1 Buy
   ObjectCreate(0,"H1_Buy",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"H1_Buy",OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(0,"H1_Buy",OBJPROP_BGCOLOR,clrGray);
   ObjectSetInteger(0,"H1_Buy",OBJPROP_XDISTANCE,100);
   ObjectSetInteger(0,"H1_Buy",OBJPROP_YDISTANCE,140);
   ObjectSetInteger(0,"H1_Buy",OBJPROP_XSIZE,50);
   ObjectSetInteger(0,"H1_Buy",OBJPROP_YSIZE,30);
   ObjectSetString(0,"H1_Buy",OBJPROP_FONT,"Arial");
   ObjectSetString(0,"H1_Buy",OBJPROP_TEXT,"Buy");
   ObjectSetInteger(0,"H1_Buy",OBJPROP_FONTSIZE,10);

//--- H1 Sell
   ObjectCreate(0,"H1_Sell",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"H1_Sell",OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(0,"H1_Sell",OBJPROP_BGCOLOR,clrGray);
   ObjectSetInteger(0,"H1_Sell",OBJPROP_XDISTANCE,160);
   ObjectSetInteger(0,"H1_Sell",OBJPROP_YDISTANCE,140);
   ObjectSetInteger(0,"H1_Sell",OBJPROP_XSIZE,50);
   ObjectSetInteger(0,"H1_Sell",OBJPROP_YSIZE,30);
   ObjectSetString(0,"H1_Sell",OBJPROP_FONT,"Arial");
   ObjectSetString(0,"H1_Sell",OBJPROP_TEXT,"Sell");
   ObjectSetInteger(0,"H1_Sell",OBJPROP_FONTSIZE,10);     

//---
   return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
  {
//---
   ObjectDelete(0,"H4_Buy");
   ObjectDelete(0,"H4_Sell");
   ObjectDelete(0,"H1_Buy");
   ObjectDelete(0,"H1_Sell");

  }

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[])
  {
  

//---

   //Reset H4 Color if start new bar
   datetime H4CandleTime = iTime(Symbol(), PERIOD_H4,0);  
   leftTime=(PERIOD_H4*60)-(TimeCurrent()-H4CandleTime); 

   if(leftTime<=30)  //Reset Color if time left less than 30 seconds
   {
        Comment("New bar");
        ObjectSetInteger(0,"H4_Buy",OBJPROP_BGCOLOR,NewPeriodReset);
      }

   return(rates_total);
  }


void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if(id==CHARTEVENT_OBJECT_CLICK)
      {
         string clickedChartObject=sparam;
         
         //H4 Buy
         if(clickedChartObject=="H4_Buy")
            {
               bool selected=ObjectGetInteger(0,"H4_Buy",OBJPROP_STATE);
               if(selected)
                    {
                     Comment("H4_Buy"+" Button pressed");
                     ObjectSetInteger(0,"H4_Buy",OBJPROP_BGCOLOR,Buy_Color);

                    }
                  else // Button is not pressed
                    {
                     Comment("H4_Buy"+" Button in not pressed");
                     ObjectSetInteger(0,"H4_Buy",OBJPROP_BGCOLOR,DeselectColor);

                    }
            }
         //H4 Sell
         if(clickedChartObject=="H4_Sell")
            {
               bool selected=ObjectGetInteger(0,"H4_Sell",OBJPROP_STATE);
               if(selected)
                    {
                     Comment("H4_Sell"+" Button pressed");
                     ObjectSetInteger(0,"H4_Sell",OBJPROP_BGCOLOR,Sell_Color);
                    }
                  else // Button is not pressed
                    {
                     Comment("H4_Sell"+" Button in not pressed");
                     ObjectSetInteger(0,"H4_Sell",OBJPROP_BGCOLOR,DeselectColor);
                    }
            }
         //H1 Buy
         if(clickedChartObject=="H1_Buy")
            {
               bool selected=ObjectGetInteger(0,"H1_Buy",OBJPROP_STATE);
               if(selected)
                    {
                     Comment("H1_Buy"+" Button pressed");
                     ObjectSetInteger(0,"H1_Buy",OBJPROP_BGCOLOR,Buy_Color);
                    }
                  else // Button is not pressed
                    {
                     Comment("H1_Buy"+" Button in not pressed");
                     ObjectSetInteger(0,"H1_Buy",OBJPROP_BGCOLOR,DeselectColor);
                    }
            }
         //H1 Sell
         if(clickedChartObject=="H1_Sell")
            {
               bool selected=ObjectGetInteger(0,"H1_Sell",OBJPROP_STATE);
               if(selected)
                    {
                     Comment("H1_Sell"+" Button pressed");
                     ObjectSetInteger(0,"H1_Sell",OBJPROP_BGCOLOR,Sell_Color);
                    }
                  else // Button is not pressed
                    {
                     Comment("H1_Sell"+" Button in not pressed");
                     ObjectSetInteger(0,"H1_Sell",OBJPROP_BGCOLOR,DeselectColor);
                    }
            }
      }      
   
  }


Please kindly advice. I stuck at this all night.

Files:
iSignal2.mq4  8 kb
 
Ne30n2 2. The latest selected or deselected color always change to default color GRAY every time I click changing timeframe

Of course, they do. Changing TF results is a de-init, unload, reload, init cycle, and you lose your setting.

Remember them in Global Variables of the Terminal

 
William Roeder #:

Of course, they do. Changing TF results is a de-init, unload, reload, init cycle, and you lose your setting.

Remember them in Global Variables of the Terminal

What would be a work around for this?