quantlab.dataset.base.bar_before_on#
- quantlab.dataset.base.bar_before_on(bars, date, n, caller, where)[source]#
Return the bar
nbars beforedateon the barsbarsgives.The arithmetic behind
BaseDataset.bar_beforeandFactor.bar_before(quantlab.factor.base), which differ only in the bars they count on and in the text naming them. The bars beforedateare those stamped strictly earlier;n=1is the last of them andn=0returnsdateitself.- Parameters:
- barscallable
Called with
dateas apd.Timestampand returning the sorted bar timestamps, at least every one beforedate, as apd.DatetimeIndex. Called only when the bars are needed, son=0reads nothing.- datestr, datetime.date or pd.Timestamp
The date to count back from.
- nint
How many bars to count back; non-negative.
- callerstr
The class name the error messages start with (
"<caller>.bar_before(): ...").- wherecallable
Returns the text naming where the bars come from, for the history error (
stores_textof the stores read); called only when that error is raised.
- Returns:
- pd.Timestamp
The timestamp of that bar.
- Raises:
- ValueError
If
nis negative.- InsufficientHistoryError
If fewer than
nbars exist beforedate.
Examples
>>> bars = pd.DatetimeIndex(["2024-01-05", "2024-01-08", "2024-01-09"]) >>> bar_before_on(lambda _: bars, "2024-01-08", 1, "Demo", lambda: "the demo bars") Timestamp('2024-01-05 00:00:00') >>> bar_before_on(lambda _: bars, "2024-01-08", 3, "Demo", lambda: "the demo bars") Traceback (most recent call last): quantlab.dataset.base.InsufficientHistoryError: Demo.bar_before(): only 1 bar(s) exist before '2024-01-08' in the demo bars, but 3 were requested.