EA getting Bullish/Bearish for previous bar wrong

Hi Everyone, Long time reader first time writer.

I'm working on an EA that uses Heiken Ashi candles and needs to know if the previous bar was bearish (red) or bullish (white). There is a buffer for close and open price and I have variables that update for previous bar close and open based on the Heiken Ashi values. The values it is giving me are correct. However, I am trying to set a bool true if prev bar is bullish and false if bearish using:

   if(NormalizeDouble(Heiken3,5) > NormalizeDouble(Heiken2,5)) {

      PrevBarBullish = True; }

      else {

   if(NormalizeDouble(Heiken3,5) < NormalizeDouble(Heiken2,5)) {   

      PrevBarBullish = False; } }


where Heiken3 is prev close and Heiken2 is prev open. Sometimes it is correct and sometimes it is wrong. There doesn't seem to be any reasoning to when it is wrong. Again, the Heiken3 and Heiken2 values being printed in the journal are correct but sometimes it is saying a candle was not bullish when it was and vice versa. I can't seem to wrap my head around it. 

The code is attached and the problem code appears to be at the bottom. You can see in the attached images that the circled bar is bullish and the values displayed for close and open show that it is bullish but the program is saying false. I would greatly appreciate any help with this.


Thank you!

Files:
Heiken1.PNG  36 kb
Heiken2.PNG  12 kb
nic88:

Hi Everyone, Long time reader first time writer.

I'm working on an EA that uses Heiken Ashi candles and needs to know if the previous bar was bearish (red) or bullish (white). There is a buffer for close and open price and I have variables that update for previous bar close and open based on the Heiken Ashi values. The values it is giving me are correct. However, I am trying to set a bool true if prev bar is bullish and false if bearish using:

where Heiken3 is prev close and Heiken2 is prev open. Sometimes it is correct and sometimes it is wrong. There doesn't seem to be any reasoning to when it is wrong. Again, the Heiken3 and Heiken2 values being printed in the journal are correct but sometimes it is saying a candle was not bullish when it was and vice versa. I can't seem to wrap my head around it. 

The code is attached and the problem code appears to be at the bottom. You can see in the attached images that the circled bar is bullish and the values displayed for close and open show that it is bullish but the program is saying false. I would greatly appreciate any help with this.

Just have to move the highlighted block from below to here:

   if(IsNewCandle()){

      if(NormalizeDouble(Heiken3,5) > NormalizeDouble(Heiken2,5)) {
         PrevBarBullish = True; }
         else {
      if(NormalizeDouble(Heiken3,5) < NormalizeDouble(Heiken2,5)) {   
         PrevBarBullish = False; } }

      Alert("New Bar");
      Alert("last bar low/high is", Heiken0);
      Alert("last bar Open is", NormalizeDouble(Heiken2,5));
      Alert("last bar close is", NormalizeDouble(Heiken3,5));
      Alert("Prev Bar Bullish?: ", PrevBarBullish);
      }
   

The reason why it showed false when the bar is bullish was because the lines that do the check was executed before the new candle, so it reflects the last bar's direction.

Seng Joo Thio:

Just have to move the highlighted block from below to here:

The reason why it showed false when the bar is bullish was because the lines that do the check was executed before the new candle, so it reflects the last bar's direction.

Thank you! Simple fix but it was driving me crazy!

Seng Joo's move fixes the timing. There's one more case hiding in that same block, though. You round both prices with NormalizeDouble(...,5) and then test greater-than and less-than. If the Heiken Ashi body is under one point, both values can round to the same number. Then neither branch runs, and PrevBarBullish just keeps whatever the previous bar left in it. So once in a while you'd get a "wrong" colour that's really a stale one.

The simple way out is to compare the values as you read them, without rounding, and decide what a zero body should mean for your EA. NormalizeDouble just rounds to a set accuracy. Here it doesn't make the comparison safer, it makes tiny bodies vanish. And it fails quietly: no error, no log line, just a bar that keeps the last bar's colour.