Adjusting risk-based position sizing based on growing equity (for backtesting)

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 :-1:).

I'd appreciate any guidance on how to solve (or where to look!)

Thanks in advance!
Craig

Hello,

for that tat the best way is to use cbt, since you can only know portfolio equity value during second phase of backtest.

Something like this:

SetCustomBacktestProc("");

if (Status("action") == actionPortfolio) 
{
    bo = GetBacktesterObject();	//  Get backtester object
    bo.PreProcess();	//  Do pre-processing (always required)

    for (i = 0; i < BarCount; i++)	//  Loop through all bars
    {
        for (sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
        {	
            if (sig.IsEntry()) // entry signal
			{
				eq=bo.Equity;
				if(eq>1000000) sig.PosSize=sig.PosSize*0.75;
				
			}	
        }	
        
        bo.ProcessTradeSignals( i );	//  Process trades at bar (always required)
    }	  
      
    bo.PostProcess();	//  Do post-processing (always required)
}

Thanks pmxgs I'll give it a try :slight_smile:

@Skippy, don't forget to verify your License

1 Like

Make sure to check Knowledge Base that covers the subject:
http://www.amibroker.com/kb/2014/10/12/position-sizing-based-on-risk/

Thanks Tomasz (and thanks for the awesome product). I have read this a few times (as you have recommended many others to do!) but need to keep working on my dev and testing to embed knowledge. appreciate you all taking time to reply.