Louis
Louis

Reputation: 41

Problems with pandas_ta and stochastic rsi

When I run this code, it gives me an error: ValueError: Wrong number of items passed 2, placement implies 1

Am i missing something ? I looked at questions related to mine but it doesn't help me.

import pandas as pd
import pandas_ta as ta
import numpy as np


df = pd.read_csv('data/1d/AAVEUSDT-1d-data.csv')

def stoch_rsi(df, length=14):
    df['stochrsi'] = ta.stochrsi(close = df['close'])
    return df['stochrsi']

stoch = stoch_rsi(df)

print(stoch)

here you can find the error message :

Traceback (most recent call last):
File    "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/si  te-packages/pandas/core/indexes/base.py", line 2898, in get_loc
return self._engine.get_loc(casted_key)
File "pandas/_libs/index.pyx", line 70, in  pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1683, in  pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'stochrsi'

The above exception was the direct cause of the following  exception:

Traceback (most recent call last):
File  "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/si te-packages/pandas/core/generic.py", line 3576, in _set_item
loc = self._info_axis.get_loc(key)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexes/base.py", line 2900, in get_loc
raise KeyError(key) from err
KeyError: 'stochrsi'

During handling of the above exception, another exception        occurred:

Traceback (most recent call last):
File "/Users/louis/Desktop/Python_projects/streamlit_apps/dashboards/test.py", line 20, in <module>
stoch = stoch_rsi(df)
File "/Users/louis/Desktop/Python_projects/streamlit_apps/dashboards/test.py", line 14, in stoch_rsi
df['stochrsi'] = ta.stochrsi(close = df['close'])
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/frame.py", line 3044, in __setitem__
self._set_item(key, value)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/frame.py", line 3121, in _set_item
NDFrame._set_item(self, key, value)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/generic.py", line 3579, in _set_item
self._mgr.insert(len(self._info_axis), key, value)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/internals/managers.py", line 1198, in insert
block = make_block(values=value, ndim=self.ndim, placement=slice(loc, loc + 1))
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/internals/blocks.py", line 2744, in make_block
return klass(values, ndim=ndim, placement=placement)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/internals/blocks.py", line 130, in __init__
raise ValueError(
ValueError: Wrong number of items passed 2, placement implies 1

Upvotes: 0

Views: 5016

Answers (1)

Nand0san
Nand0san

Reputation: 511

I can give an alternative code for this indicator from a library I'm developing for learning purposes:

def RSI(data: pd.DataFrame, window_length=14) -> pd.Series:
    """
    Calculate the RSI indicator on a moving window.
    Ref: https://www.investopedia.com/terms/r/rsi.asp
    """
    df_ = data.copy()
    close = df_['Close']
    delta = close.diff()
    up = delta.clip(lower=0)  # Convert negatives to zero.
    down = -1 * delta.clip(upper=0)  # Convert positives to zero.
    # WMA
    roll_up = up.ewm(com=window_length - 1, adjust=True, min_periods=window_length).mean()
    roll_down = down.ewm(com=window_length - 1, adjust=True, min_periods=window_length).mean()
    # Calculate the RSI
    RS = roll_up / roll_down
    return 100.0 - (100.0 / (1.0 + RS))


def Stochastic_RSI(data: pd.DataFrame, window_length=14) -> pd.Series:
    """
    The Stochastic RSI (StochRSI) is an indicator used in technical analysis that ranges between zero and one (or zero and 100 on
    some charting platforms) and is created by applying the Stochastic oscillator formula to a set of relative strength index (RSI)
    values rather than to standard price data.
    Ref: https://www.investopedia.com/terms/s/stochrsi.asp
    """
    df_ = data.copy()
    rsi = RSI(data=df_, window_length=window_length)
    return (rsi - rsi.rolling(window_length).min()) / (rsi.rolling(window_length).max() - rsi.rolling(window_length).min())

A graph:

enter image description here

Upvotes: 2

Related Questions