Erro ao tentar precificar o instrumento ao usar o Quantlib
Pergunta
Estou recebendo o seguinte erro ao tentar precificar uma troca de 20x10 de uma curva inicializada. O erro é jogado na última linha do ImpliedRate
função
SwapratateServiceTests.impliedrate_fortwenty_x_tenyearswap_returnrate: System.ApplicationException: 2ª perna: a alça vazia não pode ser desreferenciada
Não tenho a idéia mais fraca onde começar a depurar esse problema. Qualquer assistência será muito apreciada.
IMPORTANTE: Estou usando a versão C# Swig do Quantlib, então meu código de produto real é o seguinte com base no exemplo de swapvaluation.cpp:
O método de teste:
[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
}
Isso faz parte do método maior do ConstructSwappoints
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
Com o método implicado da seguinte forma (eu retire algumas peças devido a restrições de IP);
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
}
Espero que minha terminologia esteja correta, pois o jargão financeiro ainda é novo para mim.
EDIT: Eu adicionei a tag C ++, pois acho que está realmente relacionado a algum código C ++ subjacente. Esperando que essa exposição possa revelar algumas idéias sobre o que pode estar acontecendo aqui.
Solução
Com base no feedback do Lista de discussão Quantlib
O índice Jibar precisa ter uma referência à curva livre de risco criada. Sem uma estrutura de termo, o Jibar pode devolver as fixações anteriores, mas não prevê as futuras. O construtor jibar precisa ser substituído por
new Jibar(new Period(Frequency.Quarterly), DiscountingTermStructure)
com
VanillaSwap impliedSwap = new VanillaSwap(
_VanillaSwap.Type.Payer,
10000000.0,
fixedSchedule,
0.1,
Actual365FixedDayCounter,
floatSchedule,
new Jibar(new Period(Frequency.Quarterly), DiscountingTermStructure),
0,
Actual365FixedDayCounter);