are there any existing python modules that can calculate Modified and/or Macauley Duration of a bond.
I calculate duration in Python using numpy, it's nice and simple:
def durations(cfs, rates, price, ytm, no_coupons): import numpy as np mac_dur = np.sum([cfs[i]*（i+1）/np.power(1+rates[i],i+1) for i in range(len(cfs))])/price mod_dur = mac_dur/(1+ytm/no_coupons) return mac_dur, mod_dur
Go talk to Fincad. Here is their page on integrating with scripting languages:
Their analytics libraries include bond analytics, and they have a spreadsheet product so you can test methods and results before implementing them.
Disclaimer: I work for a company who is a customer of Fincad's analytics.
You can use my script:
def Duration (timetomaturity,nominalvalue,yieldrate,couponrate):
import math as m
B=0 # B is the bond's present value
for k in range(0,timetomaturity):
D=0 # D is the duration
for i in range(0,timetomaturity):
#By Tural Valiyev