In this tutorial, we’ll dive into the strategy.closedtrades.entry_bar_index() function in Pine Script, exploring its syntax, arguments, and a practical example that demonstrates its use in calculating the average number of bars per trade within a trading strategy.
Syntax
The syntax for this function is:
strategy.closedtrades.entry_bar_index(trade_num) → series int
It returns the bar_index
of a closed trade’s entry point. The bar_index
is a unique identifier for each bar on the chart, making this function crucial for analyzing trade durations and entry points.
Arguments
trade_num
(series int): The trade number of the closed trade. The numbering starts at zero for the first trade.
Practical Example: Calculating Average Bars Per Trade
Let’s explore a detailed example that utilizes this function within a strategy that enters long positions after three consecutive rising bars and exits after two consecutive falling bars. We’ll calculate the average number of bars involved in each trade.
Code Explanation
//@version=5 strategy("Trade Entry Bar Index Example") // Enter long trades on three rising bars; exit on two falling bars. if ta.rising(close, 3) strategy.entry("GainLong", strategy.long) if ta.falling(close, 2) strategy.close("GainLong") // Function that calculates the average amount of bars in a trade. avgBarsInTrade() => totalBarsInTrades = 0 for tradeIndex = 0 to strategy.closedtrades - 1 // Loop through all closed trades, starting with the oldest. tradeDuration = strategy.closedtrades.exit_bar_index(tradeIndex) - strategy.closedtrades.entry_bar_index(tradeIndex) + 1 totalBarsInTrades += tradeDuration average = nz(totalBarsInTrades / strategy.closedtrades) plot(avgBarsInTrade())
- Strategy Definition: We define a strategy named “Trade Entry Bar Index Example,” which will trigger long entries and exits based on the price movement pattern.
- Trade Entry and Exit Conditions: Using
ta.rising()
for detecting three rising bars to enter a long trade (GainLong
) andta.falling()
for detecting two falling bars to exit the trade. - Calculating Average Bars per Trade:
avgBarsInTrade()
: A function defined to calculate the average duration, in bars, of all closed trades.- Inside this function, we loop through all closed trades using
for
loop and calculate each trade’s duration by subtracting the entry bar index from the exit bar index and adding one (to include the entry bar itself in the count). nz()
: This function is used to handle cases where the denominator might be zero, preventing division by zero errors.
- Plotting the Average: The average bars per trade are plotted on the chart, providing a visual representation of how long, on average, trades remain open.
Key Takeaways
- The
strategy.closedtrades.entry_bar_index()
function is essential for analyzing trade entry points and durations. - This example demonstrates how to calculate and visualize the average duration of trades in a strategy, providing insights into trade efficiency and strategy performance.
- By modifying the code and strategy conditions, you can further explore the dynamics of trade entries and exits, adapting the analysis to fit various trading strategies.
Through this detailed walkthrough, we’ve seen how the strategy.closedtrades.entry_bar_index()
function can be effectively utilized in Pine Script to enhance trading strategy analysis and performance evaluation.