mql4 indicator code to detect change of time frame

 

Hi,

I was hoping someone could show me how to achieve this in code

thanks

 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Fernando Carreiro #:
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893

when timeframe is changed, Indicator is restarted means OnInit function is called.

To achieve this you detection you can use GlobalVariables 

//+------------------------------------------------------------------+
//|                                                    Detect_TF.mq4 |
//|                                         Copyright 2023, WIK Inc. |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, WIK Inc."
#property version   "1.00"
#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
//--- indicator buffers mapping

   if(GlobalVariableGet("tf")!=_Period)
      Alert("Timeframe changed");
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator Deinitialization function                         |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
   GlobalVariableSet("tf",_Period);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[]) {
//---

//--- return value of prev_calculated for next call
   return(rates_total);
}

//+------------------------------------------------------------------+


save current timeframe and then check OnInit if previous timeframe not equals to current timeframe.

 
Arpit T #:
#property copyright "Copyright 2023, WIK Inc."
#property version   "1.00"
#property strict
#property indicator_chart_window

ENUM_TIMEFRAMES tf=-1;

int OnInit() {
   if(tf!=_Period && tf!=-1)
      Alert("Timeframe changed");
   return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason) {
   tf=_Period;
}

How about this solution? Does it make a difference?

 
Arpit T #:

there can be as many solution as your mind can think, all works good,  👌😎

I was just wondering is there a case that you absolutely need the GlobalVariable?

 
Yashar Seyyedin #:

I was just wondering is there a case that you absolutely need the GlobalVariable?

Yes, you need to use Global Terminal Variables.

All other variables will be reinitialized, or in unknown state after a reload of the indicator has happened.

Alternatively you could use a file or an object on chart to store your data beyond the reloading of your program. Relying on a program internal variable is betting on being assigned the same physical memory from the OS. It is not going to be reliable.
 
Dominik Egert #:
Yes, you need to use Global Terminal Variables.

All other variables will be reinitialized, or in unknown state after a reload of the indicator has happened.

Alternatively you could use a file or an object on chart to store your data beyond the reloading of your program. Relying on a program internal variable is betting on being assigned the same physical memory from the OS. It is not going to be reliable.
Thanks 🙏