Reputation: 323
I'm trying this snippet (from web) in my script which calculates StochRSI.
The error output I get:
Traceback (most recent call last):
File "C:\Users\user2\PycharmProjects\xxx2\main.py", line 99, in <module>
main()
File "C:\Users\user2\PycharmProjects\xxx2\main.py", line 87, in main
stoch_rsi = StochRSI(closing_data)
File "C:\Users\user2\PycharmProjects\xxx2\main.py", line 56, in StochRSI
delta = series.diff().dropna()
AttributeError: 'numpy.ndarray' object has no attribute 'diff'
Tried searching for answers but couldn't find any to fix the problem. What's wrong here?
EDIT: Please check the full code below. I hope it clears out what I'm doing... I used numpy to get closing prices. I used that to get live EMA, and it worked fine - and now I'm trying to get StochRSI's live value.
import datetime
import config
import csv
import os.path
import sys
import numpy as np
import pandas as pd
import requests
import talib
from binance.client import Client
from binance.enums import *
from time import sleep
def get_data():
historical_data = client.get_historical_klines(symbol=config.SYMBOL, interval=config.TIME_PERIOD, start_str="1 year ago UTC", klines_type=HistoricalKlinesType.SPOT)
return_data = []
for each in historical_data:
kline = float(each[4])
return_data.append(kline)
return np.array(return_data)
def StochRSI(series, period=14, smoothK=3, smoothD=3):
# Calculate RSI
delta = series.diff().dropna()
ups = delta * 0
downs = ups.copy()
ups[delta > 0] = delta[delta > 0]
downs[delta < 0] = -delta[delta < 0]
ups[ups.index[period-1]] = np.mean(ups[:period]) #first value is sum of avg gains
ups = ups.drop(ups.index[:(period-1)])
downs[downs.index[period-1]] = np.mean(downs[:period]) #first value is sum of avg losses
downs = downs.drop(downs.index[:(period-1)])
rs = ups.ewm(com=period-1, min_periods=0, adjust=False, ignore_na=False).mean() / \
downs.ewm(com=period-1, min_periods=0, adjust=False, ignore_na=False).mean()
rsi = 100 - 100 / (1 + rs)
# Calculate StochRSI
stochrsi = (rsi - rsi.rolling(period).min()) / (rsi.rolling(period).max() - rsi.rolling(period).min())
stochrsi_K = stochrsi.rolling(smoothK).mean()
stochrsi_D = stochrsi_K.rolling(smoothD).mean()
return stochrsi, stochrsi_K, stochrsi_D
def main():
ema_200 = None
last_ema_200 = None
while True:
closing_data = get_data()
last_candle = closing_data[-1]
ema_200 = talib.EMA(closing_data, 10)[-1]
stoch_rsi = StochRSI(closing_data)
if last_candle > ema_200:
print(f"Price {last_candle} is above EMA 200 {ema_200} | RSI {stoch_rsi}")
elif last_candle < ema_200:
print(f"Price {last_candle} is below EMA 200 {ema_200} | RSI {stoch_rsi}")
if __name__ == "__main__":
# Client
client = Client(config.API_KEY, config.API_SECRET_KEY, tld='com')
print(f"Authenticated")
main()
Upvotes: 0
Views: 1461
Reputation: 5381
This function is expecting a pandas DataFrame
or Series
but is getting a numpy ndarray
.
Upvotes: 1