MQL5 TUTORIAL – SIMPLE VOLUMES FILTER

In this video we are going to create an Expert Advisor that is going to use the Bollinger Bands as an entry signal and it will also use the Volumes Indicator as a filter.
Only if the filter is positive and we have a buy or sell signal we want to open a new position.
This is going to be a standalone version and it contains three separate modules. In this video we will talk about the filter function.
Now how can we create a filter that is using the Volumes Indicator?
To do that please click on the little button here or press F4 in your Metatrader, now you should see the Metaeditor window, this is the main module, it will include the Bollinger Bands Entry (CheckEntry_Bollinger) and the Check Filter module (CheckFilter_Volumes) for the Volumes Indicator.
If you don’t know what the rest of the code does please have a look at the other videos in this channel. The “OnTick” function is called whenever the price changes and here we will use the “CheckEntry” function to get the signal, this is the Check Entry Module (CheckEntry_Bollinger) that will give us the signal for the Bollinger Bands but in this video we are going to talk about how to get the filter value.
To do that we need a new file, this one, it is called: “CheckFilter_Volumes” and we are including it here using the “include” statement.
Inside of the function we first need to create a variable for the filter that will be called: “filter” and we don’t assign a value here.
Afterwards we are going to create an array for the prices that will be called: “myPriceArray” and now we define the filter by using the “iVolumes” function that is included in MQL5, we use “_Symbol” for the first parameter to get the currency pair, that’s for the selected time period on the chart and parameter 3 is “VOLUME_TICK” all in capital letters. We use “ArraySetAsSeries” to sort the prices from the current candle downwards for our price array (myPriceArray) and now we use “CopyBuffer” to fill the array with data.
We are going to use the volumes definition (VolumesDefinition) that we have created here to fill the array (myPriceArray) for buffer 0 (zero) from the current candle 0 (zero) for 3 candles and store the values in our price array (myPriceArray).
Let’s calculate the value for the current candle that is done by looking into candle 0 (zero) of our price array (myPriceArray) and we are going to store the value in the variable: “CurrentVolumesValue“ and now we do the same for the candle before – that is candle 1– and we will store that value in the variable: “LastVolumesValue“ and in the case that the current volumes value (CurrentVolumesValue) is bigger than the last volumes value (LastVolumesValue), with other words; we have a rising volume like here, that’s when we want to assign the word: “positive” to our filter.
In the other case if the current volumes value (CurrentVolumesValue) is below the last volumes value (LastVolumesValue) that would be the case here, the current candle has a volume value of 14 and the volumes value of the candle before was 37 and in that case we want to set the filter value to negative so we assign the word: “negative” to our filter and last but not least we are going to use the return statement to return the filter value to our main module. So that’s done all by calling this function here and when the trading signal equals buy and the filter is positive and “PositionsTotal” equals 0 (zero) – that means we don’t have any open position – that’s when we use “trade.Buy” to buy 10 micro lot.
In the other case if the trading signal equal sell and the filter is positive (remember we only want to use increasing volumes because in theory an increasing volume means that the trend is going to continue) and we only want to open a new position when we don’t already have an open position and in that case we use “trade.Sell” to sell 10 micro lot.
Here is the final chart output that will output the words: “Trading signal:” followed by the calculated value for the entry signal and in a new line the word: “Filter:” followed by the “filter” that we have just calculated in our new module.
So let’s compile the main module here, that worked without any errors and now we can click on a little button here or press F4 to go back to Metatrader and in Metatrader we click on: “View/ Strategy Tester” and here I pick the main module where I import my filter I have called that: “SimpleVolumesFilter” and now I start a test.
…and here is how it looks like, currently we have a negative filter because the value for the last candle is lower, this one is positive because the current value is higher. You need to slow down the Expert Advisor to control if everything is okay because the output might be a little slower than the Indicator is calculated but now you know how to create a filter for an Expert Advisor that is only allowing to trade when the filter is positive and you have coded the Filter Module for the Volumes Indicator with a few lines of MQL5 code.

Download “MQL5 TUTORIAL - SIMPLE VOLUMES FILTER” Simple-VolumesFilter.txt – Downloaded 46 times – 1 KB