ComeçarComece de graça

Calculate the RSI

The RSI calculation follows a straightforward formula. RS, or Relative Strength, is the average of upward price changes in a chosen n periods, divided by the average of downward price changes over those n periods.

\( RSI = 100 - 100/(1+RS)\)

Where: RS = average of upward price changes / average of downward price changes

All these calculations can be handled in Python with one line of code. In this exercise, you will do your first RSI calculation using historical daily price data of the Google stock.

The daily price data has been loaded as stock_data. Also, talib has been imported for you.

Este exercício faz parte do curso

Financial Trading in Python

Ver curso

Instruções do exercício

  • Calculate the RSI using the appropriate method from talib and the Close column in the price data. Save it in a new column called RSI_14.
  • Calculate the RSI using a time period of 21 and save it in a new column called RSI_21.
  • Print the last five rows of stock_data.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# Calculate RSI with the default time period
stock_data['RSI_14'] = ____(stock_data['____'])

# Calculate RSI with a time period of 21
stock_data['RSI_21'] = ____

# Print the last five rows
print(stock_data.____())
Editar e executar o código