Balance positions based on ATR?

Hello everyone and thank you in advance for any help you can provide. I have a rotational system that maintains 4 positions on a monthly basis. Right now they are balanced 25% each. I would like to balance by equalizing their ATR. For example if Stock 1 has ATR(10) = 10 and stock 2 has ATR(10) of 5, I would like the rotation to buy 2 share of stock 2 for every one share of stock 1. This equalizes my exposure in terms of price movement. I would need the code to use position score to identify the four stocks for this rotation, then read each of those 4 stock's ATR(10) and then allocate the portfolio to balance this to as close as 100% exposure as possible. Where I am stuck is referencing the 4 selected stocks in the code to pull the atr so I can balance it.

Here is the base code:


SetOption("InitialEquity", 100000);

totalPositions =4;

SetOption("MaxOpenPositions", totalPositions);

SetOption("AllowPositionShrinking", True );

SetPositionSize(100/ totalPositions, spsPercentOfEquity);

SetOption("AllowSameBarExit", True );

SetOption("HoldMinBars", 1);


lastDayOfMonth = Month() != Ref(Month(), 1);


score = ((ROC(Close, 84) + ROC(Close,  105)) + ROC(Close, 252))/ 3 ;

PositionScore = 1000 + score;

Sell = lastDayOfMonth ;
Buy = lastDayOfMonth ;

You would need to use a CBT (Custom Backtest) to implement any sort of volatility-weighted position sizing. In the CBT you will be able to identify the top 4 symbols on each rotation date, retrieve their ATR values (saved in a static variable during phase 1), and adjust the position sizes accordingly before entering the trade. Not sure what your experience level with AFL is, but this is not an exercise for the faint of heart.

2 Likes

In case you want to try, take a look at the Mid-Level Interface examples in the link below

https://www.amibroker.org/userkb/2008/03/16/amibroker-custom-backtester-interface-2/

3 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.