Error 10 occuring at the first instance of the counter(i) in for loop

Version = 6.38

Background Information:
Goal: Run the scan on all symbols in the database.

What did i try:

  1. Tried the method mentioned in the documentation here.

  2. Tried using _TRACE() to see what is happening.

  3. Serched forum.

Conditions for Replication:

  1. I see this error occurring only when i try to run it on all symbols in AmiBroker.

  2. I do not see this error when running on Nifty 500 watchlist which has maximum stocks (496) in my watchlists.

  3. When i add all symbols in my database to a watchlist (1710 symbols), it is giving the same error.

Code:

_SECTION_BEGIN("Magic Trend Indicator Update");
SetBarsRequired( sbrAll, sbrAll );

P = Param( "Period", 20, 2, 25, 1 );
m = Param( "Multiplier", 3, 0.1, 5, 0.1 );
A = ATR( P );

for( i = 0; i < P; i++ )
{
//Calculating S value as close minus ATR of periods
    _TRACE( "This is first element of close array: " + Close[ i ] );
    S[i] = Null;

    if( i > 0 )
    {
        S[i] = C[i] - m * ( A[i] );
        T[i] = 1;
    }
    else
    {
        S[i] = C[i];
        T[i] = 1;
    }
}

for( i = P; i < BarCount; i++ )
{
    if( C[i] > S[i - 1] )
    {
        if( T[i - 1] == 1 )
        {
            S[i] = Max( C[i] - m * A[i], S[i - 1] );
            T[i] = 1;
        }
        else
        {
            S[i] = C[i] - m * A[i];
            T[i] = 1;
        }

    }
    else
    {
        if( T[i - 1] == -1 )
        {
            S[i] = Min( C[i] + m * A[i], S[i - 1] );
            T[i] = -1;
        }
        else
        {
            S[i] = C[i] + m * A[i];
            T[i] = -1;
        }
    }
}

Buy = Cover = C > S;
Short = Sell = C < S;
Buy = ExRem( Buy, Sell OR Short );
Sell = ExRem( Sell, Buy );
Short = ExRem( Short, Cover OR Buy );
Cover = ExRem( Cover, Short );
BuyPrice = ValueWhen( Buy, C, 1 );
SellPrice = ValueWhen( Sell, C, 1 );
ShortPrice = ValueWhen( Short, C, 1 );
CoverPrice = ValueWhen( Cover, C, 1 );
Offset = -12;
PlotShapes( Buy*shapeUpArrow, colorBrightGreen, 0, Min( S, L ), Offset );
PlotShapes( Short*shapeDownArrow, colorRed, 0, Max( S, H ), Offset );
PlotShapes( Cover*shapeHollowUpArrow, colorBrightGreen, 0, Min( S, L ), Offset );
PlotShapes( Sell*shapeHollowDownArrow, colorRed, 0, Max( S, H ), Offset );
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
//Plot(S,"STOP",IIf(C>S,colorBrightGreen,colorRed),styleDots|styleStaircase|styleThick|styleNoTitle );
Plot( S, "STOP", IIf( C > S, colorBrightGreen, colorRed ), styleLine | styleStaircase | styleNoTitle );
PlotOHLC( O, H, L, C, "Close", colorDefault, styleCandle | styleNoTitle );

Open_Long = Flip( Buy, Sell );
Open_Short = Flip( Sell, Buy );
Buy_Price = ValueWhen( Buy, BuyPrice, 1 );
Short_Price = ValueWhen( Short, ShortPrice, 1 );
Sell_Price = IIf( Sell, ValueWhen( Sell, SellPrice, 1 ), Short_Price );
Cover_Price = IIf( Cover, ValueWhen( Cover, CoverPrice, 1 ), Buy_Price );
Profit = IIf( Open_Long, ( Close - Buy_Price ), IIf( Open_Short, ( Short_Price - Close ), 0 ) );
Last_Profit = IIf( Ref( Open_Long, -1 ) == 1 AND Open_Long != 1, ( Sell_Price - Buy_Price ), IIf( Ref( Open_Short, -1 ) == 1 AND Open_Short != 1, ( Short_Price - Cover_Price ), 0 ) );
Cum_Profit[0] = Last_Profit[0];
PreviousProfit = ValueWhen( Last_Profit != 0, Last_Profit, 1 );

for( i = 1; i < BarCount; i++ )
{
    Cum_Profit[i] = Cum_Profit[i - 1] + Last_Profit[i];
}

Trade[0] = 0;

for( i = 1; i < BarCount; i++ )
{
    if( Buy[i] == 1 OR Short[i] == 1 )
    {
        Trade[i] = Trade[i - 1] + 1;
    }
    else
    {
        Trade[i] = Trade[i - 1];
    }
}

// Trade Report //
TR = ParamToggle( "Trade Report", "Hide|Show", 1 );

if( TR == 0 )
{
    GfxSelectFont( "Verdana", 10, 100 );
    GfxSetBkMode( 1 );
    GfxSetTextColor( ColorRGB( 50, 50, 50 ) );
    y = Status( "pxchartheight" );

    GfxTextOut( ( "Total Trades : " + WriteVal( Trade, 3.0 ) ), 13, y - 125 );
    GfxTextOut( ( "Total Profit : " + WriteVal( Cum_Profit, 4.2 ) ), 13, y - 100 );
    GfxTextOut( ( "Previous Profit : " + WriteVal( PreviousProfit, 2.2 ) ), 13, y - 50 );
    GfxTextOut( ( "Price : " + WriteVal( Close, 2.2 ) ), 13, y - 75 );
    GfxTextOut( ( "Current Profit : " + WriteVal( Profit, 2.2 ) ), 13, y - 25 );
} 
_SECTION_END();

Analysis APX is available for download here.

This loop statement is incorrect.
Because it does not take care for checking BarCount (number of bars of symbol).

You simply have symbols with Barcount smaller than P-1.


Also that loop makes no sense because ATR is NULL for the length of P-1.
So S[i] = C[i] - m * ( A[i] ); become Null also till length P-1.
So you can just remove first loop and write this before second loop.

S = Null;
T = 1;
for( i = P; i < BarCount; i++ )

and result will be same one

Think what happens here when the symbol being processed has only one bar.

1 Like

... Barcount smaller than P.

If there is such symbol then you would exceed BarCount-1 inside loop.

E.g. Barcount = 14 and P = 20.

So if i reaches last array element at BarCount-1 (i = 13) then it's still fine.
But since P-1 is greater 13 loop iteration will not halt at 13 (it will go till 19) and if it reaches 14 you will get Error 10 because then you are outside of 0 to BarCount-1 range.

1 Like

Thank you so much :pray: :pray: :pray:.

That worked, and now i also understood what was happening with the code.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.