I’m using python quantlib to price a swap, but the NPV and cashflow result differently from BBG results as all the input controls the same, fixed schedule should be the same but floating cashflow is not. I have no idea abount what’s wrong with it, can help figure out where I should modify?
code NPV result: -28451.35 BBG NPV result: -28449.30
code cashflow and BBG cashflows are:enter image description hereenter image description here
input and BBG NPV: enter image description here
curve data: enter image description here
Below is my python code.
import pandas as pd
import QuantLib as ql
calculation_date = ql.Date(22, 7, 2024)
ql.Settings.instance().evaluationDate = calculation_date
yts = ql.RelinkableYieldTermStructureHandle()
index = ql.OvernightIndex("USD Overnight Index", 0, ql.USDCurrency(), ql.UnitedStates(ql.UnitedStates.Settlement),
ql.Actual360(), yts)
swaps = {
ql.Period("1W"): 5.34063/100,
ql.Period("2W"): 5.34045/100,
ql.Period("3W"): 5.34350/100,
ql.Period("1M"): 5.34750/100,
ql.Period("2M"): 5.34040/100,
ql.Period("3M"): 5.28600/100,
ql.Period("4M"): 5.24215/100,
ql.Period("5M"): 5.20170/100,
ql.Period("6M"): 5.14835/100,
ql.Period("7M"): 5.09080/100,
ql.Period("8M"): 5.04535/100,
ql.Period("9M"): 4.98930/100,
ql.Period("10M"): 4.93130/100,
ql.Period("11M"): 4.88620/100,
ql.Period("12M"): 4.83670/100,
ql.Period("18M"): 4.51801/100,
ql.Period("2Y"): 4.33160/100,
ql.Period("3Y"): 4.08150/100,
ql.Period("4Y"): 3.94900/100,
ql.Period("5Y"): 3.87846/100,
ql.Period("6Y"): 3.84260/100,
ql.Period("7Y"): 3.82400/100,
ql.Period("8Y"): 3.81550/100,
ql.Period("9Y"): 3.81350/100,
ql.Period("10Y"): 3.81640/100,
ql.Period("12Y"): 3.83020/100,
ql.Period("15Y"): 3.84900/100,
ql.Period("20Y"): 3.83529/100,
ql.Period("25Y"): 3.75940/100,
ql.Period("30Y"): 3.67355/100,
ql.Period("40Y"): 3.47606/100,
ql.Period("50Y"): 3.26680/100
}
tenors = list(swaps.keys())
quotes = [ql.SimpleQuote(swaps[k]) for k in tenors]
handles = [ql.QuoteHandle(quote) for quote in quotes]
rate_helpers = []
for quote, tenor in zip(handles, tenors):
helper = ql.OISRateHelper(2, tenor, quote, index)
rate_helpers.append(helper)
curve = ql.PiecewiseFlatForward(calculation_date, rate_helpers, ql.Actual360())
yts.linkTo(curve)
swap = ql.MakeOIS(ql.Period('10Y'),
index, 0.03736141,
ql.Period('0D'),
swapType=-1,
nominal=10000000,
effectiveDate=ql.Date(24, 10, 2024),
paymentLag=2)
# Set the pricing engine
engine = ql.DiscountingSwapEngine(yts)
swap.setPricingEngine(engine)
# Calculate the original NPV
npv = swap.NPV()
print(npv)
cashflows = pd.DataFrame({
'nominal': cf.nominal(),
'accrualStartDate': cf.accrualStartDate().ISO(),
'accrualEndDate': cf.accrualEndDate().ISO(),
'rate': cf.rate(),
'amount': cf.amount()
} for cf in map(ql.as_coupon, swap.leg(1)))
print(cashflows)
Hanqing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.