Places stop-loss, take-profit and trailing-stop orders that close trades opened by strategy.entry.
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
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)) -> voidArguments
| Name | Type | Default | Description |
|---|---|---|---|
| id | series string | required | A name for the exit order. |
| from_entry | series string | an empty string | The id of the entry to exit. Empty means every open entry. |
| qty | series int/ | all of the entry | How many contracts, shares or units to exit. |
| qty_percent | series int/ | 100 | The part of the entry to exit, as a percent of its size, from 0 to 100. Ignored when qty is set. |
| profit | series int/ | na | The take-profit distance from the entry price, in ticks. |
| limit | series int/ | na | The take-profit price. Takes priority over profit. |
| loss | series int/ | na | The stop-loss distance from the entry price, in ticks. |
| stop | series int/ | na | The stop-loss price. Takes priority over loss. |
| trail_price | series int/ | na | The price at which the trailing stop becomes active. |
| trail_points | series int/ | na | The profit, in ticks, at which the trailing stop becomes active. |
| trail_offset | series int/ | na | How far behind the best price the trailing stop sits, in ticks. |
| oca_name | series string | an empty string | The name of an OCA group the exit orders join. |
| comment | series string | an empty string | Text shown next to the exit order on the chart and in the list of trades. |
| comment_profit | series string | the value of comment | Text for a fill of the take-profit order. |
| comment_loss | series string | the value of comment | Text for a fill of the stop-loss order. |
| comment_trailing | series string | the value of comment | Text for a fill of the trailing stop. |
| alert_message | series string | an empty string | Text sent as {{strategy.order.alert_message}} when this exit fills. |
| alert_profit | series string | the value of alert_message | Alert text for a take-profit fill. |
| alert_loss | series string | the value of alert_message | Alert text for a stop-loss fill. |
| alert_trailing | series string | the value of alert_message | Alert text for a trailing-stop fill. |
| disable_alert | series bool | false | When 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
//@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)//@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