Set quantities dynamically
Use Entry.QtyMultiplier to scale a strategy's leg quantities as account value changes. The multiplier applies to the structure's base quantities, preserving their proportions.
Scale new positions with NAV
The Script Engine provides:
initial_cash: the starting cash specified for the backtest.nav: the current Net Asset Value, including cash and open positions.
In the AI Editor, add this field to your existing Entry section:
"QtyMultiplier": "floor(max(1, nav / initial_cash))"
The formula divides NAV by starting cash, keeps the result at least 1, and rounds down to a whole number. For starting cash of $10,000:
| NAV | Multiplier |
|---|---|
| $8,000 | 1 |
| $10,000 | 1 |
| $15,000 | 1 |
| $20,000 | 2 |
| $30,000 | 3 |
For a structure with leg quantities 1, -2, and 1, a multiplier of 2 produces quantities 2, -4, and 2.
This formula keeps a minimum multiplier of 1. It can reduce the multiplier after a decline from a higher level, but never below the original structure's size. Choose a sizing rule that matches the behavior you want to test.
The multiplier sizes new positions at entry. It does not resize positions that are already open; use Adjustments to change an open position.
Scale cash-based exit thresholds
If you want a fixed cash profit target and stop loss per unit of the base structure, scale them with the same formula. Place these fields inside Exit, retaining your other exit settings:
"ProfitTarget": "1000 * floor(max(1, nav / initial_cash))",
"StopLoss": "2000 * floor(max(1, nav / initial_cash))"
With a multiplier of 2, these become a $2,000 profit target and a $4,000 stop loss. These amounts illustrate the calculation; adjust them for your strategy.
ProfitTarget and StopLoss are evaluated at entry and retained for that position. Use Exit.Conditions for rules that need to respond to changing values during the trade.
Review and test sizing
Validate the Strategy Definition in MesoSim and inspect the quantities in the backtest results. Use Clone to compare sizing rules while keeping other settings unchanged.
If the strategy holds multiple positions in flight, review their combined exposure and margin requirements. Each new position uses its own entry-time sizing calculation.