How to calculate the Relative Strength Index (RSI)

We would like to do something different in this post. Usually, we focus more on programming and Java. However, in this post, we will have some fun by discussing how to calculate the Relative Strength Index (RSI) in a step by step guide.

And because this is Nullbeans.com, we will also provide you with an implementation in the Java programming language. Therefore, after this tutorial, you will be able to add the RSI indicator to your own custom made charts and statistics application 🙂

Introduction

The RSI is one of the most used and most popular technical indicators used in stock and commodities trading. You might be wondering if it makes sense to learn how a technical indicator is calculated. 

From personal experience I found out that I could understand the technical indicator much better when I know how to calculate it. This is because you understand what the value of the indicator mathematically represents. And when you understand an indicator it means that you can use it in a better way while trading. 

How to calculate the RSI

Unfortunately calculating the RSI is not as straightforward as calculating a moving average. This is because the RSI has multiple components to be calculated first before we can reach the final value of the RSI.

Click to enlarge

The good news is that these components are simple to calculate individually. So let us start by displaying the general formula for calculating the RSI and then we will we will break down this formula into components and calculate each of the components.

Calculating Up/Down changes

The first step we need to take is to decide which time frame and length do we use when calculating the RSI. This usually depends on each individual trader. However, a daily timeframe (day candle sticks) is a popular time frame so we will go with that for our example.

The second thing to decide is the length of the RSI (designated in the formula as n). The length will decide how far back in the past do look in order to calculate the RSI value of the current candle. For example, if we use a 14 day RSI, then it means that today’s RSI value is based on the changes that occurred in the last 14 days.

Now that we have decided the length and time frame, we will need to calculate the up changes and downward changes that occurred in the last 14 days. For this, we will use the following formulas.

click to enlarge
click to enlarge

The aim of this calculation is to find out how much upward or downward force happened on each day. For example, if your favorite stock opened with the price of $10 and closed with the price of $8, then for this day:

  • Change up = 0
  • Change down = 2


On the other hand, if the opening price was $10 and the closing price was $13, then:

  • Change up = 3
  • Change down = 0


Example data set:

Candle (i)Opening priceClosing priceChange UpChange Down
1131201
2121530
3151720
4171601


When calculating the RSI of a given candle, we will need to calculate the change up and change down values for each of the last n candles. This will be important for our next step.

Calculating the SMMA of Up/Down changes

Now that we have calculated the change up and down values of the last n candles, we will need to calculate the simple or modified moving average (SMMA) of the up changes and down changes. The average is called the simple or modified because it first starts by using the simple moving average and then moves on to a “modified” moving average formula. A lot of internet articles use the simple moving average or the exponential one. However, this is not part of the original RSI formula. Here, we will show you here how to calculate the RSI as it shows on TradingView and other popular charting tools.

Just like the RSI, the SMMA also has a length and time frame. However, these remain the same as the ones chosen for the RSI. So for an n length RSI, we will need to calculate the n length SMMA of the changes.

The SMMA of day i in the chart is based on the SMMA of day i-1. And the SMMA of day i-1 depends on the SMMA of day i-2, and so on. In other words, it is a recursive calculate. However, you might ask, how do we calculate the SMMA of the first day/candle? Well, for the first candle, we will use the simple moving average value (SMA) will serve as the SMMA. The formula of the RSI is as follows.

Click to enlarge

It is important to note that if you are starting your calculations from the start of your dataset is that you will not be able to calculate the RSI or SMMA for the first n values. This is because that you will need these values to calculate the simple average of the nth value. Let us go back to our data set and calculate the 14 day SMA.

Candle (i)Change UpChange Down
101
230
320
401
502
602
710
802
920
1020
1140
1220
1310SMA (Up changes)SMA (Down Changes)SMMA (Up changes)SMMA (Down changes)
14101.2857142860.57142857141.2857142860.5714285714
1510N/AN/A1.2653061220.5306122449
1603N/AN/A1.1749271140.7069970845
1710N/AN/A1.162432320.6564972928
1801N/AN/A1.079401440.6810332005
1920N/AN/A1.145158480.6323879718
2040N/AN/A1.3490757310.5872174024
2120N/AN/A1.3955703220.5452733023
2220N/AN/A1.438743870.5063252092
2310N/AN/A1.4074050220.4701591229
2402N/AN/A1.3068760920.5794334712
2530N/AN/A1.4278135140.5380453661
2602N/AN/A1.3258268350.6424706971
2702N/AN/A1.2311249180.7394370759
2840N/AN/A1.428901710.6866201419

Now that we have calculated the SMA of the up and down changes, we can use it as the first SMMA to calculate the subsequent SMMA values by using our formula.

Calculating the RS

The RSI indicator is based on the changes in the price action and not on the actual price itself . This is where the term Relative Strength (RS) comes from.

Calculating the RS is quite simple. We need to divide the SMMA of the up changes by the SMMA of the down changes.

Calculating the final RSI value

Now that we have all the components ready to calculate the final RSI value, we will simply have to follow our formula from the beginning of the post.

Notice that the larger the upward SMMA is, the larger the RS and RSI values are.

SMMA (Up changes)SMMA (Down changes)RSRSI
1.2857142860.57142857142.2569.23076923
1.2653061220.53061224492.38461538570.45454545
1.1749271140.70699708451.6618556762.43222308
1.162432320.65649729281.77065820863.90749327
1.079401440.68103320051.58494687161.31448536
1.145158480.63238797181.81084797864.42354734
1.3490757310.58721740242.29740420869.67311446
1.3955703220.54527330232.55939602471.90534593
1.438743870.50632520922.84154105773.9687801
1.4074050220.47015912292.99346530674.95909133
1.3068760920.57943347122.25543769469.28216437
1.4278135140.53804536612.65370469572.63051934
1.3258268350.64247069712.06363782967.35906606
1.2311249180.73943707591.66494886262.4758278
1.428901710.68662014192.08106582167.5436989


You can view the full data set in the Google sheets link here.

Calculating the RSI using Java

For all those who would like to build the next smart trading program, I have prepared the following code in Java to calculate the RSI.

The data used in this example is based on the Candlestick class. The class looks as follows:

public class Candlestick {
    private Long openTime;
    private double open;
    private double close;
}

Note that the getters and setters have been ommitted for display purposes. Now that we have our data in the form of Candlesticks, we can build the RSI calculator class. To calculate the SMMA of the up and down changes, we can use the following methods.

    public static double calcSmmaUp(Candlestick[] candlesticks, double n, int i, double avgUt1){

        if(avgUt1==0){
            double sumUpChanges = 0;

            for(int j = 0; j < n; j++){
                double change = candlesticks[i - j].getClose() - candlesticks[i - j].getOpen();

                if(change > 0){
                    sumUpChanges+= change;
                }
            }
            return sumUpChanges / n;
        }else {
            double change = candlesticks[i].getClose() - candlesticks[i].getOpen();
            if(change < 0){
               change = 0;
            }
            return ((avgUt1 * (n-1)) + change) / n ;
        }

    }

    public static double calcSmmaDown(Candlestick[] candlesticks, double n, int i, double avgDt1){
        if(avgDt1==0){
            double sumDownChanges = 0;

            for(int j = 0; j < n; j++){
                double change = candlesticks[i - j].getClose() - candlesticks[i - j].getOpen();

                if(change < 0){
                    sumDownChanges-= change;
                }
            }
            return sumDownChanges / n;
        }else {
            double change = candlesticks[i].getClose() - candlesticks[i].getOpen();
            if(change > 0){
                change = 0;
            }
            return ((avgDt1 * (n-1)) - change) / n ;
        }

    }


Please note that these calculations could be done in a much more efficient way. However, the code in this post is only for demonstration purposes. If you plan to build a production application, then you will need to use a data structure other than the double, such as BigDecimals . You will also need to have a more efficient algorithm.

Now that we have the SMMAs calculated, all we need to do is to calculate the RS and the RSI.

    public static double[] calculateRSIValues(Candlestick[] candlesticks, int n){

        double[] results = new double[candlesticks.length];

        double ut1 = 0;
        double dt1 = 0;
        for(int i = 0; i < candlesticks.length; i++){
            if(i<(n)){
                continue;
            }

            ut1 = calcSmmaUp(candlesticks, n, i, ut1);
            dt1 = calcSmmaDown(candlesticks, n, i, dt1);

            results[i] = 100.0 - 100.0 / (1.0 +
                    calculateRS(ut1,
                                dt1));

        }

        return results;
    }

Notice that we feed the SMMA calculation methods with a Zero value previous SMMA. This is to indicate that we will need to calculate the SMA as this is the first SMMA value.

Summary

In this post, we discussed step by step how to calculate the RSI and implement the calculation in Java.

If you like to see more of this kind of post, then please let us know in the comments section below.

How to calculate the Relative Strength Index (RSI)
Scroll to top