Sidechair

strategy.risk.max_position_size()

Browse the reference

Caps the position size that strategy.entry orders may build, in contracts or shares.

Try it in the editor

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

Pine Script
strategy.risk.max_position_size(contracts: simple int/float) -> void

Arguments

NameTypeDefaultDescription
contractssimple int/floatrequiredThe 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

Pine ScriptNever hold more than 100Open in editor
//@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

Runs on Sidechair