Backtest and Optimize a Trading Strategy in MultiCharts
In this section we are going to look at how to backtest a trading strategy and run some optimizations to see how different parameters affect performance.
We will cover:
- Adding a strategy to the chart
- Adding technical indicators
- Correctly strategy settings
- Common errors
- Optimizing a strategy
- Importing a strategy
BackTesting a Strategy
MultiCharts is an excellent platform for backtesting rule based strategies. Of course, you can manually test your ideas by looking at the charts but the most powerful and fastest way to do backtesting is by programming the strategy rules and let MultiCharts do the backtesting for you.
You can find a list of pre-built strategies within MultiCharts. This can get you started and it’s nice to look at the strategy codes to get an understanding of Powerlanguage which is the name of the programming language which MultiCharts uses.
Pre-built strategies or “Signals” as they are called in MultiCharts include common ones like moving average crossovers, Bollinger band and MACD strategies plus many more.
To run a backtest you first must select the desired Instrument, time frame and date range. Do this as described in “Create a Chart Window” section in MultiCharts – Creating a Price Chart.
Adding a Signal (strategy)
Note that I will refer to a trading strategy as either a “strategy” or “signal” for this article just assume they are the same.
Now we can add our strategy (signal). To do this, right click the chart and Insert study. A “study” will mainly be either a signal (strategy) or an indicator like RSI for example.
But for backtesting we are going to select the Signals tab and browse the list of available built-in signals MultiCharts offers.
Note that when we create our own Signals and Indicators they automatically get added to this list.
Select the desired Signal and click OK.
For this example I am going to select “Bollinger Bands LE.” This is a basic Bollinger Band strategy and the “LE” stands for “Long Entry.”
And a Long ENTRY is all we have. If this was solely applied to the chart we would expect to see 1 entry for the first trade of the data set and that’s all. This is because we only have long entry rules in this signal. There’s no way of exiting in the form of a stop loss, take profit etc.
As a note, the strategies I develop always include exits within them. Unlike what we see with the Bolling Band example.
To get an exit let’s add another signal as per the steps above and this time add “Bollinger Bands SE.” Which gives us our Short Entry signals.
Now using the two separate signals we have a strategy we can backtest. This is now a “stop and reverse” strategy. If we get a long entry, we stay in the trade until we get a short entry at which time the long position is exited and a new short position opened and vice versa.
Adding multiple Signals to the same price chart can allow us to mix and match entries and exits.
However, as I previously said this is not something I personally do. My strategies or signals contain everything I want the strategy to do including entries, exits, stop losses, profit taking etc.
Adding an Indicator
This is done similarly to adding a signal as described above. Although this time we select an indicator from the “Indicators” tab.
Note that a strategy (signal) which uses a particular indicator does not need the indicator to be added to the chart in order to work. Adding the indicator can be nice to see that the strategy is opening and closing trades when we expect it to.
Strategy Properties
Before we look at the results of the backtest there’s a couple more settings to discuss.
Format Signals
Looking at the “Format Objects” window which appears either after you add a signal or if you right click the charts and “Format Signals” you will notice the signals applied (if you are in the “signals” tab.)
If the strategy has inputs, this is where we can make changes to them. In the case of the Bollinger Band signal example, highlight with a single click on the signal and click “Format” or double click the signal to open the Format Signal window where we see the signal’s inputs.
Adjust the inputs if desired. For example the length of the Bollinger Bands from the default 20.
Properties
On the “Format Objects” window we find a button “Properties” which when clicked opens the “Strategy Properties” window.
Maximum number of bars study will reference
This is where some common errors can occur. On the “Properties” tab which is the first to open, the first box we need to look at is the “Maximum number of bars study will reference” which is set to 50 by default.
This only needs to be changed if our strategy looks at bars further back than 50. As an example the Bollinger Band strategy uses a default length of 20 but if we want to increase that more than 50 we will get an error.
If we want to test a length of 75 instead of 20 then we must increase the value to 75 or more in the “Maximum number of bars study will reference” box.
Note that if your strategy only looks at the previous 3 bars, then you can also change this from default 50 to 3. Otherwise the first 50 bars of the backtest won’t have any trades. This can be significant if testing a strategy using weekly bars!
Base Currency
Below the “Maximum number of bars study will reference” box we see a drop down for “Base Currency.”
This is another point where errors can occur. I suggest changing the base currency to either “None” or the base currency for the instrument tested. For example if testing EUR/GBP then select the base currency GBP or None. Testing USD/JPY the base currency should be JPY etc.
If you leave the base currency set to USD for all strategies then MultiCharts will try and do a currency conversion which can cause issues with the backtest results from my experience.
Trade Size
I almost always test a Forex strategy using 1 full lot which is expressed as 100000 units in MultiCharts.
To do this in the “Fixed Shares / Contacts” box enter 100000.
Important: You must also increase the “maximum shares / contracts per position” from the default to 100000.
Common errors with settings
As previously stated, be mindful about the instrument settings in terms of “Sessions” and “Time zone.”
I see other common errors relating to “Maximum number of bars study will reference”, “Base currency” and “maximum shares / contracts per position” as detailed above.
Now you should be able to close the strategy properties windows and the strategy will be applied to your price chart. You can see each trade with a connecting line on the chart.
Not only can you see where the individual trades were taken on the chart, you can now view a detailed Strategy Performance Report.
Strategy Performance Report
Access the strategy performance report by either clicking “View, Strategy Performance Report” from the top menu, or right click the chart “Strategy Performance Report” or just hit the button in the toolbar.
Strategy Performance Report Button
I’m not going to go over the report in detail here. You should explore all the options you have from the menus on the left hand side of the report window.
Optimizing a Strategy
MultiCharts can perform extensive optimisations of inputs in our trading strategies.
Let’s use the “Bollinger Bands LE” built-in strategy as an example. Remember we needed to apply 2 signals to make up this strategy; “Bollinger Bands LE” and “Bollinger Bands SE.”
We could easily optimize the input value of these signals called “Length” which is the length of the Bollinger Band set to 20 by default.
But instead, let’s take another built-in signal.
This time we are only going to use “Bollinger Bands LE” for the entry, but we now need an exit signal.
Right click the chart, Format Signals, click Add and then select “Time Exit (Bars) LX” and hit OK.
If you still have the “Bollinger Bands SE” make sure it is not ticked or just remove it completely.
Now we have a long only strategy taking signals from the “Bollinger Bands LE” and exiting after a certain number of bars defined by the input “BarToExitOn” within the “Time Exit (Bars) LX” signal.
At this point it doesn’t matter what time frame you use on your price chart.
Running the Optimization
Once the strategy is working with the long trades generated from the Bollinger Band signals you will see exits after 5 bars.
If we want to quickly compare other exit lengths then we run an optimization.
Right click the chart and select “Optimize Strategy…” or hit the “Optimize Strategy” icon in the toolbar.
Optimize Strategy Button
You have the option of different types of optimisations but for this example we are going to use the “Regular Optimization.”
Make sure just the “Time Exit (Bars) LX” signal is ticked and you can see that the only available input to optimize for this signal is “BarToExitOn.”
Choose the value range you want to optimize. From 1 to 20 bars for example. Then choose steps. Choose 1 to check every value. If you chose 2 then it would check values starting at 1 then 3, then 5 etc..
Now hit the Optimize button in the lower right corner.
You can see a spreadsheet of all the results using the different input values.
Click on the “2D Graph” tab to see the results on a graph. You can adjust the graph settings to display the desired results.
Note that I am not for one minute suggesting this is a good trading strategy! It is just an example of how you can optimize a part of your strategy.
Common optimizations I look at are lengths of indicators, stop loss values, time of day to trade, profit targets and many more.
How to Import a Trading Strategy
If you want to use a strategy such as one of mine and have the .pla file, you need to import it into MultiCharts.
You can also create the strategy if you have the code in plain text.
Codes for strategies (signals) and indicators are created and edited within the “PowerLanguage Editor” of MultiCharts.
First, open the editor by clicking the icon in the toolbar “Open New PowerLanguage Editor Window”
PowerLanguage Editor Button
Importing a .pla File
Click File, Import… And select where you have the .pla file saved.
If the strategy doesn’t automatically compile then hit the “Compile” button in the toolbar.
Compile Button
Using Plain Text
Click File, New… and then select “Signal” and hit OK.
Give the signal (strategy) a name and hit OK.
This opens a blank page to start programming. However we are not going to manually write the code. Open the document where you have the strategy written in plain text, select all and copy.
Then simply paste the code into your new signal sheet in the PowerLanguage Editor.
Hit the “Compile” button in the toolbar and it should be ready to use.
You can find either your imported strategy or the plain text one you have created in the list of Signals when you go to insert one on the price chart.
Now you can backtest a trading strategy. Eventually you will have some strategies which you are happy with and may want to automate.
I cover automated trading the section Automated Trading
MultiCharts User Guide Index:
MultiCharts – Download and Install
MultiCharts – Data Feed and Broker Connection
MultiCharts – Creating a Price Chart
MultiCharts – Backtesting a Trading Strategy