Pine Script Max Contract Size Limit

Runs the desired order size through a firm scaling-plan cap before it reaches strategy.entry, so a stale input or a sizing bug can never place an oversized order.

Pine Script v6 Free · TradingView free plan
Pine Script
//@version=6
strategy("Target Filled - Max Contract Limit Example", overlay=true, calc_on_every_tick=false)

// --- Inputs ---------------------------------------------------
wantedQty = input.int(5, "Desired Contracts", minval=1)
firmCap   = input.int(2, "Firm Scaling Cap", minval=1,
     tooltip="Your current scaling-plan limit. Raise it as the account clears milestones.")

// --- Enforce the cap before any order is sent --------------------
sendQty = math.min(wantedQty, firmCap)

// --- Sample signal — replace with your own entry logic ---------
fastMA = ta.ema(close, 9)
slowMA = ta.ema(close, 21)

if ta.crossover(fastMA, slowMA) and strategy.position_size == 0
    strategy.entry("Long", strategy.long, qty=sendQty)
    strategy.exit("Long Exit", "Long", profit=20, loss=10)

if ta.crossunder(fastMA, slowMA) and strategy.position_size == 0
    strategy.entry("Short", strategy.short, qty=sendQty)
    strategy.exit("Short Exit", "Short", profit=20, loss=10)

// --- On-chart readout ---------------------------------------------
var table readout = table.new(position.top_right, 1, 1)
if barstate.islast
    table.cell(readout, 0, 0, "Sending " + str.tostring(sendQty) + " / cap " + str.tostring(firmCap),
     bgcolor=color.new(color.teal, 80), text_color=color.white)

Settings

InputDefaultPurpose
wantedQty5What your sizing logic wants to trade before the cap is applied.
firmCap2Hard ceiling from your scaling plan — the order can never exceed it.

How to use

  • Paste into the Pine Editor and swap the EMA-crossover placeholder for your own entry conditions.
  • If quantity comes from a risk calculation elsewhere, route its output through math.min(qty, firmCap) right before strategy.entry.
  • Bump firmCap as the account clears scaling milestones — pull the current number from your firm's dashboard first.
  • The top-right table confirms the quantity actually sent, so you can verify the clamp is doing its job.

Typical scaling-plan caps

Contract ceilings vary by firm and account size, and they change over time. The figures below are current per-firm numbers — always confirm against your own dashboard before wiring firmCap into a live strategy.

FirmAccountContract Cap
Apex50k10 MES / 10 MNQ
Apex100k2 ES
Apex150k2 NQ
Topstep50k5 micros / 1 mini
Topstep100k10 micros / 2 minis
Topstep150k15 micros / 3 minis

Frequently Asked Questions

How do I cap position size in Pine Script?
Run whatever quantity your sizing logic produces through math.min(qty, cap) before it ever reaches strategy.entry. The clamp has to sit on both the long and the short branch — a scaling bug that only guards one direction still breaches the rule the first time it fires the other way.
What is a prop firm scaling plan?
A schedule that limits how many contracts an account may hold at once, with the ceiling rising as balance or buffer grows. Apex, for example, allows 10 MES or MNQ contracts on a 50k account but only 2 ES contracts once balance supports a 100k-style position. Topstep scales differently by account size — check the current ladder on the rules database before hardcoding a number, since firms revise these periodically.

Get a Pine Script tuned to your prop firm.

Invite-only on TradingView within 24 hours. From $19/mo, cancel anytime.