quantlab.utils.resample.session_labels#

quantlab.utils.resample.session_labels(timestamps, freq, sessions, owner)[source]#

Return each timestamp’s bar inside its trading session.

Bars here are labelled at their end, as NbboResampler labels them, so a source timestamp belongs to the session whose open < t <= close. "1d" labels the whole session with its date at midnight, so a daily panel lines up with daily stores. Any other frequency cuts the session into right-closed bars from its open, labelled at their end; a partial last bar is labelled at the session close.

Parameters:
timestampsnp.ndarray

Source timestamps, naive UTC.

freqstr

A ResampleFrequency token.

sessionspd.DataFrame

Columns date, open and close (naive UTC), one row per session, sorted by open.

ownerstr

Class name quoted in error messages.

Raises:
ValueError

If a timestamp falls in no session.

Examples

>>> sessions = pd.DataFrame({
...     "date": [pd.Timestamp("2024-01-24").date()],
...     "open": [pd.Timestamp("2024-01-24 14:30")],
...     "close": [pd.Timestamp("2024-01-24 21:00")],
... })
>>> ts = pd.to_datetime(["2024-01-24 14:31", "2024-01-24 21:00"])
>>> session_labels(ts, "1d", sessions, "Demo").astype("datetime64[D]").tolist()
[2024-01-24, 2024-01-24]