Home » Technical Analysis Functions » ta.rsi Function in Pine Script

ta.rsi Function in Pine Script

Photo of author
Published on

One of the widely used indicators in technical analysis is the Relative Strength Index (RSI). In this tutorial, we will dive into the ta.rsi function in Pine Script, which allows you to efficiently calculate the RSI values for any given data series.

What is RSI?

RSI stands for Relative Strength Index. It’s a momentum oscillator that measures the speed and change of price movements. RSI oscillates between zero and 100. Traditionally and according to Wilder, RSI is considered overbought when above 70 and oversold when below 30.

Syntax of ta.rsi

Before we proceed further, let’s understand the syntax:

ta.rsi(source, length) → series float

Arguments:

source (series int/float)

This represents the series of values that the RSI will be calculated on. Most commonly, the closing prices (close) of bars are used.

length (simple int)

This refers to the number of bars over which the RSI is calculated. The default and most commonly used value for RSI is 14.

Basic Example

Now that we understand the syntax, let’s look at a basic example:

//@version=5
indicator("ta.rsi")
plot(ta.rsi(close, 7))

This script plots the 7-period RSI of the closing prices.

Custom Implementation

Although ta.rsi is efficient and straightforward to use, it’s educational to see how it’s calculated. Here’s a custom implementation:

pine_rsi(x, y) =>
    u = math.max(x - x[1], 0) // upward change
    d = math.max(x[1] - x, 0) // downward change
    rs = ta.rma(u, y) / ta.rma(d, y)
    res = 100 - 100 / (1 + rs)
    res

plot(pine_rsi(close, 7))

Explaining the Code:

  • u = math.max(x - x[1], 0): This line calculates the upward change between the current bar and the previous bar. If there’s no upward change (or if the change is negative), it defaults to 0.
  • d = math.max(x[1] - x, 0): This is similar to the previous line but calculates the downward change.
  • rs = ta.rma(u, y) / ta.rma(d, y): This line calculates the Relative Strength (RS). It’s the ratio of the average of y days of up closes to the average of y days of down closes.
  • res = 100 - 100 / (1 + rs): Finally, this line converts the RS value into an oscillating index.

Remarks

  • na values in the source series are ignored. This means the function calculates based on the length quantity of non-na values. This ensures that missing or unrecorded data doesn’t affect the calculation.

Unique Use-Case: Highlight Overbought and Oversold Areas

Let’s create a script that not only plots the RSI but also highlights areas where RSI is considered overbought (above 70) or oversold (below 30).

//@version=5
indicator("RSI with Highlighted Zones", overlay=true)
rsi_values = ta.rsi(close, 14)
plot(rsi_values, color=color.blue)
hline(70, "Overbought", color=color.red)
hline(30, "Oversold", color=color.green)
bgcolor(rsi_values > 70 ? color.red : rsi_values < 30 ? color.green : na, transp=90)
ta.rsi Function in Pine Script

In this example, the areas where the RSI goes above 70 are shaded in red, indicating overbought conditions. Similarly, areas where the RSI goes below 30 are shaded in green, indicating oversold conditions. This visual cue can help traders quickly identify potential reversal zones.

Key Takeaways:

  • RSI is a crucial momentum indicator used to identify potential overbought or oversold conditions.
  • ta.rsi in Pine Script allows for a quick and efficient calculation of the RSI.
  • While the built-in function is optimized, understanding the underlying calculation can be beneficial.
  • Custom scripts can expand the utility of the RSI by providing visual cues for traders.

Conclusion

Understanding and effectively using the RSI can be pivotal for traders. Whether you’re using the built-in ta.rsi function or implementing your custom version, Pine Script offers a flexible environment to incorporate RSI into your trading strategies. As always, while indicators like RSI can be powerful tools, they should be used in conjunction with other analysis techniques for the best results.

Leave a Comment