0

I am trying to confirm that I am using mt4's ibarshift correctly but the results seem to be not self consistent. I feed ibarshift a datetime, get the indice of the corresponding bar, then ask what time the bar corresponds to but I get a different datetime that I provided. I am performing this self consistency check to make sure I am accessing the bars that I think I am accessing. Any ideas would be welcomed, as this is a pretty bare bones example. I have updated my history for the chart and problems persist.

I have boiled it down to the following code:

//choose start date and an end date to study
datetime startTime = StrToTime(  "2016.04.01 00:00" );
datetime endTime = StrToTime(  "2019.09.30 00:00" );
Print("startTime string=",startTime);
Print("endTime string=",endTime); 

//bar indices
int startBari = iBarShift("EURUSD",PERIOD_H1,startTime);
int endBari   = iBarShift("EURUSD",PERIOD_H1,endTime); 
printf("startBari=%i",startBari);
printf("endBari=%i",endBari); 

//convert back to time
Print("start time from iBarShift = ",iTime("EURUSD",PERIOD_H1,startBari));
Print("end time from iBarShift = ",iTime("EURUSD",PERIOD_H1,endBari));

But the results I get from the terminal are: enter image description here

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
jollywer
  • 26
  • 2

1 Answers1

0

I've tried to reproduce your problem and I got wrong "start time" output:

I've tried to reproduce your problem and I got wrong "start time" output:

I think in order not to get different/wrong result one must use last parameter of iBarShift function which is what to happen if bar is not found. Default is false(when omitted), which you used, and result when bar is not found is to return nearest.

When I put last parameter as true this is output:

When I put last parameter as true this is output:

-1 sugests bar wasn't found.

So code should look like this:

//choose start date and an end date to study
datetime startTime = StrToTime(  "2016.04.01 00:00" );
datetime endTime = StrToTime(  "2019.09.30 00:00" );
Print("startTime string=",startTime);
Print("endTime string=",endTime); 

//bar indices
int startBari = iBarShift("EURUSD",PERIOD_H1,startTime,true);
int endBari   = iBarShift("EURUSD",PERIOD_H1,endTime,true); 
if(startBari>-1)
printf("startBari=%i",startBari);
else
printf("startBari NOT FOUND!!!");
if(endBari>-1)
printf("endBari=%i",endBari); 
else
printf("endBari NOT FOUND!!!"); 

//convert back to time
Print("start time from iBarShift = ",iTime("EURUSD",PERIOD_H1,startBari));
Print("end time from iBarShift = ",iTime("EURUSD",PERIOD_H1,endBari));
stack user
  • 63
  • 4