How to limit the maximum number of shares

I need some help to implement a specific position sizing by setting a limit on the maximum number of shares to trade for my strategy.

What I need to do is to trade in the amount of 15% of the current portfolio equity. But I would like to limit the number of shares to a maximum of 1000.

For example, my current equity is $100,000. The stocks price is $50. 15% of the equity is 15000. This would convert to $15000 / $50 = 3000 shares. But since I want to allow only a maximum of 1000 shares. I would need some formula to set it to 1000 shares instead of 3000 shares in the Setpositionsize command.

Thanks in advance for the help

Since the prevailing equity value at entry is not known until during the backtest, you’ll need to use the Custom Backtest Interface to do this.

You can code it using the Mid-Level CBI. An example similar to what you’re after can be found under the “Mid-Level Interface” heading on this page.