Specifies the number of shares or contracts to buy or sell short when opening a new position
Any formula specifying a number of shares or contracts, or a position value, or an allocation percentage (see QtyType)
The Quantity formula specifies the position size to use each trade in a strategy.
Quantity is calculated at position entry time using the same current bar context as EntrySetup.
If there is no Quantity formula then a strategy will always invest 100% of current Allocation in each position.
If Quantity returns 0 then the entry is skipped and "zero quantity" is displayed as the skip reason.
Quantity can optionally be used to specify the Side (long vs. short) of a trade in a strategy that can go either way, such as a hedging strategy.
If Side is specified for a strategy, then the strategy always trades that side only and Quantity should be written to return a positive number of shares or contracts whether the side is long or short.
If Side is not specified, then Quantity should return a positive number of shares to enter a long position or a negative number of shares to enter a short position.
(The default QtyType is Shares, and all of the following notes assume this type. In many cases, it may be simpler to specify QtyType as Value or a Percent.)
For one-sided strategies, the FillPrice variable is allowed to be used in the Quantity formula. This simplifies position size calculations for strategies that enter using limit or stop orders in particular.
Note that if, in your live trading, you place limit or stop orders before the market opens (with no knowledge of pre-open prices), then it is not realistic to assume that you'll be able to accurately size your positions to your actual entry prices.
If, on the other hand, you use real-time quotes to trigger your entry orders intraday, then it is completely realistic to calculate Quantity using FillPrice.
The simplest Quantity formula, such as might be used for a one-contract futures strategy, is Quantity: 1.
Similarly for stocks, a strategy that always trades 100 shares would simply use Quantity: 100.
To always invest, for example, 10% of current allocation, use Quantity: 0.1 * S.Alloc.
For risk-based position sizing, assuming that you have pre-calculated a Data item called StopPrice and want to risk 1% of current allocation on every trade, use Quantity: 0.01 * S.Alloc / Abs(FillPrice - StopPrice)
For volatility-adjusted position sizing (trading smaller when volatility is higher), there are many possible approaches, but here's an example that can work for stocks or futures: Quantity: 0.01 * S.Alloc / (3 * ATR(20)) / PointValue
The example script mr_sample_hedge.rts shows a Quantity formula that derives a long or short position size in SPY from the relative investment levels of two other strategies. Simplified to replace parameters with default values, this is: Quantity: (S.Alloc / 10) * (Extern(@mr_short,S.Positions) - Extern(@mr_long, S.Positions)) / C.
See Asset Allocation and Position Sizing for additional information.