Internal Rate of Return Python

In python, the standard way to calculate internal rate of return is through the use of the Quantlib Library.

Quantlib

bond.bond_yield(price, dayCounter, compounding, frequency, settlementDate=Date(), accuracy=1.0e-8, maxEvaluations=100, guess=0.05)


  • price (float or ql.BondPrice): The target price of the bond. Can be a clean price or specified via ql.BondPrice(val, ql.BondPrice.Clean) / ql.BondPrice.Dirty.
  • dayCount / dc (ql.DayCounter): The day count convention (e.g., ql.Actual360(), ql.Thirty360(ql.Thirty360.BondBasis)).
  • compounding / comp (ql.Compounding): The compounding type (e.g., ql.Compounded, ql.Simple, ql.Continuous).
  • frequency / freq (ql.Frequency): The coupon frequency (e.g., ql.Annual, ql.Semiannual, ql.Quarterly).
  • settlementDate (ql.Date, optional): Specific date for settlement. Defaults to an empty Date(), which automatically uses the bond's default settlement date.
  • accuracy (float, optional): Precision tolerance for the internal root-finder (default 1.0e-8).
  • maxEvaluations (int, optional): Maximum number of iterations for the solver (default 100).
  • guess (float, optional): Initial starting yield guess for the numerical solver (default 0.05).

Davinci Quantlib API

The davinci Fixed income desktop provides a wrapper to the quantlib library in order to simplify the calculations.

The following code provides a calculation of the yield to maturity of the specified bond.
import valuation as vl import contracts as ct #specify the bond contract schedule = ct.Schedule(start_date='2000-01-01', end_date='2030-01-01', frequency= ct.Frequency.Annual, calendar= cl.Calendar.USGovernmentBond, convention=ct.Convention.Following, dateroll=ct.DateRoll.Forward) bond = ct.FixedRateBond(settlement_days=2,face_value=10000, day_count=ct.DayCount.Actual360, coupons=0.05,schedule=schedule) ytm = vl.yield_to_maturity(contract=bond, curve=treasury_curve1)