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

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

Photo of author
Published on

In this tutorial, we’ll explore the strategy.opentrades.commission() function in Pine Script, a pivotal tool in quantifying the entry and exit fees associated with open trades within a trading strategy. This function is instrumental in creating more accurate and financially realistic trading strategy simulations.


The function is declared as follows:

strategy.opentrades.commission(trade_num) → series float


  • trade_num (series int): This argument specifies the number of the open trade for which you want to calculate the commission. It’s important to note that trade numbering starts at zero.


Let’s dive into a practical example that illustrates how to use strategy.opentrades.commission() in a trading strategy. This example focuses on calculating the gross profit or loss for current open positions, taking into account the commissions paid.

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

// Strategy setup for entering and exiting long trades.
if bar_index % 15 == 0
    strategy.entry("MyLongEntry", strategy.long)
if bar_index % 20 == 0

// Function to calculate gross profit or loss for open positions.
calculateTradeOpenGrossPL() =>
    totalOpenGrossPL = 0.0
    for tradeIndex = 0 to strategy.opentrades - 1
        totalOpenGrossPL += strategy.opentrades.profit(tradeIndex) - strategy.opentrades.commission(tradeIndex)


Detailed Walkthrough

  • Strategy Definition: We start by defining a strategy called “Commission Analysis Example”. Here, we specify that commissions are calculated as a percentage of the trade value, set at 0.1%.
  • Entry and Exit Conditions: The script contains logic to enter long positions (strategy.entry) every 15 bars and exit (strategy.close) every 20 bars.
  • Gross Profit or Loss Calculation: The calculateTradeOpenGrossPL() function computes the gross profit or loss for all open trades. It initializes a variable totalOpenGrossPL to accumulate the sum of open trade profits minus their associated commissions.
  • Loop Through Open Trades: A loop iterates through all open trades, using strategy.opentrades.profit(tradeIndex) to calculate the profit for each open trade and subtracting strategy.opentrades.commission(tradeIndex) to account for the commissions paid on both entry and exit.
  • Plotting the Result: Finally, the calculated total open gross profit or loss is plotted on the chart for visualization.

Key Features and Takeaways

  • Function Useability: The strategy.opentrades.commission() function is essential for calculating the total commissions paid for an open trade, offering a more nuanced view of trading strategy profitability.
  • Syntax and Application: Its straightforward syntax and integration within loop constructs make it highly effective for dynamic trade analysis over time.
  • Enhanced Strategy Simulation: By including entry and exit fees in profitability calculations, traders can simulate strategies more accurately, leading to better-informed trading decisions.

In summary, understanding and effectively utilizing the strategy.opentrades.commission() function is crucial for anyone looking to develop sophisticated and realistic trading strategies in Pine Script.

Leave a Comment