PhracturedBlue / fava-portfolio-summary

Fava extension to display a portfolio summary
MIT License
23 stars 8 forks source link

handle KeyError error when getting security prices #4

Open clsacramento opened 2 years ago

clsacramento commented 2 years ago

I encountered following error when using the extension:

Exception on /beancount/extension/PortfolioSummary/ [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/fava_portfolio_summary/__init__.py", line 71, in portfolio_accounts
    portfolio = self._account_metadata_pattern(tree, key, pattern, internal, mwr, twr)
  File "/usr/local/lib/python3.10/site-packages/fava_portfolio_summary/__init__.py", line 172, in _account_metadata_pattern
    portfolio_data = self._portfolio_data(selected_accounts, internal, mwr, twr)
  File "/usr/local/lib/python3.10/site-packages/fava_portfolio_summary/__init__.py", line 263, in _portfolio_data
    rows.append(total)
  File "/usr/local/lib/python3.10/site-packages/fava_portfolio_summary/__init__.py", line 233, in _process_node
    try:
  File "/usr/local/lib/python3.10/site-packages/fava/core/__init__.py", line 538, in prices
    all_prices = get_all_prices(self.price_map, (base, quote))
  File "/usr/local/lib/python3.10/site-packages/beancount/core/prices.py", line 303, in get_all_prices
    return _lookup_price_and_inverse(price_map, base_quote)
  File "/usr/local/lib/python3.10/site-packages/beancount/core/prices.py", line 278, in _lookup_price_and_inverse
    return price_map[base_quote]
KeyError: ('CHF', 'VT')

The problem for me is that I have securities which are traded in USD, while my operating currency is CHF. It seems operating currency does not work in the situation where the user needs to trade uisng different currencies. I propose to add this workaround for the time being and later work on a smarter solution to figure out which currency should be used to obtain the prices.

PhracturedBlue commented 2 years ago

I don't love this work-around. I'll look into finding a more-robust answer to this