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:
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:
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.wintradesThe 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 winsWhen 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:
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:
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 historyIn 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:
To fetch the wins from 10 bars ago, we do:
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:
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:
And to calculate how much (in percent) the winning trades increased compared with 100 bars ago, we code this:
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:
Or plot the winning trade count for each bar on the chart. We do that with the plot() function:
Noteworthy features of the strategy.wintrades variable are:
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.
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 tradesWhen 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:
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):
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 historyIt"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:
And to see how many losses there were 20 bar ago, we code:
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:
And just as easy we can check if more than 15 losing trades happened in the past 50 bars:
And to figure out how much (in percent) the losing trade grew in the past 100 bars, we code:
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:
Or plot the number of losing trades with the plot() function on the chart:
The strategy.losstrades variable has these characteristics:
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).
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 tradesWhen 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:
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:
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:
And to know the even trade count from 15 bars back, we write:
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:
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:
And here"s how we plot the break-even trades with the plot() function:
Some things to consider about the strategy.eventrades variable are:
To see if the strategy"s current market position trades at break-even, see if the strategy.openprofit variable equals (==) 0.
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.
TradingView (n.d.). Pine Script Language Reference Manual. Retrieved on August 8, 2019, from https://www.tradingview.com/pine-script-reference/v4/
A TradingView strategy that trades the chart opens and closes positions. We get those position sizes with the strategy.position_size variable.
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.
We get a TradingView"s strategy open and closed trades with strategy.opentrades and strategy.closedtrades. Those together are the total trade count.
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.