c# - 使用 Quantlib 时尝试为仪器定价时出错

标签 c# c++ swig quantlib

尝试从自举曲线为 20x10 隔夜利息定价时,我收到以下错误。错误在 ImpliedRate 函数的最后一行抛出

SwapRatesServiceTests.ImpliedRate_ForTwenty_x_TenYearSwap_ReturnsRate: System.ApplicationException : 2nd leg: empty Handle cannot be dereferenced

我不知道从哪里开始调试这个问题。非常感谢任何帮助。

重要提示:我使用的是 C# Swig 版本的 Quantlib,因此我的实际产品代码基于 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 可以返回过去的定盘价但不能预测 future 的定盘价。 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/

相关文章:

c# - 将代码添加到 SWIG 中自动生成的类

c# - 将 ASP.Net 控件 ID 发送到 JavaScript 函数

c# - 计算枚举上设置的标志数

java - 使用 x86_64-w64-mingw32-g++ 编译 JNI C++ native 代码

c++ - "could not match ' T [S] ' against ' std::vector<int> '"对于某些函数但不是其他具有相同参数列表的函数

c++ - CMake 无法识别混合 C++/FORTRAN 程序中的 FORTRAN 源代码

python - 使用 Swig 将 numpy 字符串数组传递给 C

c++ - Swig C++ : Interfacing vector<Class object *>

c# - 如何使 'click event' 成为用户控件在 visual studio 中的默认操作

c# - Visual Studio 2010 安装项目中的 "Runtime Implementation from ASSEMBLE NAME"是什么? (vs 2015 中的安装程序扩展设置项目)