In this video, we will create an expert advisor that is able to calculate the current spread and find out what the lowest and the highest spread was for a certain currency pair, so let’s find out how to do that with mql4.
To get started please click on a little icon here or press the 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 by creating a double variable called lowest spread value and we give it an initial value of one hundred thousand, this is definitely not the lowest spread value that you will see.
Now let’s create another variable called highest spread value and this one will have a value of zero and that’s definitely also not the highest value that you will see.
Inside of the ontick function, we want to calculate the current candle spread that can be done by using symbol info integer for the current symbol on the chart and we use symbol underscore spread – all in capital letters – and if the current candle spread is below the value that we have for lowest spread value, we want to save it as the lowest spread value that we have seen, so we assign the value for current candle spread to our variable called lowest spread value.
In the other case if the current candle spread is bigger than the highest spread value we have seen, so far we want to store the value for the current candle spread as the highest spread value.
And finally, we want to use the comment statement to create a few outputs for the current spread, the lowest spread and the highest spread for our currency pair.
That’s about it.
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. Actually, this was an idea of a premium course member and if you are already a premium course member and have an idea for a video like this one please let me know but for now please click on the compile button or press F7, you shouldn’t get any errors here and if that is the case you can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader, we need to open an empty chart, please navigate to the new file called, simple spread checker and drag it on the chart, click okay and when the next tick comes in you should see that we have some values here, I will make it a line chart, so it’s better to read, so the lowest spread value so far was sixteen, the current spread value is sixteen and the highest spread value is seventeen.
Now we see a few price changes and now the current spread is changing and we see that the highest spread value now is seventeen, the lowest one is fourteen, so our expert advisor actually works as we expected but there is one more thing to mention, please click on view, strategy tester or press control and r, please pick the new file simple spread checker dot ex4, mark the visual mode and start a test.
And this time you will see no changes at all and that’s because mql4 does not store the values for the spread for history data so if you press F2 you will see that we have a history center and everything inside is open, high, low, close and volume, there are no values for the spread and that’s why this expert advisor will not be useful inside of the strategy tester but it works on real-time charts and in this little video you have learned how to find out the current, the lowest and the highest spread for a certain currency pair and you have coded it yourself with a few lines of mql4 code.