The bar_index of the bar on which a closed trade's exit order filled.
strategy.closedtrades.exit_bar_index(trade_num) is the bar where the trade closed. A trade that closes on the same bar it opened returns equal entry and exit indexes.
Use it with strategy.closedtrades.entry_bar_index to place a box or line across the life of the trade.
Syntax
strategy.closedtrades.exit_bar_index(trade_num: series int) -> series intArguments
| 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 int: The bar_index of the bar the closing order filled on. 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("Trade boxes", overlay = true, max_boxes_count = 100)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)
if ta.crossover(fast, slow)
strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
strategy.close("Long")
if strategy.closedtrades > strategy.closedtrades[1]
n = strategy.closedtrades - 1
win = strategy.closedtrades.profit(n) > 0
box.new(strategy.closedtrades.entry_bar_index(n), strategy.closedtrades.entry_price(n), strategy.closedtrades.exit_bar_index(n), strategy.closedtrades.exit_price(n), bgcolor = color.new(win ? color.green : color.red, 85))See also
strategy.closedtrades.entry_bar_indexstrategy.closedtrades.exit_timestrategy.closedtrades.exit_pricebox.new
Runs on Sidechair