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, 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 🙂


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

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
1310SMA (Up changes)SMA (Down Changes)SMMA (Up changes)SMMA (Down changes)

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

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){

            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){
            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++){

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

            results[i] = 100.0 - 100.0 / (1.0 +


        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.


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.