I am trying to evaluate the present value of some cashflows and QuantLib does not return the discount factors that I am expecting.
I have a Risk Free (Zero Coupon Bond) Yield curve:
import QuantLib as ql
dates = [Date(1,12,2022), Date(2,12,2022), Date(1,1,2023), Date(1,2,2023), Date(1,3,2023), Date(1,4,2023), Date(1,5,2023), Date(1,6,2023)]
rates = [0.0, 0.0059, 0.0112, 0.0160, 0.0208, 0.0223, 0.0239, 0.0254]
So I create a QuantLib ZeroCurve:
discount_curve_day_count = ql.ActualActual(ql.ActualActual.ISDA)
discount_curve_compounding_frequency = ql.Annual
discount_curve_compounding_type = ql.Compounded
calendar = ql.NullCalendar()
zero_curve = ql.ZeroCurve(dates,rates, discount_curve_day_count,calendar, ql.Linear(),discount_curve_compounding_type,discount_curve_compounding_frequency)
I define the Leg of Cashflows:
cf_dates = [Date(18,1,2023), Date(18,2,2023), Date(18,3,2023), Date(18,4,2023), Date(18,5,2023), Date(18,6,2023), Date(18,7,2023), Date(18,8,2023), Date(18,9,2023), Date(18,10,2023), Date(18,11,2023), Date(18,12,2023), Date(18,1,2024), Date(18,2,2024), Date(18,3,2024), Date(18,4,2024), Date(18,5,2024), Date(18,6,2024), Date(18,7,2024), Date(18,8,2024), Date(18,9,2024), Date(18,10,2024), Date(18,11,2024), Date(18,12,2024), Date(18,1,2025)]
cf_amounts = [-30000.0, 203.84, 184.11, 203.84, 634.37, 634.37, 634.37, 634.37, 634.37, 634.37, 634.37, 634.37, 634.37, 634.37, 634.37, 634.37, 634.37, 634.37, 634.37, 634.37, 634.37, 634.37,634.37, 634.37, 634.37]
cf= []
for i in range(len(cf_dates)):
cashflow = ql.SimpleCashFlow(cf_dates[i], cf_amounts[i])
cf.append(cashflow)
leg = ql.Leg(cf)
And I want to evaluate the discount factors at the cashflow dates:
for cf in leg:
print(cf.date(), zero_curve.discount(cf.date()))
Unfortunately, the values I get are slightly off (error=0.02) from the expected ones, calculated using the following compounding formula:
$$ d = \frac{1}{(1+r)^y \left( 1+r\frac{d_p}{d_y} \right)} $$
where $r$ is the linearly interpolated rate from the curve, $y$ is the number of years that have passed from the first cashflow date, $d_p$ is the number of days that have passed from the previous payment and $d_y$ the number of days in the year in which the payment occurs (all of these are calculated using Act/Act ISDA daycount convention).
Any chance I can get those numbers right using QuantLib?