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

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

Photo of author
Published on

The strategy.closedtrades.exit_id() function stands out for its utility in strategy development. This article will guide you through the intricacies of this function, providing a modified example and a detailed code explanation.

What is the strategy.closedtrades.exit_id() Function?

The strategy.closedtrades.exit_id() function is a part of Pine Script’s strategy namespace, designed to return the ID of a strategy’s closed trade exit point. This feature is essential for detailed strategy analysis and optimization, allowing developers to track and analyze the performance of their trading strategies accurately.

Syntax

strategy.closedtrades.exit_id(closedTradeIndex) → series string

Arguments

  • closedTradeIndex (series int): This argument specifies the number of the closed trade you wish to query. The indexing starts at zero, with zero representing the first closed trade.

Example

To illustrate the use of the strategy.closedtrades.exit_id() function, let’s modify the given example slightly to ensure uniqueness and clarity in understanding:

//@version=5
strategy("Custom Exit ID Example", overlay = true)

// Strategy initiates both long and short trades for demonstration
if bar_index == last_bar_index - 14
    strategy.entry("Entry for Long",  strategy.long)
else if bar_index == last_bar_index - 9
    strategy.entry("Entry for Short", strategy.short)

// Execute exit strategy based on the type of trade (long or short)
if ta.change(strategy.opentrades)
    tradeSize = strategy.opentrades.size(strategy.opentrades - 1)
    strategy.exit(tradeSize > 0 ? "Exit for Long" : "Exit for Short", strategy.opentrades.entry_id(strategy.opentrades - 1), stop = tradeSize > 0 ? high - ta.tr : low + ta.tr)

// Labeling the exit point of a closed trade on the chart
if ta.change(strategy.closedtrades)
    exitMessage = "Exited Trade: " + strategy.closedtrades.exit_id(strategy.closedtrades - 1)
    label.new(bar_index, high + (4 * ta.tr), exitMessage)
Example

Code Walkthrough

  • Strategy Definition: The strategy is defined with a custom name and an overlay flag set to true, indicating that the strategy output will be displayed on the main chart.
  • Trade Entry Conditions: The script triggers a long entry 14 bars before the last bar and a short entry 9 bars before the last bar of the dataset.
  • Exit Strategy Execution: When a new open trade is detected, an exit strategy is formulated based on the last trade’s type—long or short—using the trade size as a determinant.
  • Labeling Closed Trades: Upon detecting a closed trade, the script places a label on the chart, showcasing the ID of the exit point for the closed trade, derived from the strategy.closedtrades.exit_id() function.

Key Features and Takeaways

  • Function Useability: The strategy.closedtrades.exit_id() function is invaluable for tracking the exit points of closed trades within a strategy, offering precise analysis and optimization opportunities.
  • Syntax Application: By passing the appropriate trade index to the function, users can retrieve the exit ID for any closed trade, enhancing strategy transparency and traceability.
  • Practical Implementation: The provided example demonstrates a practical application, emphasizing the function’s role in dynamic strategy analysis and development.

This article aims to deepen your understanding of the strategy.closedtrades.exit_id() function in Pine Script, offering insights into its practical application within trading strategy development. 

Leave a Comment