In this video we are going to create an expert advisor that is able to calculate trends on two different charts by using an exponential moving average. So let’s find out how to do that with MQL4. To get started, please click on a little icon here or press F4 on your keyboard. Now you should see the Metaeditor window and here you want to click on File, new File, Expert Advisor from Template, continue. I will call this file simple multi chart trend.
Click on continue, continue and finish. Now you can delete everything above the Ontick function and let’s also remove the two comment lines here. We start by creating a string variable. One is for the signal on the current chart, and let’s add another one, this time for the Euro US dollar. Afterwards we can use the integrated IMA function that comes with MQL4.
It will calculate our exponential moving average for the current symbol on the chart and the currently selected period on that chart. We are going to calculate it for 20 candles. We are not using a shift value. The mode is mode underscore Ema, that stands for exponential moving average. The result should be calculated based on the close price and we want to calculate it for the current candle zero.
Now let’s repeat the whole thing and this time we are going to calculate an old EMA 20. Everything is similar, but the result is calculated based on the value for candle two. Now comes the interesting part. This time we want to calculate it for the Euro US dollar, so we need to exchange the first parameter here. I have used the hard coded Euro US dollar currency pair.
When you open MetaTrader, you should see how it is shown on your own account. Depending on the account type, you could maybe see a little suffix here. So please make sure that you name it as it is shown for the account type you are using. The rest is like in the first line, and as you might have guessed, we need to calculate the same thing for candle two because now we are able to check the trend. If the current EMA 20 is above the old EMA 20, that would be a buy signal for the current chart, and that’s when we assign buy to our chart signal.
In the other case, if the EMA 20 is below the old EMA 20, that would be a sell signal, and that’s when we assign sell to our chart signal. Now it’s easy to do that for the Euro US dollar, everything is similar. All you need to do is to check if the value for the current EMA of the Euro US dollar is above the old value. That would be a buy signal, or if it is below the old value for the EMA Euro US dollar, that would be a sell signal.
Finally, we want to add a comment statement that will output the current values for both charts and the trend for both charts and that’s about it.
If this was too fast for you or if you have no idea what all the code here does, you maybe want to watch one of the other videos in the basic video series, or maybe even the premium course on our website might be interesting for you. But for now, please click on the compile button or press the F7 key. You shouldn’t get any errors here, and if that is the case, you can click on a little button here or Press F4 to go back to MetaTrader. And in MetaTrader, you want to pick an empty chart.
Select Insert, Indicators, Trend, Moving Average. We want to use a moving average that is calculated for 20 candles. No shift value.
The Ma method should be exponential, apply to the close price, you can pick a color and click on Okay. Now the moving average shows up and now you want to rightclick into the chart. Select template, save template and save it as tester.tpl and default.tpl. Tester.tpl is what will show up in the strategy tester and default TPL is what you will see when you open a new live chart. Today is Saturday so the markets are closed and we only can do a backtest. So I click on OK.
If you don’t see the strategy tester, you want to click on View, Strategy Tester or press Control and R. Please pick the new file Simple Multi Chart Trend.ex4. Mark the option for the visual mode here and start your test. Of course you need to have historical data for both currency pairs.
I have imported that before I started to make this video. Let’s speed that up a little bit and you see that it actually works. You can see that buy and sell trends are calculated. And in this little video, you have learned how to calculate multiple trends for different charts and you have coded it yourself with a few lines of MQL4 code.