MQL5 TUTORIAL BASICS – 73 SIMPLE SPREAD FILTER

video
play-sharp-fill

In this video, we are going to create an expert advisor that is able to allow or forbid trading based on the current spread, so let’s find out how to do that with mql5.
To get started please click on a little 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 filter, 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 include statement to include the file trade dot mqh. This one comes with mql5 and it provides simplified trading functions. Afterwards, we use the class ctrade to create an instance called trade and we are going to use that one later to open new positions.
And we would like to create a user-defined input variable for the max allowed spread, that value can be changed later on without recompiling the code.
Inside of the on tick function we want 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 either symbol ask or symbol bid based on what we want to calculate and with normalize double and underscore digits we make sure that we calculate the right number of digits behind the dot.
For the trading entry, we want to create a string variable, this one is called signal but we don’t assign a value here because we are going to calculate that later on. We need some price data therefore we use mql rates to create a price array.
With array set as series, we are going to sort our price array from the current candle downwards and afterwards, we use copy rates to fill our price array with data for the current symbol on the chart and the currently selected period, we start with candle zero and we want to copy the values for three candles.
Now let’s calculate the current spread, this is done by using symbol info integer for the current symbol on the chart and we use symbol underscore spread to calculate the current spread.
Let’s create another string variable called spread filter, this is going to be used for our filter and if the current spread is bigger or equal the max spread that we have allowed the spread filter will get the value, trading is not allowed because the spread is too high.
Otherwise, if the current spread is below or equal to the max value that we have allowed that’s when we assign the value, trading is allowed to our spread filter.
Now let’s create some simple entry conditions, in this case, I’ll check if the close price for candle one is bigger than the close price for candle two, in that case, I want to buy so we assign the value buy to our signal. Otherwise, if the close price for candle one is below the close price for candle two that’s when we want to sell so now we assign the word sell to our signal but before we allow any positions we make it a precondition that the spread filter has the value, trading is allowed and only if that is the case we check the entry conditions. If the signal equals sell and positions total returns the value below one that would mean we have no open positions but a sell signal and that’s when we use trade dot sell to sell ten micro lot. In the other case if the signal equals buy and we have no open positions that’s when we use trade dot buy to buy ten micro lot but we only do that if the spread filter equals trading is allowed.
Finally, we want to use the comment statement to create an output for the current spread, the max spread that we have allowed and the spread filter. 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. Actually, this was a suggestion from another 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. For now, please click on the compile button or press F7 on your keyboard, you shouldn’t get 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 control and r, please pick the new file, simple spread filter dot ex5, mark the visual mode option here and state your test.
Here we are! The expert advisor is saying that trading is not allowed so let’s fast-forward that and here is the first trade, the value here changed for a second also, let’s stop the test, click on inputs and now we change the value to ten and restart the test and we shouldn’t see any trades until the current spread is below ten. This is the case so our little expert advisor is working as expected and in this little video you have learned how to create an expert advisor that is able to allow or forbid trading based on the current spread and the max value that you have defined and you have coded it yourself with a few lines of mql5 code.