Position sizing (equal % of equity)

Hi, how would you code this ->

position size = cash / ( max position count - open position count)

(obviously cash = equity - value of open positions)

I'm sorry but I'm confused with AFL language, I can't do it :-/

Thanks :))


This is essentially the same as putting same percentage of equity in each position:


SetPositionSize( 100 / max_pos_count, spsPercentOfEquity );
1 Like

Hi Tomasz, thanks for joining the discussion. Your formula is very different from what I need. Equity is not cash (equity is cash + value of open positions) and max pos count is different from (max pos count - open pos count).
With your standard formula (percentage of equity) I get signals that don't get filled. For example I have 1000$ initial capital and I diversify in 2, first signal gets filled with 500$ right ? if this trade grows into 1000$ (lucky me !) for the next signal Amibroker will ask for 750$, (1000 of open pos +500 of cash)/2, but it won't get filled because I have only 500$ left...
You see the difference ? I need to split the cash for rolling signals, not the equity (I don't want to scale out btw)
Thanks for any help :slight_smile:

...and if I use "Allow position size shrinking" it will not work with 3 stocks or more. If my 1st position grows big Amibroker will allocate all remaining cash to the second position because it splits equity and not cash. So my 3rd position never gets filled.

You are mistaken.

If you do the math you will see. I don't have time to explain to you.
The calc are pretty basic (rounded to single dollar for simplicity):

With 33% of equity size and $1000 equity, 3 positions: you get $333 per position.

With your "other method" the result is basically the same:
First you have $1000 and (max pos count - openpos count) = 3 so your first position is... surprise = $333
Second position $667 cash left, (max pos count - openpos count) = 2, so your second position is ... .surprise $667/2 = $333
And third position $334 cash left, (max pos count - openpos count) = 1, so your Third position is ... .surprise $334/1 = $334

The same result as with the method I told you to use. There may be minor differences if open trade values move significantly (more than 10%) but otherwise it is negligible.
Of course you can do full blown "manual" pos sizing in custom backtest, but it is not worth the effort.

And yes you need "allow pos shrinking" (in fact you need it in 99% of cases because of commissions!!!!)


As you say when open trade values change the Equity percentage method does not work. Try Buy & Hold from 1980 with AMZN AMEH and AMMA you'll see that position 3 never gets filled (or maybe just 1 share). The example you give is exactly what I want but how do you code it ? What's the function to get value of cash ? and the function to get open and close positions count ?

Ok I've found the functions but I don't know how to use them. It's in Custom Backtesting:
long GetOpenPosQty() returns number of currently open positions (Methods)
double Cash returns available cash in your portfolio (Properties)

@hastoy, this is what @Tomasz was explaining to you when he said:

Of course you can do full blown "manual" pos sizing in custom backtest, but it is not worth the effort.

It's not that writing a CBT is that difficult, but if you are new to AmiBroker and AFL it might be a lot for you to take on.