Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
akitxu opened this issue Sep 2, 2022 · 1 comment
Assignees

Comments

@akitxu
Copy link

akitxu commented Sep 2, 2022

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.

@jealous
Copy link
Owner

jealous commented Nov 17, 2022

Please replace res_-10_c with res_10_c. It's a rolling window count and only accept a positive number. I will update the error in the readme. Thanks.

@jealous jealous self-assigned this Nov 17, 2022
jealous added a commit that referenced this issue Nov 17, 2022
The typical price or middle price is calculated by:
```
(high + low + close) / 3
```

But it would be accurate if the data source has the `amount` that
represents the total amount of capital traded in that period.
Use `middle = amount / volume` when amout is available.

Also fix some typos in the read me file.
Add some utilities for dropping columns, head or tail.

Add python 3.11 in CI and drop 3.9.
jealous added a commit that referenced this issue Nov 17, 2022
The typical price or middle price is calculated by:
```
(high + low + close) / 3
```

But it would be accurate if the data source has the `amount` that
represents the total amount of capital traded in that period.
Use `middle = amount / volume` when amout is available.

Also fix some typos in the read me file.
Add some utilities for dropping columns, head or tail.

Add python 3.11 in CI and drop 3.9.

Update version to 0.5.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants