I'm calculating XIRR based on investment transactions and adding the current market value as the final cash flow to close the series.
I use pyxirr, but the same issue happens with other libraries like scipy. So this seems independent of the implementation – I believe the problem lies in the input data itself, not in the code.
Here’s a simplified example (used in my test):
DEBUG XIRR INPUT:
2021-01-01 → -200.0 # BUY AAPL
2021-01-04 → 300.0 # SELL AAPL
2021-01-04 → 8.0 # Dividend
2021-01-05 → -200.0 # BUY CDR
2021-01-05 → 200.0 # current market value of CDR
The last value (+200 on 2021-01-05) is the current portfolio value, not an actual sale.
However, XIRR returns a completely unrealistic result:
XIRR: 6.531621878667661e+24%
I believe this happens because XIRR treats the last two flows (-200, +200) as an instant full return — which mathematically inflates the IRR to infinity.
What’s the best practice here? Should I avoid including market value if there was a purchase that same day? Or is there a better way to close the cash flow series for XIRR?
Thanks!
I'm calculating XIRR based on investment transactions and adding the current market value as the final cash flow to close the series.
I use pyxirr, but the same issue happens with other libraries like scipy. So this seems independent of the implementation – I believe the problem lies in the input data itself, not in the code.
Here’s a simplified example (used in my test):
DEBUG XIRR INPUT:
2021-01-01 → -200.0 # BUY AAPL
2021-01-04 → 300.0 # SELL AAPL
2021-01-04 → 8.0 # Dividend
2021-01-05 → -200.0 # BUY CDR
2021-01-05 → 200.0 # current market value of CDR
The last value (+200 on 2021-01-05) is the current portfolio value, not an actual sale.
However, XIRR returns a completely unrealistic result:
XIRR: 6.531621878667661e+24%
I believe this happens because XIRR treats the last two flows (-200, +200) as an instant full return — which mathematically inflates the IRR to infinity.
What’s the best practice here? Should I avoid including market value if there was a purchase that same day? Or is there a better way to close the cash flow series for XIRR?
Thanks!
Share Improve this question asked Mar 27 at 21:40 LUshosiuLUshosiu 2291 gold badge3 silver badges14 bronze badges 1- This is not particularly a programming problem. You've confirmed that the issue is implementation-independent, so this is a matter of getting help with the interpretation of quantitative finance. Consider instead a site like quant.stackexchange . – Reinderien Commented Mar 28 at 21:11
1 Answer
Reset to default 0I have not used the pyxirr
library, but I think the problem is with your data rather than the IRR calculation.
IRR is the (annualized) rate at which you need to discount cash flows so that your NPV is zero. In your example, your investment of 200 gives you 308 over 3 days. That is a daily rate of 15.5%. A daily rate of 15.5% compounds to an equivalent annual rate( (1 + daily_rate)^365
) of ~7E22
- which is what pyxirr
and other libraries are telling you.
The 200 in and 200 out on the same day should ideally not affect calculations as I think any library would be aggregating values for a day before applying the discount factor. Even if not, a 200 in and 200 out on the same day means an IRR of zero for that period, not infinity.
In short, I think the problem is using IRR. Either include more data (like what investment contributes the 8 dollar dividend), or use another metric.