strategy.close_all() - Pine Script Function
strategy.close_all()
Section titled “strategy.close_all()”Overview
Section titled “Overview”Creates an order to close an open position completely, regardless of the identifiers of the entry orders that opened or added to it.
Syntax
Section titled “Syntax”strategy.close_all(comment, alert_message, immediately, disable_alert) → voidParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
| comment | series string | Optional. Additional notes on the filled order. If the value is not an empty string |
| the | — | Strategy Tester and the chart show this text for the order instead of the automatically generated exit identifier. The default is an empty string. |
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=6strategy("Multi-entry close strategy")// Calculate a 14-bar and 28-bar moving average of `close` prices.float sma14 = ta.sma(close, 14)float sma28 = ta.sma(close, 28)// Place a market order to enter a long trade every time `sma14` crosses over `sma28`.if ta.crossover(sma14, sma28)strategy.order("My Long Entry ID " + str.tostring(strategy.opentrades), strategy.long)// Place a market order to close the entire position every 500 bars.if bar_index % 500 == 0strategy.close_all()// Plot the position size.plot(strategy.position_size)