Is it possible to make alerts occur on only specific timeframes?

 
I was thinking about this, and wondering if there's actually a limitation in the API.
Let's say I'm currently on the 5M timeframe, the indicator should only alert on the H1 timefreame with the Alert() function regardless of what timeframe I'm currently on. Is there a way to do this?
It seems that OnDeinit is called when changing timeframe, so I'm assuming such a case isn't possible on the same window. The workaround would probably be to have the same indicator on multiple charts with different timeframes active on each chart
 
Hello. 
What do you have monitored in m5?
 
phadeI was thinking about this, and wondering if there's actually a limitation in the API.
Let's say I'm currently on the 5M timeframe, the indicator should only alert on the H1 timefreame with the Alert() function regardless of what timeframe I'm currently on. Is there a way to do this?
It seems that OnDeinit is called when changing timeframe, so I'm assuming such a case isn't possible on the same window. The workaround would probably be to have the same indicator on multiple charts with different timeframes active on each chart
That is up to you to code in your indicator. It has nothing to do with any "API" or "limitation".
 
Fernando Carreiro #:
That is up to you to code in your indicator. It has nothing to do with any "API" or "limitation".
Well I'm wondering how people would approach this, because I can't code it if it's an impossibility.
I guess an external script would need to be made to rewrite how timeframes work, it doesn't sound like it's worth the effort.
 
phade #:
Well I'm wondering how people would approach this, because I can't code it if it's an impossibility.
I guess an external script would need to be made to rewrite how timeframes work, it doesn't sound like it's worth the effort.

As Fernando mentioned, it's as per the code in your indicator.

Just to elaborate you can code as iTime(_Symbol, PERIOD_H1, 0) or iTime(_Symbol, PERIOD_CURRENT, 0). I hope it explains.

 
Keni Chetankumar Gajanan - #:
iTime

I didn't think of making conditions with datetimes, but yes that could work. That's why I like to make these topics as I could be stubbornly thinking about it another way while someone else might be thinking clearer

 
Keni Chetankumar Gajanan - #: As Fernando mentioned, it's as per the code in your indicator. Just to elaborate you can code as iTime(_Symbol, PERIOD_H1, 0) or iTime(_Symbol, PERIOD_CURRENT, 0). I hope it explains.

You don't even need to do that. Just rely on the normal time[] passed by on OnCalculate() and synchronise it to the required alert time-frame.

For example, for an M5 chart, and a H1 alert, just monitor for when ( time[i] / 60 ) changes value.

 

I have the day off, so I made this script just now with a function that obtains the last open time of the bar (with chosen timeframe) and compares current time with the last bar time to return true when a chosen number of minutes elapses:


//|                                                 StrictAlerts.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_plots 0


bool my_signal = true;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
  
//+------------------------------------------------------------------+
//| 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[])
{
      if (AlertOnTimeframe(PERIOD_H1, 60) && my_signal){

         Alert("Hello world");
      }
   


   return(rates_total);
}



bool AlertOnTimeframe(ENUM_TIMEFRAMES selectedTimeframe, int the_minutes){

      datetime currentTime = TimeCurrent();
      datetime lastBarOpenTime = iTime(Symbol(), selectedTimeframe, 0);
      bool readyForAlert = false;
      
      
      int timeDifference = (int)(currentTime - lastBarOpenTime)/60; //get the difference in minutes
      
      if(timeDifference == the_minutes){
         readyForAlert = true;
      }
      else{
         readyForAlert = false;
      } 
      
      return readyForAlert;
}
Files:
 
Fernando Carreiro #:

You don't even need to do that. Just rely on the normal time[] passed by on OnCalculate() and synchronise it to the required alert time-frame.

For example, for an M5 chart, and a H1 alert, just monitor for when ( time[i] / 60 ) changes value.

I haven't done enough experiments to check, but is this time buffer from OnCalculate dealing with the open time of the bar on the current selected timeframe only? It might be capped to the current timeframe selected, but as said, I haven't done many tests with it (I normally use it in loops, but also use it rarely)

Reason: