Error when trying to price instrument when using Quantlib
I am receiving the following error when trying to price a 20x10 swap from a bootstrapped curve. The error get thrown on the last line of the ImpliedRate
function
SwapRatesServiceTests.ImpliedRate_ForTwenty_x_TenYearSwap_ReturnsRate: System.ApplicationException : 2nd leg: empty Handle cannot be dereferenced
I don't have the faintest idea where to start to debug this issue. Any assistance will be highly appreciated.
IMPORTANT: I am using the C# Swig version of Quantlib, so my actual prod code is as follows based on the swapvaluation.cpp example:
The test method:
开发者_运维百科[Test]
public void ImpliedRate_ForTwenty_x_TenYearSwap_ReturnsRate()
{
//Arrange
var startingDate = new Date(10,Month.October,2030); // starting date of 20x10yr swap
var length= 10;
repo.Setup(r => r.Read(It.IsAny<string>())).Returns(LoadSwapPoints()); // LoadSwapPoints returns IEnumerable<RateHelpers>
//Act
service.ConstructSwapPoints(SettlementDate);
var instrumentRate = service.ImpliedRate(startingDate, length);
//Assert
Assert.That(instrumentRate, Is.Not.Null); // this must change to a value test
}
This is part of the larger ConstructSwapPoints method
var depoFRASwapInstruments = PointVector; // RateHelperVector populated with RateHelpers
DayCounter termStructureDayCounter = new ActualActual(ActualActual.Convention.Actual365);
QuoteHandleVector quotes = new QuoteHandleVector();
DateVector quoteDates = new DateVector();
py = CreatePiecewiseLinearCurve(settlementDate, depoFRASwapInstruments, termStructureDayCounter, quotes, quoteDates);
DiscountingTermStructure = new RelinkableYieldTermStructureHandle(py); //RelinkableYieldTermStructureHandle
//DiscountingTermStructure.linkTo(py); // alternate way
PricingEngine = new DiscountingSwapEngine(DiscountingTermStructure); // DiscountingSwapEngine
With the ImpliedRate method as follows (i have snipped some parts out due to IP restrictions);
public double ImpliedRate(Date startingDate, int length)
{
var swapMaturityDate = startingDate.Add(new Period(length, TimeUnit.Years));
var curveMaturityDate = py.maxDate();
Schedule fixedSchedule = new Schedule(startingDate, swapMaturityDate, new Period(Frequency.Quarterly), SouthAfricanCalender, Convention, Convention, DateGeneration.Rule.Forward, false);
Schedule floatSchedule = new Schedule(startingDate, swapMaturityDate, new Period(Frequency.Quarterly), SouthAfricanCalender, Convention, Convention, DateGeneration.Rule.Forward, false);
VanillaSwap impliedSwap = new VanillaSwap(
_VanillaSwap.Type.Payer,
10000000.0,
fixedSchedule,
0.1,
Actual365FixedDayCounter,
floatSchedule,
new Jibar(new Period(Frequency.Quarterly)),
0,
Actual365FixedDayCounter);
impliedSwap.setPricingEngine(PricingEngine);
return impliedSwap.fairRate(); // <---exception thrown here
}
I hope my terminology is correct as the finance jargon is still new to me.
Edit: I have added the C++ tag, since I figure is actually related to some underlying C++ code. Hoping that this exposure may reveal some insights into what may be happening here.
Based on feedback from the Quantlib mailing list
The Jibar index needs to have a reference to the risk free curve created. Without a term structure, the Jibar can return past fixings but not forecast future ones. The Jibar constructor needs to be replaced with
new Jibar(new Period(Frequency.Quarterly), DiscountingTermStructure)
with
VanillaSwap impliedSwap = new VanillaSwap(
_VanillaSwap.Type.Payer,
10000000.0,
fixedSchedule,
0.1,
Actual365FixedDayCounter,
floatSchedule,
new Jibar(new Period(Frequency.Quarterly), DiscountingTermStructure),
0,
Actual365FixedDayCounter);
精彩评论