The id of the order that closed a closed trade, as passed to strategy.exit or strategy.close.
strategy.closedtrades.exit_id(trade_num) returns the id of the exit order that filled: the id you gave strategy.exit, strategy.close or strategy.order. It tells you which of several exits, a stop, a target or a signal, ended each trade.
Syntax
strategy.closedtrades.exit_id(trade_num: series int) -> series stringArguments
| Name | Type | Default | Description |
|---|---|---|---|
| trade_num | series int | required | Zero-based index of the closed trade. 0 is the first trade the strategy closed and strategy.closedtrades - 1 is the most recent. |
Returns
series string: The id string of the order that closed the trade. na when the trade does not exist.
Remarks
- Returns na when trade_num is negative, is not below
strategy.closedtrades, or points at a trade that has dropped out of the history (seestrategy.closedtrades.first_index).
Example
//@version=6
strategy("Stop-outs", overlay = false)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)
if ta.crossover(fast, slow)
strategy.entry("Long", strategy.long)
strategy.exit("Stop", "Long", stop = close * 0.98)
stops = 0
for i = strategy.closedtrades.first_index to strategy.closedtrades - 1
if strategy.closedtrades.exit_id(i) == "Stop"
stops += 1
plot(stops, "Trades closed by 'Stop'", color.red)See also
Runs on Sidechair