Sidechair

strategy.closedtrades.exit_time()

Browse the reference

The time a closed trade was exited, as a UNIX timestamp in milliseconds.

Try it in the editor

strategy.closedtrades.exit_time(trade_num) returns the time of the bar the closing order filled on, in milliseconds since 1970-01-01 UTC.

Subtract strategy.closedtrades.entry_time to get the holding time in milliseconds.

Syntax

Pine Script
strategy.closedtrades.exit_time(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 open time of the exit bar as a UNIX timestamp in milliseconds. 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).
  • On intraday charts both times carry the bar's open time, so a trade held inside one bar returns equal entry and exit times.

Example

Pine ScriptHolding time in hoursOpen in editor
//@version=6
strategy("Hours held", overlay = false)
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")
n = strategy.closedtrades - 1
hours = n >= 0 ? (strategy.closedtrades.exit_time(n) - strategy.closedtrades.entry_time(n)) / 3600000 : na
plot(hours, "Last trade, hours", color.blue, style = plot.style_stepline)

See also

Runs on Sidechair