Back to TradingView PineScript for CFD Traders
Part 3

Entries and exits: strategy.entry, strategy.exit

The order primitives: how to open a position, set a stop, take a profit, and reverse on signal.

Intermediate10 min readBeginner → AdvancedLesson 11 / 17
Try it in TradingView
Bracket order: 2% stop, 4% target

strategy.exit attaches a stop AND a target to the entry id in one call. Stops are measured from strategy.position_avg_price — the actual fill.

//@version=5
strategy("Lesson 11 - Entries and exits", overlay=true,
     initial_capital=10000,
     default_qty_type=strategy.percent_of_equity,
     default_qty_value=10)

fast = ta.ema(close, 9)
slow = ta.ema(close, 21)

plot(fast, "Fast EMA", color=color.blue, linewidth=2)
plot(slow, "Slow EMA", color=color.orange, linewidth=2)

// Open on the cross.
strategy.entry("Long", strategy.long, when=ta.crossover(fast, slow))

// Bracket: stop 2% below fill, target 4% above fill.
strategy.exit("Long TP/SL", "Long",
     stop  = strategy.position_avg_price * 0.98,
     limit = strategy.position_avg_price * 1.04)

// Also flatten if the EMAs cross back down.
strategy.close("Long", when=ta.crossunder(fast, slow))
To run it
  1. 1. Open any chart in TradingView and click Pine Editor at the bottom.
  2. 2. Select everything in the editor and paste this over it, then click Save.
  3. 3. Click Add to chart. Open the Strategy Tester tab if it is a strategy.

strategy.entry: open or reverse

strategy.entry("Long", strategy.long) opens a long position with the strategy's default size. If you are already short, it reverses. The string "Long" is the entry id — it must be unique per direction so exits can target it.

strategy.close: exit everything

strategy.close("Long") closes any open position entered with id "Long". strategy.close_all closes everything regardless of id.

strategy.exit: stop and target together

strategy.exit is the workhorse for bracket-style stops and targets:

strategy.entry("Long", strategy.long, when=ta.crossover(fast, slow))
strategy.exit("Long TP/SL", "Long", profit=200, loss=100)

profit and loss are in ticks by default — see profit_type and loss_type to switch to points, percent, currency, or ATR multiples.

Percent-based stops

strategy.exit("X", "Long", stop=close * 0.98, limit=close * 1.04) sets a 2% stop and 4% target. close here is the entry bar's close — Pine snapshots the value when the exit is placed.

ATR-based stops

atrVal = ta.atr(14)
strategy.exit("X", "Long", stop=close - 2 * atrVal, limit=close + 4 * atrVal)

This adapts the stop to recent volatility. Many retail CFD traders use 1.5x–3x ATR for stops.

Trail stops

strategy.exit supports trail_points, trail_offset, and a trail_price activation. Trail stops move in your favour and lock in gains. They are the right tool when you want to ride a trend and exit only when momentum fades.

Idempotency

You can call strategy.entry on every bar — Pine knows to skip the call if the position is already in the target state. Same for strategy.exit — setting the same exit twice in a row is fine.

Reversals

To flip long → short on the opposite signal, just call strategy.entry("Short", strategy.short). Pine closes the long first, then opens the short, in one synthetic operation. There is no "close then enter" two-step in Pine.

What you just did

Lesson 11 of 17 in TradingView PineScript for CFD Traders. When you have run the examples or read the section, tick it off and move to the next lesson.