2

I've been given this code, written in Python 3 using QuantLib. It is supposed to work as it is but I cant get it to work. Below is a minimalist example to reproduce the error. Could someone please take a look.

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) 

When I do this:

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

It throws : "ValueError: invalid null reference in method 'DayCounter_yearFraction', argument 2 of type 'Date const &'" highlighting the second line below in Quantlib code.

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

I am using QuantLib_Python-1.6.1-cp35-none-win_amd64.whl for QuantLib.

user2696565
  • 587
  • 1
  • 8
  • 17
  • 2
    The example is not self-contained. It's missing the definition of DayCount.ACT360, which is probably aliasing something in the QuantLib module. – Luigi Ballabio Oct 25 '16 at 10:40

1 Answers1

1

Python's QuantLib cannot directly recognize a date format.

Instead, you should use the following code to declare the date:

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

then you can pass myDate to the QuantLib function.

jyalim
  • 3,289
  • 1
  • 15
  • 22
Haohan Li
  • 51
  • 3