I am trying to manage position sizing and after searching this forum and Amibroker guide https://www.amibroker.com/guide/h_pyramid.html tried to create the following.
- Exit 50% position in Target 1
- Exit 50% position if hits SL 1
- Exit rest 50% position either if the Exit criteria satisfy or if SL 2 hit.
When I am trying to backtest, its not giving me any resul, however trace is giving me result.
Attaching the AFL code as reference, any help would be appreciated specially from
type or paste cFirstProfitTarget = 50; // profit
pct = 50;
FirstSL = 20;
FinalSL = 40;
//Array Initialization
Buy = Sell = Short = Cover = TSL = Null;
priceatbuy = 0;
highsincebuy = 0;
exit = 0;
text = "";
EOD_Sell = 0;
//Using Loop to generate signals
for( i = 0; i < BarCount; i++ )
{
//Long Positions
if( _Buy[ i ] AND priceatbuy == 0 AND tn[ i ] < 143100 )
{
Buy[ i ] = 1;
BuyPrice[ i ] = _BuyPrice[ i ];
priceatbuy = BuyPrice[ i ];
Qty = _Qty;
_TRACE( "Inside Buy Block " + ", Buy Price: " + priceatbuy + ", BuyBB_Price: " + Lower_BB_H + ", Buy_SAR: " + Buy_SAR + ", Buy_BB: " + ( O > Upper_BB_H ) + ", Qty: " + Qty );
}
if( priceatbuy > 0 )
{
if( Buy[i] != 1 AND exit == 0 AND High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy )
{
// first profit target hit - scale-out exit at closed price
exit = 1;
Buy[ i ] = sigScaleOut;
Qty = _Qty / 2;
_TRACE( "First Target block" + ", Target Price: " + ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy + ", Qty: " + Qty );
}
if( Buy[i] != 1 AND exit == 0 AND Low[ i ] <= ( 1 - FirstSL * 0.01 ) * priceatbuy )
{
// First SL point hit - scale-out exit at closed price
exit = 1;
Sell[ i ] = sigScaleOut;
Qty = _Qty / 2;
_TRACE( "First Stop Loss Hit: " + ", SL Price: " + ( 1 - FirstSL * 0.01 ) * priceatbuy + ", Qty: " + Qty );
}
if( ( _Sell[ i ] OR tn[ i ] >= 151400 OR Low[ i ] <= ( 1 - FinalSL * 0.01 ) * priceatbuy ) )
{
Sell[ i ] = 1; //Selling-off the Long position
if( exit = 1 ) Qty = _Qty / 2;
else
if( exit = 0 ) Qty = _Qty;
if( EOD_Sell == 0 AND _sell[ i ] )
{
SellPrice[ i ] = _SellPrice[ i ];
_TRACE( "Final Exit block" + ", Exit True :" + Short_BB[ i ] + ", Sell Price: " + _SellPrice[ i ] + ", Qty: " + Qty );
EOD_Sell = 1;
}
else
if( EOD_Sell == 0 AND tn[ i ] >= 151400 )
{
SellPrice[ i ] = O[ i ];
_TRACE( "Final Exit block" + ", Exit :" + tn[ i ] + ", Qty: " + Qty );
EOD_Sell = 1;
}
else
if( EOD_Sell == 0 AND Low[ i ] <= ( 1 - FinalSL * 0.01 ) * priceatbuy )
{
SellPrice[ i ] = ( 1 - FinalSL * 0.01 ) * priceatbuy ;
_TRACE( "Final Exit block" + ", Exit Time :" + ( 1 - FinalSL * 0.01 ) * priceatbuy + ", Qty: " + Qty );
EOD_Sell = 1;
}
exit = 2;
}
if( exit >= 2 )
{
Buy[ i ] = 0;
Sell[ i ] = exit + 1; // mark appropriate exit code
exit = 0;
priceatbuy = 0; // reset price
highsincebuy = 0;
LongFlag = 0;
}
}
if( Buy[i] )
PlotText( "Buy@\n" + Prec( ValueWhen( Buy, Buyprice[ i ] ), 1 ), i, L[ i ], colorBrightGreen, colorBlack, -65 );
if( Sell[i] )
PlotText( "Sell@\n" + Prec( ValueWhen( Sell, SellPrice[ i ] ), 1 ), i, L[ i ] , colorgold, colorBlack, -65 );
}
SetPositionSize( QTY, spsShares );
`SetPositionSize( 1, IIf( Buy == sigScaleOut OR Sell == sigScaleOut, spsShares, spsNoChange ) );`
in the above code I am more interested to know why this is not giving me any result i backtest. Specially I am not sure if the last statement in my code is correctly coded.
type oSetPositionSize( 1, IIf( Buy == sigScaleOut OR Sell == sigScaleOut, spsShares, spsNoChange ) );
Where I want to scaleout in either target 1 achieve or SL 1 hit.
Any help would be appreciated.