quantdata.biz_calendar#

Business day calendar utilities.

Uses pandas_market_calendars to get NYSE trading days and identify early-close (half) days.

quantdata.biz_calendar.DAYS_PER_YEAR = 252#

Standardized business days per year.

quantdata.biz_calendar.ONE_MINUTE = 1.0175010175010176e-05#

One minute in year fraction, assuming 6.5 trading hours per day and DAYS_PER_YEAR trading days per year.

quantdata.biz_calendar.AM_EXIRY_SUBTRACT = 0.8#

How much to subtract from an expiry (in days) to account for AM settlement.

class quantdata.biz_calendar.NormalizedStrikes(EPS=0.000496031746031746, SCALE=0.09531017980432493)[source]#

Bases: object

NMS generates relative strikes by: $$

mathrm{NMS}(z,t) := expleft( z cdot sqrt{frac{sigma_epsilon^2epsilon+sigma^2 t}{epsilon+1}} right)

$$ where:

  • $epsilon$ is EPS, and $t$ is time to expiry in years.

  • $sigma$ is the volatility scale, which defaults to SCALE.

  • $sigma_epsilon$ is the volatility scale for epsilon.

EPS: float = 0.000496031746031746#

Small epsilon for NMS computation ~ 1 hour

SCALE: float = 0.09531017980432493#

10% vol

from_nms(z, t, *, vol=None, eps_vol=None, backbone=<module 'numpy' from '/opt/hostedtoolcache/Python/3.12.13/x64/lib/python3.12/site-packages/numpy/__init__.py'>)[source]#

Converts an NMS strike to a relative strike

Parameters:
  • z (float | ndarray) – NMS strike level.

  • t (float | ndarray) – Time to expiry in years.

  • vol (float | ndarray | None) – Volatility in year fraction. If None, SCALE is used.

  • eps_vol (float | ndarray | None) – Volatility scale for epsilon. If None, SCALE is used.

  • backbone (Backbone, default BackboneNumpy) – Alternative backbone, e.g. torch or jax.

Returns:

Strikes – Relative strikes corresponding to the given NMS strikes, time to expiry and volatility.

Return type:

float or numpy.ndarray

to_nms(k, t, *, vol=None, eps_vol=None, backbone=<module 'numpy' from '/opt/hostedtoolcache/Python/3.12.13/x64/lib/python3.12/site-packages/numpy/__init__.py'>)[source]#

Converts a relative strike to an NMS strike.

Parameters:
  • k (float | ndarray) – Relative strike level.

  • t (float | ndarray) – Time to expiry in years.

  • vol (float | ndarray | None) – Volatility in year fraction. If None, SCALE is used.

  • eps_vol (float | ndarray | None) – Volatility scale for epsilon. If None, SCALE is used.

  • backbone (Backbone, default BackboneNumpy) – Alternative backbone, e.g. torch or jax.

Returns:

NMS Strikes – Relative strikes corresponding to the given NMS strikes, time to expiry and volatility.

Return type:

float or numpy.ndarray

quantdata.biz_calendar.to_date(d)[source]#

Convert d to a datetime.date, or None if d is None. This function converts:

quantdata.biz_calendar.get_business_days(start, end, exclude_half_days=False)[source]#

Return NYSE trading days within start and end.

This function returns business days inclusive of start and end. That means that if start equals end, and if that is a trading day, then this function returns that day.

Parameters:
  • start (str | date) – Start date (inclusive).

  • end (str | date) – End date (inclusive).

  • exclude_half_days (bool) – If True, remove early-close trading sessions from the calendar.

Returns:

Sorted trading dates (timezone-naive, date-only precision).

Return type:

list[datetime.date]

quantdata.biz_calendar.count_business_days_between(start, end, exclude_half_days=False)[source]#

Count the number of trading days in (start, end], i.e. from start (exclusive) to end (inclusive). If end < start, then count the number of trading days in [end, start) and return the negative of that count.

The count includes end if it is a trading day, but excludes start, assuming, essentially, an end-of-day convention. Half days are counted as full trading days unless exclude_half_days is True.

If start == end, and if that day is a business day, then this function returns 0. If start and end are non-business days bracketing no business days, this function raises a ValueError.

Parameters:
  • start (date | ndarray) – Start date(s) (exclusive).

  • end (date | ndarray) – End date(s) (inclusive).

  • exclude_half_days (bool) – If True, remove early-close trading sessions from the calendar before counting.

Returns:

Count – Number of trading days.

Return type:

int or numpy.ndarray

quantdata.biz_calendar.add_business_days(start, num_days, exclude_half_days=False, *, count_start=False)[source]#

Add a number of business days to a date.

Note that if start is a business day then use count_start to include it in counting: if start is a business day, and if count_start is True, then if num_days=1 or num_days=-1 then the function will the return start.

Parameters:
  • start (date) – Start date. This function raises a ValueError if start is not a business day, or if it is a half business day and exclude_half_days is False.

  • num_days (int) – Number of business days to add. Can be negative to subtract business days.

  • exclude_half_days (bool) – If True, remove early-close trading sessions from the calendar before counting.

  • count_start (bool) –

    Whether to count start if it is a business day. In that case start is counted in either direction as the first day. If False, then the counts starts from the first business day after start.

    This flag has no effect if start is not a business day.

Returns:

Resulting date after adding the specified number of business days.

Return type:

datetime.date

quantdata.biz_calendar.prev_business_day(date, exclude_half_days=False, *, count_start=False)[source]#

Get the previous business day before the given date.

Parameters:
  • date (date) – Reference date. This date does not have to be a business day.

  • exclude_half_days (bool) – If True, remove early-close trading sessions from the calendar before counting.

  • count_start (bool) –

    Whether to count date if it is a business day. If False, then the counts starts from the first business day before date. That means even if date is a business day, the function will return the previous business day before it. If True, then if date is a business day, the function will return that date.

    This flag has no effect if date is a business day.

Returns:

Previous business day before the given date.

Return type:

datetime.date

quantdata.biz_calendar.next_business_day(date, exclude_half_days=False, *, count_start=False)[source]#

Get the next business day after the given date.

Parameters:
  • date (date) – Reference date. This date does not have to be a business day.

  • exclude_half_days (bool) – If True, remove early-close trading sessions from the calendar before counting.

  • count_start (bool) – Whether to count date if it is a business day. If False, then the counts starts from the first business day after date. That means even if date is a business day, the function will return the next business day after it. If True, then if date is a business day, the function will return that date. This flag has no effect if date is a business day.

Returns:

Next business day after the given date.

Return type:

datetime.date

quantdata.biz_calendar.get_half_days(start, end)[source]#

Return a list of half days (early closes) between start and end, inclusive.

class quantdata.biz_calendar.DateRange(start, end, exclude_half_days=True)[source]#

Bases: object

A simple date range class.

exclude_half_days: bool = True#

Whether to exclude half days (early closes) from the business day count. Default is True.

get_business_days()[source]#

Get business days in this date range.

get_half_days()[source]#

Get half days (early closes) in this date range.

quantdata.biz_calendar.session_open_close(date)[source]#

Returns the session open and close for a given date as datetime.datetime objects in New York time zone, and a boolean indicating whether it is a half day (early close) or not.

This function will return (None, None, False) if the given date is not a trading day.

quantdata.biz_calendar.full_second_grid_ns(open_dt, close_dt)[source]#

Per-second Polars Series for RTH bins: [open_dt, open_dt+1s, ..., close_dt-1s].

The grid is end-exclusive so the last bin key is close_dt - 1s and represents [close_dt-1s, close_dt] (inclusive close handled by timestamp mapping in the caller).