Ibarshift Request for explanation.

 

Good morning
I found a workaround, but would like some explanation on how ibarshift() works

My code is in oncalculate(). Time[i] is therefore just the constant and "i" an iteration of For

The goal was to recover X bars on a higher timeframe

            i_UpIndEnd = iBarShift ( gf_symbol, gf_TFDef, time[ i], false );

             if ( i_UpIndEnd == - 1 ) {
              f_IsErrorPresent( NULL , "Synchronization 02 failed. Correction in progress" );
               return i;
            }

             //--- Get prices on upper ut

The code works as follows.

The "processing" is to find the index of the bar and find the requested

But when the code executes a For afterwards with iclose() to retrieve the gf_period bars, it generates a 4401 for me, because no bars are available on this ut.


So I wanted to add a "control" before doing the "processing" and check the presence of the bar

and that's where I don't understand everything, I did

           //--- If enough bars on the upper timeframe 
           datetime i_DateStart = time[ i] - (( gf_period + 1 ) * PeriodSeconds ());
           int i_UpIndStart = iBarShift ( gf_symbol, gf_TFDef, i_DateStart, false );
           if (  i_UpIndStart >= 0 ) {

            i_UpIndEnd = iBarShift ( gf_symbol, gf_TFDef, time[ i], false );

This solution cannot bypass 4401 because the index returned does not go far enough.
Why does Ibarshift return me a bad index?

In my debugger,

i_UpIndEnd = iBarShift ( gf_symbol, gf_TFDef, time[ i], false ); 

returns 458 and

           datetime i_DateStart = time[ i] - (( gf_period + 1 ) * PeriodSeconds ());
           int i_UpIndStart = iBarShift ( gf_symbol, gf_TFDef, i_DateStart, false );

returns 465....it should have been 458 + 20 (Value of gf_period) = 478
So I still get error 4401.....


I bypassed the code by doing

        i_UpIndEnd = iBarShift ( gf_symbol, gf_TFDef, time[ i], false );
           //--- If enough bars on the upper timeframe 
           int i_UpIndStart = i_UpIndEnd + gf_period; 

           // Check if the bar is available 
           datetime i_barTime = iTime ( gf_symbol, gf_TFDef, i_UpIndStart);

           if ( i_barTime != 0 ) {

            i_UpIndEnd = iBarShift ( gf_symbol, gf_TFDef, time[ i], false );

and there I have the index 478 which is actually not available in the history, since the condition is only valid from the index 468, not even 465....

So why didn't the following code work?

           //--- If enough bars on the upper timeframe 
           datetime i_DateStart = time[ i] - (( gf_period + 1 ) * PeriodSeconds ());
           int i_UpIndStart = iBarShift ( gf_symbol, gf_TFDef, i_DateStart, false );

THANKS

 
Gerard Willia G J B M Dinh Sy:

Good morning
I found a workaround, but would like some explanation on how ibarshift() works

My code is in oncalculate(). Time[i] is therefore just the constant and "i" an iteration of For

The goal was to recover X bars on a higher timeframe

The code works as follows.

The "processing" is to find the index of the bar and find the requested

But when the code executes a For afterwards with iclose() to retrieve the gf_period bars, it generates a 4401 for me, because no bars are available on this ut.


So I wanted to add a "control" before doing the "processing" and check the presence of the bar

and that's where I don't understand everything, I did

This solution cannot bypass 4401 because the index returned does not go far enough.
Why does Ibarshift return me a bad index?

In my debugger,

returns 458 and

returns 465....it should have been 458 + 20 (Value of gf_period) = 478
So I still get error 4401.....


I bypassed the code by doing

and there I have the index 478 which is actually not available in the history, since the condition is only valid from the index 468, not even 465....

So why didn't the following code work?

THANKS

I am having trouble with the context. Could you please share the full OnCalculate function.
 
Dominik Egert # :
I am having trouble with the context. Could you please share the full OnCalculate function.

I'm sorry, I have a lot of functions that start from oncalculate(), it's not possible to share easily.

let's keep it simple

Why can't I add time to ibarshift so that it finds the bar index?

i_UpIndEnd = iBarShift ( gf_symbol, gf_TFDef, time[ i], false ); ==> Works

 int i_UpIndStart = iBarShift ( gf_symbol, gf_TFDef, i_DateStart, false ); ==> Does not work. The index value is not correct.

 
Gerard Willia G J B M Dinh Sy #:

I'm sorry, I have a lot of functions that start from oncalculate(), it's not possible to share easily.

let's keep it simple

Why can't I add time to ibarshift so that it finds the bar index?

Use ArrayBsearch for that. It will give you what you are looking for.