strategy.closedtrades.exit_id() - Pine Script Function
strategy.closedtrades.exit_id()
Section titled “strategy.closedtrades.exit_id()”Overview
Section titled “Overview”Returns the id of the closed trade’s exit.
Syntax
Section titled “Syntax”strategy.closedtrades.exit_id(trade_num) → series stringParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
| trade_num | series int | The trade number of the closed trade. The number of the first trade is zero. |
Returns
Section titled “Returns”Returns the id of the closed trade’s exit.
Remarks
Section titled “Remarks”- The function returns na if trade_num is not in the range: 0 to strategy.closedtrades-1.
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=6strategy("strategy.closedtrades.exit_id Example", overlay = true)// Strategy calls to create single short and long tradesif bar_index == last_bar_index - 15strategy.entry("Long Entry", strategy.long)elseif bar_index == last_bar_index - 10strategy.entry("Short Entry", strategy.short)// When a new open trade is detected then we create the exit strategy corresponding with the matching entry id// We detect the correct entry id by determiningif a position is long or short based on the position quantityif ta.change(strategy.opentrades) != 0 posSign =strategy.opentrades.size(strategy.opentrades - 1)strategy.exit(posSign > 0 ? "SL Long Exit" : "SL Short Exit", strategy.opentrades.entry_id(strategy.opentrades - 1), stop = posSign > 0 ? high - ta.tr : low + ta.tr)// When a new closed trade is detected then we place a label above the bar with the exit infoif ta.change(strategy.closedtrades) != 0 msg = "Trade closed by: " +strategy.closedtrades.exit_id(strategy.closedtrades - 1)label.new(bar_index, high + (3 * ta.tr), msg)