Understanding strategy.wintrades
strategy.wintrades
is a built-in function in Pine Script Version 5 that returns the number of winning trades that have occurred during the strategy’s execution. A “winning trade” is defined as a trade that has resulted in a positive profit. This function is particularly useful for strategy analysis and optimization, as it provides direct insight into the effectiveness of a trading strategy in terms of successful trades.
Syntax
strategy.wintrades
Example
Let’s create a basic strategy to demonstrate the use of strategy.wintrades
. In this example, we’ll implement a simple moving average crossover strategy and use strategy.wintrades
to track the number of winning trades.
//@version=5 strategy("Simple MA Crossover Strategy", overlay=true) // Define moving averages fastMA = ta.sma(close, 9) slowMA = ta.sma(close, 21) // Plot moving averages plot(fastMA, color=color.red, title="Fast MA") plot(slowMA, color=color.blue, title="Slow MA") // Generate buy and sell signals longCondition = ta.crossover(fastMA, slowMA) if (longCondition) strategy.entry("Long", strategy.long) shortCondition = ta.crossunder(fastMA, slowMA) if (shortCondition) strategy.entry("Short", strategy.short) // Execute on every bar update to dynamically update labels if (bar_index % 100 == 0) // Example: Update label every 100 bars label.new(x=bar_index, y=high, text="Wins: " + str.tostring(strategy.wintrades), style=label.style_label_down, color=color.green, size=size.small) // Note: The condition `if (bar_index % 100 == 0)` is just an example to limit label creation. // You should adjust this logic based on how frequently you want to update or create new labels.
Detailed Walkthrough
//@version=5 strategy("Simple MA Crossover Strategy", overlay=true)
//@version=5
: Specifies that this script uses Version 5 of Pine Script.strategy(...)
: Initializes a new trading strategy with the given name. Theoverlay=true
parameter indicates that the strategy’s output (e.g., plots and shapes) will be displayed directly on the price chart.
fastMA = ta.sma(close, 9) slowMA = ta.sma(close, 21)
- Two simple moving averages (SMAs) are calculated using the
ta.sma(source, length)
function:fastMA
: A faster moving average calculated over the last 9 periods (short-term trend).slowMA
: A slower moving average calculated over the last 21 periods (long-term trend).
close
: Refers to the closing prices of the bars/candles, which is the data source for both moving averages.
plot(fastMA, color=color.red, title="Fast MA") plot(slowMA, color=color.blue, title="Slow MA")
- These lines plot the values of the fast and slow moving averages on the chart.
plot(series, color, title)
: Draws the specified series (in this case, the moving averages) on the chart with the given color and title.
longCondition = ta.crossover(fastMA, slowMA) if (longCondition) strategy.entry("Long", strategy.long) shortCondition = ta.crossunder(fastMA, slowMA) if (shortCondition) strategy.entry("Short", strategy.short)
- Buy (long) signal: Generated when the fast moving average crosses above the slow moving average (
ta.crossover
). - Sell (short) signal: Generated when the fast moving average crosses below the slow moving average (
ta.crossunder
). strategy.entry(id, direction)
: Executes an entry order with the specified id and direction (long or short) based on the conditions defined.
winningTrades = strategy.wintrades
strategy.wintrades
: A built-in function that returns the number of winning trades up to the current bar. A winning trade is one that has closed with a profit.
plotshape(series=winningTrades, location=location.top, style=shape.labeldown, color=color.green, size=size.small, title="Winning Trades", text="Wins: " + str.tostring(winningTrades))
plotshape(...)
: This function plots a shape on the chart for each value in theseries
argument. In this case, it displays the number of winning trades at the top of the chart.location.top
: Specifies that the shapes should be plotted at the top of the chart.style=shape.labeldown
: Uses a label with an arrow pointing down as the shape.color=color.green
: Sets the color of the shape to green.text="Wins: " + str.tostring(winningTrades)
: Displays a text label with the number of winning trades next to each shape.
Key Features and Takeaways
strategy.wintrades
is a valuable function for analyzing the performance of a trading strategy in Pine Script Version 5.- It provides a straightforward method for tracking the number of winning trades, assisting in the evaluation and optimization of a strategy.
- This function can be used in combination with other strategy statistics functions like
strategy.losstrades
andstrategy.percentprofitable
for a comprehensive strategy analysis. - Implementing
strategy.wintrades
in your Pine Script strategies can help you fine-tune your trading algorithms for better performance.
By incorporating strategy.wintrades
into your trading strategies, you can gain deeper insights into the effectiveness of your trading rules and make informed decisions to improve your trading outcomes.