session_id

Defined in fynance.data

session_id(ts, open='09:30', close='16:00', utc_offset=0.0, weekdays_only=True)[source]

Label each bar with a running trading-session id.

Bars outside a session get -1. Bars inside a session get a 0-indexed, strictly increasing id shared by every bar of that session (including across a data gap, e.g. a missing lunch bar): the id increments whenever the session’s opening day (see _session_components) changes from the previous in-session bar – not merely when an array index transition occurs – so it is robust to ts that only contains in-session samples (no off-hours rows to mark a boundary with False).

Parameters:
tsndarray of int64 or float64

Non-decreasing epoch-second (UTC) timestamps, shape (n,).

open, close, utc_offset, weekdays_only

See session_mask.

Returns:
ndarray of int64

Shape (n,); -1 outside sessions, else a 0-indexed running session id.

Raises:
ValueError

Same as session_mask.

Examples

>>> import numpy as np
>>> from fynance.data.sessions import session_id
>>> ts = np.array([9 * 3600, 10 * 3600, 17 * 3600, 33 * 3600, 34 * 3600])
>>> session_id(ts, open="09:30", close="16:00")
array([-1,  0, -1, -1,  1])