- Is it possible to...(Script/Indicator question)
- Limit drawing of open positions SL & TP to timeframe of chart
- How to add objects to a chart that does NOT have focus??
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.
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; }
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)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use