代码之家  ›  专栏  ›  技术社区  ›  Ahmad

使用quantlib时尝试为工具定价时出错

  •  1
  • Ahmad  · 技术社区  · 14 年前

    我在尝试从引导曲线为20x10交换定价时收到以下错误。在 ImpliedRate 功能

    swapratesServiceTests.impliedrate_fortwenty_x_tenyearswap_returns日期: System.ApplicationException:第2段:不能取消引用空句柄

    我不知道从哪里开始调试这个问题。如有任何帮助,我们将不胜感激。

    重要提示:我使用的是Quantlib的C_Swig版本,因此基于swapValue.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
    
        }
    

    这是更大的构造交换点方法的一部分

            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 回复  |  直到 14 年前
        1
  •  0
  •   Ahmad    14 年前

    基于 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);
    
    推荐文章