About time frames

 
Regarding indicators and time frames in the code.
https://www.mql5.com/en/docs/constants/chartconstants/enum_timeframes

EI: imomentum
https://www.mql5.com/en/docs/indicators/imomentum



Question:
Where noted and regarding the time frames. What is expected to happen when you select a static enum_timeframe for your indicator but switch charts to a time frame that does not match the enum_timeframe ?


I assume that no matter what time frame chart I view that the indicator will use the static enum_timeframe that I use and not a dynamic timeframe that you normally get when you use enum_timeframe "0".
Please correct me on this if I'm wrong about this.

The reason for my question is because currently my code is not alerting when switching the chart to a different timeframe such as 1hr. It only alerts when I switch back to the time frame that matches my static enum_timeframe. IE (5)

Please advise thanks

//+------------------------------------------------------------------+
//|                                                    momentum1.mq4 |
//|                                                Steve's FreeCode  |
//|          My first attempt refresher code re-education achievement|
//+------------------------------------------------------------------+
#property copyright "Steve's FreeCode "
#property link      "No Website Reference"
#property version   "1.00"
#property strict
#property indicator_chart_window



//--- input parameters
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

extern double       MomentumHigh=100.3;
extern double       MomentumLow=99.7;
extern int       Period=8;
extern bool      PopupAlert=true;
extern bool      SoundAlert=false;
bool             PopAlert=false;
bool             SouAlert=false;
extern int       TimeFrame=5;

//Ideas to work on later:
//These options below will be accessible through a user interface directly on the charts via a message box
extern string    Note_1="^^Change Period above as needed^^";
extern string    Note_2="^^Enter TimeFrame Above^^  in minutes";
extern string    Note_3="^^ Example^^  1, 5, 15, 60, 240, 1440";
extern color     TextColor=White;

int OnInit()
{
//----

       
   if(PopupAlert && (Momentum()>MomentumHigh || Momentum()<MomentumLow) && PopAlert==false){
       Alert(Symbol()," Momentum Indicator    Hour ",Hour(),"  Minute ",Minute(),"  ",DoubleToStr(Momentum(),3));
       PopAlert=true;
       //ObjectSet ("Momentum",OBJPROP_COLOR,Green);
       }
       

   if(SoundAlert && (Momentum()>MomentumHigh || Momentum()<MomentumLow) && SouAlert==false){
       PlaySound("alert.wav");
       SouAlert=true;
       }
   // I need to revisit this calculation to confirm it will work when changing high/low value options above.   
   if(Momentum()>99.9 && Momentum()<100.1){
       SouAlert=false;
       PopAlert=false;
       } 
    
//----
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+

double Momentum(){
   double moment= iMomentum(NULL,TimeFrame,Period,PRICE_CLOSE,0);
   return(moment);
   }
 
   
//+------------------------------------------------------------------+
//| 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[])
  {
//---
   //Comment(" Momentum ", DoubleToStr(Momentum(),3));
   ObjectCreate("Momentum", OBJ_LABEL,0,0,0);
   ObjectSetText("Momentum", IntegerToString(TimeFrame)+"m Momentum:("+IntegerToString(Period)+") = "+DoubleToStr(Momentum(),3),15,"Arial",TextColor);
   ObjectSet("Momentum", OBJPROP_CORNER,2);
   ObjectSet("Momentum", OBJPROP_XDISTANCE,0);
   ObjectSet("Momentum", OBJPROP_YDISTANCE,0);  
   
   //ObjectDelete("Momentum");
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Timeframes
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Timeframes
  • www.mql5.com
All predefined timeframes of charts have unique identifiers. The PERIOD_CURRENT identifier means the current period of a chart, at which a mql5-program is running.
 
Agent86: What is expected to happen when you select a static enum_timeframe for your indicator but switch charts to a time frame that does not match the enum_timeframe ?
On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing candle values. Or in your case, indicator values.
          Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

The function linked to, opens a hidden chart for the symbol/TF in question (if not already open,) thus updating history, and temporarily placing the symbol on Market Watch (if not already there,) so SymbolInfoDouble(symbol, SYMBOL_BID) or MarketInfo(symbol, MODE_BID) don't also return zero on the first call.

 
William Roeder:
On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing candle values. Or in your case, indicator values.
          Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

The function linked to, opens a hidden chart for the symbol/TF in question (if not already open,) thus updating history, and temporarily placing the symbol on Market Watch (if not already there,) so SymbolInfoDouble(symbol, SYMBOL_BID) or MarketInfo(symbol, MODE_BID) don't also return zero on the first call.

Thanks,

I think I understand this
So that explains why "TimeFrame=5" will only work on 5min chart but not on 1hr TF

I'll work on it some more, thanks again
Reason: