In this video we are going to automate the Relative Strength Index also known as RSI and we are going to create an Expert Advisor for Metatrader 5 that is actually going to trade.
To do that please click on the little button here or press F4 on your keyboard and now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this version: “SimpleRSIStandaloneEA”, click on “Continue”, “Continue” and “Finish”.
Now you can delete everything above the “OnTick” function and the two comment lines here.
First we are going to include the file: ”Trade.mqh”, this file comes with MQL5 and it offers a few useful trade functions, so let’s create an instance of the “CTrade” class, that will be called: “trade” and we are going to use it to open positions later on. Before we do that we need to calculate the Ask price and we need to calculate the Bid price; that is done by using “SymbolInfoDouble” for the current symbol on the chart and we use “SYMBOL_ASK” to get the Ask price and “SYMBOL_BID” to get the Bid price and with “NormalizeDouble” and “_Digits” we calculate it for the current symbol – you see that we have two different prices here, some currency pairs only use 3 digits behind the dot and others use 5 digits – and if you mark “_Digits” and press F1 you will see that the “_Digits” variable stores number of digits after the decimal point.
Let’s create an array for the price data, in this case it will be called: “myRSIArray” it’s a double array so it can hold floating type values.
Now we define the RSI (myRSIDefinition) by using the “iRSI” function for the current symbol and the currently selected period for 14 candles, you see the 14 here in the round brackets and when you click on: “Insert/ Indicators/ Oscillators/ Relative Strength Index” you will also find the 14 candles here, that’s the standard value and the result will be calculated based on the close price.
We use “ArraySetAsSeries” to sort the price data (myRSIArray) from the current candle downwards for our RSI array (myRSIArray) and with “CopyBuffer” we fill our RSI array (myRSIArray) according to the “RSIDefinition” that we have created here, we do it for buffer 0 (zero) from the current candle – that’s candle 0 (zero) – for 3 candles – so that’s the 3 here – and after we have done that we are actually able to calculate the current RSI value (myRSIValue), we just look into the value for candle 0 (zero) in our RSI array (myRSIArray) and with “NormalizeDouble” and the 2 here we get a value that has 2 digits behind the dot.
If the RSI value (myRSIValue) is above 70 – that’s the upper line here – so if the blue line is above the upper line we consider it to be overbought so we assign the word: “sell” to our signal, otherwise if the RSI value (myRSIValue) is below 30 – so now the blue line would be below the lower dotted line here – we expect it to rise and now we would assign the word: “buy” to our signal.
And if the signal equals “buy” and “PositionsTotal” is less than 1 – that would be true if we have no open positions – we use “trade.Buy” to buy 10 micro lot.
In the other case if the signal equals “sell” and we have no open positions we would use “trade.Sell” to sell 10 micro lot.
And finally we are going to create a chart output that is done by using the “Comment” function, it will output the words: “The current signal is:” followed by our calculated “signal” directly on the chart.
Okay. That’s it.
Please click on the compile button here or press F7 on your keyboard…
…oh I made a mistake! Let’s go back to the beginning of the file!
Of course we need to create a string for the signal before we use it, that’s also named: “signal” and we don’t assign an initial value here.
Let’s click on the “Compile” button again and this time it worked and in that case you can click here or press F4 to go back to Metatrader and in Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, here you want to pick the file: “SimpleRSIStandaloneEA.ex5”, enable the visualization here and start a test.
…and here it is, and this is our first trade, our Expert Advisor is working and trading, we already made a small profit here, let’s look into the history and you will see a few trades and now you know how to create an automated Expert Advisor that is actually going to trade the RSI Indicator and you have coded it yourself with a few lines of MQL5 code.


SimpleRSIStandaloneEA.txt – Downloaded 493 times – 1.35 KB