Wrong values in latest bar within Strategy Tester only

 

It seems as if the values of High[0], Low[0], and Close[0] gets overwritten with the value of Open[0] when running this simple code from within Strategy Tester.

For Instance, take this Init code:

int OnInit() {

   for(int i=0; i<2; i++){
      printf("Bar[%d]=%f|%f|%f|%f",i, Open[i], High[i], Low[i], Close[i]);
   }

   return(INIT_SUCCEEDED);
}

When I run it on the current chart, I get the correct values for all the  bars, including the first:

2017.08.29 08:47:52.956 jjj EURUSD,Daily: Bar[1]=1.194270|1.198290|1.191620|1.197800

2017.08.29 08:47:52.956 jjj EURUSD,Daily: Bar[0]=1.197790|1.199330|1.195470|1.199270


On the contrary, when the same expert is run from within Strategy Tester, here's a sample of what I get:

2017.08.29 08:33:04.199 2016.08.01 00:01:00  jjj EURUSD,Daily: Bar[1]=1.107630|1.119720|1.107200|1.117430

2017.08.29 08:33:04.199 2016.08.01 00:01:00  jjj EURUSD,Daily: Bar[0]=1.117150|1.117150|1.117150|1.117150

In this case, the value 1.117150 is the actual Open value of Bar[0], while Bar[1] values are correct. 


I just don't understand this behavior. Is there any way around this?

Thanks in advance

 

Bar 0 is current open bar, on a live chart you get current prices (when EA is started).

With the Strategy Tester, you always start with the first tick of first (current) bar, so all prices are equals.

 
Alain Verleyen:

Bar 0 is current open bar, on a live chart you get current prices (when EA is started).

With the Strategy Tester, you always start with the first tick of first (current) bar, so all prices are equals.


Thanks for your reply, it does make sense.

However, this leaves me with the question: Ho can I get the actual OHLC values for the first bar I want the EA to start on?

 

I think I found a workaround to my problem:

In my code, I check if IsTesting() is true, then I add 1 to the High[], Low[], Close[] indexes

Then, when Launching the tester, I add one minute/hour/day to the starting date.


Maybe It's not the most elegant way, but it is a quick fix.

 
You shouldn't be looking at the chart at all in OnInit. The chart may not be set up; history and market info not updated yet. Look in OnTick/OnCalculate only.
Reason: