Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daycounter cleanup #315

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion quantlib/time/daycounter.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 3 additions & 10 deletions quantlib/time/daycounters/_actual_actual.pxd
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 10 additions & 2 deletions quantlib/time/daycounters/actual_actual.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
54 changes: 10 additions & 44 deletions quantlib/time/daycounters/actual_actual.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: utf-8

'''Actual/Actual day count

The day count can be calculated according to:
Expand All @@ -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",
Expand All @@ -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)
1 change: 0 additions & 1 deletion quantlib/time/daycounters/thirty360.pxd
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 4 additions & 8 deletions quantlib/time/daycounters/thirty360.pyx
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -63,12 +60,11 @@ cdef class Thirty360(DayCounter):
* NASD
"""

def __cinit__(self, convention=BondBasis, Date termination_date=Date()):
self._thisptr = new _th.Thirty360(<QlConvention> 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(<QlConvention>Convention[convention])
return new _th.Thirty360(Convention[convention])
except KeyError:
raise ValueError("Unknown convention: {}".format(convention))
5 changes: 3 additions & 2 deletions test/test_cython_bug.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
Loading