MQL5 TUTORIAL BASICS – 80 SIMPLE SPREAD CHECKER

video
play-sharp-fill

In this video, we are going to create an expert advisor that can calculate the highest and the lowest spread for a certain number of candles in a currency pair, so let’s find out how to do that with mql5.
To get started please click on the icon 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 spread checker, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
We start with an input value for the candles to count, in my case I want to use five candles and find the highest and the lowest spread for those five candles, this input modifier here will allow us to change the value without recompilation by changing the expert properties.
Inside of the ontick function we start with a double variable called lowest spread value and in this case, I’m going to set the initial value to one hundred thousand, this is definitely not the lowest spread value that you will see but we are going to calculate that later.
Let’s create another variable, this time for the highest spread value, I will set the initial value to zero and this is also not the highest spread value that you will see.
Afterwards, we use mql rates to create an array for price information.
Mql rates stores the information about prices, volumes and spread and spread is what is interesting for us. Afterwards, we use array set as series to sort the array from the current candle downwards and now we fill the price array with data by using copy rates, in my case for the current symbol on the chart and the currently selected period on that chart, I’m going to start with the current candle zero and I will copy the data for two hundred candles and store it in the price information array.
Now we can use a for loop to go through all the candles that are interesting, the current candle number will be the number of candles to count that we have defined here, in our case five and for as long as the current candle number is bigger or equal to zero we will go through the for loop and count down the number of candles until this condition is no longer true, and to get the current candle spread we look inside of the price information array for the current candle number and we want to know the spread, so we use dot spread.
To make it easy to check if the values are correct we want to add a print statement that will output the current value for the candle, the candle number we are processing right now and the current candle spread in our journal and if the current candle spread is lower than the lowest spread value, we want to save it as the lowest spread value.
In the other case if the current candle spread is higher than the highest spread value that’s when we want to save the current candle spread as highest spread value.
Finally, we want to create a chart output by using the comment statement, this will show us the lowest and the highest spread on the chart and finally, we want to end the for loop and close the ontick function. That’s about it.
If this was too fast for you or if you have no idea what all the code here does maybe you want to watch one of the other videos in this basic video series or maybe even the premium course on our website might be interesting for you, this was another question from a premium course member and if you are already a premium course member and have an idea for a video like this one please send me an email.
So far so good! Please click on the compile button or press F7 on your keyboard, I don’t have any errors and if this is the case you can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader we want to click on view, strategy tester or press ctrl and r, please pick the new file, simple spread checker dot ex5, mark the option for the visual mode and start your test.
And here we are! We see that the spreads are changing, I have chosen a currency pair with a very high spread value and you see that it’s changing all the time, let’s actually stop, click on the inputs tab and this time I would like to do it for four candles, now restart the test with the new settings, let’s lower the speed here, it says that the highest value was one thousand three hundred and sixty, this is true for candle two and the lowest spread value was one thousand three hundred and twenty, this is true for candle four, so it looks like our expert advisor is working as expected and in this little video you have learned how to create an expert advisor that is able to calculate the highest and the lowest spread for a certain number of candles and you have coded it yourself with a few lines of mql5 code.