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_YEARtrading 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:
objectNMS 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.
- 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:
- 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:
- 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
dto adatetime.date, orNoneifdisNone. This function converts:np.datetime64strassuming it is in iso format e.g. “2024-01-31”
- 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
startandend. That means that ifstartequalsend, and if that is a trading day, then this function returns that day.
- 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. fromstart(exclusive) toend(inclusive). Ifend < start, then count the number of trading days in[end, start)and return the negative of that count.The count includes
endif it is a trading day, but excludesstart, assuming, essentially, an end-of-day convention. Half days are counted as full trading days unlessexclude_half_daysisTrue.If
start == end, and if that day is a business day, then this function returns 0. Ifstartandendare non-business days bracketing no business days, this function raises aValueError.
- 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
startis a business day then usecount_startto include it in counting: ifstartis a business day, and ifcount_startisTrue, then ifnum_days=1ornum_days=-1then the function will the returnstart.- Parameters:
start (
date) – Start date. This function raises aValueErrorifstartis not a business day, or if it is a half business day andexclude_half_daysisFalse.num_days (
int) – Number of business days to add. Can be negative to subtract business days.exclude_half_days (
bool) – IfTrue, remove early-close trading sessions from the calendar before counting.count_start (
bool) –Whether to count
startif it is a business day. In that casestartis counted in either direction as the first day. IfFalse, then the counts starts from the first business day after start.This flag has no effect if
startis 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
dateif it is a business day. IfFalse, then the counts starts from the first business day before date. That means even ifdateis a business day, the function will return the previous business day before it. IfTrue, then ifdateis a business day, the function will return that date.This flag has no effect if
dateis 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 countdateif it is a business day. IfFalse, then the counts starts from the first business day after date. That means even ifdateis a business day, the function will return the next business day after it. IfTrue, then ifdateis a business day, the function will return that date. This flag has no effect ifdateis 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:
objectA simple date range class.
- quantdata.biz_calendar.session_open_close(date)[source]#
Returns the session open and close for a given
dateasdatetime.datetimeobjects 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 - 1sand represents[close_dt-1s, close_dt](inclusive close handled by timestamp mapping in the caller).