Skip to content
Algo Trade Analytics Docs

strategy.exit() - Pine Script Function

Creates price-based orders to exit from an open position. If unfilled exit orders with the same id exist, calls to this command modify those orders. This command can generate more than one type of exit order, depending on the specified parameters. However, it does not create market orders. To exit from a position with a market order, use strategy.close or strategy.close_all.

strategy.exit(id, from_entry, qty, qty_percent, profit, limit, loss, stop, trail_price, trail_points, trail_offset, oca_name, comment, comment_profit, comment_loss, comment_trailing, alert_message, alert_profit, alert_loss, alert_trailing, disable_alert) → void
NameTypeDescription
idseries stringThe identifier of the orders
whichcorresponds to an exit ID in the strategy’s trades after an order fills. Strategy commands can reference the order ID to cancel or modify pending exit orders. The Strategy Tester and the chart display the order ID unless the command includes a comment* argument that applies to the filled order.
  • A single call to the strategy.exit command can generate exit orders for several entries in an open position, depending on the call’s from_entry value. If the call does not include a from_entry argument, it creates exit orders for all open trades, even the ones opened after the call, until the position closes. See this section of our User Manual to learn more.
//@version=6
strategy("Trailing stop strategy", overlay = true)//@variable The distance required to activate the trailing stop.float activationDistanceInput = input.int(100, "Trail activation distance, in ticks") * syminfo.mintick//@variable The number of ticks the trailing stop follows behind the price as it reaches new peaks.int trailDistanceInput = input.int(100, "Trail distance, in ticks")//@function Draws a label and line at the specified `price` to visualize a trailing stop order's activation level.drawActivation(float price) => label.new( bar_index, price, "Activation level", style = label.style_label_right, color = color.gray, textcolor = color.white )
line.new( bar_index, price, bar_index + 1, price, extend = extend.right, style = line.style_dashed, color = color.gray )//@function Stops the `l` line from extending further.method stopExtend(line l) => l.set_x2(bar_index)
l.set_extend(extend.none)// The activation line, active trailing stop price, and active trailing stop flag.var line activationLine = navar float trailingStopPrice = navar bool isActive = false
if bar_index % 100 == 0 and strategy.opentrades == 0 trailingStopPrice := na isActive := false // Place a market order to enter a long position.
strategy.entry("My Long Entry ID", strategy.long) //@variable The activation level's price. float activationPrice = close + activationDistanceInput // Create a trailing stop order that activates the defined number of ticks above the entry price.
strategy.exit( "My Long Exit ID", "My Long Entry ID", trail_price = activationPrice, trail_offset = trailDistanceInput, oca_name = "Exit" ) // Create new drawings at the `activationPrice`. activationLine := drawActivation(activationPrice)// Logic
for trailing stop visualization.
if strategy.opentrades == 1 // Stop extending the `activationLine` when the stop activates.
if not isActive and high >
activationLine.get_price(bar_index)
isActive := true activationLine.stopExtend() // Update the `trailingStopPrice`
while the trailing stop is active.
if isActive float offsetPrice = high - trailDistanceInput * syminfo.mintick trailingStopPrice :=
math.max(nz(trailingStopPrice, offsetPrice), offsetPrice)// Close the trade with a market order
if the trailing stop does not activate before the next 300th bar.
if not isActive and bar_index % 300 == 0
strategy.close_all("Market close")// Reset the `trailingStopPrice` and `isActive` flags when the trade closes, and stop extending the `activationLine`.
if ta.change(strategy.closedtrades) > 0
if not isActive
activationLine.stopExtend()
trailingStopPrice := na isActive := false// Plot the `trailingStopPrice`.
plot(trailingStopPrice, "Trailing stop", color.red, 3, plot.style_linebr)