Pine Script offers a variety of functions to analyze strategy performance within TradingView. One such function is strategy.closedtrades.exit_comment()
, which is invaluable for understanding the conditions under which trades were exited. This tutorial dives into the syntax, application, and practical example of using this function to retrieve comments from closed trades.
Syntax and Arguments
The strategy.closedtrades.exit_comment()
function is designed to fetch the exit comment of a closed trade. It returns a string containing the comment message specified at the trade’s exit or na
(not applicable) if the trade does not exist or lacks a comment. Its syntax is as follows:
strategy.closedtrades.exit_comment(trade_num) → series string
- trade_num (series int): Represents the trade number for which you want to fetch the exit comment. The numbering starts from zero for the first trade.
Example
To demonstrate the practical application of the strategy.closedtrades.exit_comment()
function, let’s walk through an example where we monitor the outcomes of closed trades based on their exit comments.
//@version=5 strategy("Trade Exit Comment Analyzer", overlay = true) exitSignal = ta.crossover(ta.sma(close, 14), ta.sma(close, 28)) if (exitSignal) strategy.entry("BuySignal", strategy.long) strategy.exit("SellSignal", stop = open * 0.94, limit = close * 1.06, trail_points = 120, trail_offset = 0, comment_profit = "GoalReached", comment_loss = "StopHit", comment_trailing = "TrailExecuted") analyzeExits() => int stopHits = 0 int goalsReached = 0 int trailsExecuted = 0 if strategy.closedtrades > 0 for i = 0 to strategy.closedtrades - 1 exitComment = strategy.closedtrades.exit_comment(i) switch exitComment "GoalReached" => goalsReached += 1 "StopHit" => stopHits += 1 "TrailExecuted" => trailsExecuted += 1 [stopHits, goalsReached, trailsExecuted] var displayTable = table.new(position.top_right, 1, 4, color.blue, border_width = 1) if barstate.islastconfirmedhistory [stopHits, goalsReached, trailsExecuted] = analyzeExits() table.cell(displayTable, 0, 0, "Trade Outcomes") table.cell(displayTable, 0, 1, "Stop Hits: " + str.tostring(stopHits)) table.cell(displayTable, 0, 2, "Goals Reached: " + str.tostring(goalsReached)) table.cell(displayTable, 0, 3, "Trails Executed: " + str.tostring(trailsExecuted))
Walkthrough
- Initialization: We start by defining our trading strategy with a simple crossover condition as the entry signal.
- Trade Entries and Exits: Upon detecting a signal, the strategy initiates a long entry. The exit conditions include a stop loss, take profit, and trailing stop, each with a unique comment to identify the exit reason.
- Exit Analysis Function: This function iterates through all closed trades, counting how many trades closed for each exit reason.
- Displaying Results: The outcome is visually presented using a table that summarizes the count of each exit reason.
Key Features and Takeaways
- Function Useability: The
strategy.closedtrades.exit_comment()
function is pivotal for post-trade analysis, enabling developers to categorize trade exits based on custom comments. - Syntax and Application: This function’s straightforward syntax facilitates easy integration into trading strategies for enhanced performance analysis.
- Practical Application: Through a detailed example, we’ve demonstrated how to utilize this function to analyze and display trade exit reasons, providing valuable insights into strategy performance.
In conclusion, the strategy.closedtrades.exit_comment()
function is an essential tool for Pine Script developers looking to gain deeper insights into their trading strategy’s performance by analyzing the reasons behind trade exits.