MQL4 TUTORIAL – ADVANCED SMA EXPERT ADVISOR

video
play-sharp-fill

In this video we are talking about one of the most popular entries that are out there; it’s the Simple Moving Average entry. This one is for 20 candles, it creates buy or sell signals directly on our chart. Whenever the price is below the Moving Average that’s a sell signal and when the price is above the Moving Average that’s a buy signal.

Now how can we create an Expert Advisor module that is able to output buy or sell signals or trade signals like this one in our main module?
To do that please click 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 one: “CheckEntrySMA” click on “Continue”, “Continue” and “Finish”, now you can delete everything above the “OnTick” function and the two comment lines here, we are not going to use “OnTick” here because this will be the module that is used to get the entries for our main module; we create a function called “string CheckEntry”; here we create a string variable for the signal that will be called: “signal”; we don’t assign a value cause now we are going to calculate it.
To do that we use the “iMA” function for the current symbol on the chart and the currently selected period, it is calculated based on 20 candles, we don’t use a shift value.

Let’s mark “MODE_SMA” a press F1 and you see that we are using the Simple Moving Average method and we also use “PRICE_CLOSE” to calculate the result based on the close price for the current candle.

…and if the Simple Moving Average is below the Bid price, so if my Moving Average (MyMovingAverage) is less than Bid that would be a signal to buy so we assign the word “buy” to our signal.
In the other case if the Simple Moving Average is above the Ask price, so if my moving average (MyMovingAverage) is greater than Ask that’s a signal to sell so now we assign the word: “sell” to our signal.

In the last step we are going to return the signal to the main module that’s done by using the “return” statement to our main module; in this case is the Forex Trading Framework, it’s basically a collection of functions that can open and handle positions.
Here we are going to include our “CheckEntry_SMA” file.
Now I need to recompile the code here and that worked without any warnings or any errors, you could download the Forex Trading Framework demo version from my website or you could use your own main module and after the compilation I click on the little button here or press F4 to go back to Metatrader and here we use a little trick we click on: “Insert/ Indicators/ Trend/ Moving Average” select a period of 20, a shift value of 0, the Moving Average method should be simple and it should be calculated based on the close price, so let’s click on “OK”.

Here is our Moving Average, let’s right click into the chart, select “Template/ Save Template” and save it as “tester.tpl” because “tester.tpl” is what we are using in the back-test. If you don’t see the Strategy Tester panel here you can click on “View/ Strategy Tester” or press CTRL and R, let’s pick the main module – in my case it’s the Forex Trading Framework – and click on start.

…and here is our little Expert Advisor at work, it creates buy or sell signals and now you know how to calculate a Simple Moving Average in MQL4 and how to create buy or sell signals that can be imported into a main module like the Forex Trading Framework and you have coded it yourself with a few lines of MQL4 code.

Download “MQL4 TUTORIAL - ADVANCED SMA EXPERT ADVISOR”

AdvancedSMA.txt – Downloaded 2944 times – 637.00 B