Sidechair

strategy.closedtrades.exit_bar_index()

Browse the reference

The bar_index of the bar on which a closed trade's exit order filled.

Try it in the editor

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

Pine Script
strategy.closedtrades.exit_bar_index(trade_num: series int) -> series int

Arguments

NameTypeDefaultDescription
trade_numseries intrequiredZero-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 (see strategy.closedtrades.first_index).

Example

Pine ScriptDraw each trade as a boxOpen in editor
//@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

Runs on Sidechair