ticker = "INFY"
start_date = "2021-01-01"
end_date = "2022-12-31"
data = nsepy.get_history(symbol=ticker, start=start_date, end=end_date)
运行代码时得到
类型错误:不支持的操作数类型 -:“str”和“str”
我只想创建 RSI 指标
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()
如何解决,找不到解决办法
您可能正在计算导致 TypeError 的任意两个字符串变量。请准确解释编译器指示错误的是哪一行语句。
str1 = "Hello"
str2 = "World"
result = str1 - str2 # This will raise a TypeError