Python 量化库错误 : DayCounter_yearFraction

标签 python quantlib

我得到了这段代码,它是使用 QuantLib 在 Python 3 中编写的。它应该按原样工作,但我无法让它工作。下面是重现错误的极简示例。有人可以看一下吗。

import QuantLib as ql
class myDate(ql.Date):
    origin = None
    year_frac = DayCount.ACT360.yearFraction

    @classmethod
    def from_timestamp(cls, date):
        return cls(date.day, date.month, date.year) 

    def __init__(self, *args):
        super().__init__(*args)
    self.t = self.year_frac(self.origin, self) 

当我这样做时:

x1 = pandas.Timestamp('2015-01-13 00:00:00')
myDate.from_timestamp(x1)

它抛出:“ValueError:方法 'DayCounter_yearFraction' 中的无效空引用,'Date const &' 类型的参数 2”在 Quantlib 代码中突出显示下面的第二行。

def yearFraction(self, *args) -> "Time":    
    return _QuantLib.DayCounter_yearFraction(self, *args) 

我正在为 QuantLib 使用 QuantLib_Python-1.6.1-cp35-none-win_amd64.whl。

最佳答案

Python 的 QuantLib 无法直接识别日期格式。

相反,您应该使用以下代码来声明日期:

myDate = DateParser.parseFormatted('2019-01-01','%Y-%m-%d')

然后您可以将myDate 传递给QuantLib 函数。

关于Python 量化库错误 : DayCounter_yearFraction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40224737/

相关文章:

python - 用字符串列表替换字符串元素,使用它们的索引

python - 不同长度的滚动窗口的总和

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

quantlib - 为什么QuantLib引入Handle类?

c++ - Octave 的 QuantLib 绑定(bind)​​?

C++ Quantlib 掉期付款日期

python - 在满足某些条件的 numpy 数组中查找长度 N 的范围

python - 在字典中查找符合条件的值

python - 在Python中编写一个保存和返回值的函数

c++ - QuantLib:需要 Garch 帮助