strategy.close() - Pine Script Function
strategy.close()
Section titled “strategy.close()”Overview
Section titled “Overview”Creates an order to exit from the part of a position opened by entry orders with a specific identifier. If multiple entries in the position share the same ID, the orders from this command apply to all those entries, starting from the first open trade, when its calls use that ID as the id argument.
Syntax
Section titled “Syntax”strategy.close(id, comment, qty, qty_percent, alert_message, immediately, disable_alert) → voidParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
| id | series string | The entry identifier of the open trades to close. |
Remarks
Section titled “Remarks”- When a position consists of several open trades and the close_entries_rule in the strategy declaration statement is “FIFO” (default), a strategy.close call exits from the position starting with the first open trade. This behavior applies even if the id value is the entry ID of different open trades. However, in that case, the maximum exit order size still depends on the trades opened by orders with the id identifier. For more information, see this section of our User Manual.
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=6strategy("Partial 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 position when `sma14` crosses over `sma28`.if ta.crossover(sma14, sma28)strategy.entry("My Long Entry ID", strategy.long)// Place a market order to close the long trade when `sma14` crosses under `sma28`.if ta.crossunder(sma14, sma28)strategy.close("My Long Entry ID", "50% market close", qty_percent = 50)// Plot the position size.plot(strategy.position_size)