quantlab.backend.resample.resample_on_read#

quantlab.backend.resample.resample_on_read(open_source, start, end, freq, how, labels, owner)[source]#

Return the source bars read and their resample, cut to start..``end``.

The source bars are opened from resample_padding(freq) before the first instant of the range to that padding after its last, so every resampled bar inside the range is whole; they are aggregated by resample_panel and the result is cut to the range.

Parameters:
open_sourcecallable

open_source(first, last): the source bars from first to last, both inclusive, already cut to the requested symbols (so only those are aggregated) and holding every variable how names.

start, endstr, datetime.date or pd.Timestamp

The range requested, both inclusive; a date-only end includes every bar of that day.

freq, how, labels, owner

As for resample_panel.

Returns:
sourcexr.Dataset

What open_source returned: the bars read, which the caller records.

panelxr.Dataset

The resampled bars from start to end.

Raises:
ValueError

As resample_panel raises. The range is not checked here: the caller checks it first (check_range), naming its own method.

Examples

>>> from quantlab.utils.resample import clock_labels
>>> hours = pd.date_range("2024-01-01", periods=72, freq="h")
>>> hourly_bars = xr.Dataset(
...     {"close": (["timestamp", "symbol"], np.arange(72.0).reshape(72, 1))},
...     coords={"timestamp": hours, "symbol": ["A"]},
... )
>>> source, daily = resample_on_read(
...     lambda a, b: hourly_bars.sel(timestamp=slice(a, b)),
...     "2024-01-02", "2024-01-02", "1d", "last", clock_labels, "Demo",
... )
>>> daily["timestamp"].values.astype("datetime64[D]").tolist()
[datetime.date(2024, 1, 2)]