The Easiest Way to Add the RSI Indicator to Your MetaTrader 5 EA
Learn how to add the RSI technical indicator straight into your Expert Advisor. It's pretty straightforward with this tutorial!
Learn how to add the RSI technical indicator straight into your Expert Advisor. It's pretty straightforward with this tutorial!
Adding the RSI (or Relative Strength Indicator) to your MT5 Expert Advisor can be a great way to give your EA insight into market conditions. The RSI is one of the world's most popular technical indicators, and it is used by many traders to identify overbought and oversold conditions.
However, actually GETTING the value of the RSI from MetaTrader 5 in a format your EA can use is…non-intuitive.
In this article, I’ll show you the easiest way to get it and add it to the rest of your trading bot.
In this series, I cover everything you need to build your own advanced Expert Advisor from scratch. I cover:
All of the code that I use is available on my public GitHub repo, and each episode contains working code snippets you can immediately use in your own trading bot.
If you’d like some more general help, join our Discord server here, and if you prefer content in a video format, you can check out my YouTube channel here.
Working Code for the Series on GitHub
Get the complete, working code for the series on my GitHub here.
By the end of this episode, you’ll have added the RSI technical indicator to your trading bot! Not only that, but I’ll show you the easiest way to modify whether you want your RSI to be the latest value or a previous one.
OnInit()
, OnDeinit()
, and OnTick()
. I recommend you check out my previous episode to see this.If you want to learn more about the RSI, check out this article or this video for more.
MT5 comes with quite a number of built in indicators. You can find them in the Insert -> Indicators menu.
The problem if you’re building an EA (or Expert Advisor) is that the MQL language doesn’t make them super easy to access.
Instead, you have to follow a multi-step process that looks a bit like this:
If you’d like that list in more technically correct language, you need to:
It can honestly be a bit overwhelming at first. Especially if you’ve come from higher level languages / platforms that make this process substantially easier.
However, here we are.
What would it take for Crypto, AI, and Cyber to truly change the world?
No spam. Unsubscribe anytime.
Here’s how you follow this process to get the RSI. If you’ve been following this series, you’ll be using the rsi_trading_bot_tutorial.mq5
Above the OnTick()
function and below the OnDeinit()
function, create a new function as follows:
double GetRSI(){
}
This function will handle all logic related to getting the RSI value you want to use in your trading bot.
Inside this function, update it with the code below so that your GetRSI()
function looks like this:
//+------------------------------------------------------------------+
//| GetRSI |
//+------------------------------------------------------------------+
double GetRSI()
{
// Initialize a buffer
double rsiBuffer[];
// Make it into an array
ArraySetAsSeries(rsiBuffer, true);
// Get the RSI Handle
int rsiHandle = iRSI(_Symbol, _Period, 14, PRICE_CLOSE);
// If the handle is invalid, return 0
if (rsiHandle == INVALID_HANDLE)
{
return(0);
}
// Copy the RSI values to the buffer
int rsiCount = CopyBuffer(rsiHandle, 0, 0, 1, rsiBuffer);
// If the copy failed, return 0
if (rsiCount == 0)
{
return(0);
}
// Get the actual RSI value
double rsi = rsiBuffer[0];
// Return the RSI value
return(rsi);
}
As you can see, this function:
rsiBuffer
iRSI()
function from MQL5Let’s add the output from this function to your EA. We will do this by inserting it into the OnTick()
function we built in the previous episode.
Currently, this function is simply printing out the most recently close price for whatever asset you are tracking. Now we will add in another value, which will be the current RSI.
Update your OnTick()
function so that it looks like this:
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double close = iClose(_Symbol, _Period, 0);
double rsi = GetRSI();
Print("Close Price: ", close, " RSI: ", rsi);
}
Compile your code update and run it in your MT5.
Although we’ve technically got an RSI up and running on your Expert Advisor, if you’re into trading, you might find that the current implementation isn’t that useful.
To see what I mean, have a look at this GIF.
You can probably see that not only is the current close jumping around, but so is the RSI.
The reason for this is that we’ve applied the RSI to the current close, which will change with every price.
The problem is that
Many algorithms actually use the PREVIOUS candles close to calculate their entry and exit points.
Let’s update your RSI function to make this work.
We will be adding two parts — an input to choose whether we want the current RSI or the previous value, and then updating the actual RSI function.
First, near the top of your EA, under the intro section, put the following line:
input bool liveRSIPrice = false; //Use the live RSI price?
This specifies an input that you can choose when you run your bot. The input allows you to use the live RSI price (select true
) or the previous candle (select false
)
Now we’re going to return to the GetRSI()
function and update it. There’s two parts to this update.
Update your RSI Function so that it looks like this:
//+------------------------------------------------------------------+
//| GetRSI |
//+------------------------------------------------------------------+
double GetRSI()
{
// Initialize a buffer
double rsiBuffer[];
// Make it into an array
ArraySetAsSeries(rsiBuffer, true);
// Get the RSI Handle
int rsiHandle = iRSI(_Symbol, _Period, 14, PRICE_CLOSE);
// If the handle is invalid, return 0
if (rsiHandle == INVALID_HANDLE)
{
return(0);
}
// Copy the RSI values to the buffer
int rsiCount = CopyBuffer(rsiHandle, 0, 0, 2, rsiBuffer);
// If the copy failed, return 0
if (rsiCount == 0)
{
return(0);
}
// Get the actual RSI value
if(liveRSIPrice == true)
{
double rsi = rsiBuffer[0];
return(rsi);
}else{
double rsi = rsiBuffer[1];
return(rsi);
}
}
If you recompile your RSI Trading EA now, you’ll be able to watch the results.
When you set your live price to true
the RSI will change every time the price moves.
When you set your live price to false
the RSI will only change when a new candle appears.
Pretty cool!
Nice work on getting to the end of the episode! You’ve now got a working RSI indicator you pull directly into your trading bot to make trading decisions with your EA.
In the next episode, I’ll show you how to start identifying entry and exit points for trades using the RSI.
My name is James, and you’ll often see me online using the AppnologyJames handle. Although my background and training is in Cyber Security, I’ve spend the last few years building trading algorithms for clients, along with a powerful market analysis platform called TradeOxy.
I love hearing from my readers and viewers, so feel free to reach out and connect with me:
//+------------------------------------------------------------------+
//| RSI_EA_from_TradeOxy.mq5 |
//| AppnologyJames, TradeOxy.com |
//| https://www.tradeoxy.com |
//+------------------------------------------------------------------+
input bool liveRSIPrice = false; // Use live RSI price?
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Print the name of the Expert Advisor and a Welcome message
Print("RSI Trading Bot Expert Advisor from TradeOxy.com");
// Return value of initialization
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Deinitialization code
Print("Expert advisor deinitialized. Reason: ", reason);
}
//+------------------------------------------------------------------+
//| GetRSI |
//+------------------------------------------------------------------+
double GetRSI()
{
// Initialize a buffer
double rsiBuffer[];
// Make it into an array
ArraySetAsSeries(rsiBuffer, true);
// Get the RSI Handle
int rsiHandle = iRSI(_Symbol, _Period, 14, PRICE_CLOSE);
// If the handle is invalid, return 0
if (rsiHandle == INVALID_HANDLE)
{
return(0);
}
// Copy the RSI values to the buffer
int rsiCount = CopyBuffer(rsiHandle, 0, 0, 2, rsiBuffer);
// If the copy failed, return 0
if (rsiCount == 0)
{
return(0);
}
// Get the actual RSI value
if(liveRSIPrice == true)
{
double rsi = rsiBuffer[0];
return(rsi);
}else{
double rsi = rsiBuffer[1];
return(rsi);
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double close = iClose(_Symbol, _Period, 0);
double rsi = GetRSI();
Print("Close Price: ", close, " RSI: ", rsi);
}