Search code examples
ironpythonquantitative-finance

How to implement NPV, IRR, etc. in IronPython


We are implementing a financial planning tool in .NET and are using IronPython for scripting. We need to evaluate standard financial functions such as NPV (Net Present Value), IRR (Internal Rate of Return), etc which are available directly in Excel as formulas.

I am wondering if these are also supported in IronPython through the use of a library. I have already searched on Google but did not come across any such libraries. If anyone has any experience with these, please let me know. Thanks for your help in advance!

-Nitesh


Solution

  • Given that many numeric/math libraries for python rely on native components not currently supported in IronPython you could look at .NET libraries.

    What is available in .NET itself is Microsoft.VisualBasic and it's Financial class. It contains functions like NPV that seem to be similar to excel.

    A rough translation of the usage sample from MSDN would look like:

    import clr
    import System
    clr.AddReference("Microsoft.VisualBasic")
    from Microsoft.VisualBasic import Financial
    
    values = System.Array[float]([ -70000.0, 22000.0, 25000.0, 28000.0, 31000.0 ])
    fixedRetRate = 0.0625
    (netPVal, refValues) = Financial.NPV(fixedRetRate, values)
    print netPVal
    

    The result of 19312.57 seems to be consistent with what Excel does. Please note that the result needs to be unpacked as a tuple (e.g. (netPVal, refValues)) as the values parameter is passed as a reference and therefore (in IronPython) has to be returned back as well.