[Python] XIRR returns absurd value when current market value is added on same day as purchase

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo

    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!

    Continue reading...

Compartilhe esta Página