strategy.closedtrades.entry_bar_index() - Pine Script Function
strategy.closedtrades.entry_bar_index()
Section titled “strategy.closedtrades.entry_bar_index()”Overview
Section titled “Overview”Returns the bar_index of the closed trade’s entry.
Syntax
Section titled “Syntax”strategy.closedtrades.entry_bar_index(trade_num) → series intParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
| trade_num | series int | The trade number of the closed trade. The number of the first trade is zero. |
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=6strategy("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 = 0for 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())