hoostus / portfolio-returns

Beancount portfolio returns generator
Other
38 stars 6 forks source link

Error with simple file (no growth) #4

Closed redstreet closed 4 years ago

redstreet commented 4 years ago
option "operating_currency" "USD"
plugin "beancount.plugins.auto_accounts"
plugin "beancount.plugins.implicit_prices"

2018-01-01 * "Buy"
   Assets:Investments 100 HOOLI {2 USD}
   Assets:Bank

Expected: 0% Result: error:

$ /home/user/gnu/portfolio-returns/irr.py --currency USD --account Assets:Investments test.bc --from 2018-01-01 --to 2018-12-31
/usr/lib/python3.6/site-packages/numpy/core/numeric.py:2531: RuntimeWarning: invalid value encountered in multiply
  x = x * ones_like(cond)
Traceback (most recent call last):
  File "/home/user/gnu/portfolio-returns/irr.py", line 248, in <module>
    r = xirr([(d, float(f)) for (d,f) in cashflows])
  File "/home/user/gnu/portfolio-returns/irr.py", line 65, in xirr
    return optimize.newton(lambda r: xnpv(r,cashflows),guess)
  File "/usr/lib/python3.6/site-packages/scipy/optimize/zeros.py", line 338, in newton
    q1 = func(p1, *args)
  File "/home/user/gnu/portfolio-returns/irr.py", line 65, in <lambda>
    return optimize.newton(lambda r: xnpv(r,cashflows),guess)
  File "/home/user/gnu/portfolio-returns/irr.py", line 46, in xnpv
    return sum([cf/(1+rate)**((t-t0).days/365.0) for (t,cf) in chron_order])
  File "/home/user/gnu/portfolio-returns/irr.py", line 46, in <listcomp>
    return sum([cf/(1+rate)**((t-t0).days/365.0) for (t,cf) in chron_order])
OverflowError: complex exponentiation
hoostus commented 4 years ago

This is actually caused by the first transaction being on the same day as the start of the measurement period. If you shift the transaction to 2018-01-02 you'll see that it works as expected.

I've committed a fix that handles this case.