quantlab.label.forward.Forward#
- class quantlab.label.forward.Forward(config)[source]#
Bases:
ComponentA factor shifted
delay + spanbars earlier, used as a label.readandcomputeask the wrapped factor for the requested range pluslookahead_bars()bars after it, counted on the factor calendar (the bars of the factor’s output,Factor.bar_after) and stopping at the calendar’s last bar, then shift the panel and trim it back to the request. A factor resampled from minute bars to daily counts its lookahead in trading days. The last bars of a request are therefore filled wherever the factor has later bars, and NaN only past the calendar’s end. AForwardowns no store:readreads the wrapped factor’s, so one factor store serves as a feature and, wrapped, as a label.- Parameters:
- configForwardConfig
The factor to shift, its
spanand itsdelay.
- Raises:
- ValueError
If
spanis below 1,delayis negative, or the factor is in stream mode.
Examples
With
momentuma 5-bar momentum factor over 60 daily bars ending on 2024-02-29:>>> label = Forward(ForwardConfig(factor=momentum, span=5)) >>> label.lookahead_bars(), label.span_bars() (6, 5) >>> Forward(ForwardConfig(factor=momentum, span=0)) Traceback (most recent call last): ValueError: Forward: span must be at least 1, got 0.
- build(start, end)[source]#
Build the factor store the label needs to
readstarttoend.The wrapped factor’s store is built from
starttolookahead_bars()factor bars afterend, or to the factor’s last bar when fewer follow;store_rangethen reports that later end.- Parameters:
- start, endstr, datetime.date or pd.Timestamp
The range the label will be read over.
- Returns:
- Forward
self, for chaining.
Examples
>>> label.build("2024-01-01", "2024-01-20").store_range() ('2024-01-01', '2024-01-26T00:00:00')
- property class_name#
Bare class name, used in log and error messages.
Examples
>>> label.class_name 'Forward'
- compute(start, end)[source]#
Compute the label from
starttoend, both inclusive.The factor is computed from
start(with its own warm-up) tolookahead_bars()factor bars afterend.- Parameters:
- start, endstr, datetime.date or pd.Timestamp
The range to return. A date-only
endincludes every bar of that day.
- Returns:
- xr.Dataset
The label panel on
(timestamp, symbol), in memory.
- Raises:
- ValueError
If
startis afterend.
Examples
The dataset ends on 2024-02-29, so only the bars up to 2024-02-23 have six later bars; the last six are NaN:
>>> panel = label.compute("2024-02-20", "2024-02-29") >>> dict(panel.sizes) {'timestamp': 10, 'symbol': 8} >>> int(panel["momentum_5"].isnull().any("symbol").sum()) 6
- config_cls#
Config class the label is built from and rebuilt with (
from_config).alias of
ForwardConfig
- delay_bars()[source]#
Return how many bars pass before the first bar the label counts:
delay.Examples
>>> label.delay_bars() 1
- extend(end)[source]#
Extend the factor store so the label can be read up to
end.The wrapped factor’s store is extended to
lookahead_bars()factor bars afterend, or to the factor’s last bar when fewer follow.- Parameters:
- endstr, datetime.date or pd.Timestamp
The new last date the label will be read up to.
- Returns:
- Forward
self, for chaining.
Examples
>>> label.extend("2024-02-10").store_range() ('2024-01-01', '2024-02-16T00:00:00')
- get_factor_names()[source]#
Return the names of the label’s variables, which are the wrapped factor’s.
Examples
>>> label.get_factor_names() ('momentum_5',)
- kind = 'return'#
What the label measures:
"return", or"volatility"for a label whose predicted level is used as a volatility. Model evaluation reads it to add level metrics, never the label’s class.
- lookahead_bars()[source]#
Return how many bars past t the label at t reads:
delay + span.Every split boundary purges this many bars from the earlier segment.
Examples
>>> label.lookahead_bars() 6
- read(start, end)[source]#
Return the label from
starttoend, read from the factor’s store.The factor store must cover
starttolookahead_bars()factor bars afterend, or to the factor’s last bar when fewer follow.- Parameters:
- start, endstr, datetime.date or pd.Timestamp
The range to return. A date-only
endincludes every bar of that day.
- Returns:
- xr.Dataset
The label panel on
(timestamp, symbol).
- Raises:
- ValueError
If
startis afterend, or the factor store does not cover the range the label needs; the message namesextend.
Examples
>>> momentum.build("2024-01-01", "2024-02-29").store_range() ('2024-01-01', '2024-02-29') >>> dict(label.read("2024-02-01", "2024-02-10").sizes) {'timestamp': 10, 'symbol': 8}
- span_bars()[source]#
Return how many bars the label accumulates over:
span.Examples
>>> label.span_bars() 5
- store_range()[source]#
Return the
(start, end)the wrapped factor’s store was built for.The end is
lookahead_bars()bars past the last date the label can be read up to.Nonewhen the store was not written bybuild.Examples
>>> label.build("2024-01-01", "2024-01-20").store_range() ('2024-01-01', '2024-01-26T00:00:00')