Calendar utilities#

sigtech.framework.infra.calendar.dates.DATE_WEEK = 'WEEK'

Increment type: week.

sigtech.framework.infra.calendar.dates.DATE_MONTH = 'MONTH'

Increment type: month.

sigtech.framework.infra.calendar.dates.DATE_QUARTER = 'QUARTER'

Increment type: quarter.

sigtech.framework.infra.calendar.dates.DATE_YEAR = 'YEAR'

Increment type: year.

sigtech.framework.infra.calendar.dates.dependencies(names: Union[str, list[str], sigtech.framework.infra.calendar.dates.FrameworkObjectCalendarType]) list[sigtech.framework.internal.infra.mu.graph.registry.factory.Dependency]

Dependencies

sigtech.framework.infra.calendar.dates.build_calendar(names: Union[str, list[str], sigtech.framework.infra.calendar.dates.FrameworkObjectCalendarType], use_business_calendar=True, quant_library=None, env: Optional[sigtech.framework.config.config.ConfiguredEnvironment] = None) Union[sigtech.framework.infra.calendar.business_calendar.CustomCalendar, QuantLib.QuantLib.Calendar]

Create a calendar library object from persistent calendar objects.

Parameters
  • names – Calendar string identifier.

  • use_business_calendar – Use Python business calendar package if True (default), otherwise use quant library calendar.

  • quant_library – Only used if use_business_calendar is set to False.

  • env – Configured environment to use (optional).

Returns

Calendar object.

sigtech.framework.infra.calendar.dates.non_business_days(calendar: Union[str, list[str], sigtech.framework.infra.calendar.dates.FrameworkObjectCalendarType], start_date: datetime.date, end_date: datetime.date) list[datetime.date]

List of non-weekend, non-business dates for calendar in [start_date, end_date] inclusive.

Parameters
  • calendar – Calendar string identifier or calendar instance.

  • start_date – Start date of the range.

  • end_date – End date of the range.

Returns

List of dates.

sigtech.framework.infra.calendar.dates.business_days(calendar: Union[str, list[str], sigtech.framework.infra.calendar.dates.FrameworkObjectCalendarType], start_date: datetime.date, end_date: datetime.date) list[datetime.date]

Return the list of calendar business dates in [start_date, end_date] inclusive.

Parameters
  • calendar – Calendar string identifier or calendar instance.

  • start_date – Start date of the range.

  • end_date – End date of the range.

Returns

List of dates.

sigtech.framework.infra.calendar.dates.business_days_ts(calendar: Union[str, list[str], sigtech.framework.infra.calendar.dates.FrameworkObjectCalendarType], start_date: datetime.date, end_date: datetime.date) pandas.core.indexes.datetimes.DatetimeIndex

Return the list of calendar business dates in [start_date, end_date] inclusive.

Parameters
  • calendar – Calendar string identifier or calendar instance.

  • start_date – Start date of the range.

  • end_date – End date of the range.

Returns

List of dates.

sigtech.framework.infra.calendar.dates.count_business_days(calendar: Union[str, list[str], sigtech.framework.infra.calendar.dates.FrameworkObjectCalendarType], start_date: datetime.date, end_date: datetime.date) int

Return the number of calendar business dates in [start_date, end_date] inclusive.

Parameters
  • calendar – Calendar string identifier or calendar instance.

  • start_date – Start date of the range.

  • end_date – End date of the range.

Returns

int.

sigtech.framework.infra.calendar.dates.date_dcf(style, start, end, bus_calendar=None)

Calculate the day count fraction using the following supported day count methods:

  • 'ACT_360'

  • 'ACT_365F'

  • 'ACT_ACT'

  • 'BUS_252'

Parameters
  • style – Day count method.

  • start – Start date.

  • end – End date.

  • bus_calendar – Calendar for business day counting (if required by style).

Returns

Day count fraction.

sigtech.framework.infra.calendar.dates.date_dcf_series(style: str, series: pandas.core.series.Series)

Faster method to get multiple day-count fractions at once, when the input is pandas series. Index is start-dates, data is end-dates. Only DAYCOUNT_ACTUAL_360 and DAYCOUNT_ACTUAL_365_FIXED have a speed up.

Parameters
  • style – Day count method.

  • series – Input day-count fraction series.

Returns

pandas Series.

sigtech.framework.infra.calendar.dates.end_of_month(d: Union[datetime.date, numpy.ndarray]) Union[datetime.date, numpy.ndarray]

Return the end of month date for the given date.

Parameters

d – Python date.

Returns

End of month date.

sigtech.framework.infra.calendar.dates.is_business_day(calendar: Union[str, list[str], sigtech.framework.infra.calendar.dates.FrameworkObjectCalendarType], day: Union[datetime.date, numpy.ndarray]) Union[bool, numpy.ndarray]

Check if a date day is a valid business day in the given calendar.

Parameters
  • calendar – Calendar string identifier or calendar instance.

  • day – Input date.

Returns

bool or array of bool.

sigtech.framework.infra.calendar.dates.is_holiday(calendar: Union[str, list[str], sigtech.framework.infra.calendar.dates.FrameworkObjectCalendarType], day: Union[datetime.date, numpy.ndarray]) Union[bool, numpy.ndarray]

Check if a date day is a valid business day in the given calendar.

Parameters
  • calendar – Calendar string identifier or calendar instance.

  • day – Input date.

Returns

bool or array of bool.

sigtech.framework.infra.calendar.dates.current_next_business_day(calendar: Union[str, list[str], sigtech.framework.infra.calendar.dates.FrameworkObjectCalendarType], day: Union[datetime.date, numpy.ndarray]) Union[datetime.date, numpy.ndarray]

Return day if it is a business day, else the next business day.

Parameters
  • calendar – Calendar string identifier or calendar instance.

  • day – Input date.

Returns

Date or array of dates.

sigtech.framework.infra.calendar.dates.current_previous_business_day(calendar: Union[str, list[str], sigtech.framework.infra.calendar.dates.FrameworkObjectCalendarType], day: Union[datetime.date, numpy.ndarray]) Union[datetime.date, numpy.ndarray]

Return day if it is a business day, else the previous business day.

Parameters
  • calendar – Calendar string identifier or calendar instance.

  • day – Input date.

Returns

Date or array of dates.

sigtech.framework.infra.calendar.dates.next_business_day(calendar: Union[str, list[str], sigtech.framework.infra.calendar.dates.FrameworkObjectCalendarType], day: Union[datetime.date, numpy.ndarray]) Union[datetime.date, numpy.ndarray]

Return the next business day after day.

Parameters
  • calendar – Calendar string identifier or calendar instance.

  • day – Input date.

Returns

Date or array of dates.

sigtech.framework.infra.calendar.dates.previous_business_day(calendar: Union[str, list[str], sigtech.framework.infra.calendar.dates.FrameworkObjectCalendarType], day: Union[datetime.date, numpy.ndarray]) Union[datetime.date, numpy.ndarray]

Return the previous business day before day.

Parameters
  • calendar – Calendar string identifier or calendar instance.

  • day – Input date.

Returns

Date or array of dates.

sigtech.framework.infra.calendar.dates.date_increment(d: Union[datetime.date, pandas.core.indexes.datetimes.DatetimeIndex, numpy.ndarray], increment: str, calendar: Union[str, list[str], sigtech.framework.infra.calendar.dates.FrameworkObjectCalendarType], backward: bool = False, bdc: str = 'FOLLOWING', eom: bool = False) Union[datetime.date, pandas.core.indexes.datetimes.DatetimeIndex, numpy.ndarray]

Increment the input date.

Parameters
  • d – Input date.

  • increment – Increment string stub, e.g. '1d', '1bd', '1b', '1cd', '1w', '1m', …

  • calendar – Calendar string identifier or calendar instance.

  • backward – Adjust backward if set True, otherwise adjust forward (default).

  • bdc – Business day convention, e.g. 'FOLLOWING', 'MOD_FOL', 'PRECEDE', 'MOD_PRE', 'NONE_RULE' (default is 'BDC_FOLLOWING').

  • eom – Apply the EOM rule if set to True (default is False).

Returns

Date or array of dates.

sigtech.framework.infra.calendar.dates.parse_increment(increment: str) tuple

Parse the increment string into 3 types (forward/backward, increment type, raw numerical amount).

Parameters

increment – Increment string stub.

Returns

tuple (forward/backward, increment type, raw numerical amount).

sigtech.framework.infra.calendar.dates.nth_day_in_month(base_date: datetime.date, day: str, n: int) datetime.date

Return the n-th occurrence of the weekday’s name day in the month from the date base_date, e.g. third Wednesday. Inputting a negative number will yield the same behaviour as indexing, e.g. -1 will give the last occurrence, -2 second to last.

Parameters
  • base_date – Input reference date.

  • day – String identifier for day of the week, i.e. 'Monday', 'Tuesday', … 'Sunday'.

  • n – N-th day in the month for day, or -1 for last, -2 for second to last, etc.

Returns

date.

sigtech.framework.infra.calendar.dates.closest_weekday(base_date: datetime.date, weekday: str) datetime.date

Return the closest weekday as date for given base date.

Parameters
  • base_date – Input reference date.

  • weekday – String identifier for day of the week, i.e. 'Monday', 'Tuesday', … 'Sunday'.

Returns

date.

sigtech.framework.infra.calendar.dates.adjust(calendar: str, ref_date: datetime.date, mode: str) datetime.date

Adjust the reference date according to supplied business day conventions.

Parameters
  • calendar – String calendar identifier.

  • ref_date – Input reference date.

  • mode – Business day convention, e.g. 'FOLLOWING', 'MOD_FOL', 'PRECEDE', 'MOD_PRE', 'NONE_RULE'.

Returns

date.

sigtech.framework.infra.calendar.dates.combined_holidays(*args: str) str

Combine a list of holidays into a string stub.

Parameters

args – List of holiday string identifiers.

Returns

str.

sigtech.framework.infra.calendar.dates.imm_date(ref_date: datetime.date, currency: str, apply_switch: Optional[bool] = False) datetime.date

Return the next IMM date for a given reference date and currency.

Parameters
  • ref_date – Input reference date.

  • currency – Currency string.

  • apply_switch – Switch where change from 2nd Monday to Monday before 3rd Wednesday rule (default is False).

Returns

date.

sigtech.framework.infra.calendar.dates.imm_code(imm_date: datetime.date, one_year_digit: bool) str

Return the IMM stub, e.g. to be used in interest rate swap generation.

Example:

imm_code(dtm.date(2020, 3, 20), False)
# Return 'H20'

imm_code(dtm.date(2020, 3, 20), True)
# Return 'H0'
Parameters
  • imm_date – Input IMM date.

  • one_year_digit – Flag to check if the year is represented with one digit.

Returns

str.

sigtech.framework.infra.calendar.dates.nth_week_day(n: int, day_of_week: Union[str, int], month: int, year: int) datetime.date

Return the date for the n-th weekday for a given month and a year.

Parameters
  • n – Ordinal number of weekday occurrence.

  • day_of_week – String identifier for the weekday, i.e. 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'. Integers are also accepted, ranging from 1 (Monday) to 7 (Sunday).

  • month – Month reference.

  • year – Year reference.

Returns

date.

sigtech.framework.infra.calendar.dates.get_month_end_dates(start: datetime.date, end: datetime.date, calendar_names: str) list[pandas.core.indexes.datetimes.DatetimeIndex]

Provide the month end dates for dates in the range [start, end] using the provider calendars.

Parameters
  • start – Start date.

  • end – End date.

  • calendar_names – String stub for calendar names.

Returns

List of month end dates.

sigtech.framework.infra.calendar.dates.calendar_ttm(start: Union[datetime.date, list[datetime.date]], end: Union[datetime.date, list[datetime.date]], calendar)

Evaluate the day fraction between two dates in terms of actual, weekends and holidays.

Parameters
  • start – Start date.

  • end – End date.

  • calendar – String calendar identifier.

Returns

Tuple of days, weekends, holidays fractions.

sigtech.framework.utils.numeric_utils.date_to_datetime(d: Union[datetime.date, datetime.datetime], tz_aware=False) datetime.datetime

Converts date (or datetime) to a tz-naive datetime.

Parameters
  • ddate or datetime.

  • tz_aware – Convert UTC tz-unaware datetime to UTC tz-aware datetime (default is False).

Returns

datetime.

sigtech.framework.utils.numeric_utils.datetime_to_date(dt: Union[datetime.date, datetime.datetime]) datetime.date

Convert datetime to date.

Parameters

dtdate or datetime.

Returns

date

sigtech.framework.utils.numeric_utils.date_to_midnight_datetime(d: Union[datetime.date, datetime.datetime, pandas.core.indexes.datetimes.DatetimeIndex]) datetime.datetime

Convert a Timestamp, date or datetime to midnight datetime. :param d: input Timestamp, date and datetime: :return: Midnight datetime.