Sidechair

strategy.exit()

Browse the reference

Places stop-loss, take-profit and trailing-stop orders that close trades opened by strategy.entry.

Try it in the editor

strategy.exit(id, from_entry, ...) arms a protective bracket for the entry named from_entry (or for every open entry when from_entry is empty). It never opens a position. The orders become active when the entry fills, so you can call strategy.exit on the same bar as strategy.entry.

Set the target with limit (a price) or profit (distance in ticks), and the stop with stop (a price) or loss (distance in ticks); when you give both a price and a tick distance for the same side, the price wins. When both a target and a stop are present, they form a bracket: whichever the price path reaches first fills and the other is cancelled. If one bar touches both, the bar path (open, nearer extreme, other extreme, close) decides which came first.

A trailing stop needs trail_offset (the distance in ticks that the stop follows the best price) and an activation: trail_price (a price level) or trail_points (profit in ticks). Before activation it does nothing; after, it ratchets in the trade's favour and never backs off.

Syntax

Pine Script
strategy.exit(id: series string, from_entry: series string (optional), qty: series int/float (optional), qty_percent: series int/float (optional), profit: series int/float (optional), limit: series int/float (optional), loss: series int/float (optional), stop: series int/float (optional), trail_price: series int/float (optional), trail_points: series int/float (optional), trail_offset: series int/float (optional), oca_name: series string (optional), comment: series string (optional), comment_profit: series string (optional), comment_loss: series string (optional), comment_trailing: series string (optional), alert_message: series string (optional), alert_profit: series string (optional), alert_loss: series string (optional), alert_trailing: series string (optional), disable_alert: series bool (optional)) -> void

Arguments

NameTypeDefaultDescription
idseries stringrequiredA name for the exit order.
from_entryseries stringan empty stringThe id of the entry to exit. Empty means every open entry.
qtyseries int/floatall of the entryHow many contracts, shares or units to exit.
qty_percentseries int/float100The part of the entry to exit, as a percent of its size, from 0 to 100. Ignored when qty is set.
profitseries int/floatnaThe take-profit distance from the entry price, in ticks.
limitseries int/floatnaThe take-profit price. Takes priority over profit.
lossseries int/floatnaThe stop-loss distance from the entry price, in ticks.
stopseries int/floatnaThe stop-loss price. Takes priority over loss.
trail_priceseries int/floatnaThe price at which the trailing stop becomes active.
trail_pointsseries int/floatnaThe profit, in ticks, at which the trailing stop becomes active.
trail_offsetseries int/floatnaHow far behind the best price the trailing stop sits, in ticks.
oca_nameseries stringan empty stringThe name of an OCA group the exit orders join.
commentseries stringan empty stringText shown next to the exit order on the chart and in the list of trades.
comment_profitseries stringthe value of commentText for a fill of the take-profit order.
comment_lossseries stringthe value of commentText for a fill of the stop-loss order.
comment_trailingseries stringthe value of commentText for a fill of the trailing stop.
alert_messageseries stringan empty stringText sent as {{strategy.order.alert_message}} when this exit fills.
alert_profitseries stringthe value of alert_messageAlert text for a take-profit fill.
alert_lossseries stringthe value of alert_messageAlert text for a stop-loss fill.
alert_trailingseries stringthe value of alert_messageAlert text for a trailing-stop fill.
disable_alertseries boolfalseWhen true, an alert on the strategy does not fire for this exit's fill.

Returns

Nothing.

Remarks

  • An exit stays in force until it fills or is cancelled, so a fixed bracket needs only one call. Call it on every bar when its levels move: a call with the id of a pending exit replaces that order.
  • qty and qty_percent limit how much of the entry is exited. With neither, the whole entry is closed. Exits close the oldest trades first (FIFO), subject to the close_entries_rule in the strategy() header.
  • When the target or stop is hit, the fill price is the order level, except on a gap through it, which fills at the bar's open.
  • comment_profit, comment_loss and comment_trailing override comment for that kind of exit, and alert_profit, alert_loss and alert_trailing override alert_message the same way.
  • Only available in strategy scripts.

Examples

Pine ScriptPercent stop and targetOpen in editor
//@version=6
strategy("Bracket exit", overlay = true)
if ta.crossover(ta.sma(close, 10), ta.sma(close, 30))
    strategy.entry("Long", strategy.long)
entry = strategy.position_avg_price
strategy.exit("Exit", "Long", limit = entry * 1.02, stop = entry * 0.99)
plot(entry, "Average price", color.gray, style = plot.style_linebr)
Pine ScriptTrailing stop in ticksOpen in editor
//@version=6
strategy("Trailing exit", overlay = true)
if ta.crossover(ta.sma(close, 10), ta.sma(close, 30))
    strategy.entry("Long", strategy.long)
strategy.exit("Trail", "Long", trail_points = 100, trail_offset = 50)
plot(ta.sma(close, 30), "SMA 30", color.orange)

See also

Runs on Sidechair