Help for Amibroker backtest error on Array subscript out of range

@AlgoGeek
Pl help me out to correct the following error while back testing in analysis window of amibroker:-
Error is : -
//urTrend = “D”;}else {curTrend = “U”;}
for (i=0; i<farback; i++) {curBar = (BarCount - 1) - i;
if (aLLVBars[curBar]
-------------------^
Error 10.
Array subscript out of range.
You must not access array elements outside 0…(BarCount-1) range. You attempted to access non-existing -1-th element of array. //

The code snipped is as under :-
_SECTION_BEGIN(“trendline”);
farback=Param(“How Far back to go”,100,50,5000,10);
nBars = Param(“Number of bars”, 12, 5, 40);
aHPivs = H - H;aLPivs = L - L;
aHPivHighs = H - H;aLPivLows = L - L;aHPivIdxs = H - H;aLPivIdxs = L - L;
nHPivs = 0;nLPivs = 0;lastHPIdx = 0;lastLPIdx = 0;lastHPH = 0;lastLPL = 0;
curPivBarIdx = 0;
aHHVBars = HHVBars(H, nBars);aLLVBars = LLVBars(L, nBars);
aHHV = HHV(H, nBars);aLLV = LLV(L, nBars);
aVisBars = Status(“barvisible”);nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));
_TRACE("Last visible bar: " + nLastVisBar);
curBar = (BarCount-1);curTrend = “”;if (aLLVBars[curBar] < aHHVBars[curBar]) {
curTrend = “D”;}else {curTrend = “U”;}
for (i=0; i<farback; i++) {curBar = (BarCount - 1) - i;
if (aLLVBars[curBar] < aHHVBars[curBar]) {
if (curTrend == “U”) {curTrend = “D”;
curPivBarIdx = curBar - aLLVBars[curBar];aLPivs[curPivBarIdx] = 1;aLPivLows[nLPivs] = L[curPivBarIdx];
aLPivIdxs[nLPivs] = curPivBarIdx;nLPivs++;}
} else {if (curTrend == “D”) {curTrend = “U”;curPivBarIdx = curBar - aHHVBars[curBar];
aHPivs[curPivBarIdx] = 1;aHPivHighs[nHPivs] = H[curPivBarIdx];
aHPivIdxs[nHPivs] = curPivBarIdx;nHPivs++;}} }
curBar = (BarCount-1);candIdx = 0;candPrc = 0;lastLPIdx = aLPivIdxs[0];lastLPL = aLPivLows[0];
lastHPIdx = aHPivIdxs[0];lastHPH = aHPivHighs[0];if (lastLPIdx > lastHPIdx) {
candIdx = curBar - aHHVBars[curBar];candPrc = aHHV[curBar];
if (lastHPH < candPrc AND candIdx > lastLPIdx AND candIdx < curBar) {
aHPivs[candIdx] = 1;
for (j=0; j<nHPivs; j++) {aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-(j+1)];
aHPivIdxs[nHPivs-j] = aHPivIdxs[nHPivs-(j+1)];}aHPivHighs[0] = candPrc ;
aHPivIdxs[0] = candIdx;nHPivs++;}} else {
candIdx = curBar - aLLVBars[curBar];candPrc = aLLV[curBar];if (lastLPL > candPrc AND
candIdx > lastHPIdx AND candIdx < curBar) {
aLPivs[candIdx] = 1;
for (j=0; j<nLPivs; j++) {aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];
aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];}aLPivLows[0] = candPrc;
aLPivIdxs[0] = candIdx;nLPivs++;}}
for (k=0; k<nHPivs; k++) {_TRACE("High pivot no. " + k

  • " at barindex: " + aHPivIdxs[k] + ", "
  • WriteVal(ValueWhen(BarIndex()==aHPivIdxs[k],
    DateTime(), 1), formatDateTime)+ “, " + aHPivHighs[k]);}
    a1=ahpivs==1;a2=alpivs==1;
    x = Cum(1);s1=L;s11=H;pS = a2 == 1;
    endt= SelectedValue(ValueWhen( pS, x, 1 ));
    startt=SelectedValue(ValueWhen( pS, x, 2 ));dtS =endt-startt;
    endS = SelectedValue(ValueWhen( pS, s1, 1 ) );
    startS = SelectedValue( ValueWhen( pS, s1, 2 ));aS = (endS-startS)/dtS;
    bS = endS;trendlineS = aS * ( x -endt ) + bS;
    g3= IIf(x>startt-10,trendlineS,-1e10);
    pR = a1== 1;endt1= SelectedValue(ValueWhen( pR, x, 1 ));
    startt1=SelectedValue(ValueWhen( pR, x, 2 ));
    dtR =endt1-startt1;endR = SelectedValue(ValueWhen( pR, s11, 1 ) );
    startR = SelectedValue( ValueWhen( pR, s11, 2 ));
    aR = (endR-startR)/dtR;bR = endR;
    trendlineR = aR * ( x -endt1 ) + bR;
    g4= IIf(x>startT1-10,trendlineR,-1e10);
    _SECTION_END();
    _SECTION_BEGIN(“trendlineA”);
    dn=g3>C;
    up=g3<C;
    bbresult1 = WriteIf( dn,“dn”, “”);
    bbresult2 = WriteIf( up,“up”, “”);
    RequestTimedRefresh( 0 );
    if ( bbresult1 ==“dn”)
    { GfxSelectSolidBrush( ColorRGB(255,0,0) );
    }
    else
    if ( bbresult2 ==“up”)
    { GfxSelectSolidBrush( ColorRGB(0,147,0) );
    }
    GfxSelectFont( “Arial”, 10, 100 );
    GfxSetBkMode( 1 );
    GfxSetTextColor( colorWhite );
    GfxSelectPen( colorWhite, 1 );
    GfxCircle( 30,229,8 );
    _SECTION_END();
    _SECTION_BEGIN(“trendlineA”);
    dn=g3>C;
    up=g3<C;
    bbresult1 = WriteIf( dn,“dn”, “”);
    bbresult2 = WriteIf( up,“up”, “”);
    RequestTimedRefresh( 0 );
    if ( bbresult1 ==“dn”)
    { GfxSelectSolidBrush( ColorRGB(255,0,0) );
    }
    else
    if ( bbresult2 ==“up”)
    { GfxSelectSolidBrush( ColorRGB(0,147,0) );
    }
    GfxSelectFont( “Arial”, 10, 100 );
    GfxSetBkMode( 1 );
    GfxSetTextColor( colorWhite );
    GfxSelectPen( colorWhite, 1 );
    GfxCircle( 168,367,8 );
    _SECTION_END();
    _SECTION_BEGIN(“TRENDLINEline”);
    GfxSelectFont( “Arial”, 10, 100 );
    GfxSetBkMode( 1 );
    GfxSetTextColor( colorWhite );
    GfxSelectPen( colorWhite, 1 );
    GfxMoveTo( 35,234 );
    GfxLineTo( 163, 362 );
    _SECTION_END();
    _SECTION_BEGIN(“traing sl”);
    function vstop_func(trBull,trBear)
    { trailArray[ 0 ] = C[ 0 ]; // initialize
    for( i = 1; i < BarCount; i++ )
    { prev = trailArray[ i - 1 ];
    if (C[ i ] > prev AND C[ i - 1 ] > prev)
    { trailArray[ i ] = Max(prev,C[ i ] - trBull[ i ]);
    }
    else if (C[ i ] < prev AND C[ i - 1 ] < prev)
    { trailArray[ i ] = Min(prev,C[ i ] + trBear[ i ]);
    }
    else if (C[ i ] > prev)
    { trailArray[ i ] = C[ i ] - trBull[ i ];
    }
    else
    { trailArray[ i ] = C[ i ] + trBear[ i ];
    }
    } return trailArray;
    }
    per = Param(“per”,20, 1, 150, 1);
    multBull = Param(“multBull”,2, 1, 4, 0.05);
    multBear = Param(“multBear”,2, 1, 4, 0.05);
    trBull = multBull * ATR(per);
    trBear = multBear * ATR(per);
    trailArray = vstop_func(trBull,trBear);
    s0=trailArray;
    s1= s0 > C ;
    s2= s0 <C ;
    ccresult1 = WriteIf( s1,“cu”, “”);
    ccresult2 = WriteIf( s2,“cd”, “”);
    GfxSelectFont(“arial”, 13, 700 ); GfxSetBkMode( colorRed);
    GfxSetTextColor( ParamColor(“Color”,colorRed) );
    Hor=Param(“Horizontal Position”,30,10,1200,1);
    Ver=Param(“Vertical Position”,185,100,50,50);
    if ( ccresult1 ==“cu”)
    { GfxTextOut(”"+s0,Hor , Ver );
    }
    else
    GfxSelectFont(“arial”, 13, 700 ); GfxSetBkMode( colorGreen );
    GfxSetTextColor( ParamColor(“Color”,colorGreen) );
    Hor=Param(“Horizontal Position”,75,10,1200,1);
    Ver=Param(“Vertical Position”,185,100,50,50);
    if ( ccresult2 =="")
    {GfxTextOut(""+s0,Hor , Ver );
    }
    _SECTION_END();