try this.
//+------------------------------------------------------------------+ //| test.mq5 | //| Copyright 2024, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2024, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ static datetime oldCandleTime; datetime Time[]; bool flag; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int OnInit() { //--- ArraySetAsSeries(Time,true); ArrayResize(Time,5, 5); flag=false; //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(!(CopyTime(NULL,PERIOD_CURRENT,0,5,Time)>1) || Time[0]<=oldCandleTime) return; else { flag=false; oldCandleTime=Time[0]; } //+------------------------------------------------------------------+
Michael's code if(!(CopyTime(NULL,PERIOD_CURRENT,0,5,Time)>1) || Time[0]<=oldCandleTime) return; else { flag=false; oldCandleTime=Time[0]; }
Simplified if(!(CopyTime(NULL,PERIOD_CURRENT,0,5,Time)>1) || Time[0]<=oldCandleTime) return; oldCandleTime=Time[0];
MT4
No need for flag when you return.datetime prevTime = oldCandleTime; oldCandleTime = Time[0]; if(prevTime == oldCandleTime) return;
-
Use not equal. There was a post around 2002, where a server clock was mis-set and then corrected — stopped the EA for days!
number 2. makes sense to me, so thanks again.
I had mine like "Simplified" until recently when I showed a couple pro coders who told me to use else. I didnt agree with em, but I added the else to my own eas. But now i will change it back. Thanks.
number 2. makes sense to me, so thanks again.
I am just wondering if we use NOT Equal, then later code will not execute, right ?
WIth return code, later code will not executed?
So I want to make flag=false when New Daily candle is formed.
If new daily candle is not formed, EA should executed later code.
if(!(CopyTime(NULL,PERIOD_CURRENT,0,5,Time)>1) || Time[0]<=oldCandleTime) return;
return will result in remainder of code to be ignored, and will not execute, that is correct. So, you need to plan the order of your code.
if you use the code in williams comment, the simplified, and also the mt4 section, so use both. Then you add the signal code below that, and new signal code will NOT execute again, until oldCandleTime is updated. This will be updated on start of new candle, and then your code below that will execute.
You may want to change when the oldCandleTime is updated, depends on your trading strategy.
You should not check the bar opening time, but the number of bars using iBars.
If the number of bars has changed by 1, it means a new bar has opened.
If the number of bars has changed by more than 1, it means MT has loaded quotes and formed new bars in history. This means you need to recalculate everything again (update all indicator data or EA).

- www.mql5.com

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi, I need some ideas.
I want to reset a flag when a new daily bar is formed.
The problem is that sometimes, when a new daily bar is formed, the MT4 server stops, and the flag doesn’t reset.
Is there any way to solve this?
or I also try this.
But the flag did not change to false.