In this video we are going to measure the long term trend and calculate if we want to open, buy or sell trades. So let’s find out how to do that with MQL5.

To get started please click on the little button here.

It is now called IDE. Now you should see the Meta Editor window and a bunch of new icons 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 on tick function and let’s also remove the two comment lines here. First we want to include the file Trade.Mqh. This one is part of MQL5 and it provides some simplified trading functions and now we can create an instance of the Class C trade.

This is called Trade and we are going to use it to open new positions. Inside of the on tick function we first need to create a string variable for the entry.

We use Normalize Double and Symbol Info Double to calculate the ask and the bid price for the current symbol on the chart.

We either use symbol underscore Ask or underscore Bid and with underscore digits and normalize Double we make sure that we calculate either three or five digits behind the dot. That is depending on the currency pair. Now let’s create two arrays. The first one is called My Moving Average Array 950. The second one is called My Moving Average Array 1000 because this is for 1000 candles and this is for 950 candles. Let’s continue and define the properties. We are going to use the integrated IMA function for the current symbol on the chart and the currently selected period on that chart. In this case for 950 candles. We don’t need any shift value and we are going to use Mode EMA for Exponential Moving Average and this should be calculated based on the close price.

Let’s repeat that and calculate the other one.

Everything is similar but this time we are going to use 1000 candles. Afterwards we can use array set as series to sort the moving average for the 950 candles and 1000 candles from the current candle downwards and with copy buffer we fill the Moving Average arrays according to the definition that we have created before. For buffer zero starting from candle zero and we want to copy the price values for three candles and store them in the array for 950 candles or in the array for 1000 candles.

Because that makes it possible to calculate the entry signal and if the Moving average array for 950 candles for the current candle zero is bigger than the moving Average array for a thousand candles for candle zero that would mean we have a buy signal and that’s when we assign buy to our entry signal. In the other case, if the Moving Average array for 950 candles for candle zero is below the Moving Average array value for 1000 candles for candle zero that would mean we have a sell signal and that’s when we assign the word sell to our entry signal.

Finally we want to check if the entry signal equals sell and if positions total is below one that would mean we have a sell signal and we have no open positions and that’s when we want to use trade sell to sell ten micro lot.

In the other case if the entry signal equals buy and we have no open positions we use trade dot buy to buy ten microlot. These parts here is the stop loss and the take profit value. You see that I have chosen 50 points for each value and this is the very interesting part.

If you play around with the settings here you will find out what you can do to improve your results. So really do that! For now we are done.

I have to remove the additional bracket here. Now let’s click on the compile button.

Or Press F7 on your keyboard.

That should work without any errors 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 please click on view, strategy tester or press control and R. Please pick the new file simple long term trend EX5, mark the option for the visual mode here and start your test.

Here we are, the expert advisor is running, you see these two moving averages here and we already have the first position. So our little expert advisor works as expected and in this video you have learned how to create an expert advisor that is able to calculate the long term trend with two moving averages and you have coded it yourself with a few lines of MQL5 code.