Exits the trades opened by one entry id with a market order on the next bar.
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
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)) -> voidArguments
| Name | Type | Default | Description |
|---|---|---|---|
| id | series string | required | The id of the entry whose trades to close. |
| comment | series string | an empty string | Text shown next to the order on the chart and in the list of trades. |
| qty | series int/ | all open size of that entry | How many contracts, shares or units to close. |
| qty_percent | series int/ | 100 | The part of the open size to close, as a percent from 0 to 100. Ignored when qty is set. |
| alert_message | series string | an empty string | Text sent as {{strategy.order.alert_message}} in an alert created on the strategy when this order fills. |
| immediately | series bool | false | When true, the order fills at the close of the current bar instead of the open of the next one. |
| disable_alert | series bool | false | When 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
//@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