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!

Reason: