TradingView Strategy Testing 101

Transcript

Hello traders,

Tim here, and I’d like to welcome you back to this video tutorial series on trading strategy optimization, using Pine Optimizer.

In the previous session, we explored some of the core principles of strategy optimization and looked at what it takes to get started using Pine Optimizer.

In this session, we’ll do a high-level walk-though of the TradingView strategy tester. Although we won’t be using this tool directly in our strategy optimization, it’s important to understand how it works, since Pine Optimizer uses it, extensively, behind the scenes.

The goal of this video is not to make you an expert in backtesting - at least, not right away - but simply to get you familiar with what the Strategy Tester is capable of.

So, without further rambling, let’s jump into TradingView.

This chart shows Bitcoin against Tether on the 1-hour timeframe. I didn’t choose this symbol or timeframe for any particular reason, other than that they are quite popular with traders. You can certainly use a Pine Script strategy on any symbol and timeframe, but you should be prepared for the reality that the performance of a strategy will vary significantly between different chart scenarios.

There are several different methods we can use to apply a trading strategy to a chart in order to execute a backtest. One of those methods entails working with Pine Script code. We’ll come back to that approach, later in this video. But, as a starting point, let’s look at how we can deploy a ready-made strategy from the TradingView library.

If we open the indicators menu and switch to the strategies tab, we see list of twenty or so ready-made strategies, all created by TradingView and available free to users on every price plan, including the free plan.

For this demo, I’m going to choose the MACD strategy. Again, this is not because I prefer it, but simply because the MACD concept is well-known to most traders.

As soon as I select a strategy, it triggers a backtest against the current chart’s price history. This results in two notable changes to what we see in TradingView. The first is that the chart itself is annotated to show the entry and exit points of all the resulting trades. At the same time, we also see a new panel, in the lower part of the TradingView window, which presents the detailed results of the backtest.

These results are spread across 4 tabs. The overview tab shows us a few key metrics that reflect the trading performance achieved by the backtest, together with an equity curve that plots how the account balance evolved over the duration of the test period. If you’ve seen equity curves before, you’ll immediately recognize this as one of the uglier examples. It’s ugly, not just because it lacks profitability, but also because it suffers from significant spells of drawdown, meaning that the gains achieved during a run of winning trades are subsequently wiped out by a similar run of losing trades.

It’s these two factors - profitability and consistency - that we aim to optimize when we’re searching for a viable strategy and tuning it for peak performance. We won’t get into the details of strategy optimization at this point, but keep in mind that a healthy equity curve is actually more like a straight line. It maintains a smooth upward trajectory as it moves to the right and we definitely don’t want to see periods of drawdown below the opening account balance, like the deficits, on this chart, reflected by the red regions.

It’s worth noting that this backtest reflects 873 closed trades, which is many more than we can see plotted on the price chart. That’s because the price chart currently only shows the most recent period of price history. If we adjust the chart coverage, to show an older historic period, it becomes clear that many trades occurred prior to what we originally saw on the chart. The backtest incorporates the entire set of available candle data, but the exact number of available bars depends on the price tier of your TradingView account. The free account that I’m using for this demo can backtest against 5,000 bars of historic data, but the higher-priced plans offer considerably more price history, up to a maximum of 20,000 bars on Premium plan accounts.

The next tab of the Strategy Tester is the performance summary. This presents almost 30 different backtest performance metrics, analyzed into 3 categories: long trades, short trades and all trades. If we were attempting to optimize a strategy using TradingView alone, this is where we would spend a lot of our time looking for clues that might lead us to better overall performance.

For example, if we look at the net profit achieved by this MACD strategy on the Bitcoin/Tether 1-hour chart, it’s not at all impressive, earning us just a quarter percent on our equity balance over the entire test period. But, if we look individually at the long and short trade categories, it’s clear that they are polar opposites: short trades contribute a significant net loss to our account, while long trades earn us a similar amount in profit. So, the simple act of eliminating all the short trades from our MACD trading would instantly 10x its profitability. Most strategy optimization is a bit more nuanced than this, but the principle still holds: we need to study the backtest metrics to uncover hints that will guide us towards meaningful performance improvements.

The performance summary tab includes a feature for exporting its metrics to a CSV file, which can be useful when we’re comparing the performance of multiple backtests. As we’ll see in a future video, backtest comparison is a critical tactic in strategy optimization and typically delivers the greatest opportunities for performance gain. Unfortunately, the CSV export feature is only available in higher-paid TradingView plans, which means that it may be off limits to many traders. In any case, this method of comparing backtests is cumbersome and error prone, so we need a more efficient alternative. We’ll introduce that alternative, when we explore the workings of Pine Optimizer, in an upcoming tutorial.

The third Strategy Tester tab is the List of Trades. As the name suggests, this view presents a table of the entire set of trades executed during the backtest, and includes a few key metrics, such as the entry and exit price, the position size and the profitability of the trade, as well as its effect on the runup and drawdown experienced by the trading account.

The listed trades are initially sorted in reverse chronological sequence, but we can re-sort them in the opposite sequence if we need to view the backtest run from the beginning. We typically only need to examine individual trades in this level of detail when we are debugging a strategy script, but this data can still be useful in calculating other aggregate metrics that are useful in strategy optimization.

As with the Performance Summary, trade data is also exportable in CSV file format. Unfortunately, this feature is also restricted to the higher-priced TradingView plans and the exported data is similarly inconvenient to work with. So, as with the performance metrics data, we’ll show you a more efficient approach in a future video.

The final Strategy Tester tab presents a summary of the backtest properties. This gives us details such as the date and time range of the test period, as well as the settings used when executing testing. These settings are divided into two groups: inputs and properties and together they determine precisely how the strategy executes.

Let’s take a closer look at these settings and see how we configure them. But before we make any changes, we’ll switch back to the Performance Summary tab, so that we can see the effect of our changes.

By opening the settings dialog for the strategy, we can individually configure its properties and inputs.

The Properties tab contains standard configuration settings, applicable to every strategy execution, and includes options such as the initial backtest equity balance and the formula used to determine trade position size. TradingView currently offers 11 strategy settings that are configurable through this interface, and it will always appear in this format, regardless of the strategy you are configuring.

If we switch to the Inputs tab, we see a range of configuration settings that are unique and specific to a given strategy script. In this case, it’s the MACD strategy, and these inputs are determined by the coding in the script itself. When we optimize a strategy, it’s typically here that we focus most of our effort.

If we change one of the parameter values, TradingView will immediately execute a new backtest, using the modified settings, and update both the price chart and the Strategy Tester to reflect the new result set. As you can see, the Net Profit improved slightly, as a result of the change that we made, which means that we accidentally performed a small optimization.

It’s worth noting that, every time you modify any strategy setting, TradingView will launch a new backtest. The results are presented almost instantaneously, and this enables us to compare the performance of different combinations of strategy settings. Of course, analysing the performance of two backtests entails much more than just comparing their net profit metrics, and we would typically perform many comparisons, by permutating all the available input parameter values, but what you have seen is the essence of strategy optimization, albeit in a very simplified form.

Before we wrap up this walk-through of the TradingView Strategy Tester, let’s look at one other way of applying a strategy to a chart, which entails working directly with Pine Script code.

If we open the Pine Editor, we see a code window that is pre-populated with a simple Pine Script template. This is where we would usually edit code when we’re writing our own strategy scripts, and it’s where you could equally paste a code block that you obtained from another script author.

But if we hover over the strategy that is already applied to the chart, we see a button that indicates that the script source code of this strategy is available for us to read and modify. When we click that button, the source code is pasted into the Pine Editor in read only mode.

In this way, we can read the script and learn from its structure, or we can use it as the basis of a custom strategy script, simply by creating a working copy from it. With our own working copy in the editor, we can modify one of its code lines, for example by changing one of the default input values, and then it’s ready to be applied back to the chart.

Before I add the custom script to the chart, I’m going to remove the existing strategy, as I don’t want two similar strategies to be executing simultaneously.

Now that the chart is back in its clean state, I’ll add my custom MACD strategy implementation to it, and a new backtest will be executed, as we would expect.

If we now open the strategy settings, we see that the default value of FastLength has reduced to 11, which reflects the code change that we just made.

If we want to keep this modified version of the MACD strategy for future use, we can save it.

Once saved, the custom strategy is available via the Indicators menu. It’s located under the Personal tab.

And that’s probably all we need to know about the TradingView Strategy Tester, in order to make effective use of Pine Optimizer.

What we’ve covered, in this session, is not intended to be a comprehensive tutorial for strategy testing with TradingView. The goal was to provide a just high-level understanding of how the strategy tester works.

In the next session, we’ll be putting this knowledge to use, when we examine the limitations to this approach and discover how Pine Optimizer delivers practical solutions to overcome them.

Until then, thanks for watching.