MQL4 TUTORIAL BASICS – 19 HOW TO TRADE A SIMPLE MOVING AVERAGE

video
play-sharp-fill

In this video we will create this simple Expert Advisor that is actually going to trade. It crates buy or sell signals based on the Simple Moving Average that you see here.
When you hover the red line you will see that’s Moving average for 20 candles, so let’s find out how to create such a simple system.
To do that first please click on the little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleMovingAverageEA”, click on “Continue”, “Continue” and “Finish”.
Let’s remove everything that is above the “OnTick” function and delete the two command lines here.
We start by creating a string variable for the signal that will be called: “signal” and it doesn’t contain any value so far because we are going to assign that later on.
Now we want to calculate the Moving Average for 20 candles.
MQL4 comes with a function that is called: “iMA”, we pass a few parameters here;
The first one will get the current symbol on the chart,
The second one will find out what the time period on that chart is,
The third parameter here is for the number of candles that we are going to use.
This value here and the last one are shift values; the first one could be used to move the drawing of the red line here to the right or to the left,
The last one here is used to tell Metatrader for which candle we want the value because all those candles here have an on value for the Simple Moving Average and all the values are connected to draw the line.
This parameter here (MODE_SMA) is to define that we want to use the Simple Moving Average or SMA and it should be calculated based on the close price (PRICE_CLOSE).
And if the Moving Average is below the current price we want to assign the value: “buy” to our signal. Otherwise if the Moving Average is above the current price we want to assign the value: “sell” to our signal.
Now we need to check if the signal equals buy and the other condition we want to check if is “OrdersTotal” equals 0 (zero) so we have no open order.
You see this operator here (&&)?
It is used to check if both conditions are true, so only if the left condition is true and the right condition is true we use “OrderSend” to buy 10 micro lot.
Otherwise if the signal equals sell and if “OrdersTotal” is 0 (zero) we use “OrderSend” to sell 10 micro lot.
If you have problems understanding this line please go back to the former basic videos. For this simple version we will just need to add one more thing, we use the “Comment” line to output the sentence: “The current signal is:” followed by the signal.
That’s about it. Let’s click on the “Compile” button, we don’t have any errors so now we can go back to Metatrader.
Now if we start our Expert Advisor we don’t see the Simple Moving Average here, to change that we use a little trick; we just click on: “Insert/ Indicators/ Trend/ Moving Average”, here we pick the 20 candles, the shift value is 0 (zero), we want the Simple Moving Average and that should be calculated based on the close price, so lets click on “OK”.
Here is our Indicator, now we right click into the chart, select: “Templates/ Save Template” and we save it as: “tester.tpl” because this is the template that is going to be used in our Strategy Test.
So let’s click on: “View/ Strategy Tester” or press CTRL and R, pick the new file: “SimpleMovingAverageEA.ex4”, mark the visual mode here and start a test.
And here we are! This is the Simple Moving Average that we have calculated, we get buy or sell signals directly on our chart, this is our first sell position here so the little EA is actually trading and in this little video you have learned how to create a working Expert Advisor that actually trades buy and sell signals for a Simple Moving Average and you have coded it yourself with a few lines of MQL4 code. Download “CODE - SIMPLE MOVING AVERAGE EA” SimpleMovingAverageEA.mq4 – Downloaded 492 times – 1,014.00 B