quantlab.dataset.sharadar.point_in_time.TradingCalendar#

class quantlab.dataset.sharadar.point_in_time.TradingCalendar(days, *, through, start, end)[source]#

Bases: object

SEP’s trading days, the calendar every Sharadar release panel is placed on.

Parameters:
dayspl.Series

The trading days, sorted, as ns datetimes named timestamp.

throughdate

The last day the raw tables behind the panel are complete through; a row dated after it is not read.

start, enddate-like

The configured window. days_in never returns a day outside it; place uses every day, so a row released before the window is still placed.

Examples

>>> import polars as pl
>>> from datetime import date
>>> days = pl.Series("timestamp", ["2024-01-04", "2024-01-05", "2024-01-08"]).str.to_datetime(time_unit="ns")
>>> calendar = TradingCalendar(days, through=date(2024, 1, 8), start="2024-01-05", end="2024-12-31")
>>> calendar.days_in("2024-01-01", "2024-01-31").dt.date().to_list()
[datetime.date(2024, 1, 5), datetime.date(2024, 1, 8)]
days_in(start, end)[source]#

Return the trading days in both start..``end`` and the configured window.

Examples

>>> import polars as pl
>>> from datetime import date
>>> days = pl.Series("timestamp", ["2024-01-04", "2024-01-08"]).str.to_datetime(time_unit="ns")
>>> calendar = TradingCalendar(days, through=date(2024, 1, 8), start="2024-01-01", end="2024-12-31")
>>> calendar.days_in("2024-01-08", "2024-01-08").dt.date().to_list()
[datetime.date(2024, 1, 8)]
classmethod of_sep(vendor_root, tables, *, start, end)[source]#

Return SEP’s trading days up to the day every one of tables is complete through.

Parameters:
vendor_rootstr or Path

<download-dir>/sharadar.

tablestuple of str

The raw tables the panel reads, "sep" among them (quantlab.dataset.sharadar.tables.raw_through).

start, enddate-like

The configured window.

Examples

calendar = TradingCalendar.of_sep(root, ("sf1", "sep"), start="2024-01-01", end="2024-12-31")
calendar.through  # datetime.date(2024, 1, 11)
place(rows, on='date', *, lag=None, drop_earlier=False)[source]#

Return rows with their available date, the first trading day on or after on + lag.

A row the calendar does not reach (dated after its last day) is dropped: it is not available yet.

Parameters:
rowspl.DataFrame

Rows with a date column on.

onstr, default "date"

The column holding the day a row became public (or the day its lag starts from).

lagtimedelta, optional

Added to on before placing (13F holdings: 45 days after the quarter end).

drop_earlierbool, default False

Drop a row dated before the calendar’s first day instead of placing it there. A state still holds on the first day; an event (a filing) would be misdated.

Returns:
pl.DataFrame

rows and an available column (ns datetime, a trading day), row order not kept.

Examples

2024-01-02 + 3 days is a Friday; a row past the calendar’s last day is dropped, and so, on request, is a row before its first:

>>> import polars as pl
>>> from datetime import date, timedelta
>>> days = pl.Series("timestamp", ["2024-01-04", "2024-01-05", "2024-01-08"]).str.to_datetime(time_unit="ns")
>>> calendar = TradingCalendar(days, through=date(2024, 1, 8), start="2024-01-01", end="2024-12-31")
>>> rows = pl.DataFrame({"date": [date(2024, 1, 2), date(2024, 1, 9)]})
>>> calendar.place(rows, lag=timedelta(days=3)).get_column("available").dt.date().to_list()
[datetime.date(2024, 1, 5)]
>>> calendar.place(rows).get_column("available").dt.date().to_list()
[datetime.date(2024, 1, 4)]
>>> calendar.place(rows, drop_earlier=True).height
0