Please help: iBarShift returns incorrect value for period = PERIOD_MN1

 

The following script returns a '1' for any index of Time[], when run on a chart in any timeframe. For example, it seems to me that the barshift for 10 weeks back should be '2'.

This error only seems to occur with period = PERIOD_MN1. It seems to work fine with any other period.

Anyone have any ideas why that might be?

Any help is appreciated. Thanks.

int start() {
   
   int test = iBarShift(Symbol(), PERIOD_MN1, Time[10], false); 
    
   Print("iBarshift returns: " + test);
   
   return(0);
}
 
Coyote:

For example, it seems to me that the barshift for 10 weeks back should be '2'.

on the begining of the month = 3

I suggest you to add this line to understand how this function works

   Print("Time[10] is: " + TimeToStr(Time[10],TIME_DATE));
 
qjol:


on what TF u testing it ?


Sorry, I should have been clearer: It was running on a weekly chart. Again, it seems to return '1' on any TF and for any Period of iBarShift.
 

Ooops, I did not mean to say "on any Period for iBarShift," just any TF for PERIOD_MN1

 

have you tried my suggestion?

 
qjol:

have you tried my suggestion?

I have. I had previously added a line to confirm that Time[] was returning te value I was expecting, identical to the line you posted.

To be thorough I pasted your line of code in the script to make sure we would get the same result. It did.

Am i missing some specific point you are making about Time[]?

Thanks, again.

 

what is the result of this line ?

 

On a week TF:

2010.11.30 20:30:54 _ Scrap 2 EURUSD,Weekly: Time[10] is: 2010.09.19

2010.11.30 20:30:54 _ Scrap 2 EURUSD,Weekly: iBarshift returns: 1

Code looks like so:

int start() {
   
   int test = iBarShift(Symbol(), PERIOD_MN1, Time[10], false); 
    
   Print("iBarshift returns: " + test);
   Print("Time[10] is: " + TimeToStr(Time[10],TIME_DATE));
   
   return(0);
}


 

what happens if you replace to true

int test = iBarShift(Symbol(), PERIOD_MN1, Time[10], true);
 

Systematic thinking, but from the iBarShift definition we're told that if we specify 'true' for the 'exact' parameter the function will look to match the bar open time exactly rather than return the next closest.

Again, in order to be thorough, I tried it and it did return '-1', meaning it didn't find an exact match.

This is definitely one of those times where I'm afraid the solution is right in front of my nose, and I keep looking past it.

Are there any other switches we can flip?

 

this take place on backtest some time.

Reason: