尝试从自举曲线定价20x10互换时,我收到以下错误。错误被抛出在ImpliedRate
函数的最后一行
SwapRatesServiceTests.ImpliedRate_ForTwenty_x_TenYearSwap_ReturnsRate:
System.ApplicationException:第二条腿:空不能取消引用句柄
我不知道从哪里开始调试此问题。任何帮助将不胜感激。
重要说明:我正在使用Quantlib的C#Swig版本,因此基于swapvaluation.cpp示例,我的实际产品代码如下:
测试方法:
[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
}
这是较大的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
使用如下的ImpliedRate方法(由于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
}
我希望我的术语是正确的,因为金融术语对我来说还是新的。
编辑:我添加了C ++标记,因为我认为实际上与某些底层C ++代码有关。希望这次接触可以揭示一些有关这里可能发生的情况的见解。
最佳答案
根据Quantlib mailing list的反馈
Jibar指数需要参考创建的无风险曲线。没有期限结构,Jibar可以返回过去的定价,但不能预测未来的定价。 Jibar构造函数需要替换为
new Jibar(new Period(Frequency.Quarterly), DiscountingTermStructure)
与
VanillaSwap impliedSwap = new VanillaSwap(
_VanillaSwap.Type.Payer,
10000000.0,
fixedSchedule,
0.1,
Actual365FixedDayCounter,
floatSchedule,
new Jibar(new Period(Frequency.Quarterly), DiscountingTermStructure),
0,
Actual365FixedDayCounter);
关于c# - 使用Quantlib尝试对工具定价时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4013277/