When would this statement be false?

 

Ok. So, in my quest to find the perfect new candle code, I tried something new. Price based!

if (Open[0]==Close[0] && Close[0]==High[0] && High[0]==Low[0]) Alert("New Bar!");

I put this in an EA and loaded on a 1 minute chart. Worked each time.

Now, what I'm wondering is under what condition would this 'if' statement be false on the 1st tick of a new candle? I can't think of any. But I wanted to ask the broader community.

 
nondisclosure:

Ok. So, in my quest to find the perfect new candle code, I tried something new. Price based!

I put this in an EA and loaded on a 1 minute chart. Worked each time.

Now, what I'm wondering is under what condition would this 'if' statement be false on the 1st tick of a new candle? I can't think of any. But I wanted to ask the broader community.


1. That is redundant. If High==Low then Open and Close must also be the same value.

2. High will always be equal to Low on the first tick, but it can also be true on subsequent ticks if only the ask price changes giving you >1 alerts for the bar.

3. You are not guaranteed to see every tick giving the possibility of a missing alert.

 
euclid:


1. That is redundant. If High==Low then Open and Close must also be the same value.

2. High will always be equal to Low on the first tick, but it can also be true on subsequent ticks if only the ask price changes giving you >1 alerts for the bar.

3. You are not guaranteed to see every tick giving the possibility of a missing alert.


1. Good point.

2. Didn't think about that one.

3. Not sure what you mean by this one.

 
nondisclosure:

Ok. So, in my quest to find the perfect new candle code, I tried something new. Price based!

I put this in an EA and loaded on a 1 minute chart. Worked each time.

Now, what I'm wondering is under what condition would this 'if' statement be false on the 1st tick of a new candle? I can't think of any. But I wanted to ask the broader community.

Further to uclid's points, it also has been shown in the past that we should not depend on double == double. Use time instead.

   static datetime last_time = Time[0];
   bool newbar = false;
   if(Time[0] > last_time)
   {
    newbar = true;
    last_time = Time[0];
   }
   
   if(newbar) Alert("newbar");
 

SDC, that's pretty much what I have but more simplified using time.

et al,

It wasn't my new bar code that was the problem. For some reason, my terminal was seeing current positions open when there were none. So I deleted all .hst file and let MT4 re download them. All works now.

 
nondisclosure: Ok. So, in my quest to find the perfect new candle code, I tried something new. Price based!
if (Open[0]==Close[0] && Close[0]==High[0] && High[0]==Low[0]) Alert("New Bar!");

  1. If you receive the very first tick of a new bar that works. If you miss the very first tick, your code never runs. Always use time.
  2.   if(Time[0] > last_time)
    This fails when broker resets their clocks for some reason.
  3. int start(){
      static datetime time0; bool isNewBar = time0 != Time[0]; time0 = Time[0];
      :
      if(isNewBar) Alert(...);
    
    This works but won't handle deinit/init cycles - see Trailing Bar Entry EA - MQL4 forum


 

2) Brokers would have to set their clocks backwards by more minutes than the period of the chart in use, and during trading hours, for that code to fail. Do you think that actually happens ? Although like you said, we could use != instead of > to solve that possibility.

3) It would only fail if the deinit / reinit cycle spanned the time period from the current bar through to the next or subsequent bars in which case the first ticks of those bars would already have been missed while the EA was not operating so those first bar ticks are already rendered irrelvent.

We could code a user input to choose whether or not to consider the first tick recieved ater init to be the first tick of a new bar, or wait for the first tick of the next new bar.

 
SDC:

2) Brokers would have to set their clocks backwards by more minutes than the period of the chart in use, and during trading hours, for that code to fail. Do you think that actually happens ? Although like you said, we could use != instead of > to solve that possibility.

3) It would only fail if the deinit / reinit cycle spanned the time period from the current bar through to the next or subsequent bars in which case the first ticks of those bars would already have been missed while the EA was not operating so those first bar ticks are already rendered irrelvent.

We could code a user input to choose whether or not to consider the first tick recieved ater init to be the first tick of a new bar, or wait for the first tick of the next new bar.

  1. -
  2. There was a post several years ago, where the brokers clock was off by several days (correct time.) Once they corrected it, it took the several days before the EA started working again. No trailing stops, thousands of dollars of profit lost. EAs must be programmed to recover from the unexpected (like power failure/terminal crash/chart close) and restore state (common/static variables.) This is in that catagory. The code I used/posted always used > until that post - No I use/post !=.
  3. The EA also fails when you change pairs. The chart is completely different, but new bar doesn't trigger. It should do the same as it does on initial load (#4.)
  4. As the link I posted showed - The resetting static.
 
if (LastBar!=Bars) Alert("New Bar!");
LastBar=Bars;

i always use this to my EA.

it works fine both in standart chart and in Renko Chart

 
WDholic: i always use this to my EA.
if (LastBar!=Bars) Alert("New Bar!");

it works fine both in standart chart and in Renko Chart

Until you reach max bars on chart or a history request creates more bars. Always use time.
 
WHRoeder:
  1. -
  2. There was a post several years ago, where the brokers clock was off by several days (correct time.) Once they corrected it, it took the several days before the EA started working again. No trailing stops, thousands of dollars of profit lost. EAs must be programmed to recover from the unexpected (like power failure/terminal crash/chart close) and restore state (common/static variables.) This is in that catagory. The code I used/posted always used > until that post - No I use/post !=.
  3. The EA also fails when you change pairs. The chart is completely different, but new bar doesn't trigger. It should do the same as it does on initial load (#4.)
  4. As the link I posted showed - The resetting static.

4) It does work when you change pairs. It makes no difference if the static is reset or not. The next bar will trigger it either way because Time[0] will be different to last_time. I did find one thing though, when running the code in an indicator on build 646, If you change pairs to a pair that has not recently been used, it can cause the array out of range error on initializing the static datetime to Time[0] and the global initialization failed error.

   static datetime last_time = Time[0];
   bool newbar = false;  
//---
   if(Time[0] != last_time)
   {
    newbar = true;
    last_time = Time[0];
   }
   
   if(newbar) Alert("new bar");

2014.05.15 19:56:40.746 Custom indicator Test for NewBar USDCHF,M1: removed
2014.05.15 19:56:40.746 Test for NewBar USDCHF,M1: global initialization failed
2014.05.15 19:56:40.731 array out of range in 'Test for NewBar.mq4' (36,36)
2014.05.15 19:56:40.668 Test for NewBar XAUUSD,M1: uninit reason 3 //---------------------------- changing pair to USDCHF
2014.05.15 19:55:57.684 Test for NewBar XAUUSD,M1: Alert: new bar
2014.05.15 19:55:11.445 Test for NewBar XAUUSD,M1: initialized
2014.05.15 19:55:08.559 Custom indicator Test for NewBar XAUUSD,M1: loaded successfully

Service desk report #1013762