The Easiest RSI Algorithm for Your MetaTrader Expert Advisor
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.
About This Series
In this series, I cover everything you need to build your own advanced Expert Advisor from scratch. I cover:
- How to get started
- Using built in technical indicators from MetaTrader
- Making your EA autotrade
- Adding some simple-yet-powerful risk management techniques to your EA
- Backtesting and Optimizing your soon-to-be-awesome algorithm
- Adding some advanced features
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.
Working GitHub Code
The complete, working GitHub code for this series is here:
About This Episode
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).
What You’ll Need to Complete This Episode
- The numerical value of the RSI in your EA. Use this article if you’d like to learn how to do this.
- A working MT5 version
Initial RSI Algorithm for MT5 EA
In its simplest form, an RSI algorithm works as follows:
- BUY — if the value of the RSI is below the
rsiLow
then BUY the asset - SELL — if the value of the RSI is above the
rsiHigh
then SELL the asset
In future articles, I’ll show you how to make this algorithm more complex, but for now, we’ll start with this simple version.
The RSIAlgorithm Function
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:
- BUY — for when the algorithm should enter a BUY position
- SELL — for when the algorithm should enter a SELL position
- HOLD — for when the algorithm should do nothing
Further, for our first version of the algorithm, we will be setting our BUY and SELL RSI values as:
- BUY — if the RSI is BELOW 30
- SELL — if the RSI is ABOVE 70
- HOLD — any value between 30 and 70
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");
}
}
Update OnTick()
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 + Run 💻
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:
Make Your RSI Algorithm More Flexible (Useful)
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:
- Able to modify the BUY entry point
- Able to modify the SELL entry point
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");
}
}
Recompile
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.
OnTick() Update
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:
- Removing the line
Print("Close Price: ", close, " RSI: ", rsi);
- Adding a branch to the
signal
for BUY and SELL - Doing nothing for a HOLD signal
- Removing the
iClose()
function from theOnTick()
function
Update 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");
}
}
Recompile Your MT5 Expert Advisor
If you recompile your EA now, then run it, you should see the following changes:
- On every tick, the signal should be printed to your
Experts
tab - Whenever a BUY or SELL is detected, an extra line will be printed say that either a BUY or SELL has been detected.
Note. 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.
Wrapping Up
You’ve already got the beginnings of a very powerful and easy to use RSI Algorithm MT5 Expert Advisor. You can:
- Modify the BUY and SELL values
- Choose what value of the Close price you want to use
- Get notified when a BUY or SELL trade is imminent
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!
List of Articles in this Series
- Build Your Own AutoTrading MetaTrader 5 Expert Advsior
- The Easiest Way to Add the RSI Indicator to Your MetaTrader 5 EA
- The Easiest RSI Algorithm for Your MetaTrader Expert Advisor
- The Easiest Way to Place BUY and Sell Orders with your MetaTrader EA
- The Simplest Way to Manage Your Expert Advisor Trades on MetaTrader
- Four Simple Steps to Backtest and Optimize Your EA on MetaTrader 5
Connect With Me
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:
- On YouTube
- Through TradeOxy
- On my Discord Channel
- On LinkedIn
Full Code for This Article
//+------------------------------------------------------------------+
//| 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");
}
}