0
ticker = "INFY"
start_date = "2021-01-01"
end_date = "2022-12-31"
data = nsepy.get_history(symbol=ticker, start=start_date, end=end_date)

while running the code getting

TypeError: unsupported operand type(s) for -: 'str' and 'str'

I just want to create the RSI Indicator

import nsepy
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def RSI(data, n=14):
    '''
    Calculates the RSI (Relative Strength Index) for the given data.
    data: pandas dataframe containing the stock price data.
    n: the number of periods to consider for the RSI calculation.
    '''
    delta = data.diff()
    gain = delta[delta > 0].fillna(0).cumsum()
    loss = -delta[delta < 0].fillna(0).cumsum()
    avg_gain = gain.rolling(window=n).mean()
    avg_loss = loss.rolling(window=n).mean()
    rs = avg_gain / avg_loss
    rsi = 100 - (100 / (1 + rs))
    return rsi

# Get stock data using NSEPY library
ticker = "INFY"
start_date = "2020-01-01"
end_date = "2021-12-31"
data = nsepy.get_history(symbol=ticker, start=start_date, end=end_date)

# Calculate RSI
rsi = RSI(data["Close"], 14)

# Generate buy and sell signals
buy_signal = rsi < 30
sell_signal = rsi > 70

# Plot the RSI and buy/sell signals
plt.plot(rsi, label='RSI')
plt.plot(buy_signal*100, 'g^', label='Buy Signal')
plt.plot(sell_signal*100, 'rv', label='Sell Signal')
plt.legend()
plt.show()

How to resolve it, not able to find a solution

RF1991
  • 2,037
  • 4
  • 8
  • 17

0 Answers0