Topics like health, travel, finance, home, living, education and careers | Media Search Home

Get winning, losing, and even trades in TradingView: the strategy.wintrades, strategy.losstrades, and strategy.eventrades variables

When a trading strategy submits orders to trade setups, it also generates performance metrics. One group of statistics are the strategy"s winning, losing, and break-even trades. Let"s see how we access those in our TradingView code.

IN THIS ARTICLE:

  • Get win, lose, and even strategy trades in TradingView Pine
  • Get the total win trade count: strategy.wintrades
    • Get a strategy"s current number of wins
    • Compare winning trades against recent history
    • Use winning trade count with TradingView functions
    • Features of TradingView"s strategy.wintrades variable
  • Get the number of strategy losses: strategy.losstrades
    • Current number of losing trades
    • Compare losing trades against recent history
    • Using the losses count with TradingView functions
    • Features of TradingView"s strategy.losstrades variable
  • Get the number of break-even trades: strategy.eventrades
    • Get a strategy"s current number of even trades
    • Compare break-even trades with recent history
    • Using the even trade count with TradingView functions
    • Features of TradingView"s strategy.eventrades variable
  • Summary
# Get win, lose, and even strategy trades in TradingView Pine

Our strategy code can monitor the script"s performance in several ways. One option is to see how many winning, losing, and break-even trades there were. That tells us something about the strategy"s profitability, but can also serve as a risk management rule.

TradingView has three variables for the total wins, losses, and evens:

  • strategy.wintrades returns the total number of winning trades.
  • strategy.losstrades gives us the total number of losing trades.
  • And strategy.eventrades tells how many break-even trades happened.

To know a strategy"s total number of closed trades, either sum the above three variables or use TradingView"s strategy.closedtrades variable.

Let"s explore these variables and how we use them.

strategy.wintrades""># Get the total win trade count: strategy.wintrades

The strategy.wintrades variable returns the total number of winning trades the strategy did (TradingView, n.d.). That number is from closed trades only, and based on the backtest and all real-time signals combined.

The value of strategy.wintrades updates as the strategy processes bars on the chart. Because of that its value is different on bar number 50 than on bar 250 (assuming the strategy had winning trades in the mean time). Let"s see how we use this variable.

# Get a strategy"s current number of wins

When we use the strategy.wintrades variable, we get how many winning trades the strategy at that moment. There are several ways to use that information.

We could, for instance, compare that value against some target:

// See if the strategy's win target is already achieved winTarget = (strategy.wintrades > 100)

This winTarget variable is true whenever the strategy"s closed winning trades (strategy.wintrades) are above (>) 100. Without that target reached, the variable is false. Once we have a variable like that, we can use it in our strategy conditions.

Another way to use strategy.wintrades is to compare the strategy"s wins against the losses:

// See if there are more wins than losses winsOverLosses = (strategy.wintrades > strategy.losstrades)

This winsOverLosses variable is true each time the strategy has more winning trades than losing trades (strategy.losstrades). When there are as many wins as losses, or more losses than wins, that variable is false.

But there are more ways to use strategy.wintrades"s current value. We can, for example, stop trading after a losing streak. And reset that losing streak each time strategy.wintrades signals there"s a new winner.

# Compare winning trades against recent history

In TradingView Pine we can also get the previous bar values of the strategy.wintrades variable. To do that we place the history referencing operator ([]) behind the variable. Between those square brackets we place a number. That number determines from how many bars back strategy.wintrades returns its value.

So to see how many winning trades the strategy had at the end of the previous bar, we do:

// Get number of wins on previous bar lastWinTrades = strategy.wintrades[1]

To fetch the wins from 10 bars ago, we do:

// Get wins from 10 bars ago previousWins = strategy.wintrades[10]

When we subtract a previous value from the current strategy.wintrades value, then we know how many wins happened over a certain number of bars. This code, for example, says how many winning trades occurred in the last 30 bars:

// Get winning trades last 30 bars recentWinCount = strategy.wintrades - strategy.wintrades[30]

We can also compare the current strategy.wintrades value against a historical value. The following code determines if more than 3 wins happened in the last 10 bars:

// Check if strategy's wins increased recently winsIncreased = (strategy.wintrades - strategy.wintrades[10]) > 3

And to calculate how much (in percent) the winning trades increased compared with 100 bars ago, we code this:

// Calculates percentage growth win trades last 100 bars winGrowth = ((strategy.wintrades - strategy.wintrades[100]) / strategy.wintrades[100]) * 100
# Use winning trade count with TradingView functions

Because strategy.wintrades is a variable with a value on each bar, we can use it with any function that accepts a series of values. That includes the built-in functions but custom functions too.

So we can calculate the 30-bar moving average of a strategy"s winning trades:

// Calculate moving average of total win count maWins = sma(strategy.wintrades, 30)

Or plot the winning trade count for each bar on the chart. We do that with the plot() function:

// Display wins on the chart as a green line plot(series=strategy.wintrades, color=color.green)
strategy.wintrades variable""># Features of TradingView"s strategy.wintrades variable

Noteworthy features of the strategy.wintrades variable are:

  • The variable says how many trades closed with profit. It excludes the performance of the strategy"s open position.
  • The variable"s value is from the strategy"s entire backtest period and all real-time signals. It doesn"t specify winning trades per time period, only the total count.
  • The value that strategy.wintrades returns only applies to that specific strategy script. Other strategies on the same or a different chart are excluded.
  • The previous bar values (like strategy.wintrades[3]) says how many closed winning trades there were at the end of that particular bar.
  • The variable tells how many trades closed with profit, not how many positions were profitable. There"s a nuance between those two: several trades (that is, entry orders) can make up just one position.
  • We cannot use strategy.wintrades in indicator scripts. When we do, we get the ‘You can"t use strategy functions (strategy.wintrades) in study script’ TradingView error.

To know how the strategy"s open position performs, use the strategy.openprofit variable. That variable returns a value greater than (>) zero with profitable open positions.

strategy.losstrades""># Get the number of strategy losses: strategy.losstrades

The strategy.losstrades variable returns the strategy"s total number of losing trades (TradingView, n.d.). That count is from closed trades only, and derived from the strategy"s backtest and its real-time signals.

As the strategy processes price bars, the value of strategy.losstrades updates to reflect the strategy"s current losing trade count. That makes the variable"s value different on the 125th bar than on bar 5,430. Let"s see how we use this variable.

# Current number of losing trades

When our code uses the strategy.losstrades variable, we get how many losing trades the strategy did right until that moment.

A basic way to use that information is to see how the losses compare against some target:

// Check if losses remain under maximum lossesOkay = (strategy.losstrades < 45)

This lossesOkay variable is true as long as the strategy"s closed losing trades remain under (<) 45. When there are more than 45 losses, the variable is false. Once we code something like this we can include it in our strategy"s decision making.

We can also see how losses stack up against winning trades (strategy.wintrades):

// Check if total lose trades are less than win trades lessLossThanWin = (strategy.losstrades < strategy.wintrades)

This lessLossThanWin variable is true when the number of losses are below (<) the winning trade count. If there are as many or more losses than wins, the variable is false.

While these are just quick examples, there are much more ways to use strategy.losstrades. With that variable we can also stop a strategy based on weekly losing trades. Or stop trading after n wins in a row. That latter article resets the winning streak whenever strategy.losstrades registers a new losing trade.

# Compare losing trades against recent history

It"s also possible to access previous bar values of strategy.losstrades. For that we place TradingView"s history referencing operator ([]) behind the variable. Then between those square brackets we put an integer. That integer says from how many bars back we want data.

So to get the number of losses at the end of the previous bar, we do:

// Fetch loss count as they were at the // end of the last price bar lastLossCount = strategy.losstrades[1]

And to see how many losses there were 20 bar ago, we code:

// Get loss count from 20 bars back prevLossCount = strategy.losstrades[20]

This way we can also compare current strategy.losstrades values against historical data. This code for instance sees how many losses happened in the last 35 bars:

// Get number of losses in last 35 bars recentLosses = strategy.losstrades - strategy.losstrades[35]

And just as easy we can check if more than 15 losing trades happened in the past 50 bars:

// See if there are more than 15 losses in the last 50 bars tooManyLosses = (strategy.losstrades - strategy.losstrades[50]) > 15

And to figure out how much (in percent) the losing trade grew in the past 100 bars, we code:

// Calculate percentage growth of losses in last 100 bars lossGrowth = ((strategy.losstrades - strategy.losstrades[100]) / strategy.losstrades[100]) * 100
# Using the losses count with TradingView functions

Because strategy.losstrades has a series of historical values, we can use this variable with any function that accepts a numerical series argument. That includes most of TradingView"s built-in functions, but also custom functions you make yourself.

So if we want, we can calculate the 22-bar RSI of losing trades:

// For whichever reason, calculate RSI of losing trades rsiLoss = rsi(strategy.losstrades, 22)

Or plot the number of losing trades with the plot() function on the chart:

// Display total losses count on the chart plot(series=strategy.losstrades, color=color.red)
strategy.losstrades variable""># Features of TradingView"s strategy.losstrades variable

The strategy.losstrades variable has these characteristics:

  • The variable says how many trades closed with a loss. It doesn"t say how the strategy"s open trades perform.
  • The variable"s value from the strategy"s entire backtest period and all real-time signals. It doesn"t report losing trades per time period, only the total sum.
  • The variable of strategy.losstrades applies to the current strategy only. The variable doesn"t know about other strategies that run on the same or different charts.
  • The previous bar values (like strategy.losstrades[5]) tell how many closed losing trades there were at the end of that bar.
  • The variable says how many trades lost money, now how many positions were unprofitable. There"s a subtle difference between those two: a single position can be opened with several scale-in entry orders.
  • We cannot use strategy.losstrades in indicator scripts. When try we, we get the ‘You can"t use strategy functions (strategy.losstrades) in study script’ error message instead.

To know if the strategy"s open position loses money, check if the strategy.openprofit variable returns a value that"s less than (<) zero (0).

strategy.eventrades""># Get the number of break-even trades: strategy.eventrades

The strategy.eventrades variable returns a strategy"s total number of break-even trades (TradingView, n.d.). That value is from all the trades the strategy closed during the entire backtest period and all real-time signals.

As the strategy processes price bars, the value of strategy.eventrades updates in side step. That makes the variable"s value different on bar 400 from bar 7,600. Here are some ways to use that variable:

# Get a strategy"s current number of even trades

When we use the strategy.eventrades variable in our code, we get how many break-even trades the strategy did right up to that moment.

We can compare that count against some target to see how the strategy performs:

// Look if even trades are below 20 fewEvenTrades = (strategy.eventrades < 20)

This fewEvenTrades variable is true when there are less than (<) 20 break-even trades. With twenty or more even trades, the variable"s value is false.

If we see even trades as losses, then we calculate the strategy"s unprofitable trades by adding strategy.losstrades to strategy.eventrades:

// Calculate total losses by including even trades totalLosses = strategy.eventrades + strategy.losstrades
# Compare break-even trades with recent history

To know how many break-even trades the strategy had in the past, we place TradingView"s history referencing operator ([]) behind the variable. Inside those square brackets we put a number. That says from how many bars back we want data.

So to get the total number of break-even trades at the end of the previous bar, we code:

// Get total number of even trades as they were // on the previous price bar previousEvens = strategy.eventrades[1]

And to know the even trade count from 15 bars back, we write:

// Get even trades from 15 bars back lastEvenTrades = strategy.eventrades[15]

This way we can also compare the strategy"s current performance against recent history. For instance, the following code looks if there was any break-even trade during the last 30 bars:

// See if there's at least one break-even trade during the last 30 bars recentEvenTrades = (strategy.eventrades - strategy.eventrades[30]) > 0
# Using the even trade count with TradingView functions

Because strategy.eventrades has a value on each bar, we can use this variable with every function that accepts a series of numerical values. That includes most built-in functions but also custom functions.

This for example calculates the 20-bar moment of break-even trades with the mom() function:

// Calculate 20-bar momentum of even trades, because we can evenMom = mom(strategy.eventrades, 20)

And here"s how we plot the break-even trades with the plot() function:

// Display total break-even trades count as a teal line plot(series=strategy.eventrades, color=color.teal)
strategy.eventrades variable""># Features of TradingView"s strategy.eventrades variable

Some things to consider about the strategy.eventrades variable are:

  • The variable says how many trades closed without a profit or loss. It doesn"t say anything about the strategy"s open position performance.
  • The variable"s value is the total count from the entire backtest period and all real-time signals.
  • Its value only reflects the performance of the strategy in which we use this variable. But strategy.eventrades doesn"t know about other strategies that run on the same or different charts.
  • Previous bar values (such as strategy.eventrades[12]) return how many break-even trades there were at the end of that particular bar.
  • The variable says how many trades booked neither profit or loss. That, however, doesn"t say how the strategy"s positions performed; a single position can be opened with multiple trades. Say there are 5 scale-in orders into a single position, and 4 of those trades were break-even and 1 lost money. The result is one losing position, even though 80% of trades were break-even.
  • We can"t use strategy.eventrades in indicator scripts. When we do, we get the ‘You can"t use strategy functions (strategy.eventrades) in study script’ error for our troubles.

To see if the strategy"s current market position trades at break-even, see if the strategy.openprofit variable equals (==) 0.

# Summary

There are three TradingView variables that say how the strategy"s trade count looks. With the strategy.wintrades variable we get the total number of winning trades. The strategy.losstrades variable tells us how many losing trades the strategy had. And the strategy.eventrades variable signals the number of even trades.

When we use these variables in our code, they tell the strategy"s trade count at that moment in time. To access their historical values we use TradingView"s history referencing operator ([]). That says how many trades there were when a particular bar closed.

References

TradingView (n.d.). Pine Script Language Reference Manual. Retrieved on August 8, 2019, from https://www.tradingview.com/pine-script-reference/v4/

Published September 27, 2019.
# Related TradingView tutorials
  • How to get the position size of a TradingView strategy? (strategy.position_size explained)

    A TradingView strategy that trades the chart opens and closes positions. We get those position sizes with the strategy.position_size variable.

  • How to see (with code) that a TradingView strategy is long, short, or flat?

    TradingView"s strategy.position_size variable returns the strategy"s position size. But it can also tell if the strategy is long, short, or flat.

  • Get a strategy"s open and closed trade count: TradingView"s strategy.closedtrades and strategy.opentrades variables

    We get a TradingView"s strategy open and closed trades with strategy.opentrades and strategy.closedtrades. Those together are the total trade count.

  • What code can see if a TradingView strategy went long, short, or flat?

    When a TradingView script trades the chart"s instrument, it opens long and short positions. This article explains how code can spot new market positions.

« All TradingView strategy information articles


Email: contact@about.com.vn - Phone: 818.333.007 - Website: Media Search Home
Copyright © 2007 - Noos. All rights reserved