How to calculate the Relative Strength Index (RSI)

Disclaimer: This post is not meant to serve as financial advice. It is only meant for information and educational purposes., its owners and writers are not financial advisers and are not responsible for any financial decisions or actions you or others perform due to information in this post.

In this post, we will discuss 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! So read on 😉

By the way, if you are ever looking for a resource to learn trading, then I would highly recommend the book  “The New Trading for a Living (click to check the price on Amazon). I find the book quite interesting because it takes trading from the perspective of a psychologist.

Not only do you get to learn about trading and trading tools in general, but you also get to learn how to control your emotions and to use your analytical brain to not make rushed moves when prices are rising or falling.

You do not need to take my word for it. You can do your own research, check what other customers say and make up your mind 😉


The RSI is one of the most used and most popular technical indicators used in stock and commodities trading. If you are new to RSI then I recommend that you check our article here to learn more about the indicator and how is it used during 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

So if you followed our previous article about the RSI you already know that the 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:

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.

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.

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 values of a commodity. We discussed how to calculate the different components that go into the RSI and provided an example program in Java for calculating it.

If you fancy an assistant on your desk to entertain you, tell you about the weather or your appointments, then I recommend the Anki Cozmo – Collector’s Edition robot (click to check the price on Amazon).

The Anki Cozmo will entertain you during those boring or lonely moments at work, or while studying, and will help you to keep your schedule and be more alert throughout the day.

How to calculate the Relative Strength Index (RSI)

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top