Hi, I am trying to rebuild the futures trend following strategy from Andreas Clenow's book Following The Trend. I am having a slight issue with the position sizing formulas and I have checked it several ways, even calculating manually. Basically I want position size to be based on 1 ATR with 0.20% of equity at risk.
When I use the SetPositionSize function I end up getting a very slightly different number of contracts than when I calculate it manually (which I have done using the formulas provided in the AFL guide). I can see from the trade log exactly what the starting equity was and I can see the ATR based on the chart. I can't figure out why the number of contracts is different and I have tried a number of things - remove commissions, remove interest on cash. The difference is small (i.e. 2 decimal places) but as the account grows in the future the number of contracts differs more meaningfully.
Happy to hear any suggestions. Thanks!
Portfolio = 1; // 1 = Portfolio, 0 = All Trades (Switch between a portfolio test or an all trades test)
SameBarExit = 0; // 1 = allow same bar exit, 0 = disallow
EntryTiming = 1; // 0 = Same Bar on Close, 1 = Next Bar (Controls entry timing)
ExitTiming = 1; // 0 = Same Bar on Close, 1 = Next Bar (Controls exit timing)
PosQty = 50; // Controls portfolio size / total number of positions
Margin = 300; // Sets account margin. 100 = no margin
////////////////////////////////////////////////////
// Position Sizing Algo
ATRLength = 100;
RiskPerContract = 1 * ATR( ATRLength );
PositionRisk = 0.2; // % risk per position
PctSize = ( PositionRisk * MarginDeposit ) / ( RiskPerContract * PointValue );
Breakout = 50;
Stoploss = 25;
Breakdown = 50;
ShortStoploss = 25;
////////////////////////////////////////////////////
// Set up and verify AB environment
SetOption("InitialEquity",IIf(Portfolio,1000*1000,5000*1000)); // If test is portfolio start with $1m otherwise start with $5m
SetOption("CommissionMode",3); // Set commision mode to 3 = $ per share/contract
SetOption("CommissionAmount",IIf(Portfolio, 0, 0)); // If test is portfolio set commission to $0.01 per share otherwise zero
SetOption("AllowSameBarExit", SameBarExit); // Call same bar exit rule in controls
SetOption("MaxOpenPositions",IIf(Portfolio,PosQty,2500)); // If test is portfolio allow PosQty (50) positions otherwise 2500
SetOption("AccountMargin",IIf(Portfolio,Margin,500)); // If test is portfolio call Margin otherwise leave at 100
SetOption("UsePrevBarEquityForPosSizing",IIf(EntryTiming==0,False,True)); // If entry is same bar do not use previous bar for position sizing
SetOption("AllowPositionShrinking", True); // Allow smaller position size if not enought capital available
SetTradeDelays(EntryTiming,ExitTiming,EntryTiming,ExitTiming); // Set trade delays according to timing controls above
SetOption("ActivateStopsImmediately", IIf(ExitTiming==0,False,True)); // If exit is same bar do not active stop immediately
SetPositionSize(IIf(Portfolio,PctSize,1),IIf(Portfolio,spsPercentOfEquity,spsShares)); // Set position size to percent of equity if portfolio test otherwise set to 1 contract
RoundLotSize = IIf(Portfolio,0,1); // Round position sizing to 1 share/contract (no fractional shares)
////////////////////////////////////////////////////
// High-level AFL logic
// Initialize all arrays so the backtest will always run
Buy = Sell = Short = Cover = 0;
BuyPrice = ShortPrice = O; // If entry timing is 0 buy/short on the close otherwise use the average price
SellPrice = CoverPrice = O; // If exit timing is 0 sell/cover on the close otherwise use the average price
//////////////////////////////////////////
// ENTER YOUR ENTRY AND EXIT RULES BELOW
Buy = C > Ref(HHV(C,breakout),-1);
Sell = C < Ref(LLV(C,stoploss),-1);
Short = C < Ref(LLV(C,breakdown),-1);
Cover = C > Ref(HHV(C,ShortStoploss),-1);