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

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

Photo of author
Published on

In this article, we delve into the intricacies of the strategy.closedtrades.commission() function within Pine Script, the scripting language for TradingView charts. This function is a vital tool for traders and strategists who want to account for the impact of commission fees on their trading strategies directly within their scripts. Let’s break down the syntax, usage, and practical applications of this function through an illustrative example.


The function is structured as follows:

strategy.closedtrades.commission(trade_num) → series float


  • trade_num (series int): This is the trade number for the closed trade you’re querying about. It is important to note that Pine Script indexes trades starting at zero, meaning the first trade is referenced with the number zero.


Let’s examine the provided script, which showcases the use of strategy.closedtrades.commission() in a practical scenario:

strategy("Commission Example", commission_type = strategy.commission.percent, commission_value = 0.1)

// Strategy initiates long positions every 15 bars and exits them every 20 bars.
if bar_index % 15 == 0
    strategy.entry("EnterLong", strategy.long)
if bar_index % 20 == 0

// Visualization of total fees for the most recently closed trade.
plot(strategy.closedtrades.commission(strategy.closedtrades - 1))

Code Walkthrough

  • Strategy Definition: The strategy is defined with a name, "Commission Example", and set to calculate commissions as a percentage of the trade value, at a rate of 0.1%.
  • Trade Entry and Exit: The script enters a long position ("EnterLong") every 15 bars and exits the position every 20 bars. The if statements check the modulo of the current bar index (bar_index) against 15 and 20, respectively, to determine when to enter and exit trades.
  • Plotting Commissions: The last line plots the commission fees of the most recently closed trade. This is done by calling strategy.closedtrades.commission() with strategy.closedtrades - 1 as its argument, which refers to the last closed trade. The result is a series of floating-point numbers representing the commission paid for each closed trade, visualized on the chart.

Key Features

  • Function Usability: The strategy.closedtrades.commission() function provides a direct way to monitor and analyze commission fees within your trading strategies, making it easier to manage and optimize trading costs.
  • Syntax and Application: By accepting the trade number as an argument, the function offers flexibility in querying specific trades, allowing for detailed analysis and reporting on the impact of commissions on trade performance.


  • The strategy.closedtrades.commission() function is essential for incorporating commission fees into Pine Script trading strategies.
  • By understanding and utilizing this function, traders can gain insights into the true net performance of their strategies, accounting for all associated trading costs.
  • The example provided demonstrates a practical application of the function, offering a template for traders to customize and integrate into their own scripts.

This overview of the strategy.closedtrades.commission() function underscores its importance in creating more transparent and accurate trading strategies in Pine Script, providing users with the tools necessary to account for every aspect of trade performance.

Leave a Comment