Skip to content
Algo Trade Analytics Docs

strategy.close_all() - Pine Script Function

Creates an order to close an open position completely, regardless of the identifiers of the entry orders that opened or added to it.

strategy.close_all(comment, alert_message, immediately, disable_alert) → void
NameTypeDescription
commentseries stringOptional. Additional notes on the filled order. If the value is not an empty string
theStrategy Tester and the chart show this text for the order instead of the automatically generated exit identifier. The default is an empty string.
//@version=6
strategy("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 == 0
strategy.close_all()// Plot the position size.
plot(strategy.position_size)