Caps the position size that strategy.entry orders may build, in contracts or shares.
strategy.risk.max_position_size(contracts) limits the net position. When an entry would take the position past the limit, its quantity is cut so the position ends exactly at the limit; if the cut leaves nothing to trade, the order is not placed.
The rule applies to strategy.entry. It is the simple way to keep a pyramiding script from stacking beyond a fixed size.
Syntax
strategy.risk.max_position_size(contracts: simple int/float) -> voidArguments
| Name | Type | Default | Description |
|---|---|---|---|
| contracts | simple int/ | required | The largest position, in contracts, shares or units, that strategy.entry may build. |
Returns
Nothing. The rule stays in force for the whole run.
Remarks
- The limit is on the absolute position, so it caps shorts the same way as longs.
- Call it once, at the top level of the script, with a fixed number.
Example
//@version=6
strategy("Position cap", overlay = true, pyramiding = 5, default_qty_value = 40)
strategy.risk.max_position_size(100)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)
if fast > slow and bar_index % 20 == 0
strategy.entry("Layer " + str.tostring(strategy.opentrades + 1), strategy.long)
if ta.crossunder(fast, slow)
strategy.close_all()
plot(strategy.position_size, "Position", color.blue, display = display.data_window)See also
strategy.risk.max_intraday_filled_ordersstrategy.position_sizestrategy.entrystrategy.default_entry_qty
Runs on Sidechair