How to limit the amount that can be invested in one stock

Hi - is there a way in backtesting to cap the amount that can be invested in one stock?

I am using portfolio testing to test buying 1-5 stocks in a portfolio:

pos = Optimize("Max Positions",3,1,5,1);
SetOption("MaxOpenPositions", pos);
SetPositionSize(1, spsShares);
PositionSize = -(100/pos);

The issue I have is that when, for example, pos=1, and the equity gets to above, say, $700,000 - that this becomes unnatural to buy this amount of the stock in the real world.

So the tests are giving inaccurate results.

Is it possible to Optimize the MaxOpenPositions as above, but cap the amount one stock can buy?

Thanks :slight_smile:

Look at Analysis settings - Portfolio tab - Limit trade size...


Thanks fxshrat - as always :slight_smile:

Is there a way to do this in AFL?

Yes, you can do so via mid level CBT.

(Depending on what's your volume array) you may calculate shares or position value within CBT and if signal then check that size against bar volume of symbol (volume array is passed to CBT via static variables). If pos. value or shares exceeds certain level (n-percent of bar volume) then you set sig.PosSize to n-percent of bar volume (or whatever appropriate value of your choice).