Home » Strategy Functions » Understanding the strategy.closedtrades.profit() Function in Pine Script

Understanding the strategy.closedtrades.profit() Function in Pine Script

Photo of author
Published on

In this article, we’ll delve into the strategy.closedtrades.profit() function in Pine Script, a powerful tool for developing and testing trading strategies within the TradingView platform. 

Syntax

The syntax for the strategy.closedtrades.profit() function is straightforward:

strategy.closedtrades.profit(trade_num) → series float

This function returns a series of float values, representing the profit or loss of each closed trade. Losses are shown as negative values, providing a clear financial performance overview.

Arguments

  • trade_num (series int): This argument specifies the number of the closed trade you want to inquire about. It’s important to note that the numbering starts at zero, making the first trade trade_num = 0.

Example: Calculating Average Gross Profit

To illustrate the use of the strategy.closedtrades.profit() function, let’s consider an example where we calculate the average gross profit of closed trades in a strategy that enters long trades every 15 bars and exits them every 20 bars.

//@version=5
strategy("Average Gross Profit Example")

// Strategy calls to enter long trades every 15 bars and exit long trades every 20 bars.
if bar_index % 15 == 0
    strategy.entry("EntryLong", strategy.long)
if bar_index % 20 == 0
    strategy.close("EntryLong")

// Calculate average gross profit by adding the difference between gross profit and commission.
averageGrossEarnings() =>
    totalGrossProfit = 0.0
    for tradeIndex = 0 to strategy.closedtrades - 1
        totalGrossProfit += strategy.closedtrades.profit(tradeIndex) - strategy.closedtrades.commission(tradeIndex)
    averageProfit = nz(totalGrossProfit / strategy.closedtrades)

plot(averageGrossEarnings(), "Average Gross Earnings")
Example

Line-by-Line Explanation

  • Strategy Declaration: The script begins with a strategy declaration, naming the strategy as “Average Gross Profit Example”.
  • Entry and Exit Logic: It then sets up conditions for entering and exiting long trades based on the bar index.
  • Average Gross Profit Calculation:
    • A function named averageGrossEarnings is defined to calculate the average gross profit.
    • Inside the function, a loop iterates through all closed trades, summing up the profits (minus commissions) for each trade.
    • The average profit is then calculated by dividing the total gross profit by the number of closed trades.
  • Plotting: Finally, the script plots the average gross profit on the chart for visualization.

Key Features and Takeaways

  • Function Useability: The strategy.closedtrades.profit() function is invaluable for quantitatively assessing the financial performance of trading strategies.
  • Syntax and Application: It requires specifying the trade number as an argument, making it flexible for analyzing specific trades or iterating over multiple trades.
  • Practical Example: Our example script demonstrates calculating and plotting the average gross profit, showcasing the function’s practical application in strategy development and optimization.

This detailed breakdown of the strategy.closedtrades.profit() function in Pine Script equips you with the knowledge to implement this tool in your trading strategies, enabling a deeper analysis of your trades’ profitability.

Leave a Comment