Hi All, I'm relatively new to AFL, have some prior programming experience although not an expert. I have searched Web, KBs, and AFL Guide and whilst there are several references to position sizing and equity i haven't been able to pull together a backtest module that meets my goals.
My goal: adopt a risk-based position size approach for futures (forex) trading, with:
Equity 0-1m => 2% of equity risked per trade (using a 2xATR based stop for my 1R position)
Equity 1m+ => 1.5% of equity risked per trade (same ATR range stop for 1R calcs)
Code snippet below:
RiskAppetite = 2; // initial risk appetite = 2 % StopLoss_ATR_Range = 2; // calculate 1R exit based on 2xATR retracement AvgTrueRange = Ref( ATR( Param_ShortPeriod ), -1); // calc short period average true range StopLossRiskPerShare = StopLoss_ATR_Range * AvgTrueRange; // calc stop loss SingleTradeEquityRiskPct = RiskAppetite / 100 / StopLossRiskPerShare; SetPositionSize( SingleTradeEquityRiskPct, spsPercentOfEquity ); // running in futures mode with 100:1 leverage //i'd love to somehow hav this varying based on equity getting to thresholds // have tried: // RiskAppetite = IIf (Equity() < 1000000, RiskAppetite, RiskAppetite* 0.75); // as an alternative to current RiskAppetite calc // have also tried: // SetPositionSize( IIf (Equity() < 1000000, SingleTradeEquityRiskPct, SingleTradeEquityRiskPct * 0.75), spsPercentOfEquity ); // as an alternative to current position size calc
Problem i'm experiencing: I'm finding that the RiskAppetite seems to remain static through the entire backtest cycle, so my position sizes are calculated based on 2% in all cases. I can see Equity() growing so i'm assuming i'm doing something wrong with my array referencing (or my approach in general stinks ).
I'd appreciate any guidance on how to solve (or where to look!)
Thanks in advance!