I think your idea of using two loops is correct.
but i suggest that the 1st for loop start from barcount - 1 like you 
do and finish off at -Days/2;
the 2nd j loop starts at barcount + Days/2 and finishes off at 0

the if statement should be if(td[j] <= backday)

result[i] = array[j] and break after
hope that helps.

--- In [email protected], "tipequity" <[EMAIL PROTECTED]> wrote:
>
> I am having a hard time relating dates to bar indecies. In code 
> provided below I am trying to calculate the bar index of a year ago 
> date with little success. I appreciate any help and explanation.
> 
> SetBarsRequired(10000);
> 
> function RefDays( Array, Days ) 
> { 
>   td = DaysSince1900(); 
>   result = Null; 
> 
>   if( Days < 0 ) 
>   { 
>      for( i = BarCount -1; i >= -Days; i = i - 1 ) 
>      { 
>        backday = td[ i ] + Days; // Days is negative 
>        for( j = -Days/2; j < i; j++ ) 
>        { 
>           if( td[ i - j ] <= backday ) 
>            { 
>              result[ i ] = Array[ i - j ]; 
>              break; 
>            } 
>        } 
>      } 
>   } 
>   return result; 
> } 
> 
> yy = Year();
> dy = DayOfYear();
> leapyear = ( yy % 4 ) == 0 AND yy != 2000;
> yearlen = IIf( leapyear, -366, -365 ); 
> 
> aYearAgoDate = RefDays(DateTime(), LastValue(yearlen));
> dn = DateNum();
> BarIndx = BarIndex();
> 
> StartDateBar = LastValue( ValueWhen ( dn == StartDate ,BarIndx ) );
>


Reply via email to