How to make sure function only runs at specified interval and not when its initially placed on chart
static datetime prevDispBarTime = 0; if(prevDispBarTime==0) prevDispBarTime=iTime(_Symbol,PERIOD_H4,0);
but make sure that the H4 TF is fully updated.
-
curDispBarTime = iTime(_Symbol,PERIOD_H4,0);
On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 2019.05.20
Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 2020.12.15
Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum 2019.05.31
Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 2018.07.17
SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum 2019.09.03 - Initialize your static on first tick and return.
bool isFirstTick; int OnInit() { isFirstTick = true; return(INIT_SUCCEEDED); } void OnTick() { static datetime curDispBarTime = 0; datetime preDispBarTime = curDispBarTime; curDispBarTime = iTime(_Symbol,PERIOD_H4,0); if(curDispBarTime == 0) return; // Updatings H4. if(isFirstTick){ isFirstTick = false; return; } // static initialized. bool isNewDispBar = curDispBarTime != prevDispBarTime;
but make sure that the H4 TF is fully updated.
Thanks. It's working.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
In the code below I run a function every H4. But the issue is when I put it on the chart it runs immediately for the first time before it starts running every 4 hours. How can I fix this code so it runs only when the chart 4 hour duration ends? Please note that I've only included the most relevant part of the code with respect to the question.