Engulfing Bars Problem

 

I am trying to code for to identify engulfing bars but I am getting the wrong open, close, high, low values.

This is the snippet of code:

      int currentBar = Bars - 1;
    
   // Get the high,low,open and close prices of the current and previous bars
      double currentHigh = High[currentBar];
      double currentLow = Low[currentBar];    
      double currentOpen = Open[currentBar];
      double currentClose = Close[currentBar];
      double previousOpen = Open[currentBar - 1];
      double previousClose = Close[currentBar - 1];

Am I using the predefined variable "Bars" correctly? if so, why do I get the wrong values?

 
Forgot to say that the incorrect values are generated when I run the script in the MT4 Strategy Tester
 
      double previousOpen = Open[currentBar - 1];
      double previousClose = Close[currentBar - 1];
Previous bar is +1
 
William Roeder #:
Previous bar is +1

Not sure if I understood your comment correctly because when I change to 

double previousOpen = Open[currentBar + 1];

I get the following error:

2023.09.28 14:39:24.164 2022.08.01 01:00:00  Quatro NZDUSD,H1: array out of range in 'Quatro.mq4' (1272,33)


 

For some or other reason the values of open, close, high, etc. stays the same although the Bar number changes.

Here is the data from my Journal:

2023.09.28 14:59:52.750 2022.08.01 03:00:00  Quatro NZDUSD,H1: BearishEngulfingBar is false BullishEngulfingBar is false currentbar is 1003 currentOpen is 0.64796 previousClose is 0.64934 currentLow is 0.646 currentClose is 0.64717 previousOpen is 0.6472

2023.09.28 14:59:52.750 2022.08.01 03:00:00  Quatro NZDUSD,H1: BuySLsize is 0

2023.09.28 14:59:52.373 2022.08.01 02:00:00  Quatro NZDUSD,H1: BearishEngulfingBar is false BullishEngulfingBar is false currentbar is 1002 currentOpen is 0.64796 previousClose is 0.64934 currentLow is 0.646 currentClose is 0.64717 previousOpen is 0.6472

2023.09.28 14:59:52.373 2022.08.01 02:00:00  Quatro NZDUSD,H1: BuySLsize is 0

2023.09.28 14:59:51.997 2022.08.01 01:00:00  Quatro NZDUSD,H1: BearishEngulfingBar is false BullishEngulfingBar is false currentbar is 1001 currentOpen is 0.64796 previousClose is 0.64934 currentLow is 0.646 currentClose is 0.64717 previousOpen is 0.6472


 
Ernest Klokow #:

Not sure if I understood your comment correctly because when I change to 

I get the following error:

2023.09.28 14:39:24.164 2022.08.01 01:00:00  Quatro NZDUSD,H1: array out of range in 'Quatro.mq4' (1272,33)

      int currentBar = Bars - 1;

Of course, you do. You set currentBar to the oldest in history. There can be no previous.

 
Ernest Klokow #:

Not sure if I understood your comment correctly because when I change to 

I get the following error:

2023.09.28 14:39:24.164 2022.08.01 01:00:00  Quatro NZDUSD,H1: array out of range in 'Quatro.mq4' (1272,33)


What he is saying is that Bars-1 is the oldest bar on your chart . It cannot be any more “ pervious” beyond that.:: so your currentHigh should be High[current-1] and prevHigh should be High[current] .  
 

Thank you so much Daniel!

You speak in a way that makes things so much clearer!