MQL5 TUTORIAL BASICS – 132 STANDALONE BOLLINGER BANDS RSI EXPERT ADVISOR

video
play-rounded-fill

 

In this video we are going to create an Expert Advisor like this one that is able to trade the Bollinger Band signal and it also uses the RSI Oscillator as a filter.

 

So let’s find out how to do that with MQL5.

 

To get started, please click on the little button here or press F4 on your keyboard.

 

Now you should see the meta editor window and here we have a pre coded version for the Bollinger Bands RSI Expert Advisor the first thing we do is to use the include function to include the file trade Mqh.

 

This one comes with MQL5 and it provides some simplified trading functions and we also use to include two more modules that we are going to create later on.

 

The first one is to check the entry for the bollinger bands and the second one is for the RSI Oscillator. We start by calculating the ask price and the bid price.

 

That is done by using symbol info double for the current symbol on the chart.

 

We either use symbol underscore ask or symbol underscore bit and with normalize double and underscore digits we make sure to calculate the right number of digits behind the dot, that is depending on the currency pair we use.

 

Now we use Ctrade to create an instance called trade. That will be used to open the trades later on. And we create an array called price info by using MQL rates.

 

We do that outside of the ontick function because we need the same thing here in the bollinger bands module and in the on tick function we start with array set as series.

 

To sort our price info array from the current candle downwards. Then we can use copy rates to fill our price info array for the current symbol on the chart and the currently selected period on that chart starting from candle zero for three candles.

 

And to calculate the current price we look into the price info array, especially at candles zero and at close price. Now let’s find out how to get the trading signal.

 

That can be done by using a function called check entry bollinger and this function exists in this module here.

 

It is called Check entry bollinger and it will give back a string value to our main module.

 

We start by creating a string variable for the signal.

 

Now we create three arrays for the middle, the upper and the lower band.

 

Array and we sort all three arrays by using array set as series.

 

Afterwards we use the built in iBands function of MQL5 for the current symbol on the chart and the currently selected period on the chart.

 

We want to calculate the values for 20 candles, no shift value and the deviation value is two.

 

The results will be calculated based on the close price.

 

Let’s continue to fill our arrays with copy buffer according to the bollinger bands definition that we have created above. We are going to do that for buffer zero.

 

That’s the middle band array, buffer one for the upper band array and buffer two for the lower band array.

 

All three values are starting from candle zero.

 

That’s the current candle and we want to copy the values for three candles and store them in the according arrays.

 

That makes it possible to get the current values for the middle band, the upper band and the lower band by looking at candle zero in the according arrays.

 

That will give us the price for the current candle and we do that again for candle one. That’s the candle before the current one.

 

And if the close price for candle one of our price info array was below the lower bollinger band, and if it is now above the lower bollinger band, that would mean we have a buy signal.

 

In the other case, if the close price for candle one was above the upper band, and if it is now below the upper band for kennel zero that would mean we have a sell signal and that’s when we assign the word sell to our signal.

 

And finally we return our signal to the main module. And that’s where we check the filter. By using a function check entry RSI, we can find that one in the second module.

 

It is called Checkentry RSI and it returns a string value.

 

Once again we create a signal and we need to create an array for the RSI and then we can use the built in Irsi function for the current symbol on the chart and the currently selected period on the chart to calculate the value for 14 candles based on the close price.

 

Let’s also sort this array by using array set as series with copy buffer we fill our RSI array according to the definition that we have created here.

 

For buffer zero, starting from the current candle zero for three candles. And that makes it possible to get the current RSI value by looking at candle zero in our array.

 

And with normalize double and the last parameter here, we make the output having two digits behind the dots because that is how the RSI value looks like on the chart.

 

Now, if the RSI value is above 70, that would mean we have a sell signal and if the RSI value is below 30, we consider that to be a buy signal.

 

So finally we return the signal to our main module and here we can check if the trading signal equals buy and if the filter also equals buy.

 

And when this condition – and when these conditions are true and positions total returns a value of zero, that means we have a buy signal for the filter and for the signal and we have now open trades.

 

And that’s when we use trade buy 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 we use Trade Sell to sell ten Microlot

 

Finally, we use the comment statement to output the trading signal and to output the filter directly on our chart.

 

So far so good.

 

If this was too fast for you or if you have no idea what all the code here does, you maybe want to watch one of the other basic videos in this video series.

 

And on our website you can find the premium course. That might be interesting for you.

 

Or maybe you want to download some of the source codes here from our store so you don’t have to type everything.

 

But for now we click on the compile button or press F7 on our keyboard.

 

That worked without 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 Meter Trader you want to click on View, Strategy Tester or press Control and R. This brings up the Strategy Tester panel here. Please mark the visible mode option, select the file Standalone bollinger RSI ex five and start your test.

 

Here is our little expert advisor at work.

 

Now we should see a signal in a few seconds, and as soon as the signal and the filter point into the right direction, we have the first trade.

 

So our little expert advisor works as designed.

 

And in this little video you have learned how to create an expert advisor for the bollinger bands and the RSI.

 

And you have coded it yourself with a few lines of MQL5 code.