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.
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.
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”.
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()
{
static datetime last_time=0;
datetime lastbar_time=SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);
if(last_time==0)
{
last_time=lastbar_time;
return(false);
}
if(last_time!=lastbar_time)
{
last_time=lastbar_time;
return(true);
}
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)
{
if(OrdersTotal()<TotalTrades)
{
if(!ReverseOrders)
{
if(StaticSlTp)
{
sl=Ask+StopLoss*Point;
tp=Ask-TakeProfit*Point;
}
int Ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,sl,tp);
}
else
{
if (StaticSlTp )
{
sl=Bid-StopLoss*Point;
tp=Bid+TakeProfit*Point;
}
int Ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,sl,tp);
}
}
else
{
if(!ReverseOrders)
{
if(CloseLastBuy())
{
if(StaticSlTp)
{
sl=Ask+StopLoss*Point;
tp=Ask-TakeProfit*Point;
}
int Ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,sl,tp);
}
}
else
{
if(CloseLastSell())
{
if(StaticSlTp)
{
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)
{
if(OrdersTotal()<TotalTrades)
{
if(!ReverseOrders)
{
if(StaticSlTp)
{
sl=Bid-StopLoss*Point;
tp=Bid+TakeProfit*Point;
}
int Ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,sl,tp);
}
else
{
if (StaticSlTp )
{
sl=Ask+StopLoss*Point;
tp=Ask-TakeProfit*Point;
}
int Ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,sl,tp);
}
}
else
{
if(!ReverseOrders)
{
if(CloseLastSell())
{
if(StaticSlTp)
{
sl=Bid-StopLoss*Point;
tp=Bid+TakeProfit*Point;
}
int Ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,sl,tp);
}
}
else
{
if(CloseLastBuy())
{
if(StaticSlTp)
{
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”.
- Backtest finished. later on we will going to make backtests using different settings.
We will make two more backtests with the following inputs:
-
Setting1:
-
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
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.