Skip to content
Algo Trade Analytics Docs

strategy.closedtrades.entry_bar_index() - Pine Script Function

Returns the bar_index of the closed trade’s entry.

strategy.closedtrades.entry_bar_index(trade_num) → series int
NameTypeDescription
trade_numseries intThe trade number of the closed trade. The number of the first trade is zero.
//@version=6
strategy("strategy.closedtrades.entry_bar_index Example")// Enter long trades on three rising bars; exit on two falling bars.
if ta.rising(close, 3)
strategy.entry("Long", strategy.long)
if ta.falling(close, 2)
strategy.close("Long")// Function that calculates the average amount of bars in a trade.avgBarsPerTrade() => sumBarsPerTrade = 0
for tradeNo = 0 to strategy.closedtrades - 1 // Loop through all closed trades, starting with the oldest. sumBarsPerTrade +=
strategy.closedtrades.exit_bar_index(tradeNo) - strategy.closedtrades.entry_bar_index(tradeNo) + 1 result = nz(sumBarsPerTrade / strategy.closedtrades)
plot(avgBarsPerTrade())