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

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

Photo of author
Published on

Introduction

In Pine Script, the strategy.closedtrades.entry_comment() function is a powerful tool for traders who want to analyze their trading strategy’s performance by examining comments associated with entry orders of closed trades. This function returns the comment message of a trade’s entry point, allowing for a detailed review of trading decisions based on the strategy’s logic.

Syntax

strategy.closedtrades.entry_comment(trade_num) → series string

Arguments

  • trade_num (series int): This argument specifies the number of the closed trade you want to examine. The numbering starts from zero, meaning the first trade executed by the strategy is referred to as trade number 0.

Example

In our example, we’re using the strategy.closedtrades.entry_comment() function to retrieve the comment of the entry order for the last closed trade. Let’s break down the code step by step.

//@version=5
strategy("strategy.closedtrades.entry_comment() Example", overlay = true)

entryStopPrice = open * 1.01

buySignal = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))

if (buySignal)
    strategy.entry("GoLong", strategy.long, stop = entryStopPrice, comment = str.tostring(entryStopPrice, "#.####"))
    strategy.exit("Leave", trail_points = 1000, trail_offset = 0)

var displayTable = table.new(position.top_right, 1, 3, color.orange, border_width = 1)

if barstate.islastconfirmedhistory or barstate.isrealtime
    table.cell(displayTable, 0, 0, 'Last closed trade:')
    table.cell(displayTable, 0, 1, "Entry Order Stop Price: " + strategy.closedtrades.entry_comment(strategy.closedtrades - 1))
    table.cell(displayTable, 0, 2, "Confirmed Entry Price: " + str.tostring(strategy.closedtrades.entry_price(strategy.closedtrades - 1)))
Example
  • Variable Naming and Strategy Setup: The strategy is set up with a name and overlay = true. The entry stop price (entryStopPrice) is calculated as 1.01 times the open price. This demonstrates how to initiate a buy order based on a specific condition with a dynamic stop price.
  • Buy Signal Condition: A crossover of two simple moving averages (SMA) determines the buySignal. This condition, when met, triggers the entry order.
  • Strategy Entry and Exit: Upon the buySignal, an entry order named “GoLong” is placed. The entry order includes a stop price and a comment detailing the stop price. An exit strategy named “Leave” is also defined but without a specific exit condition, focusing on trailing stops.
  • Displaying Trade Information: A table named displayTable is created to display information about the last closed trade in real-time or at the end of historical data. It shows the comment from the trade’s entry order, highlighting how the strategy.closedtrades.entry_comment() function is utilized.

Key Features

  • Function Usability: The strategy.closedtrades.entry_comment() function is invaluable for debugging and analyzing a strategy’s historical performance by providing insights into the conditions at the time of trade entries.
  • Syntax and Application: The function requires a trade number as an argument and returns a string containing the comment of the entry order. This can be particularly useful for strategies that use dynamic conditions for trade entries.

Takeaways

  • The strategy.closedtrades.entry_comment() function allows for an in-depth analysis of trade entry decisions by retrieving the comment associated with a trade’s entry order.
  • By examining entry comments, traders can gain insights into the effectiveness of their strategy and make necessary adjustments for improvement.
  • The example provided demonstrates how to implement this function within a trading strategy, showing its practical application in real-world trading scenarios.

Understanding and applying the strategy.closedtrades.entry_comment() function can significantly enhance the analysis and debugging of automated trading strategies in Pine Script.

Leave a Comment