使用Quantlib时尝试定价仪器时出错

时间:2010-10-25 09:28:38

标签: c# c++ swig quantlib

我在尝试从自举曲线定价20x10交换时收到以下错误。错误将在ImpliedRate函数

的最后一行抛出
  

SwapRatesServiceTests.ImpliedRate_ForTwenty_x_TenYearSwap_ReturnsRate:   System.ApplicationException:2nd leg:empty句柄无法解除引用

我不知道从哪里开始调试此问题。任何帮助将受到高度赞赏。

重要提示:我使用的是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 ++代码有关。希望这种曝光可以揭示对这里可能发生的事情的一些见解。

1 个答案:

答案 0 :(得分:0)

根据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);