Skip to content

Commit

Permalink
fix xirr calculation when capital gains is involved
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthakumaran committed Jan 26, 2024
1 parent 78fe6dc commit 8942670
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 8 deletions.
5 changes: 3 additions & 2 deletions internal/service/xirr.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ func XIRR(db *gorm.DB, ps []posting.Posting) decimal.Decimal {
today := utils.EndOfToday()
marketAmount := utils.SumBy(ps, func(p posting.Posting) decimal.Decimal {
if IsCapitalGains(p) {
return p.Amount.Neg()
return decimal.Zero
}
return p.MarketAmount
})
cashflows := lo.Reverse(lo.Map(ps, func(p posting.Posting, _ int) xirr.Cashflow {
if IsInterest(db, p) || IsInterestRepayment(db, p) || IsCapitalGains(p) {
if IsInterest(db, p) || IsInterestRepayment(db, p) {
return xirr.Cashflow{Date: p.Date, Amount: 0}
} else {
return xirr.Cashflow{Date: p.Date, Amount: p.Amount.Neg().Round(4).InexactFloat64()}
}
}))

cashflows = append(cashflows, xirr.Cashflow{Date: today, Amount: marketAmount.Round(4).InexactFloat64()})
return xirr.XIRR(cashflows)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixture/inr-beancount/assets_balance.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"marketAmount": 0,
"balanceUnits": -100,
"latestPrice": 0,
"xirr": 149.2,
"xirr": 167.56,
"gainAmount": 27,
"absoluteReturn": 0.0027
},
Expand Down
2 changes: 1 addition & 1 deletion tests/fixture/inr-beancount/gain.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"balanceUnits": 0,
"netInvestmentAmount": -27
},
"xirr": 149.2,
"xirr": 167.56,
"postings": [
{
"id": 11,
Expand Down
2 changes: 1 addition & 1 deletion tests/fixture/inr-hledger/assets_balance.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"marketAmount": 0,
"balanceUnits": -100,
"latestPrice": 0,
"xirr": 151.56,
"xirr": 170.5,
"gainAmount": 27.3,
"absoluteReturn": 0.00273
},
Expand Down
2 changes: 1 addition & 1 deletion tests/fixture/inr-hledger/gain.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"balanceUnits": 0,
"netInvestmentAmount": -27.3
},
"xirr": 151.56,
"xirr": 170.5,
"postings": [
{
"id": 15,
Expand Down
2 changes: 1 addition & 1 deletion tests/fixture/inr/assets_balance.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"marketAmount": 0,
"balanceUnits": -100,
"latestPrice": 0,
"xirr": 151.56,
"xirr": 170.5,
"gainAmount": 27.3,
"absoluteReturn": 0.00273
},
Expand Down
2 changes: 1 addition & 1 deletion tests/fixture/inr/gain.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"balanceUnits": 0,
"netInvestmentAmount": -27.3
},
"xirr": 151.56,
"xirr": 170.5,
"postings": [
{
"id": 11,
Expand Down

0 comments on commit 8942670

Please sign in to comment.