Scale in errors?

Hi all,
I have a AFL freelancer helping me with coding. The rules I have provided for LONG trades as follows:

2.1 Rule 1 – Entry Long
1. If yesterday’s Close is Higher than today’s Low then
1.1. use 1/xth of PortEquity to buy
1.2. at today’s Close.
1.3. Yesterday (“Ref -1”) should be a parameter (1,1,5,1)
2. X is a parameter between 5 and 30 with default of 20
3. For example:
3.1. If Equity is $100,000,
3.2. X = 20
3.3. buy trigger is activated (yesterday’s close is higher than today’s low)
3.4. today’s close is $10
3.5. then buy $5000 at $10 = Qty: 500
4. repeat this buy process every time Rule#1 is triggered until there is no available $
5. Only trigger the exit rule (below) once there is no available $
2.2 Rule 2 – Exit long
1. Exit at HHV Close (parameter 200, 10,300,10)

Here is the code:

SetTradeDelays( 0, 0, 0, 0 ); // Trade happens on the same day
PortEquity = Foreign( "~~~EQUITY", "C" );

GfxSelectFont( "Tahoma", 11, 110 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );


// user input
initial_equity = Param( "Initial Equity", 100000, 1, 10000000, 1 );
X = Param( "X port of equity", 20, 10, 100, 10 );
lookback = Param( "Lookback (ref-1)", 1, 1, 5, 1 );
HHV_length = Param( "Lookback HHV close Exit", 200, 100, 300, 20 ); ;

// Initial equity
SetOption( "InitialEquity", initial_equity );


//GfxTextOut("check: "+((1/20)*100000),30,50);

//HHV
Highest_close = HHV( close, HHV_length );
abs_amount = ( ( 1 / x ) * initial_equity );

_buy 		= 	Ref( Close, -lookback ) > low;
_Sell 		= 	Close > Ref( Highest_close, -1 );


Plot( Ref( Highest_close, -1 ), "Highest close", colorWhite );

Buy = Sell = Null;
LongFlag =  0;
_BuyPrice = null;
bp = 0;
LF = 0;
capital = initial_equity;
CP = 0;
trade_counter = tc = 0;
entry_qty = EQTY = 0;
entry_value = Ev = 0;
check = 0;
exit_qty = XQTY = 0;
buyamount = BA = initial_equity;
BB = 0;
for( i = 1; i < BarCount; i++ )
{

    if( _Buy[ i ]  AND buyamount[i]  > C[i] AND  capital[i] > C[i] ) //bal capital should be more then current price
    {

        if( trade_counter == 0 )
        {
            Buy[ i ] = 1;
            buyamount = ( ( 1 / X ) * capital[i] );
        }
        else
        {
            Buy[i] = sigScaleIn;
        }

        BuyPrice = Close[i];

        _BuyPrice = Close[i];

        LongFlag = 1;

        capital -=  buyamount[i] ;

        entry_qty +=  buyamount[i]  / _BuyPrice[i];

        entry_value = Min( buyamount[i], capital[i] );

        //Min(abs_amount[i],capital[i]);	//( ( 1 / X ) * initial_equity );

        trade_counter += 1;

        exit_qty = 0;
		
		BB[i]=1;
    }

    check[i] = entry_qty[i];

    //buyamount = ( ( 1 / X ) * capital[i] );
    BA[i] = buyamount[i];

    if( _sell[i]  AND capital[i] < C[i] AND LongFlag[i] == 1 AND BB[i]==0)
    {

        Sell[i] = 1;
        SellPrice[i] = Close[i];
        capital = Close[i] * entry_qty[i] + capital[i];
        longflag = 0;
        trade_counter = 0;
        exit_qty = entry_qty[i];
        entry_qty = 0;
        buyamount = capital[i];


    }


    CP[i] = capital[i];
    LF[i] = longflag[i];;
    EV[i] = entry_value[i];
    XQTY[i] = exit_qty[i];
    EQTY[i] = entry_qty[i];
    TC[i] = trade_counter[i];


}


GfxTextOut( "Entry Qty: " + check, 30, 50 );
GfxTextOut( "Capital: " + cp, 30, 100 );
GfxTextOut( "TC: " + TC, 30, 150 );
GfxTextOut( "Buy Amount: " + BA, 30, 200 );
GfxTextOut( "Entry Value: " + EV, 30, 250 );
//dist = 1.3*ATR(10);

//for( i = 0; i < BarCount; i++ )
//{
//if( Buy[i] ) PlotText( "Buy @ " + C[ i ] + " :: "+EV[i] + " :: "+EQTY, i, L[ i ], colorGreen );
//if( Sell[i] ) PlotText( "Sell @ " + C[ i ] + " :: "+CP[i]+" :: "+XQTY, i, H[ i ]+dist[i], colorRed );

//}


PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorGreen, 0, L, Offset = -40 );
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorLime, 0, L, Offset = -50 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorWhite, 0, L, Offset = -45 );
PlotShapes( IIf( Sell , shapeStar, shapeNone ), colorRed , 0, H, Offset = 45 );
SetPositionSize( BA, spsValue ); // for first entry
SetPositionSize( BA, IIf( Buy == sigScaleIn, spsValue, spsNoChange ) ); // scale in 25% after initial position

_SECTION_BEGIN( "New Chart" );

SetOption( "WarningLevel", True );

barcolor = ( ( IIf( C > O, colorGreen, colorRed ) ) );

SetBarFillColor( barcolor );

Plot( C, "Price", barcolor, styleCandle );


SetChartOptions( 0, chartShowArrows | chartShowDates );
SetBarsRequired( sbrAll, sbrAll );

GraphXSpace = 10;
dec = ( 2 / 10 ) + 1;
Title = EncodeColor( 55 ) +  Title = Name() + "     " + EncodeColor( 32 ) + Date() +
                                     "      " + EncodeColor( 5 ) + "{{INTERVAL}}  " +
                                     EncodeColor( 55 ) + "     Open = " + EncodeColor( 52 ) + WriteVal( O, dec ) +
                                     EncodeColor( 55 ) + "     High = " + EncodeColor( 5 ) + WriteVal( H, dec ) +
                                     EncodeColor( 55 ) + "      Low = " + EncodeColor( 32 ) + WriteVal( L, dec ) +
                                     EncodeColor( 55 ) + "    Close = " + EncodeColor( 7 ) + WriteVal( C, dec ) +
                                     EncodeColor( 55 ) + "    Volume = " + EncodeColor( 11 ) + WriteVal( V, 1 ) +
                                     EncodeColor( 55 ) + "    ROC = " + EncodeColor( colorOrange ) + WriteVal( SelectedValue( ROC( C, 1 ) ) );
EncodeColor( colorBlack );
EnableTextOutput( False );

_SECTION_END();

The problem I have is that whilst the backtest trade list starts with 20 scale in (0-19), after some time the number of scale ins drop. We have tested this with TQQQ (NASDAQ). Can someone help with defining why the number of scale-ins are decreasing in time?

Thanks,
Arpad

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

Specifically in this case use DETAILED REPORT MODE and see what it says. You might be hitting liquidity limits. Check this: Liquidity limit hit, was: SetPositionSize