As mentioned in the title, i'm having trouble with pricing USD swaps in quantlib.
I wanted to take some inputs (Prices of swaps in the market) and be able to construct a yield term structure that allows me to price say a 1y1y swap. However, it seems that that when constructing a yield curve and then pricing a swap, my prices do not line with with the exact instruments im using and im wondering if how i can reconcile the two such that the model outputs == inputs for the tenors below
import QuantLib as ql
today = ql.Date().todaysDate()
calendar = ql.UnitedStates()
spot = calendar.advance(today, 2, ql.Days)
helpers = ql.RateHelperVector()
index = ql.IborIndex('MyIndex',
ql.Period('3m'),
2,
ql.USDCurrency(),
ql.UnitedStates(),
ql.ModifiedFollowing,
True,
ql.Thirty360())
helpers.append( ql.DepositRateHelper(0.01, index) )
swaps = [
('1Y', 0.015),
('2Y', 0.02),
('3Y', 0.025)
]
for tenor, rate in swaps:
swapIndex = ql.UsdLiborSwapIsdaFixAm(ql.Period(tenor))
helpers.append( ql.SwapRateHelper(rate, swapIndex) )
curve = ql.PiecewiseSplineCubicDiscount(spot, helpers, ql.Thirty360())
yts = ql.YieldTermStructureHandle(curve)
engine = ql.DiscountingSwapEngine(yts)
index = ql.IborIndex('MyIndex',
ql.Period('3m'),
2,
ql.USDCurrency(),
ql.UnitedStates(),
ql.ModifiedFollowing,
True,
ql.Thirty360(),
yts)
print("maturity, market, model")
for tenor, rate in swaps:
swap = ql.MakeVanillaSwap(ql.Period(tenor), index, 0.01, ql.Period('0D'), pricingEngine=engine)
print(f"{tenor}, {rate:.6f}, {swap.fairRate():.6f}")
maturity | market | model |
---|---|---|
1Y | 0.015000 | 0.014831 |
2Y | 0.020000 | 0.019809 |
3Y | 0.025000 | 0.024755 |