Sidechair

strategy.close()

Browse the reference

Exits the trades opened by one entry id with a market order on the next bar.

Try it in the editor

strategy.close(id) closes the open trades whose entry order was named id. It sends a market order, so it fills at the open of the next bar (or, with process_orders_on_close = true in the header, at the close of the current bar). Set immediately to true to fill on the current bar's close regardless of that setting.

qty or qty_percent closes only part of the trades. If the entry with that id has not filled yet, nothing happens: use strategy.cancel() to withdraw a pending entry.

Syntax

Pine Script
strategy.close(id: series string, comment: series string (optional), qty: series int/float (optional), qty_percent: series int/float (optional), alert_message: series string (optional), immediately: series bool (optional), disable_alert: series bool (optional)) -> void

Arguments

NameTypeDefaultDescription
idseries stringrequiredThe id of the entry whose trades to close.
commentseries stringan empty stringText shown next to the order on the chart and in the list of trades.
qtyseries int/floatall open size of that entryHow many contracts, shares or units to close.
qty_percentseries int/float100The part of the open size to close, as a percent from 0 to 100. Ignored when qty is set.
alert_messageseries stringan empty stringText sent as {{strategy.order.alert_message}} in an alert created on the strategy when this order fills.
immediatelyseries boolfalseWhen true, the order fills at the close of the current bar instead of the open of the next one.
disable_alertseries boolfalseWhen true, an alert on the strategy does not fire for this order's fill.

Returns

Nothing.

Remarks

  • To close the whole position whatever the entry ids, use strategy.close_all().
  • Closing is FIFO unless the close_entries_rule in the strategy() header says otherwise.
  • Only available in strategy scripts.

Example

Pine ScriptClose on a trend breakOpen in editor
//@version=6
strategy("Close below SMA", overlay = true)
trend = ta.sma(close, 50)
plot(trend, "Trend", color.blue)
if ta.crossover(close, trend)
    strategy.entry("Long", strategy.long)
if ta.crossunder(close, trend)
    strategy.close("Long", comment = "trend broke")

See also

Runs on Sidechair