Pine Script strategy.entry: The Complete Parameter Reference

March 17, 2026

Pine Script strategy.entry: The Complete Parameter Reference

March 17, 2026

Pine Script's strategy.entry() is the function you call to open a position in a TradingView strategy. Understanding its parameters — especially the difference between market, limit, and stop order types, how position sizing works, and what alert_message does — is essential before you can write any non-trivial strategy. This guide covers the complete parameter reference with practical examples ranging from a simple MA crossover to pyramiding.

Parameter Type Description id string Unique name for this entry direction const strategy.long or .short qty float Contracts/shares (optional) limit float Limit order price stop float Stop order price comment string Label shown on chart alert_message string Text sent with alert/webhook
Complete strategy.entry() parameter reference

1. Basic Usage

pine
//@version=5
strategy("My Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)

fastMA = ta.ema(close, 9)
slowMA = ta.ema(close, 21)

// strategy.entry(id, direction)
// id: unique string that identifies this order (used to cancel or close it later)
// direction: strategy.long or strategy.short

if ta.crossover(fastMA, slowMA)
    strategy.entry("Long Entry", strategy.long)

if ta.crossunder(fastMA, slowMA)
    strategy.entry("Short Entry", strategy.short)

2. Order Types: Market, Limit, and Stop

The limit and stop parameters change the order type:

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

signal = ta.crossover(ta.ema(close, 9), ta.ema(close, 21))

if signal
    // Market order (default — no limit or stop parameter)
    strategy.entry("Market Long", strategy.long)

    // Limit order — fills only at price ≤ limitPrice (for long)
    limitPrice = close * 0.99   // 1% below current close
    strategy.entry("Limit Long", strategy.long, limit=limitPrice)

    // Stop order — activates when price reaches stopPrice, then fills at market
    stopPrice = close * 1.01    // 1% above current close
    strategy.entry("Stop Long", strategy.long, stop=stopPrice)

    // Stop-limit — both params: activates at stop, fills at limit
    strategy.entry("StopLimit", strategy.long, stop=stopPrice, limit=limitPrice)

3. Position Sizing with qty

//@version=5
// default_qty_type controls what default_qty_value means
strategy("Sizing Demo", overlay=true,
    default_qty_type  = strategy.percent_of_equity,  // use % of equity
    default_qty_value = 10)                          // default: 10% per trade

// Override qty for a specific entry (in contracts/shares if no default_qty_type override)
strategy.entry("Big Trade", strategy.long, qty=100)

// Size based on ATR (volatility-adjusted)
atrSize = ta.atr(14)
riskAmount = strategy.equity * 0.01   // risk 1% of equity
qtyFromRisk = math.floor(riskAmount / (atrSize * 2))

if ta.crossover(ta.ema(close, 9), ta.ema(close, 21))
    strategy.entry("Volatility Sized", strategy.long, qty=qtyFromRisk)

4. comment and alert_message

//@version=5
strategy("Comment Demo", overlay=true)

buySignal = ta.crossover(ta.ema(close, 9), ta.ema(close, 21))

if buySignal
    strategy.entry(
        id            = "Buy",
        direction     = strategy.long,
        comment       = "EMA Cross",           // label shown on the chart at entry bar
        alert_message = '{"action":"buy","symbol":"' + syminfo.ticker + '","price":' + str.tostring(close) + '}'
    )
    // alert_message is sent when the strategy's alert fires
    // Create the alert: Alerts → Create → Condition: "Your Strategy" → "Order fills only"

5. strategy.entry vs strategy.order vs strategy.close

  • strategy.entry() — opens a new position (or adds to existing if pyramiding is enabled). If an opposite position is open, it closes it first then opens the new one. Two entries with the same id and direction cancel and replace each other.
  • strategy.order() — lower-level function with more control. Doesn't automatically close opposite positions. Use when you need precise order management.
  • strategy.close() — closes an existing entry by its id.
  • strategy.close_all() — closes all open positions immediately.

6. Pyramiding Example

//@version=5
// pyramiding=3 allows up to 3 entries in the same direction
strategy("Pyramid Demo", overlay=true, pyramiding=3,
    default_qty_type=strategy.percent_of_equity, default_qty_value=5)

rsi = ta.rsi(close, 14)

// Each entry has a unique id — all three can be open simultaneously
if rsi < 35
    strategy.entry("Long 1", strategy.long, comment="RSI < 35")
if rsi < 30
    strategy.entry("Long 2", strategy.long, comment="RSI < 30")
if rsi < 25
    strategy.entry("Long 3", strategy.long, comment="RSI < 25")

// Close all when RSI recovers
if rsi > 60
    strategy.close_all(comment="RSI recovered")

To trigger alerts when entries fire, see the Pine Script alerts guide. To control what inputs drive your strategy, see Pine Script input() guide.

Summary

The minimum call is strategy.entry("id", strategy.long). Add limit=price for limit orders, stop=price for stop orders, qty=n to override default sizing, comment="text" for chart labels, and alert_message="json" for webhook payloads. Use unique id strings to differentiate entries so you can close specific positions with strategy.close("id").