Issue with ibarshift

 

Hi everyone,

I have an issue with ibarshift.

I have the following code

int init()

{

int time = Time[0];

}


int start()

{

price = High[iHighest(Symbol(), 0, MODE_HIGH, fPeriod(), 0)];

}

fPeriod(){

if(Time[0] == time)
return(0);
if(Time[0] != time)
return(iBarShift(Symbol(), 0, time, true));

}



After several test, ibarshift is almost never giving me the right amount of bars. When it's supposed to be 2, I got 779 for example.

Thanks for your help on this one.

I am thinking about calculating the shift with a loop since this function doesn't seem to work.

Cheers.

 

OK, I see a few issues . . . they may or may not be related to your problem but you should fix them anyway . .

Time[] is a datetime, not an int

int time = Time[0];

should be

datetime time = Time[0];    // <----  type is datetime

fPeriod() is not declared as a type to be able to return anything . . . it should be . . .

int fPeriod(){       // <---- add int

if(Time[0] == time)
return(0);
if(Time[0] != time)
return(iBarShift(Symbol(), 0, time, true)); 

}
 
RaptorUK:

OK, I see a few issues . . . they may or may not be related to your problem but you should fix them anyway . .

Time[] is a datetime, not an int

fPeriod() is not declared as a type to be able to return anything . . . it should be . . .

What he said

plus

you need to declare the variable time outside of the init function (ie globally) so that the other functions can see it!

datetime time=0;

int init(){

    time = Time[0];
}


int start(){

     price = High[iHighest(Symbol(), 0, MODE_HIGH, fPeriod(), 0)];

}

int fPeriod(){

    if(Time[0] == time)
         return(0);

    // there is no need for another test
    return(iBarShift(Symbol(), 0, time, true));

}
 
RaptorUK:

OK, I see a few issues . . . they may or may not be related to your problem but you should fix them anyway . .

Time[] is a datetime, not an int

fPeriod() is not declared as a type to be able to return anything . . . it should be . . .


Oups, thanks for spotting that. When copy pasting and deleting the useless code, I forgot some of the original code.

However, everything is as it should be in the code. Still not getting the right shift.

Thanks.

 
dabbler:

What he said

plus

you need to declare the variable time outside of the init function (ie globally) so that the other functions can see it!


I moved the declaration of time outside of init but still not getting the right shift.

Really strange

 
fftremblay:

I moved the declaration of time outside of init but still not getting the right shift.

Really strange

How do you know ? what are you Print ing to determine it is wrong ?
 
RaptorUK:
How do you know ? what are you Print ing to determine it is wrong ?

I am printing iHighest(Symbol(), 0, MODE_HIGH, fPeriod(), 0)
 
Try adding a Print to the function to see what it is returning . . .
 
RaptorUK:
Try adding a Print to the function to see what it is returning . . .

Found the problem.

if(Time[0] == time)
         return(0);

It needs to be


if(Time[0] == time)
         return(1);


Thanks for your help!

 
fftremblay:

Thanks for your help!

You are welcome . . . :-) Going to bed. Night.