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

发布于 2024-09-29 06:26:31 字数 3043 浏览 1 评论 0原文

当我尝试从自举曲线对 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++ 代码相关。希望这次曝光能够揭示一些对这里可能发生的事情的见解。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫九 2024-10-06 06:26:31

基于 Quantlib 邮件列表<的反馈/a>

Jibar 指数需要引用所创建的无风险曲线。如果没有期限结构,Jibar 可以返回过去的定价,但无法预测未来的定价。 Jibar 构造函数需要替换为

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文