The Easiest RSI Algorithm for Your MetaTrader Expert Advisor
Learn how to add the RSI technical indicator to your MetaTrader 5 Expert Advisor using code.
Learn how to add the RSI technical indicator to your MetaTrader 5 Expert Advisor using code.
The Relative Strength Indicator, or RSI, is used by traders all over the world to help identify when an asset is overbought or oversold. It is one of the worlds most popular technical indicators.
Therefore, adding the RSI to your trading bot, or Expert Advisor, can be a great way to use the features of the RSI to help your Expert Advisor make more effective buy and sell trades.
In this article, I’m going to show you how to do exactly that.
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 check out my YouTube channel here.
The complete, working GitHub code for this series is here:
By the end of this article, you’ll be able to identify BUY and SELL trades using the RSI overbought and oversold levels.
Even better, you’ll be able to manually adjust your preferred overbought and oversold levels so you can optimize them for your chosen asset (I cover optimization in a future episode).
In its simplest form, an RSI algorithm works as follows:
rsiLow
then BUY the assetrsiHigh
then SELL the assetIn future articles, I’ll show you how to make this algorithm more complex, but for now, we’ll start with this simple version.
To enable us to easily separate out our algorithm logic from our trading logic, we’re going to create a new function in our Expert Advsior called RSIAlgorithm
. This function will be responsible for determining three states for our algorithm, which will become your RSI Algorithm Signal:
What would it take for Crypto, AI, and Cyber to truly change the world?
No spam. Unsubscribe anytime.
Further, for our first version of the algorithm, we will be setting our BUY and SELL RSI values as:
To do this, add the code below to your Expert Advisor. If you are following the series, it should go below the GetRSI()
function and above the OnTick()
function:
//+------------------------------------------------------------------+
//| RSIAlgorithm Function |
//+------------------------------------------------------------------+
string RSIAlgorithm(double rsiValue){
// See if the value is below the 30 threshold
if(rsiValue < 30){
return("BUY");
// See if the value is above the 70 threshold
}else if (rsiValue > 70){
return("SELL");
// Otherwise return HOLD
}else{
return("HOLD");
}
}
Next, update the OnTick()
function so that the RSIAlgorithm()
function will fire on every tick. Update your OnTick()
function as below:
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double close = iClose(_Symbol, _Period, 0);
double rsi = GetRSI();
Print("Close Price: ", close, " RSI: ", rsi);
string signal = RSIAlgorithm(rsi);
Print("Signal: ", signal);
}
Compile your updated code and then check it running on your MT5 (use this tutorial or video if you haven’t already). You should see something like this:
Now that you have the framework of your RSI algorithm set up, we can start making it more flexible and useful.
The changes we will introduce will make your algorithm:
As a result, you’ll be able to optimize your RSI algorithm values to work on whatever asset you want to trade. This will make your algorithm more broadly useful.
I cover backtesting and optimization in a later article.
First, add two input
values at the top of your Expert Advisor as follows:
input int rsiHigh = 70; // RSI High threshold
input int rsiLow = 30; // RSI Low threshold
Next, update your RSIAlgorithm()
function to use the input
values as the comparison for BUY, SELL, and HOLD decisions, rather than the previous hard coded ones.
Here’s what it should look like:
//+------------------------------------------------------------------+
//| RSIAlgorithm Function |
//+------------------------------------------------------------------+
string RSIAlgorithm(double rsiValue){
// See if the value is below the 30 threshold
if(rsiValue < rsiLow){
return("BUY");
// See if the value is above the 70 threshold
}else if (rsiValue > rsiHigh){
return("SELL");
// Otherwise return HOLD
}else{
return("HOLD");
}
}
The cool thing about how we’ve built the algorithm so far is that you can immediately recompile this code and see the benefits and updates.
For instance, once you’ve recompiled, go to your EA settings and you will see the extra inputs already available:
You can also start playing around with these inputs and see the impact on the algorithm signal.
You can also use the first input, Use live RSI price?
from the previous episode to determine which RSI value you want to use — either the current close or the most recently completed full candles close.
To finish off the article, we can update the OnTick()
function to make it a bit more streamlined. This will also prepare it for the next article, where we will get the algorithm buying and selling assets.
I’ll be making four changes to the function:
Print("Close Price: ", close, " RSI: ", rsi);
signal
for BUY and SELLiClose()
function from the OnTick()
functionUpdate your OnTick()
function so that it looks like this:
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double rsi = GetRSI();
string signal = RSIAlgorithm(rsi);
Print("Signal: ", signal);
if(signal == "BUY"){
Print("BUY Signal Detected");
}else if(signal == "SELL"){
Print("SELL Signal Detected");
}
}
If you recompile your EA now, then run it, you should see the following changes:
Experts
tabNote. If you wanted to reduce the number of things being printed to your screen further, you could remove or comment out the line Print("Signal: ", signal)
. This would mean that a printout only occurs when a BUY or SELL is detected.
Up to you.
You’ve already got the beginnings of a very powerful and easy to use RSI Algorithm MT5 Expert Advisor. You can:
In the next episode, I’ll show you a really easy way to convert these signals into actual BUY and SELL orders.
If you just want the RSI signal in your EA, then you’ve already got it!
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?
input int rsiHigh = 70; // RSI High threshold
input int rsiLow = 30; // RSI Low threshold
//+------------------------------------------------------------------+
//| 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);
}
}
//+------------------------------------------------------------------+
//| RSIAlgorithm Function |
//+------------------------------------------------------------------+
string RSIAlgorithm(double rsiValue){
// See if the value is below the 30 threshold
if(rsiValue < rsiLow){
return("BUY");
// See if the value is above the 70 threshold
}else if (rsiValue > rsiHigh){
return("SELL");
// Otherwise return HOLD
}else{
return("HOLD");
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double rsi = GetRSI();
string signal = RSIAlgorithm(rsi);
Print("Signal: ", signal);
if(signal == "BUY"){
Print("BUY Signal Detected");
}else if(signal == "SELL"){
Print("SELL Signal Detected");
}
}