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.