Aug 12, 2025 • 19 min read

Pine v6 Strategy Orders and Position Sizing

Everything you need to place entries/exits cleanly: order types, sizing models, pyramiding, and risk controls—with copy‑ready code.

Order functions and types

  • strategy.entry(id, dir, qty, limit, stop) places market/limit/stop/stop‑limit based on params.
  • strategy.exit(id, from_entry, stop, limit, trail_points, trail_offset) manages protective/target exits.
  • strategy.order() for one‑shot custom orders; prefer strategy.entry/exit for readability.

Market vs Limit vs Stop

//@version=6
strategy("Order Types", overlay=true)

// Market buy
if ta.crossover(ta.ema(close, 20), ta.ema(close, 50))
    strategy.entry("L", strategy.long)

// Limit buy example
limitPrice = close - 0.5 * ta.atr(14)
strategy.entry("Llim", strategy.long, limit=limitPrice)

// Stop buy example
stopPrice = close + 0.5 * ta.atr(14)
strategy.entry("Lstop", strategy.long, stop=stopPrice)

// Stop‑limit example
strategy.entry("Lstoplim", strategy.long, stop=stopPrice, limit=stopPrice + syminfo.mintick)

Protective exits and targets

//@version=6
strategy("Stops & Targets", overlay=true)

atr = ta.atr(14)
if ta.crossover(ta.ema(close, 20), ta.ema(close, 50))
    strategy.entry("L", strategy.long)

strategy.exit("LX", from_entry="L", stop=close - 1.5 * atr, limit=close + 2.5 * atr)

Pyramiding and scaling

Enable pyramiding in strategy() or Strategy Properties. Use distinct IDs for separate legs, and manage exits per leg, or use partial exits with strategy.exit().

Sizing models: fixed, contracts, and percent of equity

//@version=6
strategy("Position Sizing", overlay=true, initial_capital=100000)

// Fixed qty
if ta.crossover(ta.sma(close, 20), ta.sma(close, 50))
    strategy.entry("Lfix", strategy.long, qty=1)

// Percent of equity sizing
perc = input.float(2.0, "Risk % of Equity", minval=0.1)
atr = ta.atr(14)
stopPts = 2.0 * atr
cashRisk = strategy.equity * (perc / 100.0)
qty = math.max(1, math.floor(cashRisk / stopPts / syminfo.pointvalue))
if ta.crossover(ta.rsi(close, 14), 50)
    strategy.entry("Leq", strategy.long, qty=qty)

Daily loss caps and session gating

//@version=6
strategy("Daily Loss Cap", overlay=true)

sess = input.session("0930-1600", "Session (local)")
inSess = time(timeframe.period, sess)
newSess = ta.change(inSess)

var float eqOpen = na
if newSess
    eqOpen := strategy.equity

lossPerc = (strategy.equity - nz(eqOpen)) / nz(eqOpen)
allowed = inSess and lossPerc > -0.03 // stop for the day at -3%

if allowed and ta.crossover(ta.ema(close, 20), ta.ema(close, 50))
    strategy.entry("L", strategy.long)

FAQ

Why didn’t my limit order fill in backtest?

Limit orders require price to trade through the limit. If price touched but didn’t trade through, it may not fill; consider stop‑limit or market orders depending on intent.

How do I prevent over‑levering?

Use a risk model tied to ATR or a percent of equity. Set realistic commission and slippage in Strategy Properties.