chart event chart change bugging

 
Hey guys
So I was coding something and needed to use the OnChartEvent function to detect timeframe change, so in the documentation they said to use the CHARTEVENT_CHART_CHANGE as id which i did now the code works just that i don't why it gets executed 2 times instead of once. Maybe I don't understand well the sequence of operation of that code.Here's how the code is. Thanks for any help or leads.
void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
{

        if(id == CHARTEVENT_CHART_CHANGE){ //Detect Chart Change I guess

                if(Period() == PERIOD_D1){//if new timeframee is daily execute code inside but this code execute twice, which i don't understand bcs i guess it is supposed to be executed just after tf changed to daily 

                }

        }

}
 
Bryan Djoufack Nguessong: So I was coding something and needed to use the OnChartEvent function to detect timeframe change,

Your ChartEvent shows you something on the chart changed. It does not ever give you timeframe changes.

The ChartEvent event is generated by the client terminal when a user is working with a chart:

  • keystroke, when the chart window is in focus;
  • graphical object created
  • graphical object deleted
  • mouse press on the graphical object of the chart
  • move of the graphical object using the mouse
  • end of text editing in LabelEdit.
          Client Terminal Events - MQL5 programs - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

When a timeframe change is requested, OnDeinit is called. Indicators are removed, the new chart is loaded, and your OnInit is called.

Reason: