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

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

Photo of author
Published on

In this tutorial, we explore the strategy.closedtrades.entry_id() function in Pine Script, a powerful tool for analyzing the performance of trading strategies by accessing details about closed trades. This function is particularly useful for debugging and refining trading algorithms in the TradingView platform.


The strategy.closedtrades.entry_id() function retrieves the entry ID of a closed trade, which is crucial for tracking and analyzing individual trades within a trading strategy. Understanding how to utilize this function effectively can enhance your strategy analysis and development process.


strategy.closedtrades.entry_id(trade_number) → series string


  • trade_number (series int): The trade number for which you want to get the entry ID. It’s important to note that trade numbering starts at zero.

Example Walkthrough

Consider a simple strategy that enters a short position at a specific bar and exits it near the end of the chart. Here’s how we can implement this strategy and use the strategy.closedtrades.entry_id() function to display the ID of the last entry position.

strategy("Entry ID Retrieval Example", overlay = true)

// Entering a short position and closing it before the last bar.
if bar_index == 1
    strategy.entry("ShortEntry" + str.tostring(bar_index), strategy.short)
if bar_index == last_bar_index - 2

// Displaying the ID of the last entry position.
if barstate.islastconfirmedhistory
    label.new(last_bar_index, high, "Last Entry ID: " + strategy.closedtrades.entry_id(strategy.closedtrades - 1))

Code Explanation

  1. Strategy Definition: We start by defining our trading strategy with strategy(), naming it “Entry ID Retrieval Example” and setting overlay to true for visualization on the price chart.
  2. Entering a Short Position: We use if bar_index == 1 to specify that our entry condition is the second bar of the chart (since indexing starts at 0). The strategy.entry() function is called to enter a short position, with the entry name dynamically created using the current bar_index.
  3. Closing the Position: Before the last bar of the chart (if bar_index == last_bar_index - 2), we call strategy.close_all() to close any open positions.
  4. Displaying the Last Entry ID: To display the entry ID of the last closed trade, we check if the current bar is the last confirmed historical bar using barstate.islastconfirmedhistory. We then create a new label with label.new(), positioning it at the last_bar_index and high price, and setting its text to include the entry ID retrieved with strategy.closedtrades.entry_id(strategy.closedtrades - 1).

Key Features and Takeaways

  • Function Useability: The strategy.closedtrades.entry_id() function is invaluable for identifying and analyzing closed trades, providing a direct way to access the entry ID of each trade.
  • Syntax and Application: This function accepts a trade number as its argument and returns the corresponding entry ID as a string, allowing for detailed trade analysis.
  • Practical Use Case: In the example provided, we demonstrated how to use this function to monitor and display the entry ID of the most recently closed trade, a practice that can be adapted to various strategy analysis needs.

Understanding and applying the strategy.closedtrades.entry_id() function is essential for Pine Script users looking to deepen their strategy development and analysis capabilities.

Leave a Comment