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

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

Photo of author
Published on

This tutorial dives into the function’s syntax, and application of strategy.closedtrades.exit_price() and provides practical examples to illustrate its use.

Syntax

The function is structured as follows:

strategy.closedtrades.exit_price(trade_num) → series float

Arguments:

  • trade_num (series int): Specifies the trade number for which you want to retrieve the exit price. The numbering starts at zero for the first trade.

Example

Let’s explore two examples that demonstrate the function’s application.

Example 1: Tracking Exit Prices of Long Trades

//@version=5
strategy("Exit Price Tracker 1")

// Trigger a long trade every 5 bars
if bar_index % 5 == 0
    strategy.entry("Buy",  strategy.long)
strategy.close("Buy")

// Retrieve the exit price for the most recent closed trade
lastExitPrice = strategy.closedtrades.exit_price(strategy.closedtrades - 1)

plot(lastExitPrice, "Exit Price for Buy")
Example

This example triggers a long trade every 5 bars and plots the exit price of the most recent closed trade. Here’s a breakdown:

  • Entry and Exit: A long position is opened (strategy.entry) every 5 bars and subsequently closed (strategy.close) at the next bar.
  • Retrieving the Exit Price: lastExitPrice fetches the exit price of the latest closed trade using strategy.closedtrades.exit_price(strategy.closedtrades - 1).
  • Plotting: The exit price is visualized on the chart with plot().

Example 2: Calculating Average Profit Percentage

//@version=5
strategy("Profit Percentage Calculator")

// Strategy to initiate trades at specific bars
if bar_index == last_bar_index - 15
    strategy.entry("Buy Entry",  strategy.long)
else if bar_index == last_bar_index - 10
    strategy.close("Buy Entry")
    strategy.entry("Sell", strategy.short)
else if bar_index == last_bar_index - 5
    strategy.close("Sell")

// Calculate profit for all closed trades
totalProfitPct = 0.0
for i = 0 to strategy.closedtrades - 1
    buyPrice = strategy.closedtrades.entry_price(i)
    sellPrice = strategy.closedtrades.exit_price(i)
    totalProfitPct += (sellPrice - buyPrice) / buyPrice * strategy.closedtrades.size(i) * 100

// Calculate the average profit percentage
averageProfitPct = nz(totalProfitPct / strategy.closedtrades)

plot(averageProfitPct, "Avg Profit %")
Example

This script calculates the average profit percentage from closed trades:

  • Trade Execution: It opens and closes both long and short positions based on the bar_index.
  • Profit Calculation: Loops through all closed trades, computing the profit percentage for each by comparing entry and exit prices.
  • Average Profit: The script calculates and plots the average profit percentage of these trades.

Key Takeaways

  • The strategy.closedtrades.exit_price() function is essential for analyzing the exit prices of closed trades in Pine Script.
  • It requires specifying the trade number, enabling targeted analysis of trade performance.
  • The function enhances strategy backtesting by allowing detailed trade performance review and optimization.

In summary, understanding and utilizing the strategy.closedtrades.exit_price() function can significantly augment your trading strategy analysis, providing clear insights into the profitability and performance of your trades.

Leave a Comment