In this video we are going to create an expert advisor that uses the bollinger bands to create a signal. And we also want to use the RSI filter to confirm the signal.
So let’s find out how to do that with MQL4.
To get started, please click on a little button here or press F4 on your keyboard.
Now you should see the Metaeditor.
And here we have a precoded version for this expert advisor. We start by using the Include function.
It will include two modules for the bollinger bands and the RSI that are coded here.
Inside of the ontick function we open a function that is called Check Entry Bollinger to create a trading signal.
And for the filter we open a function that is called Check Entry RSI.
This one is coded in the second module and only if the trading signal equals buy and if the filter also equals buy, we want to check if we have no open positions.
That would be the case when orders total delivers a return value of zero. And if all conditions are true, we use order send for the current symbol on the chart to open a buy trade for ten microlot.
In the other case, if the trading signal equals sell and if the filter also equals sell and we have no open positions, that’s when we want to use order send for the current symbol on the chart to create a sell trade for ten microlot.
At the end of the main module we use the comment statement to output the trading signal for the bollinger bands and the filter signal for the RSI filter.
And that’s it for the main module.
But these two functions here need to be created. So let’s continue with the bollinger bands entry. Our function is called Check Entry Bollinger.
And it will return a string value.
Therefore we create a string variable for the signal.
We don’t assign a value here because we are going to calculate that now. And to do that we need to use the ibands function that comes with MQL4 for the current symbol on the chart and the currently selected period on that chart.
We do it for 20 candles.
The standard deviation is two.
We don’t need a shift value here.
We calculate the results based on the close price. We use mode lower for the lower bollinger band or mode upper for the upper bollinger band. And here we have the candle number one. Now let’s repeat that for the last bollinger bands value.
And here we want to do the whole thing again.
This time we do it for candle two. And when the close price for candle two is below the last lower bollinger band and the close price for candle one is now above the lower bollinger band, that would mean we have a re-entry from outside the bollinger bands from below and that is considered to be a buy signal.
So we assign the word Buy to our signal.
In the other case, if the close price for candle two was above the last upper bollinger band, and if the close price for candle one is now below the upper bollinger band, that would be a sell signal.
And that’s when we assign the word sell to our signal.
Finally, we return our signal to the main module by using the return statement here.
That’s it for the signal.
Now let’s continue with the filter. To get the filter value we use the function checkentry RSI. That is coded in this module. It also returns a string value.
Once again we create a signal variable of the type string.
Now we use Irsi for the current symbol on the chart and the currently selected period on that chart. We calculate the value for 14 candles for the close price and we do it for the current candle.
That’s candle zero.
And if the RSI value is below 30, that would mean we have a buy signal.
And that’s when we assign the word buy to our signal.
In the other case, if the RSI value is above 70, that could be a sell signal.
And that’s when we assign the word sell to our signal.
Finally, we return the signal to our main module by using the return statement.
And now you need to save the include modules and compile the main module. That can be done by clicking on the little button here or by pressing F7 on your keyboard. In my case I have no errors.
But if you don’t know what all the code here does, or if this was too fast for you, you may want to check out one of the other basic videos in our video series.
Or maybe you want to buy the premium course.
You can find that one on our webpage where also is now a store available in case you don’t want to type everything, you can find the source codes in the store. If the compilation was successful, you can click on a little button here or press F4 to go back to MetaTrader.
And in MetaTrader you want to click on View, Strategy Tester or press CTRL and R. Please pick the new file standalone bollinger RSI.Ex4. Now click on Start and please don’t forget to enable the visual mode here. Here is our expert advisor.
And if you don’t see the bollinger bands and the RSI you can pick a simple chart. Click on Insert Indicators, trend, bollinger bands, confirm the standard values here. Now you see the bollinger bands. Now let’s repeat that for the RSI. Insert, Indicators, Oscillators. And here we want to pick the relative strength index, also for the standard values. And now you can right click into the chart and select template, save template and we want to save it as default tpl, that’s the template for the standard chart and as tester tpl. That’s the template for the strategy tester. You can override the current one. Now let’s go back to our current strategy test, and you see we have the first trade here. Let’s speed that up a little bit.
And we should see a few positions. Okay!
Here is the current result. We made a few profits, and in this little video, you have learned how to create an expert advisor that is able to trade the bollinger bands. And it also uses the RSI Oscillator to confirm the signal.
And you have coded it yourself with a few lines of MQL4 code.