MQL4 – Advanced Sma Entry


In this new video we are introducing a well known and very common entry type which is the Simple Moving Average entry.
The Expert Advisor that you see on the chart right now is calculated for 20 candles and it generates buy or sell signals.
If the price falls below the Simple Moving Average we want to sell and if the price rises above we want to buy.
Let’s see now how to code this Expert Advisor that will open buy or sell positions in the main module!
To begin let’s start the Metaeditor from Metatrader, please hit the F4 key or go to “File/ New/ Expert Advisor (template)” from template, click “Next”, let’s call the file: “CheckEntrySMA”, click on “Next”, “Next” again then “Finish”.
Let’s remove all the things above the “OnTick” function and we don’t need the two comment lines either, please delete them too.
We are going to code a “string CheckEntry” function instead of the “OnTick” because this is an entry module which sends the signal to our main module.
Next we create a variable of a string type for the signal, we also name it as: “signal”, we leave it empty without any value because the calculation will be done later in the code.
That is happening right here as we use the “iMA” function and other necessary parameters to get all the calculations done. This one here is for the symbol which is selected on the chart, and the next one is for the period also the one on the chart, we are using the last 20 candles and we don’t use a shift.
We define the Moving Average method with “MODE_SMA” and if you hit the F1 key you can see that it stands for Simple Moving Average. We want to go with the close price, so that is “PRICE_CLOSE” in the code and for the current candle.
Let’s get to the conditions for the signal. We get a buy signal if the Simple Moving Average is below the Bid price (if my Moving Average (MyMovingAverage) is smaller than Bid) and if that is the case we render the word: “buy” to the signal.
For the sell signal we check if the Simple Moving Average has moved above the Ask price (if my moving average (MyMovingAverage) is greater than Ask) and if the condition is true we render the word: “sell” to the signal.
Once we received a buy or a sell signal we want to return the value to the main module, so we use the “return” function to do that.
My main module is the Forex Trading Framework; this contains several functions which are opening, managing and closing positions. Here you can use your own main module or if you don’t have one you can download the Forex Trading Framework demo version from my website.
So now we are importing the “CheckEntry_SMA” file we just have created into our main module by the “include” function and we recompile the code. Well done, we don’t have errors and no warnings, so we can get back to Metatrader and run a test. (Please hit the F4 key or use this little button here.)
When the test is running we would like to see the Simple Moving Average Indicator displayed on the chart, so we use a simple trick: please go to “Insert/ Indicators/ Trend/ Moving Average”, let’s set the period to 20, the shift value to 0 (zero), the Moving Average method to simple and the price calculation method to close price. Click on “OK”. Now we have the Simple Moving Average on our chart, let’s right-click into the chart then go to “Template/ Save Template” and let’s save the template with a name: “tester.tpl”.
This way when we run the back-test we will see the same template displayed on the chart.
Now we can start the Strategy Tester panel by clicking on “View/ Strategy Tester” or you can just press CTRL + R. Here we select the main module – so that’s the Forex Trading Framework in my case – then start the test by clicking on “start”.
Here we go, our EA is already trading based on the buy or sell signal and now you just learned how to code a “CheckEntry” module that is importing buy or sell signals into the main module (like the Forex Trading Framework) based on the calculation of the Simple Moving Average and you built it in MQL4 with only a few lines of code.

Download “MQL4 - Advanced SMA Entry” MQL4-TUTORIAL-ADVANCED-SMA-EXPERT-ADVISOR.png – Downloaded 80 times – 349 KB