Skip to content

Commit

Permalink
clean up frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasibule committed Feb 19, 2024
1 parent 69b658b commit cdded9f
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 93 deletions.
4 changes: 2 additions & 2 deletions quantlib/indexes/inflation/ukrpi.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ from libcpp cimport bool
from quantlib.currency.api import GBPCurrency
from quantlib.indexes.regions import UKRegion
from quantlib.time.date cimport Period
from quantlib.time.date import Monthly, Months

from quantlib.time.date import Months
from quantlib.time.frequency cimport Monthly
from quantlib.indexes.inflation_index cimport ZeroInflationIndex
from quantlib.termstructures.inflation_term_structure \
cimport ZeroInflationTermStructure
Expand Down
2 changes: 1 addition & 1 deletion quantlib/indexes/inflation_index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ from libcpp.string cimport string

from quantlib.index cimport Index
from quantlib.time.date cimport Period, period_from_qlperiod
from quantlib.time._period cimport Frequency
from quantlib.time.frequency cimport Frequency
from quantlib.indexes.region cimport Region

from quantlib.currency.currency cimport Currency
Expand Down
12 changes: 4 additions & 8 deletions quantlib/mlab/fixed_income.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from quantlib.time.calendars.null_calendar import NullCalendar
from quantlib.time.calendars.target import TARGET
from quantlib.time.date import (
Date, Days, Period, Years, str_to_frequency)
Date, Days, Period, Years)

from quantlib.time.schedule import Schedule
from quantlib.time.dategeneration import DateGeneration
Expand Down Expand Up @@ -88,8 +88,6 @@ def _bndprice(bond_yield, coupon_rate, pricing_date, maturity_date,
Clean price and accrued interest of a bond
"""

_period = str_to_frequency(period)

evaluation_date = pydate_to_qldate(pricing_date)

settings = Settings()
Expand All @@ -115,7 +113,7 @@ def _bndprice(bond_yield, coupon_rate, pricing_date, maturity_date,
fixed_bond_schedule = Schedule.from_rule(
effective_date,
termination_date,
Period(_period),
Period(period),
calendar,
ModifiedFollowing,
ModifiedFollowing,
Expand Down Expand Up @@ -147,7 +145,7 @@ def _bndprice(bond_yield, coupon_rate, pricing_date, maturity_date,
calendar=NullCalendar(),
daycounter=cnt_yield,
compounding=Compounded,
frequency=_period)
frequency=period)

discounting_term_structure.link_to(flat_term_structure)

Expand Down Expand Up @@ -201,8 +199,6 @@ def _cfamounts(coupon_rate, pricing_date, maturity_date,
cash flow schedule
"""

_period = str_to_frequency(period)

evaluation_date = pydate_to_qldate(pricing_date)

settings = Settings()
Expand All @@ -225,7 +221,7 @@ def _cfamounts(coupon_rate, pricing_date, maturity_date,
fixed_bond_schedule = Schedule.from_rule(
effective_date,
termination_date,
Period(_period),
Period(period),
calendar,
ModifiedFollowing,
ModifiedFollowing,
Expand Down
5 changes: 2 additions & 3 deletions quantlib/termstructures/yield_term_structure.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ from cython.operator cimport dereference as deref
from libcpp cimport bool
from libcpp.vector cimport vector

from quantlib.time._period cimport Frequency
from quantlib.time.frequency cimport Frequency, Annual
from quantlib.time.calendar cimport Calendar
from quantlib.time.daycounter cimport DayCounter
from quantlib.time.date cimport Date, date_from_qldate, Period

from quantlib.compounding cimport Compounding
from quantlib.time.date import Annual

cimport quantlib.termstructures._yield_term_structure as _yts
cimport quantlib._quote as _qt
Expand Down Expand Up @@ -56,7 +55,7 @@ cdef class YieldTermStructure(Observable):
self.as_ptr().disableExtrapolation()

def zero_rate(self, d, DayCounter day_counter=None,
Compounding compounding=Compounding.Continuous, int frequency=Annual,
Compounding compounding=Compounding.Continuous, Frequency frequency=Annual,
bool extrapolate=False):
""" Returns the implied zero-yield rate for the given date.
Expand Down
13 changes: 6 additions & 7 deletions quantlib/termstructures/yields/flat_forward.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
include '../../types.pxi'
from cython.operator cimport dereference as deref

from quantlib.time._period cimport Frequency
from quantlib.time.frequency cimport Frequency, Annual
from quantlib.time.calendar cimport Calendar
from quantlib.time.daycounter cimport DayCounter
from quantlib.time.date cimport Date, date_from_qldate

from quantlib.compounding cimport Compounding
from quantlib.time.date import Annual

from quantlib.handle cimport shared_ptr, RelinkableHandle, Handle
from . cimport _flat_forward as ffwd
Expand Down Expand Up @@ -52,7 +51,7 @@ cdef class FlatForward(YieldTermStructure):
DayCounter daycounter=None,
int settlement_days=0, Calendar calendar=None,
Compounding compounding=Compounding.Continuous,
frequency=Annual):
Frequency frequency=Annual):

#local cdef's
cdef shared_ptr[ffwd.YieldTermStructure] _forward
Expand All @@ -70,15 +69,15 @@ cdef class FlatForward(YieldTermStructure):
(<Quote>forward).handle(),
deref(daycounter._thisptr),
compounding,
<Frequency>frequency
frequency
))
else:
_forward = shared_ptr[ffwd.YieldTermStructure](new ffwd.FlatForward(
deref(reference_date._thisptr),
<ffwd.Rate>forward,
deref(daycounter._thisptr),
compounding,
<Frequency>frequency
frequency
))
elif settlement_days is not None and \
calendar is not None:
Expand All @@ -90,7 +89,7 @@ cdef class FlatForward(YieldTermStructure):
(<Quote>forward).handle(),
deref(daycounter._thisptr),
compounding,
<Frequency>frequency
frequency
))
else:
_forward = shared_ptr[ffwd.YieldTermStructure](new ffwd.FlatForward(
Expand All @@ -99,7 +98,7 @@ cdef class FlatForward(YieldTermStructure):
<Real>forward,
deref(daycounter._thisptr),
compounding,
<Frequency>frequency
frequency
))
else:
raise ValueError('Invalid constructor')
Expand Down
8 changes: 4 additions & 4 deletions quantlib/time/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@


from .date import (
Date, Months, Period, today, Years, Days, Annual, Semiannual, Weeks,
Quarterly, Frequency,
Date, Months, Period, today, Years, Days, Weeks,
January, February, March, April, May, June, July, August,
September, November, December,
Jan, Feb, Mar, Apr, Jun, Jul, Aug, Sep, Oct, Nov, Dec,
Daily, Monthly, Annual, NoFrequency, Once,
pydate_from_qldate, qldate_from_pydate,
local_date_time, universal_date_time

)

from .frequency import (
Frequency, Daily, Monthly, Quarterly, Semiannual, Annual, NoFrequency, Once
)
from .schedule import Schedule
from .dategeneration import DateGeneration
26 changes: 1 addition & 25 deletions quantlib/time/date.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,6 @@ cpdef enum Weekday:
Fri = _date.Fri
Sat = _date.Sat

cpdef enum Frequency:
NoFrequency = frequency.NoFrequency # null frequency
Once = frequency.Once # only once, e.g., a zero-coupon
Annual = frequency.Annual # once a year
Semiannual = frequency.Semiannual # twice a year
EveryFourthMonth = frequency.EveryFourthMonth # every fourth month
Quarterly = frequency.Quarterly # every third month
Bimonthly = frequency.Bimonthly # every second month
Monthly = frequency.Monthly # once a month
EveryFourthWeek = frequency.EveryFourthWeek # every fourth week
Biweekly = frequency.Biweekly # every second week
Weekly = frequency.Weekly # once a week
Daily = frequency.Daily # once a day
OtherFrequency = frequency.OtherFrequency # some other unknown frequency

def frequency_to_str(Frequency f):
""" Converts a PyQL Frequency to a human readable string. """
cdef frequency.stringstream ss
ss << <frequency.Frequency>f
return ss.str().decode()

def str_to_frequency(str name):
""" Converts a string to a PyQL Frequency. """
return Frequency[name]

class TimeUnit(IntEnum):
Days = _period.Days #: Days = 0
Expand Down Expand Up @@ -141,7 +117,7 @@ cdef class Period:

property frequency:
def __get__(self):
return Frequency(self._thisptr.get().frequency())
return self._thisptr.get().frequency()

def normalize(self):
'''Normalises the units.'''
Expand Down
4 changes: 2 additions & 2 deletions quantlib/time/frequency.pxd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from libcpp.string cimport string

cdef extern from 'ql/time/frequency.hpp' namespace "QuantLib":
cdef enum Frequency:
cdef extern from 'ql/time/frequency.hpp' namespace "QuantLib" nogil:
cpdef enum Frequency:
NoFrequency = -1 # null frequency
Once = 0 # only once, e.g., a zero-coupon
Annual = 1 # once a year
Expand Down
Empty file added quantlib/time/frequency.pyx
Empty file.
6 changes: 3 additions & 3 deletions test/test_bondfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
from quantlib.time.calendars.null_calendar import NullCalendar
from quantlib.compounding import Compounded, Continuous
from quantlib.time.date import (
Date, Days, Semiannual, January, August, Period, March, February, April, May,
Jul, Annual, Years
Date, Days, January, August, Period, March, February, April, May,
Jul, Years
)
from quantlib.time.api import (TARGET, Period, Months, Years, Days,September, ISDA, today, Mar,
ModifiedFollowing, Unadjusted, Actual360, Thirty360, ActualActual, Actual365Fixed,
Annual, UnitedStates, Months, Actual365Fixed)
Annual, UnitedStates, Months, Actual365Fixed, Annual, Semiannual)
from quantlib.time.daycounters.actual_actual import Bond, ISMA
from quantlib.time.schedule import Schedule
from quantlib.time.dategeneration import DateGeneration
Expand Down
5 changes: 3 additions & 2 deletions test/test_bonds.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
from quantlib.time.calendars.null_calendar import NullCalendar
from quantlib.time.calendars.target import TARGET
from quantlib.compounding import Compounded, Continuous
from quantlib.time.frequency import Quarterly, Semiannual, Annual
from quantlib.time.date import (
Date, Days, Semiannual, January, August, Period, March, February,Oct,Nov,
Jul, Annual, Years, Quarterly
Date, Days, January, August, Period, March, February,Oct,Nov,
Jul, Years
)
from quantlib.time.daycounters.simple import Actual365Fixed, Actual360
from quantlib.time.daycounters.actual_actual import ActualActual, Bond, ISMA
Expand Down
Loading

0 comments on commit cdded9f

Please sign in to comment.