Home / Articles / #4



/imgs/articles/RSI-TurnOvers-Cover-Image.jpg



The RSI Strategy: Your New Best Friend... Well, Sort Of


Hadi

2024-09-08 08:42:11


So, what exactly is the RSI strategy? For those not in the know, RSI stands for Relative Strength Index. It tells us when things are looking a little too good to be true, or perhaps a little too grim. In trading terms, this means we’re looking for overbought and oversold levels, which give us perfect opportunities to make our move.



OverBought Point

The Relative Strength Index (RSI) is a popular momentum oscillator that measures the speed and change of price movements, typically over a 14-day period. It ranges from 0 to 100 and is primarily used to identify overbought and oversold conditions in a market. An RSI value above 70 generally indicates that an asset may be overbought, suggesting that the price has risen too quickly and may be due for a pullback or correction.


MetaBacktest RSI OverBought point

OverSold point

Conversely, an RSI value below 30 indicates that an asset may be oversold, implying that the price has fallen too sharply and could be due for a rebound. An oversold RSI can signal a buying opportunity, as the asset may be undervalued and positioned for a future rally.

MetaBacktest RSI OverSold Point



What’s Cooking at MetaBacktest?

Our ambitious plan is to develop an EA that goes long when the RSI signals an oversold condition and shorts when it’s overbought.
But here’s where it gets spicy: once we've opened our enticing long or short positions, we’ll close and reverse them with every overbought and oversold signal. We’re here for the rhythm!


Let us begin by initiating the creation of a new Expert Advisor file

  • Open the MetaEditor window by clicking the icon or by pressing F4 on the terminal.
  • Select “File” -> “New” -> “Expert Advisor (template)”.
  • Name the file “RSI-TurnOvers” and click “Next” -> “Next” -> “Finish”.


//+------------------------------------------------------------------+
//|                                                RSI-TurnOvers.mq4 |
//|                                    Copyright 2024, MetaBacktest. |
//|                                     https://www.MetaBacktest.com |
//+------------------------------------------------------------------+

#property copyright   "Copyright 2024, MetaBacktest."
#property link      "https://www.MetaBacktest.com"
#property version   "1.00"
#property strict


//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+


         

isNewCandle()   Function

Lets now write out a function we shall call isNewCandle() that will run its code whenever a new candle opens. The eventual consequence of this method will be better efficiency of algorithms and decrease in false RSI value impulses caused by price modifications during the opening times of candlesticks.




//+------------------------------------------------------------------+ 
bool    IsNewCanlde()
{
    //--- memorize the time of opening of the last bar in the static variable
    static datetime last_time=0;
    //--- Get current time
    datetime lastbar_time=SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);
    //--- See if it is the first call of the function
    if(last_time==0)
    {
    //--- set the time then exit
    last_time=lastbar_time;
    return(false);
    }
    //--- if the time differs
    if(last_time!=lastbar_time)
    {
    //--- memorize the time then  return true
    last_time=lastbar_time;
    return(true);
    }
    //--- Then the bar is not new; return false
    return(false);
}

//+------------------------------------------------------------------+



         

Extern Variables

Next, we shall define some external setting variables. These variable are designed to govern the settings and inputs of the Indicator/EA, thereby facilitating the ability to run and test each aspect. This enhancement will ultimately provide greater flexibility in our operations.
We will implement two key features:

  • ReverseOrders:When enabled, this feature will execute a short position on oversold conditions and a long position on overbought conditions.
  • StaticSlTp:This feature will allow users to set fixed levels for stop loss and take profit, ensuring more predictable trade management.



extern int RSIPeriod=14;
extern int OverBoughtPoint=70;
extern int OverSoldPoint=30;
extern int TotalAllowedTrades=1;
extern bool StaticSlTp=false;
extern int StopLoss=500;
extern int TakeProfit=500;
extern double LotSize=0.1;
extern bool ReverseOrders=false;

         
  • Calling IsNewCanlde() function
  • and obtaining the RSI Value.

if(IsNewCanlde())
{
  double sl,tp;
  double  RSIVal=iRSI(Symbol(),0,RSIPeriod,PRICE_CLOSE,0);


}


         

  • Recognizing and addressing OVERBOUGHT condition coding
  • (StaticSlTp & ReverseOrders Features also Implemented)



if(RSIVal>OverBoughtPoint)
{//it's an overbought point

  if(OrdersTotal()<TotalTrades)
   {//if there is no trade, then open a short position
   
    if(!ReverseOrders)
      {
        if(StaticSlTp)
         {//if StaticSlTp is enabled 
            sl=Ask+StopLoss*Point;
            tp=Ask-TakeProfit*Point;
         }
         int Ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,sl,tp);
      }
      else 
      {
        if (StaticSlTp )
         {//if StaticSlTp is enabled  
            sl=Bid-StopLoss*Point;
            tp=Bid+TakeProfit*Point;
         }
         int Ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,sl,tp);
      }
   }
   else
   {//if there is a long position
      
      if(!ReverseOrders)
      {
        if(CloseLastBuy())
         {
      
            if(StaticSlTp)
            {//if StaticSlTp is enabled 
               sl=Ask+StopLoss*Point;
               tp=Ask-TakeProfit*Point;
            }
            int Ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,sl,tp);
         }
      }
      else
      {
        if(CloseLastSell())
         {
          if(StaticSlTp)
            {//if StaticSlTp is enabled 
               sl=Bid-StopLoss*Point;
               tp=Bid+TakeProfit*Point;
            }
            int Ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,sl,tp);
         }
      }
   
   }

}


         


  • Recognizing and addressing OVERSOLD condition coding
  • (StaticSlTp & ReverseOrders Features also Implemented)



if(RSIVal<OverSoldPoint)
{//it's an oversold point

  if(OrdersTotal()<TotalTrades)
   {//if there is no trade, then open a long position
   
    if(!ReverseOrders)
      {
        if(StaticSlTp)
         {//if StaticSlTp is enabled 
            sl=Bid-StopLoss*Point;
            tp=Bid+TakeProfit*Point;
         }
         int Ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,sl,tp);
      }
      else 
      {
        if (StaticSlTp )
         {//if StaticSlTp is enabled  
          sl=Ask+StopLoss*Point;
          tp=Ask-TakeProfit*Point;
         }
         int Ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,sl,tp);
      }
   }
   else
   {//if there is a short position
      
      if(!ReverseOrders)
      {
        if(CloseLastSell())
         {
      
            if(StaticSlTp)
            {//if StaticSlTp is enabled 
               sl=Bid-StopLoss*Point;
               tp=Bid+TakeProfit*Point;
            }
            int Ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,sl,tp);
         }
      }
      else
      {
        if(CloseLastBuy())
         {
          if(StaticSlTp)
            {//if StaticSlTp is enabled 
              sl=Ask+StopLoss*Point;
              tp=Ask-TakeProfit*Point;
            }
            int Ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,sl,tp);
         }
      }
   
   }

}


         

Now compile and save the code.



  • Now we'll conduct a series of backtests using various input settings on the EUR/USD symbol data over a period of 5 years and save the report.

_To access the compiled EA:
  • Open Terminal.
  • Click on “File” -> “Open Data Folder”.
  • Navigate to “MQL4” -> “Experts”.
  • And there you can find “RSI-TurnOvers.ex4”.

_Let's start Backtesting:
  • Login into your MetaBacktest account.
  • Navigate to “Backtest” page.
  • Click on upload box to select the EA, or simply drag&drop it.
  • And there you can find “RSI-TurnOvers.ex4”.


  • MetaBacktest RSI TurnOver Strategy Backtesting

  • Backtest finished. later on we will going to make backtests using different settings.

MetaBacktest RSI TurnOver Strategy  Backtest Finished



We will make two more backtests with the following inputs:
  • Setting1:
    • ReverseOrders: True
  • Setting2:
    • ReverseOrders: True
    • StaticSlTp: True
    • StopLoss: 550
    • TakeProfit: 950


Time for Metrics Comparison:
  • Navigate to the History Page from the left sidebar after logging into your dashboard.
  • Select/Check box the results (Setting0, Setting1, Setting2).
  • Click on 'Compare' button.
  • Checking the Metrics: The Good, the Bad, and the Predictably Ugly


MetaBacktest Compare Detailed RSI Turnover Backtest Results


MetaBacktest  Detailed RSI Turnover Backtest Results comparison graph

MetaBacktest Compare Detailed RSI Turnover Backtest Results

Download Box


RSI-TurnOvers.ex4
RSI-TurnOvers.mq4 

RSI-TurnOvers.ex4-Setting1.set RSI-TurnOvers.ex4-Setting2.set
Result-RSI-TurnOvers.ex4.zip Result-RSI-TurnOvers.ex4-Setting1.zip Result-RSI-TurnOvers.ex4-Setting2.zip

Please Login or Signup
to be able to download the files.