MQL5 TUTORIAL – CROSSOVER STANDALONE EXPERT ADVISOR

video
play-sharp-fill

In this video we are going to talk about one of the most popular entries; this is a crossover entry for a Moving Average that is calculated for 50 candles and for 20 candles, and whenever those two lines cross we have a buy or a sell signal.
Most people think it’s only about the entry, so let’s create a standalone module to disprove that.
To do that 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: “SimpleCrossoverStandalone”, click on “Continue”, “Continue” and “Finish” and now you can delete everything above the “OnTick” function and the two comment lines here.
First we need to include the “Trade.mqh” file that is needed to create an instance of “CTrade” that will be called: “trade”. In the “OnTick” function we create an empty string, let’s call this one: “entry”, we don’t assign the value here because we are going to calculate that later.
Now we use “SymbolInfoDouble” for the current symbol to get the Ask price and the Bid price. If you click on one of the symbols here you will see that we have two prices; the Ask price is what we need to pay if we buy something and the Bid price is what we get when we sell something and we use “NormalizeDdouble” and “_Digits” to get the right number of digits behind the dot because you see that this currency pair has 3 digits behind the dot and this one has 5.
Now we need two arrays for several prices; one for the Moving Average array (MovingAverageArray20) for 20 candles and another one for the Moving Average array (MovingAverageArray50) for 50 candles, now we define the properties for the Moving Average by using the “iMA” function for the current symbol and the currently selected period on the chart.
This one is for 20 and this one is for 50 candles, we will use “MODE_EMA” to get an Exponential Moving Average and it will be calculated based on the close price.
Let’s sort the price array from the current candle downwards; that is done by using “ArraySetAsSeries” for the Moving Average array (MovingAverageArray20) for 20 and 50 candles (MovingAverageArray50) and with “CopyBuffer” we fill our Moving Average for the first buffer – that’s buffer zero – from candle 0 – that’s the current candle – for 3 candles and store the result in my moving average array 20 (myMovingAverageArray20) and in my moving average array 50 (myMovingAverageArray50) and here comes the entry: if my moving average array 20 (myMovingAverageArray20) for the current candle is bigger than my moving average array 50 (myMovingAverageArray50) for the current candle and if my moving average array 20 (myMovingAverageArray20) for the last candle was below my moving average 50 (myMovingAverageArray50) for the last candle that’s when we have a crossover and now we want to buy. If my moving average array 20 (myMovingAverageArray20) for the current candle is below my moving average array 50 (myMovingAverageArray50) for the current candle and if my moving average array 20 (myMovingAverageArray20) for the last candle was above my moving average array 50 (myMovingAverageArray50) for the last candle that’s a crossover in the other direction and now we want to sell.
So far so good, if the entry equals sell and we have less than one position we want to sell 10 micro lot in the other case if the entry equals buy and we have less than one position we want to buy 10 micro lot.
So this is it. When you’re 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 here or press F4 to go back to Metatrader.
In Metatrader you want to click on “View/ Strategy Tester” or press CTRL and R, this is the Strategy Tester panel and here you want to select the new file: “SimpleCrossoverStandalone.ex5”, please enable the visual mode here and start your test.
…and here is our little Expert Advisor at work, it had just had crossed and opened a sell trade.
Now you can speed up everything and you see a result like this one; actually we made a loss because the entry signal isn’t everything. Well, you can make a profit with this kind of entry but the entry isn’t the most important thing. It’s more about what you do after you have opened the trade.
In this video you have learned how to create a standalone crossover Expert Advisor that is actually going to trade and you have coded it yourself with a few lines of MQL5 code.

Download “MQL5 TUTORIAL - SIMPLE CROSSOVER STANDALONE EXPERT ADVISOR”

SimpleCrossoverStandalone.txt – Downloaded 541 times – 2.15 KB