stockstats icon indicating copy to clipboard operation
stockstats copied to clipboard

Error calculating the number of prices greater than the close of the last 10 periods

Open akitxu opened this issue 1 year ago • 1 comments

Thanks for sharing this work. I'm trying to adapt to it and I just found two problems. My DataFrame is

df = yf.download("AAPL", start="2020-01-01", end="2020-12-31") stock_df = StockDataFrame.retype(df)

when executing

tp = stock_df['middle'] stock_df['res'] = stock_df['middle'] > df['close'] stock_df[['middle', 'close', 'res', 'res_-10_c']]

it returns the error

`-------------------------------------------------------------------------- KeyError Traceback (most recent call last) ~/anaconda3/envs/yfinance1/lib/python3.9/site-packages/stockstats.py in getitem(self, item) 1249 try: -> 1250 result = wrap(super(StockDataFrame, self).getitem(item)) 1251 except KeyError:

~/anaconda3/envs/yfinance1/lib/python3.9/site-packages/pandas/core/frame.py in getitem(self, key) 3463 key = list(key) -> 3464 indexer = self.loc._get_listlike_indexer(key, axis=1)[1] 3465

~/anaconda3/envs/yfinance1/lib/python3.9/site-packages/pandas/core/indexing.py in _get_listlike_indexer(self, key, axis) 1313 -> 1314 self._validate_read_indexer(keyarr, indexer, axis) 1315

~/anaconda3/envs/yfinance1/lib/python3.9/site-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis) 1376 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique()) -> 1377 raise KeyError(f"{not_found} not in index") 1378

KeyError: "['res_-10_c'] not in index"

During handling of the above exception, another exception occurred:

IndexError Traceback (most recent call last) /tmp/ipykernel_7091/1854355926.py in 2 tp = stock_df['middle'] 3 stock_df['res'] = stock_df['middle'] > df['close'] ----> 4 stock_df[['middle', 'close', 'res', 'res_-10_c']]

~/anaconda3/envs/yfinance1/lib/python3.9/site-packages/stockstats.py in getitem(self, item) 1253 if isinstance(item, list): 1254 for column in item: -> 1255 self.__init_column(column) 1256 else: 1257 self.__init_column(item)

~/anaconda3/envs/yfinance1/lib/python3.9/site-packages/stockstats.py in __init_column(self, key) 1244 self[key] = [] 1245 else: -> 1246 self.__init_not_exist_column(key) 1247 1248 def getitem(self, item):

~/anaconda3/envs/yfinance1/lib/python3.9/site-packages/stockstats.py in __init_not_exist_column(self, key) 1229 c, r, t = ret 1230 func_name = 'get{}'.format(t) -> 1231 getattr(self, func_name)(c, r) 1232 elif len(ret) == 2: 1233 c, r = ret

~/anaconda3/envs/yfinance1/lib/python3.9/site-packages/stockstats.py in get_c(self, column, shifts) 251 """ 252 column_name = '{}{}_c'.format(column, shifts) --> 253 shifts = self.get_int_positive(shifts) 254 self[column_name] = self[column].rolling( 255 center=False,

~/anaconda3/envs/yfinance1/lib/python3.9/site-packages/stockstats.py in get_int_positive(self, windows) 958 window = self.to_int(windows) 959 if window <= 0: --> 960 raise IndexError("window must be greater than 0") 961 return window 962

IndexError: window must be greater than 0`

I will appreciate help to solve this problem.

akitxu avatar Sep 02 '22 16:09 akitxu