HomePine Script Snippets › Max Contract Limit

Pine Script Max Contract Size Limit

Caps order size at your firm’s scaling-plan contract limit so an oversized order never breaches the rule.

//@version=6
strategy("Max Contract Limit", overlay=true, calc_on_every_tick=false)

// ── Inputs ──────────────────────────────────────────────
desiredQty   = input.int(5, "Desired Contracts", minval=1)
maxContracts = input.int(2, "Firm Max Contracts", minval=1,
     tooltip="Your scaling-plan limit. Check your firm's current ladder before setting this.")

// ── Capped quantity ──────────────────────────────────────
qty = math.min(desiredQty, maxContracts)

// ── Entry logic (replace with your own conditions) ───────
longCondition = ta.crossover(ta.ema(close, 9), ta.ema(close, 21))
if longCondition and strategy.position_size == 0
    strategy.entry("Long", strategy.long, qty=qty)
    strategy.exit("Long Exit", "Long", profit=20, loss=10)

shortCondition = ta.crossunder(ta.ema(close, 9), ta.ema(close, 21))
if shortCondition and strategy.position_size == 0
    strategy.entry("Short", strategy.short, qty=qty)
    strategy.exit("Short Exit", "Short", profit=20, loss=10)

// ── Readout ──────────────────────────────────────────────
var table t = table.new(position.top_right, 1, 1)
if barstate.islast
    table.cell(t, 0, 0, "Order qty: " + str.tostring(qty) + " (cap " + str.tostring(maxContracts) + ")",
     bgcolor=color.new(color.orange, 80), text_color=color.white)

Settings

How to use

Frequently Asked Questions

How do I cap position size in Pine Script?

Run the quantity through math.min(desiredQty, maxContracts) before passing it to strategy.entry. Whatever your sizing logic wants, the order that reaches the firm can never exceed the cap. Guard both long and short entries — the limit applies to either direction of exposure.

What is a prop firm scaling plan?

Most futures prop firms limit how many contracts you may trade at each stage of the account, expanding the limit as your balance grows. An Apex 50k starts around 5 minis (50 micros); Topstep ties the ladder to buffer milestones. Exceeding the limit is a rule breach at most firms — check the rules database for your firm’s exact ladder.

Related

Want a complete eval-ready strategy sized for your firm?

This snippet is a building block. Our paid scripts are complete strategies — entries, contract caps, kill switch, session filter, and EOD flatten tested on 3 years of MES/NQ data. Instant delivery from $50.

View Plans — From $50