Relative Strength Index (RSI) and Momentum Trading in Algorithmic Strategies

Momentum trading aims to capitalise on the strength or speed of a price movement, and the Relative Strength Index (RSI) is one of the most popular indicators for this purpose. Developed by J. Welles Wilder, RSI measures the speed and magnitude of price changes, identifying overbought and oversold conditions. In this article, we’ll dive into how RSI works, its calculation, and its application in momentum trading algorithms. We’ll also provide a practical example in MQL4 for implementing an RSI-based trading strategy.

What is the Relative Strength Index (RSI)?

The RSI is a momentum oscillator that ranges from 0 to 100. It’s primarily used to identify overbought and oversold conditions:

  • Overbought: When RSI is above 70, it suggests the asset might be overbought and due for a reversal.
  • Oversold: When RSI is below 30, it indicates the asset could be oversold and primed for a price increase.

RSI values oscillate between these levels, giving traders a signal to buy during oversold conditions and sell during overbought conditions.

How RSI is Calculated

The RSI calculation uses the average of upward and downward price changes over a specified period, typically 14 days. The formula is as follows:

1. Calculate the Relative Strength (RS):

Relative Strength

2. Calculate RSI:

RSI

The standard period (n) is 14, but shorter or longer periods can be used based on the trader’s preference and strategy requirements.

Application of RSI in Momentum Trading

  1. Overbought/Oversold Strategy: Buy when RSI falls below 30 (oversold) and sell when RSI rises above 70 (overbought).
  2. Trend Confirmation: RSI can confirm trends. For example, in a strong uptrend, RSI might stay above 50, and in a strong downtrend, it may remain below 50.
  3. Divergence: RSI divergence occurs when the price makes a new high/low, but RSI does not. This discrepancy can indicate a potential reversal.

RSI Momentum Trading Strategy Example in MQL4

The following MQL4 example demonstrates a basic momentum trading strategy using RSI. It buys when RSI falls below 30 (indicating oversold conditions) and sells when RSI rises above 70 (indicating overbought conditions).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// RSI Momentum Trading Strategy in MQL4
input int RSIPeriod = 14;            // Period for RSI
input double OverboughtLevel = 70;   // RSI level for overbought condition
input double OversoldLevel = 30;     // RSI level for oversold condition
input double LotSize = 0.1;          // Lot size for orders
 
// Global variables to track trade status
bool buyTradeOpen = false;
bool sellTradeOpen = false;
 
void OnTick() {
   double rsi = iRSI(NULL, 0, RSIPeriod, PRICE_CLOSE, 0);
 
   // Buy signal: RSI falls below the oversold level
   if (rsi < OversoldLevel && !buyTradeOpen) { if (sellTradeOpen) CloseSell(); OpenBuy(); } // Sell signal: RSI rises above the overbought level else if (rsi > OverboughtLevel && !sellTradeOpen) {
       if (buyTradeOpen) CloseBuy();
       OpenSell();
   }
}
 
// Function to open a buy position
void OpenBuy() {
   int ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, 0, 0, "RSI Buy", 0, 0, clrGreen);
   if (ticket > 0) {
       buyTradeOpen = true;
       sellTradeOpen = false;
       Print("Buy order opened.");
   } else {
       Print("Error opening buy order: ", GetLastError());
   }
}
 
// Function to open a sell position
void OpenSell() {
   int ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, 0, 0, "RSI Sell", 0, 0, clrRed);
   if (ticket > 0) {
       sellTradeOpen = true;
       buyTradeOpen = false;
       Print("Sell order opened.");
   } else {
       Print("Error opening sell order: ", GetLastError());
   }
}
 
// Function to close any open buy position
void CloseBuy() {
   for (int i = OrdersTotal() - 1; i >= 0; i--) {
       if (OrderSelect(i, SELECT_BY_POS) && OrderType() == OP_BUY) {
           OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrGreen);
           buyTradeOpen = false;
           Print("Buy order closed.");
       }
   }
}
 
// Function to close any open sell position
void CloseSell() {
   for (int i = OrdersTotal() - 1; i >= 0; i--) {
       if (OrderSelect(i, SELECT_BY_POS) && OrderType() == OP_SELL) {
           OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrRed);
           sellTradeOpen = false;
           Print("Sell order closed.");
       }
   }
}

Explanation of the RSI Strategy

1. Parameters:

  • RSIPeriod: Defines the number of periods for calculating RSI, with 14 as the default.
  • OverboughtLevel and OversoldLevel: Set the RSI thresholds for overbought and oversold conditions.
  • LotSize: Determines the trade size for each position.

2. Trade Execution:

  • A buy signal is generated when RSI drops below the oversold level, suggesting a reversal may be due.
  • A sell signal is triggered when RSI crosses above the overbought level, indicating a potential pullback.

3. Functions:

  • OpenBuy() and OpenSell() open trades when signals occur.
  • CloseBuy() and CloseSell() close positions if an opposing signal is detected.

This RSI strategy captures momentum shifts by responding to extreme price conditions, providing a straightforward method for identifying entry and exit points based on market sentiment.

Tips for Optimising RSI Strategies

  1. Adjust Overbought/Oversold Levels: For highly volatile markets, using different levels (e.g., 80 for overbought, 20 for oversold) can help filter out false signals.
  2. Combine with Other Indicators: Confirm RSI signals with trend indicators like the EMA to avoid trading against the broader trend.
  3. Experiment with RSI Period: Shorter periods make RSI more sensitive, while longer periods smooth out fluctuations, so test to find the optimal setting.

Conclusion

RSI is a powerful tool in momentum trading, providing insights into overbought and oversold conditions that can lead to profitable reversals. This MQL4-based strategy showcases how to leverage RSI to identify these conditions, making it a valuable addition to any trader’s algorithmic toolkit. In the next article, we’ll explore the Moving Average Convergence Divergence (MACD) indicator, covering calculations, strategies, and coding examples for algorithmic trading.