I’m running a simulation in which I want to calculate the NPV of 100 swaps over 1000 (or even much more) different interest rate curves.
It looks like Quantlib is not really fast in performing these calculations or my code is just not optimal.
Script does the following (for all curves):
- Create Quantlib curve objects (based on these simulated interest rate curves)
- Create swap object per swap
- Set swappriceEngine per swap object
- Calculate NPV per swap
Code example for every swap over all simulated interest rate curves:
fixed_schedule = ql.Schedule(settle_date, maturity_date,
fixed_leg_tenor, calendar,
ql.ModifiedFollowing, ql.ModifiedFollowing,
ql.DateGeneration.Forward, False
float_schedule = ql.Schedule (settle_date, maturity_date,
float_leg_tenor, calendar,
ql.ModifiedFollowing, ql.ModifiedFollowing,
ql.DateGeneration.Forward, False
swap = ql.VanillaSwap(ql.VanillaSwap.Payer, notional, fixed_schedule,
fixed_rate, fixed_leg_daycount, float_schedule,
6M_index, float_spread, float_leg_daycount)
swap.setPricingEngine(DiscountingSwapEngine(YieldTermStructureHandle(discount_curve)))
swap.NPV()
I’m wondering if it is possible to create only 100 different swap objects and just update the interest rate curves needed to calculate the NPV instead of creating swap objects in every loop. So only update the 6M_index in swap object and update discountcurve in swap.setPricingEngine.
Is that possible?