Sidechair

strategy.close_all()

Browse the reference

Closes the whole open position with a market order on the next bar, whatever entry ids opened it.

Try it in the editor

strategy.close_all() flattens the position. Like strategy.close, it sends a market order that fills at the next bar's open, or at the current bar's close when process_orders_on_close is true or immediately is true.

It affects only open trades; pending entry, limit and stop orders stay in place, so use strategy.cancel_all() as well when you want a clean slate.

Syntax

Pine Script
strategy.close_all(comment: series string (optional), alert_message: series string (optional), immediately: series bool (optional), disable_alert: series bool (optional)) -> void

Arguments

NameTypeDefaultDescription
commentseries stringan empty stringText shown next to the order on the chart and in the list of trades.
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

  • A common use is an end-of-day exit: close_all on the last bar of the session.
  • Only available in strategy scripts.

Example

Pine ScriptFlat before the session endsOpen in editor
//@version=6
strategy("Day trade", overlay = true, process_orders_on_close = true)
if session.isfirstbar
    strategy.entry("Long", strategy.long)
if session.islastbar
    strategy.close_all(comment = "end of day")
bgcolor(session.islastbar ? color.new(color.red, 85) : na)

See also

Runs on Sidechair