SYNTAX SetPositionSize( size, method )
RETURNS ARRAY
FUNCTION This function allows to control trade (position) size in four different ways, depending on 'method' parameter.
Parameters:
size (ARRAY) defines desired trade size
method (ARRAY) defines how 'size' is interpreted
spsValue (=1) - dollar value of size (as in previous versions)
spsPercentOfEquity (=2) - size expressed as percent of portfolio-level equity (size must be from ..100 (for regular accounts) or .1000 for margin accounts)
spsShares (=4) - size expressed in shares/contracts (size must be > 0 )
spsPercentOfPosition (=3) - size expressed as percent of currently open position (for SCALING IN and SCALING OUT ONLY)
spsNoChange (=0) - don't change previously set size for given bar
New SetPositionSize function automatically encodes new methods of expressing position size into old "positionsize" variable as follows:
values below -2000 encode share count,
values between -2000 and -1000 encode % of current position
values between -1000 and 0 encode % of portfolio equity
values above 0 encode dollar value
Although it is possible to assign these values directly to old-style PositionSize variable, new code should use SetPositionSize function for clarity.
EXAMPLE For example to liquidate 50% of position simply use
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) );
Special value spsNoChange (=0) means don't change previously set size for given bar (allows to write constructs like that):
SetPositionSize( 100, spsShares ); // 100 shares by default
SetPositionSize( 50, IIf( Buy == sigScaleOut, spsPercentOfPosition, spsNoChange ) ); // for scale-out use 50% of current position size
Example of code that exits 50% on first profit target, 50% on next profit target and everything at trailing stop:
Buy = Cross( MA( C, 10 ), MA( C, 50 ) );
Sell = 0;
// the system will exit
// 50% of position if FIRST PROFIT TARGET stop is hit
// 50% of position is SECOND PROFIT TARGET stop is hit
// 100% of position if TRAILING STOP is hit
FirstProfitTarget = 10; // profit
SecondProfitTarget = 20; // in percent
TrailingStop = 10; // also in percent
priceatbuy=0;
highsincebuy = 0;
exit = 0;
for( i = 0; i < BarCount; i++ )
{
if( priceatbuy == 0 AND Buy[ i ] )
{
priceatbuy = BuyPrice[ i ];
}
if( priceatbuy > 0 )
{
highsincebuy = Max( High[ i ], highsincebuy );
if( exit == 0 AND
High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy )
{
// first profit target hit - scale-out
exit = 1;
Buy[ i ] = sigScaleOut;
}
if( exit == 1 AND
High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy )
{
// second profit target hit - exit
exit = 2;
SellPrice[ i ] = Max( Open[ i ], ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy );
}
if( Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * highsincebuy )
{
// trailing stop hit - exit
exit = 3;
SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 0.01 ) * highsincebuy );
}
if( exit >= 2 )
{
Buy[ i ] = 0;
Sell[ i ] = exit + 1; // mark appropriate exit code
exit = 0;
priceatbuy = 0; // reset price
highsincebuy = 0;
}
}
}
SetPositionSize( 50, spsPercentOfEquity );
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position
SEE ALSO
References:
The SetPositionSize function is used in the following formulas in AFL on-line library:
Connors TPS
Customised Avg. Profit %, Avg. Loss % etc
Ed Seykota's TSP: EMA Crossover System
Scale Out: Futures
testing multiple system simulataneously
Trend Following System
Unreviewed user comments:
MikeB
-------
2006-04-09 08:32:33 Note that the price used to sell partial (first profit target) is NOT the SellPrice but the BuyPrice. So, the code should read as follows...
if( exit == 0 AND
High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy )
{
// first profit target hit - scale-out
exit = 1;
Buy[ i ] = sigScaleOut;
BuyPrice[ i ] = ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy; // Set SellPrice via BuyPrice
}
}