Thanks to everyone for input in this thread! Thanks Red for the code change! I'll try that later today and over a few weeks. If it doesn't work for all of my trades I'll see if I can refine it further.
Another question: Given that there are some bugs in the beangrow code and it is not actively maintained, is there any other tool in the beancount ecosystem that can calculate IRR per group of investments in a similar way to beangrow, but correctly? On Thursday, November 28, 2024 at 9:55:04 AM UTC+1 Red S wrote: > Hi Eric, > > Applying this patch should help. It uses XIRR from this library > <https://anexen.github.io/pyxirr/functions.html#xirr>. It certainly > resolves your test case, and works on several accounts of mine (but not > all). However, I’ve barely tested it, and there are cases where it might > not work. Nor do I have an explanation for why the test case you presented > wasn't caught earlier. Posting it here for you or anyone who is inspired to > dig deeper into this issue. > --- /home/user/beangrow/beangrow/returns.py +++ returns.py @@ -17,7 +17,7 > @@ from beancount.core.inventory import Inventory from > beancount.core.number import ZERO from beancount.core.position import > Position -from scipy.optimize import fsolve +import pyxirr from > beangrow.investments import AccountData, CashFlow, Cat, compute_balance_at > @@ -196,14 +196,10 @@ years = [(flow.date - end_date).days / 365 for flow > in dated_flows] years = np.array(years) - # Start with something reasonably > normal. - estimated_irr = 0.2 * np.sign(np.sum(cash_flows)) + date_list = > [d.date for d in dated_flows] + irr = pyxirr.xirr(date_list, cash_flows) + > return irr - # Solve for the root of the NPV equation. - irr, *_ = fsolve( > - net_present_value, x0=estimated_irr, args=(cash_flows, years), > full_output=True - ) - return irr.item() > > -- You received this message because you are subscribed to the Google Groups "Beancount" group. To unsubscribe from this group and stop receiving emails from it, send an email to beancount+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/beancount/158d0a39-82ca-44dc-9b6e-5cbaa02f3843n%40googlegroups.com.