How to force EA to check for new values only when a bar is completed and new bar is starting - page 2

 
Revo Trades:
yes, i agree, but the first bit of the loop code

starts from 0, as you say. Or am i wrong? Sorry if I have hijacked your thread.

Marco? is my statement correct?

oops. assuming that ID10Tp is the previous bar.

Hi Revo

ID10Tp is actually the look back period :) - so if the value is 100 it will look for the highest and lowest in the past 100 bars.

 
Marco vd Heijden:

Copy paste had worked in this case.

But you changed it so that's not exactly copy paste.

Hi Marco - I agree. There is the technical  & programmatically correct way and then there is the ID10T copy and paste way that luckily worked in this case :)

I think if I post my EA here you guys are going to have a good laugh and most probably find about every rule in the book bent or thrashed :)

 

That's ok we have all been there :)

You need to start somewhere.

 

IMO, if you're going to copy and past then you're better off using a pure function which will not clutter your namespace with extra global variables. This is a cleaner way to implement new bar checking.

void OnTick()
{
   if(is_new_bar())
      Alert("NEW BAR");
}

bool is_new_bar()
{
   static datetime last_time = 0;
   datetime curr_time = (datetime)SeriesInfoInteger(
      _Symbol, PERIOD_CURRENT, SERIES_LASTBAR_DATE
   );
   if(last_time == curr_time)
      return false;
   bool result = (last_time != 0);
   last_time = curr_time;
   return result;
}
 
nicholi shen:

IMO, if you're going to copy and past then you're better off using a pure function which will not clutter your namespace with extra global variables. This is a cleaner way to implement new bar checking.

Very nice function!

 
nicholish en #:

IMO, if you're going to copy and past then you're better off using a pure function which will not clutter your namespace with extra global variables. This is a cleaner way to implement new bar checking.

                Nice code. Modification allows this so be placed somewhere (at begining) of OnTick() to that all code after it will only run at each new bar and not at each tick, as the code in OnTick() does. Other methods require the right               brace bracket ,}, to be used.

            Thank you


void OnTick()
   {
    if(!is_new_bar()){Alert("not a NEW BAR");return;}
   }

//+------------------------------------------------------------------+

bool is_new_bar()
{
   static datetime last_time = 0;
   datetime curr_time = (datetime)SeriesInfoInteger(Symbol(), PERIOD_CURRENT, SERIES_LASTBAR_DATE);
   if(last_time == curr_time) return false;
   last_time = curr_time;
   return true;
}
 

You can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed, and that tick could arrive almost at the end of a bar's duration.

For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
          MT4: New candle - MQL4 programming forum #3 (2014)
          MT5: Accessing variables - MQL4 programming forum #3 (2022)

I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
          Running EA once at the start of each bar - MQL4 programming forum (2011)