Home » Strategy Functions » Understanding the strategy.closedtrades.size() Function in Pinescript

Understanding the strategy.closedtrades.size() Function in Pinescript

Photo of author
Published on

Let’s dive into the strategy.closedtrades.size() function in Pine Script, exploring its purpose, syntax, and practical application through examples. This function is an invaluable tool for analyzing the size of positions in closed trades within a trading strategy. Understanding its use can significantly enhance strategy development and backtesting in TradingView.

What is strategy.closedtrades.size()?

The strategy.closedtrades.size() function in Pine Script is designed to return the size of a position for a specific closed trade within a trading strategy. This size can be positive, indicating a long position, or negative, reflecting a short position. 


strategy.closedtrades.size(trade_num) → series float


  • trade_num (series int): This argument specifies the trade number of the closed trade you want to analyze. The numbering starts at zero for the first trade.


Example 1: Tracking Contracts Traded in Last Closed Trade

strategy("Trade Size Analysis Example 1")

maxShares = math.floor(strategy.equity / close)

if bar_index % 15 == 0
    strategy.entry("GoLong", strategy.long, qty = maxShares)
if bar_index % 20 == 0

plot(strategy.closedtrades.size(strategy.closedtrades - 1), "Contracts Traded")

This example demonstrates how to plot the number of contracts traded in the last closed trade. The strategy initiates long positions every 15 bars and exits them every 20 bars. The strategy.closedtrades.size(strategy.closedtrades - 1) part calculates the size of the last closed trade.

Example 2: Calculating Average Profit Percentage for Closed Trades

strategy("Trade Size Analysis Example 2")

if bar_index % 15 == 0
    strategy.entry("GoLong", strategy.long)
if bar_index % 20 == 0

avgProfitPct = 0.0
for tradeIndex = 0 to strategy.closedtrades - 1
    entryPrice = strategy.closedtrades.entry_price(tradeIndex)
    exitPrice = strategy.closedtrades.exit_price(tradeIndex)
    avgProfitPct += (exitPrice - entryPrice) / entryPrice * strategy.closedtrades.size(tradeIndex) * 100

avgProfit = nz(avgProfitPct / strategy.closedtrades)


In the second example, we calculate and plot the average profit percentage for all closed trades. It iterates through each closed trade, calculating the profit percentage for each based on the entry and exit prices, as well as the size of the trade. Finally, it computes the average profit percentage and plots this value.

Key Features and Takeaways

  • The strategy.closedtrades.size() function is crucial for analyzing the size of positions in closed trades, aiding in strategy evaluation.
  • It returns a positive value for long positions and a negative value for short positions, indicating the direction of the trade.
  • The examples demonstrate practical applications, including plotting the number of contracts traded and calculating average profit percentages.
  • Understanding and utilizing this function can significantly enhance the development and analysis of trading strategies in Pine Script.

By mastering the use of the strategy.closedtrades.size() function, traders and developers can gain deeper insights into their trading strategy’s performance, enabling more informed decision-making and strategy optimization.

Leave a Comment