MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 Tutorial – Simple Force Index Trading Robot
    Okay this time we are talking about the Force Index Indicator. It is a very simple indicator. It’s easy to read and also easy to code. If the indicator crosses the line in the middle from below and stays above the line, that could mean that we are bullish. And when it crosses the line from above and stays below the line like in this case, we have a bearish trend and we would like to sell. According to the website www.traderhq.com, this is also an indicator that was developed by Dr Alexander Elder, the author of the book: Trading for a Living. Okay now that we know how it works how can we create an Expert Advisor to use the indicator? The first step is to click the little button over here or hit the F4 key on your keyboard. That will start the Meter Editor and we want to click on File, New, Expert Advisor from template, Continue. We will call it Simple Force Index Robot. Now click on Continue, Continue, Finish, and delete everything above the Ontick function. These two comment lines are also no longer needed, because here we want to create an array, it shall holds several prices so it’s a double array and I will call it my price array. And now it’s time to define the Force Index definition by using the iForce function that is built-in into MQL 5. It takes some parameters, the first one is the current symbol on the chart, the second one is the period you have selected on your chart. The Force Index is calculated based on the last thirteen candles. You will also see it here in this little brackets. It is using the MODE_SMA that stands for Simple Moving Average and we want to use Tick volume. If you insert the indicator by clicking on Insert indicators, Oscillators, Force Index, you will see the exact same values here: thirteen candles, simple moving average based on the tick volume. Okay, let’s use the array set as series function that will sort the price array from the current candle downwards and now we use Copy Buffer for the Force Index definition we have created here. We need one line for the indicator that’s a zero here and we want to fill our array from the current candle that’s candle zero, for three candles that’s the three and store the result in our price array. And from our price array we are now going to extract the Force Index value for the current candle zero and we will use normalize double for six digits to get the six digits behind the point. And now we want to create a chart outputs depending on the value and if our Force Index value is above zero, we want to output the comment “trending upwards” directly on our chart and in the other case if the Force Index value is below zero, we want to have the text “trending downwards” on our chart. Okay, now please click on the little Compile button here or press the F7 key on your keyboard. That should compile your Expert Advisor without any errors or warnings here. And if that was going well please click on the button here or press F4 to go back to MetaTrader. Now in MetaTrader you want to click on View, Strategy Tester or press control and R. And in the Strategy Tester you want to select the Simple Force Index Robot.ex5 file. Let’s pick a currency pair here, enable the visualisation mode and click on Start and here we go. Now the indicator is below the line so it says trending downwards and as soon as the little indicator crosses the line from below, that changes to trending upwards. Okay, now you know how to create an Expert Advisor for Meta Trader5 that is using the Force Index Indicator and you have created it yourself in five minutes with a few lines of MQL5 code.
    6 min
  • MQL5 Tutorial – Simple Bears Power Expert Advisor
    With MQL5 and Metatrader5 we can use the iBearsPower function to create an Expert Advisor for the Bears Power Indicator that is included in Metatrader. Use it in your trading robot to find out, if the bulls or the bears are stronger… This time we are talking about an Expert Advisor created for the Bear’s power oscillator. You might know that Bulls and Bears are always fighting every day and with this oscillator you can see how strong the Bears actually are. The largest value on the whole day actually happened here, right before the price went through the roof. This oscillator was created by Alexander Elder. And he believes that the lowest price displays the maximum seller’s power within a day. You might know his book- Trading for a living. I think that is an interesting read and in this case he’s right. Now how can we create an Expert Advisor for this oscillator in MQL 5? First please click on this little button here or press the F4 key to bring up the Meta-Editor, and in the MetaEditor click on File, New, Export Advisor from template, continue, we will give it the name simple Bear’s power. Now click on continue, continue, finish and remove everything above the Ontick function, and the two comment line here can also be removed. And we will start by creating a price array. This is a double array called my price array and now we are going to define the properties of the Bear’s power EA by using the function iBears power for the current symbol and the selected period on your chart . That might be a one minute or hourly chart for example and we want to calculate our value based on the last thirteen candles, because this is what Mr Elder recommended. You might also use the indicator together with a trend indicator like a moving average. But back to topic, after we have defined our iBears power indicator we want to sort the price array from the current candle downwards by using the arraysetasseries function for our defined price array and now we use copybuffer and the Bear’s power definition we have created here to calculate one line or, in our case one value, that’s what the zero stands for. For the current candle that’s candle zero; for three candles and store it in my price array, the array that we have created here. And now we actually want to calculate the Bear’s power value. We grab the value of candle zero in my price array and now we want to output the text Bear’s power value and the calculated value on our chart by using the comment function. And that’s it. Let’s compile the Expert Advisor by clicking this little button or pressing the F7 key and you should see no errors and no warnings. So now we can click this little button here or press the F4 key to go back to Metatrader. In Meta Trader please click on View, Strategy Tester or hit the Control and the R key, and that will bring up the little Strategy Tester Panel here. And there you want to select the SimpleBearspower.ex5 file. Please mark the visualisation option here and click on start. And this is what you should see on your chart. The calculated value here is a little bit longer than the indicator output. Now I would change this double value to a float type and compile the Expert Advisor again. And now it says we could have a possible loss of data and this is true we have lost one digit, but it will now also only consume four bytes instead of eight bytes of memory. Okay, now you know how to create an Expert Advisor for the Bear’s power indicator and you have done it with a few lines of MQL 5 code.
    5 min
  • MQL5 Tutorial – Simple Demarker Trading Robot with MQL5
    This time we are talking about an indicator that is called the Demarker indicator. This indicator is also simple to understand. You have two levels; one level is at 0.3, that’s the lower line here and if the index line is below the lower line, you have a weak trend. The other line is at a level at 0.7, that’s the upper line here and if the indicator line is above the upper line you have a strong trend. So if the indicator line is above and crosses the upper line the trend may be over. That is what happened here. And now we want to create an Expert Advisor that uses the Demarker indicator to tell us if we have a strong trend. Now how can we do that? First of all please click on the little button over here or hit the F4 key your keyboard. That will bring up the Meta Editor. And here you want to click on File, New, Expert Advisor from template, Continue and we will call the Expert Advisor Simple Demarker. Now click on Continue, Continue, Finish, and delete everything above the Ontick function. I will also remove the two comment lines here and now I need an array that can hold several prices and I call it my price array. And it’s not necessary to define the properties for the Demarker EA by using the iDemarker function that is included with MQL 5. It’s a very easy indicator because it only needs the symbol, the period and the period for the Demarker EA. So we will use underscore symbol for the current chart. Underscore period for the selected time frame on your chart and we want to calculate the Demarker value for fourteen candles, because if you click on Insert, Indicators, Oscillators, Demarker, you will see that fourteen candles is the standard value. After we have done that we want to use the arraysetasseries function to sort our prices in the array from the current candle downwards. And then we use copy buffer and the Demarker definition we have created here. Our indicator has just one line so that’s the first zero here and we need the current candle that’s the second zero, and we want to copy the values of three candles that’s the three here and store our result in our price array. And now that we have done all this, we want to get the value of the candle zero, that’s the current candle of our price array and store the result in the variable called Demarker value. And the next thing we need is the chart output depending on the value. If our Demarker value is above 0.7, we have a strong trend so we use the comment function to output this text on our chart. And if it is below the lower line, and that would be the case if the Demarker value is below 0.3, we want the text “Weak Trend” on our chart. And if our value is between the two lines, we don’t have any signal so we will create an empty output. Okay that’s it, now please hit the compile button here or press the F7 key and you should get zero errors and no warnings here and if everything went right please click on the little button here or press F4 to go back to MetaTrader. In Meta Trader you want to click on View, Strategy Tester or press control and R on your keyboard to get the Strategy Tester panel up and running and here you want to select the Simple Demarker.ex5 file. Please don’t forget to enable the visualization mode, pick any period and currency here, and click on start. And now you can see that our Expert Advisor is working. We have a weak trend because the line is below the lower line here and when we have a strong trend, you see that the line is above the upper line here. Okay, now you know how to create an Expert Advisor in MQL 5 that uses the Demarker indicator to tell you if a trend is strong or weak and you have created it yourself with a few lines of MQL 5 code.
    6 min
  • MQL5 Tutorial – Simple Commodity Channel Index
    With MQL5 for Metatrader5 we can use the ICCI function that is built in to create an Expert Advisor that will use the Commodity Channel Index Indicator to create entry signals on our Forex Trading Chart… This video is about the commodity channel index. The commodity channel index is an oscillator and it will tell you if something is overbought or oversold. This is one of the oscillators I like because it is very easy to understand. It has one hundred plus and a one hundred minus line here and whenever the value rises above the upper line, it’s overbought. In the other case if the indicator falls below the lower line, it’s oversold. The commodity channel index can be created with the MQL 5 function ICCI that is built-in into MQL 5. And now we want to create a little Expert Advisor that will tell us when the market is overbought or oversold. To do that please click the little button here or hit the F4 key on your keyboard. That will bring up the Meta Editor, and here we want to select File, New, Expert Advisor from template, continue. And we will call the Expert Advisor Simple Commodity Channel Index. Now click on Continue, Continue, Finish, everything above the Ontick function can be removed and we will also delete the two comment lines here and we will start by creating a little array for our prices. It’s a double array and we will call it my price array. And for the definition of our commodity channel index, we will use the ICCI function of MQL 5. For the current symbol on the chart and the selected period, this might be Euro/U.S. dollar and this might be the minute chart or the hourly chart, and the calculation is done for fourteen candles, because this is the default value of the commodity channel index when you drag it on your chart by using Insert, Indicators, Oscillators, commodity channel index. And it is calculated based on the close prices. In the next step we need to sort the price array from the current candle downwards by using the function arraysetasseries for the price we have created here. And now we use copy buffer for the CCI definition from line here. This is for the first line, it is line zero. And for the current candle that’s candle zero, and we need the values for three candles, that’s the three here, and we want to store the results in my price array. We want to get the CCI value of the current candle in our price array. Okay now we need the chart output depending on the value so if our value is above the upper line we want to put the text “Overbought” on our chart by using the comment function. And if it’s below the lower line we want to output the text “Oversold.” And when it’s between the lines we just use an empty output because we have no signal. Okay that’s it. If you’re ready, please click on the little compile button here or hit the F7 key on your keyboard. That should compile your Expert Advisor without any error or warnings. And in that case we want to bring up the MetaTrader by pressing the F4 key or hitting the little button over here. In Metatrader please click on View, Strategy Tester or hit control and R. That should show you the strategy tester panel, and here we want to select the simple commodity channel index.ex5 file. Please make sure to mark the visualisation option here and click on start. And here is our Expert Advisor at work. Now it’s below the lower line and then it says oversold and when it’s above the upper line it says overbought. Okay, now you know how you can use the Commodity Channel Index Indicator to create entry signals in your own Expert Advisor. And you have done it yourself with a few lines of MQL 5 code.
    6 min
  • MQL5 Tutorial – Simple Bulls Power Expert Advisor with MQL5
    With MQL5 for Metatrader5 we can create an Expert Advisor for the Forex Trading Market that is able to determine if the Bulls or the Bears dominate the Forex Market right now. In this video we are going to talk about the Bulls power indicator. It’s an oscillator, and it was created to tell the traders if the Bulls are stronger than the Bears. When you zoom in into this indicator you will see, that the indicator shows you the biggest peak when the price is very high and when the indicator changes from below the line to above the line that could mean that the price will go up for a while. The Bulls Power oscillator was also developed by Alexander Elder; you might know his book- Trading for a living. The Bulls power indicator is also calculated on the basis of the last thirteen candles and it is said the indicator is better to use together with the Trend indicator like a moving average for example. The Bulls and the Bears power indicators are both integrated in Meta Trader 5. If you click on Insert, indicator, oscillators, you will see the Bears power oscillator and we have already created another video for this one, and this time we are going to create an Expert Advisor for the Bulls power. You see these are the thirteen candles for the calculation and this is how it looks like. Now how can we create an Expert Advisor for this indicator? First of all please click on the little button over here or hit the F4 key. That will bring up the Meta Editor and here we want to select File, New Expert Advisor from template, continue. We will call it simple Bulls power, continue, continue, finish. And now we want to delete everything above the Ontick function. Please also delete the two comment lines here. And now we want to create a double array called my price array, and it will be an array for several prices. And in the next step we are going to create a Bull’s power definition by using the iBullsPower function that is built in with MQL 5, and the calculation should be for the current symbol on the chart and the selected period. That might be Euro, US dollar, and the sixty minutes chart for example, and we want to calculate the value for the last thirteen candles. Now that we have done that, we want to sort the price from the current candle downwards by using the arraysetasseries function for the price array we have created here. And after that we use copy buffer according to our definition from here for the first line or value of the indicator . That’s zero here, from the current candle zero for three candles and we want to store the result in the price array here. And now we need to get the value of the current candle, we have created the float variable called Bulls Power value for the current candle in the price array. And the last step in our little Expert Advisor would be to create a chart output that will show the text Bulls power value directly on your chart by using the comment function. Now please click on the compile button here or press the F7 key, okay we have missed a semicolon, let’s repeat that, and now it works. We have zero errors and one warning, that is because I have used a float variable here. A float has a size of four bytes and a double has a size of eight bytes and in our case a float is enough. Okay, now please click on this little button here or press the F4 key to go back to MetaTrader. In Meta Trader you want to click on View, Strategy Tester or press the control and the R key. That will show the little Strategy Tester panel here and there we want to select the simple Bulls power.ex5 file and please don’t forget to mark the visualisation option here, pick any currency pair and start the test. And this is how our simple Bulls power Expert Advisor works, you see we have one digit less behind the dot here, but this is still precise enough to generate an entry signal. Okay, now you know how to create an Expert Advisor by using the simple Bulls power indicator and you have done it with a few lines of MQL 5 code.
    6 min
  • MQL5 Tutorial – Simple Envelopes Expert Advisor with MQL5
    In this video we are going to create an Expert Advisor for the Envelopes Indicator. The Envelopes Indicator simply draws two lines on your chart and when a candle closes above the upper line, this generates an entry signal. We now expect the price to hit this line. It took a while but actually here it happened, the indicator is similar to the Bollenger band indicator. If you place your mouse cursor over the line here you will see a value, in our case it is 117.4636 that matches with the calculated value here. And the value of the lower band as 117.2289, and that also matches. According to Wikipedia some traders think that when the price breaks through the upper line it is a sign for a strong trend and other traders consider this to be a signal for an overbought price, and they expect the price to go down. It’s up to you how you use it but if the price is below the lower band we want to output the text lower signal here. And if the last candle closed above the upper band, we want to output the text upper signal on the chart. Now how can we create an Expert Advisor for the Envelopes Indicator? First of all, please click on this little button here in MetaTrader or press the F4 key, and that will bring up the MetaEditor, here we select File, New, Expert Advisor from template, Continue, and we will give it the name Simple Envelopes. Please click on Continue, Continue, Finish, and now remove everything above the OnTick function. We will also delete the two comment lines here and the first thing we need to create is an array. We use the MQLrates function and we will give it the name price information and now we need to sort the price information from the current candle to the oldest candle. That is done with arraysetasseries, for the array we have created here. And now we want to fill our array with price data. This is done by using copy rates; it will copy the prices for the current symbol on the chart and the selected period on the chart from the current candle for all the available candles on the chart and the prices will be stored in the price information array. And now we need two more arrays. The first one will hold the prices for the upper band and the second one for the lower band. Because we have two lines here, this is the upper and this is the lower envelope band, so the prices need to be sorted for both of our arrays and now we define the envelopes by using the iEnvelopes function that is built-in in MQL 5 for the current symbol and the current period. We will use fourteen candles, no shift to the right or to the left, we will calculate based on a simple moving average for the close price of the candles on the chart and we will use a deviation 0.100, because that is what you will see when you click on the properties, here you will find the same values. After we have defined the envelopes, we will use copybuffer to fill the two arrays according to the envelopes definition we have created here. This zero is for the upper band, this one is for the lower band and for both bands we will copy from the current candle for three candles downwards. And in this step we will calculate the EA value for the upper band value of the current candle and for the lower band value of the current candle. And now we need that chart output so if the last candle of the price information, remember that is our prices array here, so if the last candle closed above the upper band value, we want to output the text upper signal. But if the last candle closed below the lower band, we want the text lower signal on our chart. And if the price is between the two bands, we will output the text for both bands and the calculated values. So let’s compile the Expert Advisor, and you should have no errors and no warnings. So let’s click the little button here to go back to MetaTrader. In Meta Trader we click on View, Strategy Tester, and you should select the Simple Envelope.ex5 file, mark the visualization option here and click on Start.
    7 min
  • MQL5 Tutorial – Simple Average True Range Robot with MQL5
    With MQL5 for Metatrader5 you can calculate the Standard Deviation Indicator to use it in your own Expert Advisor – for example to combine the standard deviation value with an simple moving average to open a trade when the expected volatility changes… In this video we are talking about the Average True Range Indicator and we are going to create a little Expert Advisor to calculate if we have a strong or a weak Average True Range value. This is a sideways market and you see that nothing moves, and when nothing moves, the ATR value you see here down below has a low value. And here we have bigger movements the ATR value rises, and if you are somebody who wants to get in when the Average True Range is very high, you would wait with your trading entry until the value starts to rise because you can expect the markets to make big movements after very slow periods. And now the ATR value rises and if it rises a little bit more, we would see it here on the chart. If it’s low it says weak Average True Range value, and if the value is high it would say strong Average True Range value. The Average True Range indicator is also an indicator created by J. Welles Wilder Jr., who created the RSI and the Parabolic SAR indicator. You will find these indicators in other videos of this channel. The ATR indicator will measure the volatility, in our case for fourteen candles. Now how can we create an Expert Advisor for this little indicator? To do that please hit this little button over here to bring up the Meta Editor. And here you want to click on File, New, Expert Advisor from Template, Continue and we will call it Simple Average True Range. Click on Continue, Continue, Finish, and remove everything above the OnTick function. You can also remove these two comment lines and the first thing we need to create is an array that will hold our price data. And now we want to define our Expert Advisor by using the iATR function that is built in with MQL 5 for the current symbol on the chart and the selected period on the chart, and we will use fourteen candles because this is what Wilder recommended. And now we would use the function arraysetasseries to sort our price array from the current candle downwards and after that we will use the copybuffer function to fill our price array. According to our definition we have created here for the first line of the indicator from the current candle zero for three candels. And now we want to get the value of the current candle, that’s candle zero, from the Average True Range value and we will use normalizedouble to get the five digits we need after the point. And now we need to chart output if the Average True Range value is greater than this, we want to output the text strong average range value and the value. And if it’s below this value we want to output the text weak Average True Range value and the value, and if it’s between this value and this value we don’t have a signal so we don’t want any text on the chart and we will just output the calculated value here. So let’s compile the code and that worked without any errors and any warnings. So let’s click this little button over here to change back to MetaTrader and here we want to select View, Strategy Tester and you can also hit the control and the R key. That will bring up the Strategy Tester Panel here and there you want to select the Simple AverageTrueRange.ex5 file. Please mark the visualization option don’t here and click on start. And now you should see it running. Please compare the value down here with the calculated value here, it should always match. Okay now you know how to calculate the Average True Range indicator to use it in one of your Expert Advisors and you did it with a few lines of MQL 5 code.
    6 min
  • MQL5 Tutorial – Simple SAR Expert Advisor with MQL5
    Today we are going to create a little Expert Advisor for the parabolic SAR. Most traders love this indicator, because it will show you when the direction changes. If the value of the parabolic SAR is above the price, it is going down for a while and if the value of the parabolic SAR is below the price, it is going up. So if you see a change like this one and you go short here, you could have made a profit all the way down until the direction changed here. Now how can we create an Expert Advisor in MQL 5 to calculate the SAR value here? As always please click on this little button here or hit the F4 key to bring up the Meta Editor and now click on File, New, Expert Advisor from template, continue. We will call it simple SAR, continue, continue, finish. Now please delete everything above the OnTick function and the two comment lines here. And the first thing we need is an array. So we will create one and call it my SAR array. In the next step, we want to define the properties of the SAR by the way you can find it on Wikipedia. It’s called parabolic SAR, it was created by a guy named J. Welles Wilder Junior to find potential reversals in the market price and you can insert it on your chart by selecting insert indicators parabolic SAR. And if you do that you will see these green bubbles here, so let’s bring up the properties for the Expert Advisor here and we have a step of 0.02 and the maximum of 0.2. And that’s exactly what we have chosen here. This is the same value as step value here and this is the maximum value and we want to use the iSAR function for the current symbol and the selected period on the chart with a step value of 0.02 and a maximum value of 0.2. And afterwards we are going to sort our price array from the current candle downwards with the function arraysetasseries for mySAR array we have defined here. And after we have done that, we want to fill it with data for the SAR definition we have created here. This zero is used because we only have one line. This is for the current candle and we want three candles and store the result in mySAR array. Now we want to calculate the EA for the current candle. So we use a double variable called SARvalue for the candle zero the current candle of my SAR array. And I would like to use the function normalize double to calculate it with five digits behind the point. And in the last step we want to output the text SAR value and SAR value we have calculated here directly on our chart. Okay that’s it. Let’s compile the Expert Advisor and you should have no errors and no warnings here. So let’s go to the Metatrader by pressing this little button or hitting the F4 key. Now we want to activate the strategy tester by clicking here or pressing control and R and select the simple SAR.ex5 file, select any currency pair here. And you should also activate the visualization option, if you don’t see it you have to drag this a little bit higher. So please enable this check mark here and start your test. And now you should see something like this, let’s speed it up a little bit, pause it and you can see the SAR indicator was below the price and it changed its direction here and afterwards it went down all the way until it changed the direction again at this point. And now it’s going up. If you hover your mouse above the last dot here, you can see a value of 1.50962, and that’s exactly the value you’ll see in the upper left corner here on a chart. Now you could implement that in your system and check if the price here is above or below the last dot and use the parabolic SAR indicator for your own automated MQL 5 trading system as an entry signal. And you did it with a few lines of MQL 5 code for this little Expert Advisor here.
    6 min
  • MQL5 Tutorial – Simple Momentum Expert Advisor with MQL5
    Some people like to use the momentum indicator and they say it should be a way to predict if a trend is going to continue, it should remain to be positive when an uptrend is sustained or negative when a downtrend sustained. You can find the momentum indicator if you click on insert, indicators, oscillators and momentum. It has a value of fourteen for the period, that’s the number of the calculated candles. It is applied to the closed price and it looks like this. Now how can that the calculated with MQL 5? Well, first of all please click on this little button here or press the F4 key that should open your MetaEditor. Now we click on file, new, Expert Advisor from template, continue. We will call that simple momentum continue, continue, finish. Now remove everything above the OnTick function and remove the two comment lines here. And as often, the first thing we need is the price array that can hold several prices of the candles on the chart. And after that we want to define the properties for the momentum here, we will store them in iMomentum definition. We will use the function iMomentum for the current symbol on your chart and the currently selected time period on your chart. We want to calculate fourteen candles for the closed price of these candles. We are going to sort our price array downwards from the current candle by using arraysetasseries and with copybuffer we are going to fill our price array according to our definition we created here. We only have one line to calculate we want to go from the current candle zero and collect the prices of three candles and now we want to get the value of the current candle and store the value in my momentum value. This normalizedouble function will get the current candle zero from our array mypricearray and we only need two digits after the point. Okay that’s it for the calculation. Now we want to have a chart output depending on the value… and if the value is greater than one hundred we want to output “strong momentum” and the current value. And if it’s below 99.9 we want the output “weak momentum” on the chart followed by the calculated momentum value and if it’s between 99.9 and 100, we don’t have any special condition so we don’t need any text here and we’re only going to output the momentum value on the chart. Okay let’s compile that Expert Advisor by clicking on this little button here or pressing the F7 key and you should see zero errors and no warnings. So let’s click this little button here or press F4 to return to the MetaTrader. Okay please click on view strategy tester or hit control and R to bring up the strategy tester, select the simple momentum.ex5 file and mark this option for visualization. Select any currency pair you like and click start, and this is what you should see. Let’s speed that up a little bit, hit the pause button. You might notice that this peak was created a little bit before this peak actually happened and you also could see the momentum indicator going mad here before the price was falling. Most of the time the momentum indicator will be used with another indicator like a moving average. It’s up to you to find out if you want to use it. But now you know how to program it in MQL 5 with only a few lines of MQL 5 source code.
    5 min
  • MQL5 Tutorial – Simple Moving Average Crossover Robot with MQL5
    Okay you have asked for it so here it is. A YouTube subscriber with the name Laurent ZB wanted me to create an Expert Advisor where two exponential moving averages cross. So now I create the video. Okay what you see here is a crossing of two exponential moving averages, this one is calculated for twenty candles and this one is calculated four fifty candidates. And the idea is, that if the twenty candles exponential moving average crosses the fifty candle exponential moving average from below, you should buy. In the other case that the twenty candle moving average crosses the fifty candle moving average here, you should sell. And in this situation it is likely that the two moving averages will cross soon. Now that they have crossed each other you can see, there is a buy signal in the left upper corner of your chart. In the best case this crossing could be the optimal point to go short, and this is where you would close your trade. In the other case you would buy here, go long and you would close your trade here. This is very easy to understand, but if the market is going sideways this indicator might produce several false signals as every other indicator does. But if everything is going well, you would have sold here and it goes all the way down until the next crossing happens here. Now how can we create a crossing moving average Expert Advisor for MQL 5? As always please click on this little button here or press the F4 to bring up the Meta Editor, and we will create a new file from template, it will be an Expert Advisor, continue. We will call it simple EMA crossover. This is exponential moving average, continue, continue, finish and remove everything above the OnTick function. We also delete the comment lines here and the first step is to create two arrays, we will call them my moving average array one and my moving average array two. Now we want to define the properties for the first EMA by using the IMA function for the current currency pair on the chart and the currently selected time period on my chart. And we want to calculate twenty candles, we don’t want any shift to the left or to the right and we want to use the mode EMA, that’s an exponential moving average. For a simple moving average we would use mode SMA and we want to calculate based on the close prices of the last twenty candles. For the second exponential moving average we will do all the same, but we want to calculate fifty candles and now for both of our arrays, we want to use arraysetasseries for my moving average array one and my moving average array two. To sort the price array from the current candle downwards, and with the function copy buffer we will fill my moving average array one and my moving average array two according to our definition that we created here. This zero stands for only one line. This zero is for the current candle and we want to copy the prices of three candles. Okay let’s calculate if we have an entry signal, because if my moving average array one for the current candle is greater than my moving average two for the current candle, and if the last value for the candle before was lower than the last value of my moving average array two then we have a crossover because conditions changed from candle one to candle zero. Candle zero is always the current candle and candle one is the candle before and if this is the case we want to use the comment function and it will output the word “buy” on the left upper corner of the chart screen. And in the other case if my moving average array one for the current candle is now below my moving average array two for the current candle, and if it was above my moving average array two for the last candle, then I want to output the word “sell” on the left upper corner of the chart. Okay so let’s compile that Expert Advisor, click this little button over here or hit the F4 key to bring up the MetA Trader. Click on View strategy tester and select the simple EMA crossover.
    7 min