Skip to content
Algo Trade Analytics Docs

strategy.opentrades.entry_bar_index() - Pine Script Function

Returns the bar_index of the open trade’s entry.

strategy.opentrades.entry_bar_index(trade_num) → series int
NameTypeDescription
trade_numseries intThe trade number of the open trade. The number of the first trade is zero.
// Wait 10 bars and then close the position.//@version=6
strategy("`strategy.opentrades.entry_bar_index` Example")
barsSinceLastEntry() => strategy.opentrades > 0 ? bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades - 1) : na// Enter a long position
if there are no open positions.
if strategy.opentrades == 0
strategy.entry("Long", strategy.long)// Close the long position after 10 bars.
if barsSinceLastEntry() >= 10
strategy.close("Long")