santoshlite / Empyrial

An Open Source Portfolio Backtesting Engine for Everyone | 面向所有人的开源投资组合回测引擎
https://empyrial.gitbook.io/empyrial/
MIT License
916 stars 124 forks source link

Error when running fundlens #17

Closed karim1104 closed 3 years ago

karim1104 commented 3 years ago

All functions ran perfectly except for fundlens. I got the following error:


KeyError Traceback (most recent call last)

in 8 ) 9 ---> 10 fundlens(portfolio) ~/anaconda3/lib/python3.8/site-packages/empyrial.py in fundlens(my_portfolio, period) 484 485 #book value --> 486 book_value = yahoo_financials.get_book_value() 487 488 #operating income ~/anaconda3/lib/python3.8/site-packages/yahoofinancials/__init__.py in get_book_value(self) 794 # Financial Statement Data Methods 795 def get_book_value(self): --> 796 return self._financial_statement_data('balance', 'balanceSheetHistoryQuarterly', 797 'totalStockholderEquity', 'quarterly') 798 ~/anaconda3/lib/python3.8/site-packages/yahoofinancials/__init__.py in _financial_statement_data(self, stmt_type, stmt_code, field_name, freq) 685 except (IndexError, AttributeError, TypeError): 686 date_key = list(re_data[self.ticker][0])[0] --> 687 data = re_data[self.ticker][0][date_key][field_name] 688 else: 689 data = {} KeyError: 'totalStockholderEquity'
karim1104 commented 3 years ago

When I use different tickers (portfolio= ["TSLA","AMZN","FB","GOOG","AAPL"]), I get a different error:


KeyError Traceback (most recent call last) ~/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2897 try: -> 2898 return self._engine.get_loc(casted_key) 2899 except KeyError as err:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'inventory'

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

KeyError Traceback (most recent call last)

in 8 ) 9 ---> 10 fundlens(portfolio) ~/anaconda3/lib/python3.8/site-packages/empyrial.py in fundlens(my_portfolio, period) 503 504 #inventory --> 505 inventory = df.iloc[:,0]['inventory'] 506 507 #total liabilities ~/anaconda3/lib/python3.8/site-packages/pandas/core/series.py in __getitem__(self, key) 880 881 elif key_is_scalar: --> 882 return self._get_value(key) 883 884 if is_hashable(key): ~/anaconda3/lib/python3.8/site-packages/pandas/core/series.py in _get_value(self, label, takeable) 988 989 # Similar to Index.get_value, but we do not fall back to positional --> 990 loc = self.index.get_loc(label) 991 return self.index._get_values_for_loc(self, loc, label) 992 ~/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2898 return self._engine.get_loc(casted_key) 2899 except KeyError as err: -> 2900 raise KeyError(key) from err 2901 2902 if tolerance is not None: KeyError: 'inventory'
santoshlite commented 3 years ago

Thanks for raising the issue, just fixed it in the new version =)

You can try it out here: https://colab.research.google.com/drive/1x22fJ5bnc5ZVwj05_k-cKarj9GXw5J8W?usp=sharing

If it's ok for you, we can close this issue.

karim1104 commented 3 years ago

Confirmed working with the new version. Thank you very much! And by the way, incredible work you're doing here!