IRR Calculation using Python Code

Python Code Snippet

Visitor : 27

Note : Assuming mutual fund investment is monthly.

 

def calculate_irr():

    zlist = ISIN_Portfolio_m.objects.filter(mf_type='SIP')
    for mf in zlist:
        for i in range(-99,99):
            zsiphis = MF_SIP_History_m.objects.filter(mf_ISIN_id = mf.mf_ISIN_id,purchdate = mf.sipdate)
            t = 0 ; zinflow = 0 ; znr0 = 0 ; zret = 0 ; zdr = 0
            for zsip in zsiphis:
                t = t + 1
                zt = Decimal(t/12) # monthly MF investment
                zdr = 1 + Decimal(i/100)
                zdr = zdr ** zt
                zinflow = zinflow + (Decimal(mf.sipamt)/Decimal(zdr)) 
                #print(zinflow,zdr)
            # Add returns to investment
            zret = Decimal(mf.current_value) / Decimal(zdr)
            znr =  zinflow - zret
            print("IRR",znr,i)
            if znr > 0 :
                print("MF",mf.mf_ISIN.mf_desc,"IRR",i-1)
                mf.irr_ret = i-1
                mf.save()
                break
            else :
                continue

 

Reference

Understanding IRR

https://erprealm.com/blog/understanding-irr-calculation-logic/