MQL4 TUTORIALBASICS – 128 SIMPLE LONG TERM TREND

video

In this video we are going to create an Expert Advisor that is able to calculate buy and sell trades based on the long term trend.

 

So let’s find out how to do that with MQL4. To get started please click on on a little icon here or press F4 on your keyboard. Now you should see the Meta Editor window. And here you want to click on File, new File, Expert Advisor from Template, Continue. I will call this file Simple Long Term 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 want to define two moving averages.

 

The first one is for 950 candles, the second one is for 1000 candles. Inside of the on tick function we want to create a string variable called signal. But we don’t assign a value here.

 

Because we are going to calculate that now. And to find the long term trend we want to create a moving average. That can be done by using the integrated IMA function for the current symbol on the chart and the currently selected period on the chart. We want to calculate our small SMA for 950 candles. The zero here means we have no shift value. We want to use mode underscore SMA. That stands for Simple Moving Average. And the result should be calculated based on the close price. We want to do that for candle one. Let’s repeat that. Everything is similar, but this time we are doing it for the big moving average.

 

The only difference is the name of this variable and this parameter here is calculated based on 1000 candles. And now we are able to define the long term trend.

 

We simply want to check if the big moving average is bigger than the small moving average. And if that is true that would mean that we have a sell signal. So we assign the word sell to our signal. And of course if the big moving average is smaller than the small moving average, that would mean we have a buy signal and in that case we assign the word buy to our signal. And if we have a buy signal and the return value for orders total is zero, that would mean we have no open orders but a buy signal. And that’s when we use Order Send for the current symbol to open ten microlot buy trade. This part here is the value for the stop loss and for the take profit. If you modify these numbers and do a lot of testing, you will find out what really can improve your results. But back to topic. And if our signal equals sell and we also have no open orders, that’s when we use Order Send to send a sell trade for the current symbol. This one is also a ten microlot position and we use 50 points for the Take profit and the Stop loss value and that’s about it.

 

Well, if this was too fast for you or if you have no idea what all the code here does, maybe you want to watch one of the other videos or maybe even the premium course on our website might be interesting for you. That’s also where you can find a shop link, if you don’t want to type everything yourself. For now, let’s remove these empty lines here and click on the compile button or press F7 on your keyboard. You shouldn’t get any errors and if that is the case you can click on a little button here or Press F4 to go back to Meta Trader. And in MetaTrader you want to click on View, Strategy Tester or press Control and R. Please pick the new file simple long term trend ex four.

 

Mark the option for the visible mode here and start your test. If you don’t see the moving average line appear on your chart, just click on stop, right click into the chart. You should now see it and select template, save template and save the template as tester.tpl, because this is what we see when we start a strategy test. You can override the current value. Now restart the test and now the moving average values should be visible and we already have the first position on our chart. So our little expert advisor is working as expected and in this video you have learned how to calculate the long term trend by using two moving averages and you have coded it yourself with a few lines of MQL4 code.