quantlab.dataset.base.bar_after_on#

quantlab.dataset.base.bar_after_on(bars, date, n, caller)[source]#

Return the bar n bars after date on the bars bars gives, or the last bar there is.

The arithmetic behind BaseDataset.bar_after and Factor.bar_after (quantlab.factor.base), the forward counterpart of bar_before_on: the bars after date are those stamped strictly later, n=1 is the first of them, n=0 returns date itself, and when fewer than n bars follow date the last bar is returned, date itself when none follow, so the result is never before date.

Parameters:
barscallable

Called with date as a pd.Timestamp and returning the sorted bar timestamps, at least every one after date, as a pd.DatetimeIndex. Called only when the bars are needed, so n=0 reads nothing.

datestr, datetime.date or pd.Timestamp

The date to count forward from.

nint

How many bars to count forward; non-negative.

callerstr

The class name the error message starts with ("<caller>.bar_after(): ...").

Returns:
pd.Timestamp

The timestamp of that bar.

Raises:
ValueError

If n is negative.

Examples

>>> bars = pd.DatetimeIndex(["2024-01-05", "2024-01-08", "2024-01-09"])
>>> bar_after_on(lambda _: bars, "2024-01-05", 1, "Demo")
Timestamp('2024-01-08 00:00:00')
>>> bar_after_on(lambda _: bars, "2024-01-05", 5, "Demo")  # fewer follow: the last bar
Timestamp('2024-01-09 00:00:00')