In this video we are going to create an expert advisor for the RSI indicator, in this case for multiple values in different time frames, 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 multi RSI, click on continue, continue and finish.
Now you can delete everything above the ontick function and let’s also remove the two comment lines here.
We start with an include statement to include the file trade dot mqh. Afterwards, we are going to create an instance of the class ctrade, this is called trade and we are going to use it to open positions later on.
Inside of the ontick function we calculate the ask price and the bid price that is done by using symbol info double, for the current symbol we use either symbol ask or a symbol bid – all in capital letters – and with normalize double and underscore digits we automatically calculate the right number of digits behind the dot.
Afterwards, we create an empty string called signal, we don’t assign a value here and now we need three arrays, one for the current time frame on the chart, one for the thirty-minute time frame and one for the sixty-minute time frame.
Let’s define the current one, this is done by using the iRSI function for the current symbol on the chart and the currently selected period on that chart. We want to calculate the value for fourteen candles and the result should be calculated based on the close price.
Let’s use array set as series to sort the RSI array for the current value, afterwards we use copy buffer for the current value that we have defined here and we store the values in the RSI current array and afterwards, we are able to calculate the current value. All we need to do is to get the value for the current candle zero out of our RSI array current and with normalize double we cut the result to show two digits because that is what the original RSI indicator also does and that’s it for the first value.
Now we repeat the whole process to get values for thirty minutes, now we use the iRSI function for the current symbol on the chart but this time we are going to use period underscore m thirty and if you mark that and press F1 you will see that we have values for all the possible timeframes here, this one is for thirty minutes and this one is for one hour, so, for now, we use period underscore m thirty. Again we use array set as series to sort the RSI array for thirty candles, afterwards we use copy buffer according to the definition that we have created here and store the values inside of our RSI array thirty and to calculate the RSI value for thirty minutes we take a look at candle zero inside of our RSI array thirty and again we use normalize double and cut the result to have two digits behind the dot.
To get the values for one hour we do the same again, this should be no surprise anymore we just use different variable names and this time we use period underscore h one, everything else is basically the same.
And if all three values are above seventy that would mean that the blue indicator line here is above the upper dotted line for the seventy percent level, that would be a sell signal and now we would assign the word sell to our signal.
If all three values are below thirty we would consider that to be a buy signal because that would mean that the signal line is below the lower dotted line here and that’s when we want to buy.
So if the current signal equals sell and we get a return value that is below one for positions total that would mean that we have a sell signal and no open positions and now we use trade dot sell to sell ten micro lot.
Otherwise, if the signal equals buy and we have no open positions we would use trade dot buy and buy ten micro lot.
Finally, we use the comment statement to create a chart output for the current value, the thirty-minute value and the sixty-minute value and below that, I would like to see the current signal and that’s about it!
Well, if you don’t understand what all the code here does or if this was too fast for you 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, for now, we click on the compile button, you shouldn’t get any errors and if that is the case you can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader I would like to show you how the expert advisor works, so I have opened a clean chart here, this is for one minute and now I insert an indicator, let’s click on insert, indicators, oscillators, relative strength index, I pick a period for fourteen candles, click on okay, here is the RSI oscillator, now I drag our new file simple multi RSI on the chart, click on okay and we can see that values are calculated, the current value here is equal to the one we see below, now let’s click on the thirty minutes chart and this time the second one should be equal to the one that is calculated below, let’s repeat that for one hour and now the RSI value for sixty minutes should match the value that you see in the oscillator window.
Of course, we can also click on view, strategy tester and start a new test here, this also works, we already have the first buy trade but inside of the strategy tester it’s hard to check if the calculated values are correct that’s why I would prefer to compare it on a live chart of your demo account but in this little video you have learned how to create an expert advisor that is able to calculate RSI values for different time frames and you have coded it yourself with a few lines of mql5 code.