Skip to content

Commit

Permalink
closer to upstream constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasibule committed Feb 27, 2024
1 parent cdded9f commit 7246e3a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 36 deletions.
4 changes: 4 additions & 0 deletions quantlib/time/calendars/null_calendar.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from quantlib.time.calendar cimport Calendar

cdef class NullCalendar(Calendar):
pass
7 changes: 2 additions & 5 deletions quantlib/time/calendars/null_calendar.pyx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
cimport quantlib.time.calendars._null_calendar as _nc
from quantlib.time.calendar cimport Calendar


cdef class NullCalendar(Calendar):
'''Calendar for reproducing theoretical calculations.
This calendar has no holidays. It ensures that dates at whole-month
This calendar has no holidays. It ensures that dates at whole-month
distances have the same day of month.
'''

def __cinit__(self):
self._thisptr = _nc.NullCalendar()

69 changes: 38 additions & 31 deletions quantlib/time/schedule.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ cimport cython
import numpy as np
cimport numpy as np
np.import_array()
from .businessdayconvention cimport Following, BusinessDayConvention
from .businessdayconvention cimport Following, Unadjusted, BusinessDayConvention
from .dategeneration cimport DateGeneration

from .calendar cimport Calendar
from .calendars.null_calendar cimport NullCalendar
from .date cimport date_from_qldate, Date, Period

import warnings
Expand All @@ -25,51 +26,57 @@ cdef class Schedule:
BusinessDayConvention business_day_convention=Following,
BusinessDayConvention termination_date_convention=Following,
DateGeneration date_generation_rule=DateGeneration.Forward, bool end_of_month=False,
from_classmethod=False
):

if not from_classmethod:
warnings.warn("Deprecated: use class method from_rule instead",
DeprecationWarning)

self._thisptr = new _schedule.Schedule(
deref(effective_date._thisptr),
deref(termination_date._thisptr),
deref(tenor._thisptr),
calendar._thisptr,
business_day_convention,
termination_date_convention,
date_generation_rule, end_of_month,
_date.Date(), _date.Date()
)
else:
pass
warnings.warn("Deprecated: use class method from_rule instead",
DeprecationWarning)

self._thisptr = new _schedule.Schedule(
deref(effective_date._thisptr),
deref(termination_date._thisptr),
deref(tenor._thisptr),
calendar._thisptr,
business_day_convention,
termination_date_convention,
date_generation_rule, end_of_month,
_date.Date(), _date.Date()
)

@classmethod
def from_dates(cls, dates, Calendar calendar not None,
BusinessDayConvention business_day_convention=Following,
BusinessDayConvention termination_date_convention=Following,
def from_dates(cls, dates, Calendar calendar=NullCalendar(),
BusinessDayConvention business_day_convention=Unadjusted,
termination_date_convention=None,
Period tenor=None,
DateGeneration date_generation_rule=DateGeneration.Forward, bool end_of_month=False,
rule=None,
end_of_month=None,
vector[bool] is_regular=[]):
# convert lists to vectors
cdef vector[_date.Date] _dates = vector[_date.Date]()
cdef vector[_date.Date] _dates
cdef Date date
for date in dates:
_dates.push_back(deref((<Date>date)._thisptr))
_dates.push_back(deref(date._thisptr))

cdef Schedule instance = cls.__new__(cls)
cdef Schedule instance = Schedule.__new__(Schedule)
cdef optional[BusinessDayConvention] opt_termination_convention
cdef optional[_calendar.Period] opt_tenor
cdef optional[DateGeneration] opt_rule
cdef optional[bool] opt_end_of_month
if tenor is not None:
opt_tenor = deref(tenor._thisptr)
cdef optional[BusinessDayConvention] opt_termination_convention = termination_date_convention
if termination_date_convention is not None:
opt_termination_convention = <BusinessDayConvention>termination_date_convention
if rule is not None:
opt_rule = <DateGeneration>rule
if end_of_month is not None:
opt_end_of_month = <bool>end_of_month
instance._thisptr = new _schedule.Schedule(
_dates,
calendar._thisptr,
business_day_convention,
opt_termination_convention,
opt_tenor,
optional[DateGeneration](date_generation_rule),
optional[bool](end_of_month),
opt_rule,
opt_end_of_month,
is_regular
)

Expand All @@ -81,18 +88,18 @@ cdef class Schedule:
Period tenor not None, Calendar calendar not None,
BusinessDayConvention business_day_convention=Following,
BusinessDayConvention termination_date_convention=Following,
DateGeneration date_generation_rule=DateGeneration.Forward, bool end_of_month=False,
DateGeneration rule=DateGeneration.Forward, bool end_of_month=False,
Date first_date=Date(), Date next_to_lastdate=Date()):

cdef Schedule instance = cls.__new__(cls)
cdef Schedule instance = Schedule.__new__(Schedule)
instance._thisptr = new _schedule.Schedule(
deref(effective_date._thisptr),
deref(termination_date._thisptr),
deref(tenor._thisptr),
calendar._thisptr,
business_day_convention,
termination_date_convention,
date_generation_rule, end_of_month,
rule, end_of_month,
deref(first_date._thisptr), deref(next_to_lastdate._thisptr)
)
return instance
Expand Down

0 comments on commit 7246e3a

Please sign in to comment.