diff --git a/quantlib/time/daycounter.pyx b/quantlib/time/daycounter.pyx index 65b9584c6..12d498c4c 100644 --- a/quantlib/time/daycounter.pyx +++ b/quantlib/time/daycounter.pyx @@ -77,7 +77,7 @@ def _get_daycounter_type_from_name(name): return (name, None) -cdef _daycounter.DayCounter* daycounter_from_name(basestring name, basestring convention) except NULL: +cdef _daycounter.DayCounter* daycounter_from_name(str name, str convention) except NULL: """ Returns a new DayCounter pointer. The QuantLib DayCounter don't have a copy constructor or any other easy diff --git a/quantlib/time/daycounters/_actual_actual.pxd b/quantlib/time/daycounters/_actual_actual.pxd index 09ce13505..4c398a3d3 100644 --- a/quantlib/time/daycounters/_actual_actual.pxd +++ b/quantlib/time/daycounters/_actual_actual.pxd @@ -1,16 +1,9 @@ from quantlib.time._daycounter cimport DayCounter -cdef extern from 'ql/time/daycounters/actualactual.hpp' namespace 'QuantLib::ActualActual': - cdef enum Convention: - ISMA - Bond - ISDA - Historical - Actual365 - AFB - Euro -cdef extern from 'ql/time/daycounters/actualactual.hpp' namespace 'QuantLib': +cdef extern from 'ql/time/daycounters/actualactual.hpp' namespace 'QuantLib': cdef cppclass ActualActual(DayCounter): + enum Convention: + pass ActualActual(Convention) diff --git a/quantlib/time/daycounters/actual_actual.pxd b/quantlib/time/daycounters/actual_actual.pxd index 418af1397..79d5526ad 100644 --- a/quantlib/time/daycounters/actual_actual.pxd +++ b/quantlib/time/daycounters/actual_actual.pxd @@ -5,6 +5,14 @@ from quantlib.time.daycounter cimport DayCounter cdef class ActualActual(DayCounter): pass +cdef extern from 'ql/time/daycounters/actualactual.hpp' namespace 'QuantLib::ActualActual': + cpdef enum Convention: + ISMA + Bond + ISDA + Historical + Actual365 + AFB + Euro -cdef _daycounter.DayCounter* from_name(basestring convention) - +cdef _daycounter.DayCounter* from_name(str convention) diff --git a/quantlib/time/daycounters/actual_actual.pyx b/quantlib/time/daycounters/actual_actual.pyx index b710b2296..80766ca84 100644 --- a/quantlib/time/daycounters/actual_actual.pyx +++ b/quantlib/time/daycounters/actual_actual.pyx @@ -1,5 +1,3 @@ -# encoding: utf-8 - '''Actual/Actual day count The day count can be calculated according to: @@ -12,42 +10,19 @@ The day count can be calculated according to: - the AFB convention, also known as "Actual/Actual (Euro)". For more details, refer to -http://www.isda.org/publications/pdf/Day-Count-Fracation1999.pdf +https://www.isda.org/a/pIJEE/The-Actual-Actual-Day-Count-Fraction-1999.pdf ''' -from __future__ import unicode_literals - cimport quantlib.time._daycounter as _daycounter cimport quantlib.time.daycounters._actual_actual as _aa from quantlib.time.daycounter cimport DayCounter -cpdef enum Convention: - ISMA = _aa.ISMA - Bond = _aa.Bond - ISDA = _aa.ISDA - Historical = _aa.Historical - Actual365 = _aa.Actual365 - AFB = _aa.AFB - Euro = _aa.Euro - -CONVENTIONS = { - 'ISMA' : _aa.ISMA, - 'Bond' : _aa.Bond, - 'ISDA' : _aa.ISDA, - 'Historical' : _aa.Historical, - 'Actual365' : _aa.Actual365, - 'AFB' : _aa.AFB, - 'Euro' : _aa.Euro -} - - cdef class ActualActual(DayCounter): - _valid_names = [ - 'ACT/ACT({})'.format(convention) for convention in CONVENTIONS.keys() - ] - __doc__ = """ Actual/Actual day count - The day count can be calculated according to: + def __init__(self, Convention convention=Convention.ISDA): + """ Actual/Actual day count + + The day count can be calculated according to: - the ISDA convention, also known as "Actual/Actual (Historical)", "Actual/Actual", "Act/Act", and according to ISDA also "Actual/365", @@ -57,22 +32,13 @@ cdef class ActualActual(DayCounter): - the AFB convention, also known as "Actual/Actual (Euro)". For more details, refer to - http://www.isda.org/publications/pdf/Day-Count-Fracation1999.pdf - - Valid names for ACT/ACT daycounters are: \n {} - """.format('\n'.join(_valid_names)) - - def __init__(self, _aa.Convention convention=ISDA): + https://www.isda.org/a/pIJEE/The-Actual-Actual-Day-Count-Fraction-1999.pdf + """ self._thisptr = <_daycounter.DayCounter*> new \ _aa.ActualActual(convention) -cdef _daycounter.DayCounter* from_name(basestring convention): - - cdef _aa.Convention ql_convention = <_aa.Convention>CONVENTIONS[convention] - - cdef _daycounter.DayCounter* return_val = new _aa.ActualActual(ql_convention) - - return return_val - +cdef _daycounter.DayCounter* from_name(str convention): + cdef Convention ql_convention = Convention[convention] + return new _aa.ActualActual(ql_convention) diff --git a/quantlib/time/daycounters/thirty360.pxd b/quantlib/time/daycounters/thirty360.pxd index ad72da6dc..d3f099340 100644 --- a/quantlib/time/daycounters/thirty360.pxd +++ b/quantlib/time/daycounters/thirty360.pxd @@ -1,6 +1,5 @@ cimport quantlib.time._daycounter as _daycounter from quantlib.time.daycounter cimport DayCounter -from . cimport _thirty360 as _th cdef class Thirty360(DayCounter): pass diff --git a/quantlib/time/daycounters/thirty360.pyx b/quantlib/time/daycounters/thirty360.pyx index 9bf205460..b18fa7d9b 100644 --- a/quantlib/time/daycounters/thirty360.pyx +++ b/quantlib/time/daycounters/thirty360.pyx @@ -1,9 +1,6 @@ -from cython.operator import dereference as deref +from cython.operator cimport dereference as deref from quantlib.time.date cimport Date -cimport quantlib.time._daycounter as _daycounter from . cimport _thirty360 as _th -from .thirty360 cimport Convention as QlConvention -from quantlib.time.daycounter cimport DayCounter cdef class Thirty360(DayCounter): """30/360 day count convention @@ -63,12 +60,11 @@ cdef class Thirty360(DayCounter): * NASD """ - def __cinit__(self, convention=BondBasis, Date termination_date=Date()): - self._thisptr = new _th.Thirty360( convention, - deref(termination_date._thisptr)) + def __cinit__(self, Convention convention=Convention.BondBasis, Date termination_date=Date()): + self._thisptr = new _th.Thirty360(convention, deref(termination_date._thisptr)) cdef _daycounter.DayCounter* from_name(str convention) except NULL: try: - return new _th.Thirty360(Convention[convention]) + return new _th.Thirty360(Convention[convention]) except KeyError: raise ValueError("Unknown convention: {}".format(convention)) diff --git a/test/test_cython_bug.pyx b/test/test_cython_bug.pyx index 3bf8119d5..d71f0251d 100644 --- a/test/test_cython_bug.pyx +++ b/test/test_cython_bug.pyx @@ -16,7 +16,8 @@ from quantlib.time.calendars._target cimport TARGET from quantlib.time._schedule cimport Schedule from quantlib.time.dategeneration cimport DateGeneration from quantlib.time.date cimport date_from_qldate, Date -from quantlib.time.daycounters._actual_actual cimport ISMA, ActualActual +from quantlib.time.daycounters._actual_actual cimport ActualActual +from quantlib.time.daycounters.actual_actual cimport Convention from quantlib._settings cimport Settings @@ -82,7 +83,7 @@ cdef FixedRateBond* get_bond_for_evaluation_date(QlDate& in_date): face_amount, fixed_bond_schedule, coupons, - ActualActual(ISMA), + ActualActual(Convention.ISMA), Following, redemption, issue_date,