AFL for backtesting -requesting modifications

#1
The system starts with buying 1 contract and then keeps on adding one contract per x dollars of profit, while it squares off 1 contract per y dollars of loss..

Found this afl.But need help to modify according to my needs.



// percent equity change threshold when pyramiding is performed
PyramidThreshold = 5;

// regular trading rules (no pyramiding)
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );

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, 500, 1000 ); // enter and scale-in size $1000, scale-out size: $500

Source : http://www.amibroker.com/guide/h_pyramid.html
 
#5
Adding LotSize when Profit increases certain Percentage
and decreasing LotSize when Profit Decreases Certain Percentage

Lets Assume Capital : 100000
Drawdown: 30%
Position Sizing with Capital :200// which means using 1:1 as Margin

Code:
Cap=100000;
dd=23;

if ( CurrentPortfolioEquity <=Cap )
sig.PosSize = -200; // 1st Tier
if ( CurrentPortfolioEquity > (Cap*(DD*1)/100 )+Cap AND CurrentPortfolioEquity < (Cap*(DD*2)/100 )+Cap )
sig.PosSize = - 220; // 2nd Tier
if ( CurrentPortfolioEquity > (Cap*(DD*2)/100 )+Cap AND CurrentPortfolioEquity < (Cap*(DD*3)/100 )+Cap )
sig.PosSize = -240; // 3 Tier
if ( CurrentPortfolioEquity > (Cap*(DD*3)/100 )+Cap AND CurrentPortfolioEquity < (Cap*(DD*4)/100 )+Cap )
sig.PosSize = -260; // 4 Tier
if ( CurrentPortfolioEquity > (Cap*(DD*4)/100 )+Cap AND CurrentPortfolioEquity < (Cap*(DD*5)/100 )+Cap)
sig.PosSize = -280; // 5 Tier
if ( CurrentPortfolioEquity > (Cap*(DD*5)/100 ) +Cap AND CurrentPortfolioEquity < (Cap*(DD*6)/100 )+Cap)
sig.PosSize = -300; // 6 Tier
if ( CurrentPortfolioEquity >(Cap* (DD*6)/100)+Cap AND CurrentPortfolioEquity < (Cap*(DD*7)/100 )+Cap)
sig.PosSize = -320; // 7 Tier
if ( CurrentPortfolioEquity >( Cap*(DD*7)/100 )+Cap AND CurrentPortfolioEquity < (Cap*(DD*8)/100 )+Cap)
sig.PosSize = -340; // 8 Tier
 

Similar threads