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!
Reason: