In this video we are going to create a crossover Expert Advisor, we have two Moving Averages, one for 50 candles and one for 20 candles, and when we see a crossover of these two Moving Averages it will create a signal that should happen in a few seconds and then we will see, yes, here is the first trade that has been automatically opened, so let’s find out how to do that with MQL4.
Download “CODE - SIMPLE CROSSOVER EA”
To start please click on the little icon 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: “SimpleCrossoverEA”, click on “Continue”, “Continue” and “Finish”.
Now you can remove everything above the “OnTick” function and let’s also delete the two comment lines here.
First we need to declare two variables; one is for the small Moving Average (SmallSMA) that will be for 20 candles, the other one is for the big Moving Average (BigSMA), that’s for 50 candles.
You see this modifier: “input” here, it creates an external variable, an external variable can be changed without recompilation.
Inside of the “OnTick” function we start by creating a variable for the signal (signal), that’s a string variable and we don’t assign any value right now because we need to calculate it later on, but first please let me calculate a few Moving Average values.
The first one will be called small Moving Average one (SmallMovingAverage1), we use the “iMA” function that comes with MQL4 for the current symbol on the chart and the currently selected period on that chart.
The value for the candles will be this one, the small SMA (SmallSMA) for 20 candles, we don’t need a shift value, we use “MODE_SMA” to calculate a Simple Moving Average and the calculation should be done based on the close price (PRICE_CLOSE) for candle 1.
Now we do the same thing for the big Moving Average (BigSMA) for candle 1, this line is almost identical, we have a different variable name here and a different value because big SMA (BigSMA) equals 50 candles, and to calculate if we have a crossover we also need to do that for candle 2.
So basically we copy both lines, change the last variable here for candle 2 and please do not forget to also change the name of the variable here.
So now we have four Moving Average values and if the big Moving Average (BigMoingAverage1) is greater than the small Moving Average for candle 1 (SmallMovingAverage1) and the big Moving Average for candle two (BigMoingAverage2) was below the small Moving Average for candle two (SmallMovingAverage2) that would be a sell signal so now we assign the word: “sell” to our signal.
Otherwise if the big Moving Average (BigMoingAverage1) now is smaller than the small Moving Average for candle 1 (SmallMovingAverage1) and the big Moving Average for candle two (BigMoingAverage2) was bigger than the small Moving Average for candle two (SmallMovingAverage2) that would be a crossover in the other direction and now we have a buy signal, so we assign the word: “buy” to our signal, and if our signal equals buy and “OrdersTotal” is 0 (zero) that means that we have no open orders we will use the “OrderSend” function to buy 10 micro lot.
Otherwise if the signal equals sell and we also have no open position or open order we use the “OrderSend” command to sell 10 micro lot.
Finally we want to create a chart output, so we use the “Comment” function to output the words: “The current signal is:” and the signal we have calculated.
If this was too fast for you maybe you want to watch the other MQL4 starter videos or maybe even take the premium course.
When you are done you can click on the “Compile” button or press F7 on your keyboard, that should work without any errors and in that case you can click on the little button here or press F4 to go back to Metatrader.
In Metatrader we use a little trick, please click on: “Insert/ Indicators/ Trend/ Moving Average”, select 20 for 20 candles in the period, the shift will be 0 (zero), we want to calculate a Simple Moving Average and that should be done based on the close price, I want the color red, so let’s click on “OK”.
Here is the first Simple Moving Average, now we repeat that and the next Moving Average should have a value of 50 in the period, I want yellow, so let’s click on “OK”, now we right click on the chart, select: “Template/ Save Template” and we save it as: “tester.tpl” because this is the template that is going to be used in our back test.
If you don’t see the Strategy Tester please click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleCrossoverEA.ex4”, mark the visual mode here and start your test.
Here we are, we see our two Moving Averages, for now let’s stop the test because when you click on: “Expert properties/ Inputs” you can now change the value here.
Let’s say 30 for the small Moving Average but don’t forget to also change the value for the template, click on “OK” and save the template again as “tester.tpl”, you can replace the old one, now let’s start another test and this time the red line here is for a 30 candle Moving Average.
Let’s speed up the process and we should see a crossover within a few seconds.
Here it is, and this is our first sell trade and in this little video you have learned how to create an Expert Advisor for two Simple Moving Averages, it generates buy and sell signals for each crossover and you have coded it yourself with a few lines of MQL4 code.
Download “CODE - SIMPLE CROSSOVER EA”SimpleCrossoverEA.mq4 – Downloaded 1759 times – 1.69 KB