Your First Optimization Campaign
Transcript
Hello traders,
This is Tim, and we're continuing our video tutorial series on trading strategy optimization, using Pine Optimizer.
In the last session, we saw how to prepare your device for working with Pine Optimizer, including a quick test to verify its connectivity with TradingView.
In this session, we'll see what it's like to work with Pine Optimizer, by creating a strategy optimization project and using it to execute our first optimization campaign.
But, before we get to that, let's do a quick recap of how we would backtest and optimize a strategy, using TradingView.
Here, I've opened a TradingView price chart to visualize Bitcoin against Tether on the one-hour timeframe.
To run a backtest on this chart, I'll open the Pine Editor and re-use the custom MACD strategy script that we created previously, which is actually still hanging around from our earlier demo.
If we add this strategy to the chart, it immediately executes a backtest.
And if I wanted to optimize this strategy for better performance, I'd open the strategy settings and adjust one or more of its parameters.
As we saw earlier, every strategy has its own unique set of parameters, and adjusting any of their values will have an effect on the performance of the strategy; either a positive or a negative effect.
To find the optimum configuration for a given strategy, on a particular symbol and timeframe, we essentially need to try every combination of parameter values and then choose the one that yields a trading performance that is closest to our expectations.
As you can see, the MACD strategy has 3 configurable parameters, and if we assume that each of these parameters has, perhaps, 10 values that we might wish to test out, that represents 1,000 backtests that need to be executed.
Even if we make our optimization campaign less ambitious, by only testing 5 values for each parameter, that's still 125 backtests that need to be executed, with each of their result sets captured and recorded, in such a way that we can easily compare them, in order to choose a winner.
We've already seen that TradingView isn't well adapted to this kind of repetitive chore, which is why Pine Optimizer was created.
So, instead of doing our backtesting inside TradingView, we'll now switch to Pine Optimizer, in order to gain access to a much more efficient way to create and execute our optimization campaigns.
Before we do that, I'm going to make a few changes, so that it's easier for you to see exactly what's happening.
First, I'll clean up the chart view, by removing the MACD strategy that's currently added to it.
I'll also switch to the Strategy Tester view, so that we can see the results of any backtests that TradingView executes on our behalf.
Now, if I switch to this second browser tab, you can see that I've already opened Pine Optimizer and logged into my account. This is a fresh account, so it doesn't yet have any optimization history.
Before creating my first optimization project, I'm going to drag the TradingView tab off of its containing window and dock it to the left side of the screen.
Then I'll select the Pine Optimizer tab and dock it to the right side of the screen.
Finally, I'll resize these two windows so that we can see a little more of Pine Optimizer and a little less of TradingView.
As you can see, I now have two browser tabs open, but they're arranged in separate windows, side by side, so that we can see what's happening in both tabs at the same time. Of course, you don't usually need to separate the tabs like this, but I have done it this way, so that you can see exactly what's happening in both TradingView and Pine Optimizer, in real time.
With all that preparation out of the way, let's create our first Pine Optimizer strategy project. I’ll do this by clicking the + button in the header of the strategies panel, which launches the "New Strategy" dialog.
I'll give this strategy the name MACD, so that it reflects the actual Pine Script strategy that we're optimizing, but you can choose any name that you prefer.
If you wish, you can also add a description of your trade idea, which may be helpful if you return to review this optimization project at some time in the future, but I'm going to continue without a description.
If I switch to the Script tab, there is a field where I can paste the Pine Script code for my strategy script. You might use this approach if you are optimizing a strategy that uses code copied from the internet. But, in many cases, the script code for your strategy will come directly from TradingView, and you can simply import your current Pine Editor script using this import button.
Switching to the properties tab, you'll see the same set of strategy property definitions that the TradingView Strategy Tester uses. You can configure these properties to suit your trading preferences, and they'll be applied to every backtest executed from within this optimization project.
If you wish, you can also define global preferences for you strategy properties. These default values will be applied to every new strategy that you create, eliminating the need for this 3rd step in the configuration of new projects. I'll show you how to do this in an upcoming session, when we cover customizing Pine Optimizer.
With this 3-step entry process complete, we can confirm our entries and click OK to create the new strategy.
You'll notice that creating a new strategy automatically assigns it a first version. We'll talk more about versions, in a later video.
But, for now, let's continue by creating a campaign to optimize the strategy that we just created.
A campaign is an experiment, whose goal is to provide some useful insight about the performance of your strategy that will lead you closer to the optimum configuration. In an upcoming session, I'll go into much more detail about how to structure and organize your optimization campaigns. But, in this demo, we'll keep things simple and focus purely on how to create and execute a campaign.
I'll start this process by clicking the + button in the header of the campaigns panel, which launches the "New Campaign" dialog.
As when creating a strategy, we are asked to assign our campaign a name and an optional description. It's usually a good idea to give your campaign a meaningful name that clearly indicates its role in your optimization structure, which we'll cover in a future video. But for this demo, I'm going to assign the campaign an arbitrary name and skip the description.
Moving to the Parameters tab, we see fields that correspond to the chart symbol and timeframe, as well as one field for each of the parameters exposed by our MACD strategy. If you were optimizing a complex strategy, with many parameters, this entry form would have additional fields that reflect that complexity.
All of the fields on this form allow you to enter one or more values. Every field must have at least one value entered, but you are free to enter additional values if you wish.
To understand how this works, let's start with the Symbol field. If I click the + plus button, to the right on the symbol field, I'm presented with a list of chart symbols to choose from. What you're seeing here is my personal favorite list of symbols, which reflects my individual trading preferences.
In a future video, I'll show you how to configure your own favorites list, which will be available to you on all new strategies that you create. For this demo, I'll simply choose a symbol from my own favorites.
You'll notice that I chose the same symbol that's already active on the TradingView chart. This isn't mandatory; you can choose any symbol, and Pine Optimizer will automatically switch the active chart symbol to reflect your choice. You can also choose multiple symbols, and Pine Optimizer will try to optimize for all of them, but that's a more complex scenario that we'll cover, in depth, when we look at campaign structure, in a future session.
As with the chart symbol, we also need to select a chart timeframe. And, again, we are choosing from a favorites list that is individually configurable. Once again, although I could choose multiple timeframes from this list, I'm going to select just the 1-hour timeframe, which happens to match what is currently active on the TradingView chart.
Now, we turn to the strategy parameters. As I mentioned, we need to assign one or more values to each parameter. In its current state, the form is pre-populated with the default value for each parameter, as defined in the Pine Script. And, since every entry field now has a value, the OK button has become activated, which means that it's possible to launch the campaign, in this state, if we wish to.
If we were to launch the campaign, with this configuration, it would execute just one backtest. And, because we're using the default parameter values, it would produce a backtest result that is identical to the one that we created manually, using TradingView.
But, our goal is not simply to execute a single backtest. We want to run a sequence of backtests that represents a complete optimization experiment. To do that, we need to add some more parameter values that will be permutated, to generate a variety of unique backtest configurations.
For example, the MACD strategy defines FastLength and SlowLength parameters, whose default values are 12 and 26 respectively. If our goal is to fully optimize this strategy, it probably makes sense to test a range of values between 8 and 16 for the FastLength and between 22 and 30 for the SlowLength. You might even find that you get viable results when testing parameter values that are way outside those ranges.
But to keep this demo simple, I'm are going to restrict this test campaign to just a few values for each parameter. For the FastLength parameter, I am going to add the values 10 and 14.
I do this by typing directly into the relevant parameter field and using the tab key to complete each entry.
In a future update to Pine Optimizer, we’ll also be introducing an alternative method of configuring parameters, that will support stepping through a range of values. We’ll update this video when we introduce that change.
To remove a value that I’ve entered wrongly, I can simply click the cross adjacent to it.
You may have noticed that, when I entered these 2 additional values, the number of backtest permutations in the campaign increased from 1 to 3. This is because the backtest permutation count is the product of all the parameter value counts. We previously had just one value assigned to each parameter, and these counts have a product of 1. But, by assigning 2 additional values to one of the parameters, the product increases accordingly.
If we similarly update the SlowLenth parameter to include 2 additional values, the backtest permutation count increases to 9, once again reflecting the product of all the parameter value counts.
To keep things simple, I'm not going to add more values to the MacdLength parameter, but you usually would, since it's advantageous to test multiple values for every strategy parameter.
As you can imagine, a strategy with several parameters, each having multiple values to test, could result in a campaign with many hundreds, maybe even thousands, of backtests to execute. This is not ideal, as it could potentially take TradingView a long time to execute those backtests. Instead, it's better to use the more efficient progressive optimization technique, which we'll discuss in a later video.
But, for this demo, we only have 9 backtests to execute, so let's save the campaign configuration and launch it.
Once the campaign has been created, we can verify how many backtests it will execute, by hovering over its launch button. And we can start that execution by clicking that button.
Pine Optimizer immediately sends a request to TradingView to execute the first backtest. As you can see, TradingView runs the backtest in exactly the same way as it would have done if you'd executed it manually.
All of this happens within a few seconds, before the next backtest starts and the cycle repeats itself.
As the campaign progresses, you can track it by opening Pine Optimizer's progress panel. This also enables you to pause or cancel the running campaign sequence, if you need to.
If you want to monitor the outcome of individual backtests, you can also open the history panel, which records each backtest, as it occurs, and shows colored icons to indicate whether each backtest was successful.
If you pause a sequence, it remains active, but idle, for as long as you keep the Pine Optimizer page open in your browser, and you can resume execution at any time. If you close the page, it's equivalent to cancelling the sequence.
When you cancel a sequence, the campaign itself remains intact, but any remaining queued backtests are reverted to the pending state. This results in a campaign that is partially complete, which means that you can re-launch it at some future time. When you do that, only the pending backtests will be run.
In this case, the sequence completed without any issues, so we don't see any backtest failures.
In a future session, we'll discuss how to deal with backtest failures, when they occasionally occur.
But, for this initial demo, let's move on and view the results of our campaign.
The results of the backtests were compiled by TradingView, but they've been automatically imported into Pine Optimizer, so that we can compare them.
If we click this button, to view the outcome of the campaign, we can see 9 sets of backtest results in a table.
This is the backtest results view and it contains all the valuable testing data generated by your campaign.
In the next session, we'll see how to use the various tools in this Pine Optimizer view to analyse backtest results and extract useful insights from them. In the meantime, thanks for watching.