MQL5 TUTORIAL BASICS – 52 SIMPLE EXTERNAL CANDLE NUMBER EA

video

In this video we are going to create an expert advisor that is able to calculate the number of candles that are used for a simple moving average, in this case, we are using twenty candles, we can change the number of candles that are used so let’s find out how to do that with mql5.
To get started 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 file, expert advisor from template, continue, I will call this file simple external candle number ea, click on continue, continue and finish.
Now you can delete everything above the on tick function and the two comment lines here.
We start with an include statement to include the file trade dot mqh, this is part of mql5 and the file provides a few simplified trading functions that we are going to use to open positions.
Afterwards, we want to create an external input, this is the input modifier and it will create an external variable for the number of candles that can be changed without changing the source code.
Inside of the on tick function we first need to calculate the ask price and the bid price that is done by using symbol info double for the current symbol on the chart, we use symbol underscore ask or symbol underscore bid and with normalize double and underscore digits we automatically calculate the right number of digits behind the dot that depends on each currency pair.
Now let’s use mql rates to create a price array that will be called price info, we use array set as series to sort the price info array from the current candle downwards and we use copy rates to fill the price array for the current symbol and the currently selected period starting from candle zero for three candles and store the values in the price info array.
Actually, we could use underscore symbol and underscore period because when you mark underscore symbol and press F1 you will see that both functions do the same and that’s also true for underscore period.
Now we need to create a string variable called signal, this one has no value so far because we are going to calculate the value later and to do that we need a second array, this one is called my moving average array, we will use it for the simple moving average values and to calculate the moving average we use the included function ima for the current symbol on the chart and the currently selected period on that chart. We use the number of candles that we have defined here, the default value is one, this one here stands for a shift value but we don’t need the shift value, you could use it to set a value for a horizontal shift to move the moving average to the right or to the left but we don’t want to do that.
We want to calculate a simple moving average so we use mode underscore sma here, ema would be an exponential moving average for example and the values should be calculated based on the close price.
Now let’s also sort this array by using array set as series and afterwards we can fill it with price data that is done by using copy buffer according to the moving average definition that we have created here.
We do that for buffer zero – that is the red line here – we want to start with the current candle zero and we want to fill it with data for three candles.
Now it’s time to check for the entry conditions so if the close price for candle one in the price info array is bigger than the value for the moving average array for candle one and if it was below the simple moving average before we consider that to be a buy signal so we assign the value buy to our signal.
In the other case if the close price for candle one is now below the moving average value and if it was above the moving average before we consider that to be a sell signal so now we assign the word sell to our signal and if the signal equals sell and the return value for positions total is below one – that would mean that we don’t have any open positions – and now we use trade dot sell to sell ten micro lot.
In the other case if the signal equals buy and positions total is below one we would use trade dot buy and buy ten micro lot.
Finally, we use the comment statement to create an output, we want to see the text the signal is now followed by the calculated signal and in a new line I would like to see the number of candles followed by the value that we have defined and that’s about it.
Well if you don’t know what all the code here does or if this was too fast for you maybe you want to watch one of the other videos in this basic video series first or maybe the premium course on our website might be interesting for you, for now, please click on the compile button or press F7 on your keyboard…
I have messed something up here, it looks like I have forgotten to create an instance of the class ctrade, so let’s go to the part of the source code where that needs to be done, recompile the code and this time it looks better, we don’t have any errors here and in that case, you can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader, we click on view, strategy tester or press control and r, let’s pick the new file simple external candle number ea dot ex5, please enable the visible mode here and start a test.
Here we are, currently, the moving average has a value of one, you can see that in the settings of the moving average so let’s stop the test, click on inputs and change the value to thirty, now start a new test and this time the moving average is calculated based on thirty candles.
Let’s do a final test for eight candles, click on start and this time the moving average is much closer so we see more signals and in this little video you have learned how to create an expert advisor that is able to automatically calculate a simple moving average based on an external variable and you have coded it yourself with a few lines of mql5 code.