MQL4 TUTORIAL BASICS – 73 SPREAD FILTER

video
play-sharp-fill

In this video we are going to create an expert advisor that is able to allow or disallow trading based on the current spread, so let’s find out how to do that with mql4.
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 by creating an input variable, this one actually is adapted code from mql5, this modifier will allow the user to change the value without any recompilation, in our case we use an integer value for the max spread that is allowed and the first value that we are going to assign here is fifteen but we can change that later on without recompilation.
Inside of the on tick function, we create a string variable for the signal but we don’t assign any values here. Let’s use mql rates to create a price array as I said this is adapted from mql5 to get the price data because mql rates stores the information about price, volume and spread.
We use array set as series to sort the price array from the current candle downwards and then we use copy rates 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 and store them in our price array.
And now we can use symbol info integer for the current symbol on the chart to calculate the spread for the symbol, we use symbol underscore spread – all in capital letters – and that will be our current spread, let’s add another string variable called spread filter and now we want to find out if the current spread is bigger or equal to the max spread that we have allowed if that is the case we assign the words, trading is not allowed to our spread filter.
Otherwise, if the current spread is below or equals the max spread that we have allowed we assign the words, trading is allowed to our spread filter.
Now let’s add a simple entry condition here if the close price for candle one in our array is bigger than the close price for candle two we would consider that to be a buy signal so we assign the word buy to our signal otherwise if the close price for candle one is below the close price for candle two in our price array we would assign the word sell to our signal and now we make it a precondition that our spread filter should equal, trading is allowed before we check for any entry signals and only if this is true we are going to check if the signal equals buy and if orders total equals zero if this is true we have no open orders and a buy signal so we use order send to buy ten micro lot.
In the other case if the signal equals sell and we have no open orders we use order send to sell ten micro lot.
Finally, we use the comment statement to output the current spread, the max allowed spread and the value for the spread filter.
Well if this was too fast for you or if you don’t understand what all the code here does maybe you want to watch one of the other videos in our basic video series or maybe even the premium course on our website might be interesting for you. Actually, this video was another suggestion from a premium course member and if you are 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 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 pick an empty chart, in this case on my demo account, now we can drag and drop the new simple spread filter onto the chart, here is the max value that we are going to allow, so let’s say the value should be four.
Here is our little expert advisor at work, we can see that trading is allowed or not allowed based on the spread, in this case, I have disabled auto trading, it also does work in the strategy tester but you see that the current spread isn’t changing that’s why I used a live chart in this case and in this little video you have learned how to create an expert advisor that is able to allow or disallow automated trading based on the spread value and you have coded it yourself with a few lines of mql4 code.