- Joined
- 18 January 2007
- Posts
- 178
- Reactions
- 0
guys, I have been testing a system with simple buy and sell rules and I was wondering how would the system perform with my Money Management and Position Sizing rules. So I looked in the help files and found the following code.
The rules I follow are:
For entry:
total fund: $100,000
every stock that will be bought will get $10,000 allocated fund (1 lot). Initial entry will be with 20% of the lot, if the stock moves in my favour then the rest of the 80% will be invested in.
For exit:
Close 50% of the position if 50% profit achieved.
Close 100% of the position if 100% profit acheived. (am I limiting my profits here ? should I let the profit run and let the trailing stop loss take care of it ?)
trailing stop loss is set to 5%.
Does the code below reflect the above rules :
The rules I follow are:
For entry:
total fund: $100,000
every stock that will be bought will get $10,000 allocated fund (1 lot). Initial entry will be with 20% of the lot, if the stock moves in my favour then the rest of the 80% will be invested in.
For exit:
Close 50% of the position if 50% profit achieved.
Close 100% of the position if 100% profit acheived. (am I limiting my profits here ? should I let the profit run and let the trailing stop loss take care of it ?)
trailing stop loss is set to 5%.
Does the code below reflect the above rules :
Code:
//-----------------Money MGMT-----------------//
PyramidThreshold = 100; // percent equity change threshold when pyramiding is performed
e = Equity(1); // generate equity without pyramiding effect
PcntProfit = 100 * ( e - ValueWhen( Buy, e ) )/ValueWhen( Buy, e );
InTrade = Flip( Buy, Sell );
// ExRem is used here to ensure that scaling-in/out occurs
// only once since trade entry
DoScaleIn = ExRem( InTrade AND PcntProfit > PyramidThreshold, Sell );
DoScaleOut = ExRem( InTrade AND PcntProfit < -PyramidThreshold, Sell );
// modify rules to handle pyramiding
Buy = Buy + sigScaleIn * DoScaleIn + sigScaleOut * DoScaleOut;
PositionSize = IIf( DoScaleOut, 5000, 5000 ); // enter and scale-in size $5000, scale-out size: $5000
//-----------------------sell---------------------------//
// 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 = 50; // profit
SecondProfitTarget = 100; // in percent
TrailingStop = 5; // 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
//---------------------------------------//