Need your idea how to reset bool flag when a new bar is formed

 

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?


datatime Day_Open_Time;
 

if (TimeDay(TimeCurrent()) != TimeDay(Day_Open_Time)) {

flag=false;

 Day_Open_Time = TimeCurrent();
}


 or I also try this.


But the flag did not change to false.
     if(Day_Open_Time!=iTime(Symbol(),PERIOD_D1,0)){
         flag=false;
         Day_Open_Time =iTime(Symbol(),PERIOD_D1,0);
        }
                 
 

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];
     }
//+------------------------------------------------------------------+
   
 
oops. maybe should have made the flag true when Time<=oldCandleTime;
 
Michael Charles Schefe #:
oops. maybe should have made the flag true when Time<=oldCandleTime;


Thank you!

BTW, I have been working mql4, so can you advise with mql4 ?

 
Hong Ling Mu #:


Thank you!

BTW, I have been working mql4, so can you advise with mql4 ?

i think it is the same.

 
Hong Ling Mu #: so can you advise with mql4 ?
  1. 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;
  2. Use not equal. There was a post around 2002, where a server clock was mis-set and then corrected — stopped the EA for days!

 
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.
 
William Roeder #:
  1. Michael's code
    Simplified
    MT4
    No need for flag when you return.
  2. Use not equal. There was a post around 2002, where a server clock was mis-set and then corrected — stopped the EA for days!

Thank you so much for your advice. I will try to test it!

 
Michael Charles Schefe #:
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;
 
Hong Ling Mu #:


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).

https://docs.mql4.com/series/ibars

https://www.mql5.com/en/docs/series/ibars

Documentation on MQL5: Timeseries and Indicators Access / iBars
Documentation on MQL5: Timeseries and Indicators Access / iBars
  • www.mql5.com
Returns the number of bars of a corresponding symbol and period, available in history. Parameters symbol [in]  The symbol name of the...