I'm having an issue with a Python program. I'm trying to generate cash flows using QuantLib, but I'm encountering a problem: my results are significantly different from those of Superderivatives or Bloomberg. Could you help me identify the error?
Thank you very much !
from QuantLib import *
import pandas as pd
import numpy as np
# Parameters
start_date = Date(27, 2, 2015)
end_date = Date(27, 2, 2045)
face_value = 1900000
frequency = Semiannual # semi-annual payments
spread_in_bp = -19.264
spread = spread_in_bp / 10000 # Convert from basis points to a decimal
calendar = UnitedKingdom()
schedule = Schedule(start_date, end_date, Period(frequency), calendar,
Following, Following, DateGeneration.Forward, False)
curve = pd.read_excel('USD_LIBOR_ZC-Curve.xlsx')
# Convertion
curve_dates = [Date(d) for d in curve['Date']]
curve_dates_numerical = [d.to_date().toordinal() for d in curve_dates] # we need numerical dates for interpolation
curve_rates = [r/100 for r in curve['Rate']] # Here we are assuming the rates are in percentage
# Create a zero curve and its handle
zero_curve = ZeroCurve(curve_dates, curve_rates, Actual360())
zero_curve_handle = YieldTermStructureHandle(zero_curve)
# 6-month LIBOR
reference_rate = USDLibor(Period(6, Months), zero_curve_handle)
# Add each interpolated fixing to the reference_rate
fixing_dates = [reference_rate.fixingDate(d) for d in schedule][:-1] # get the fixing dates from the schedule
fixing_dates_numerical = [d.to_date().toordinal() for d in fixing_dates] # we need numerical dates for interpolation
for fixing_date, fixing_date_num in zip(fixing_dates, fixing_dates_numerical):
fixing_rate = np.interp(fixing_date_num, curve_dates_numerical, curve_rates) # interpolate the rate
reference_rate.addFixing(fixing_date, fixing_rate)
bond = FloatingRateBond(settlementDays=0, faceAmount=face_value, schedule=schedule,
index=reference_rate, paymentDayCounter=Actual360(), spreads=[spread])
# Evaluation of the bond using a discounting bond engine
bond_engine = DiscountingBondEngine(zero_curve_handle)
bond.setPricingEngine(bond_engine)
for i, cashflow in enumerate(bond.cashflows()):
print(f"Cashflow {i + 1}: {cashflow.amount()} on {cashflow.date()}")
print(f"Bond's clean price: {bond.cleanPrice()}")
I tried to get the implied rate Rate but I don't really understand. I'm really confused...