quantlab.api.forward_returns#
- quantlab.api.forward_returns(frame, *, price='open', span=1, delay=1, binary=False, columns=None, as_xarray=False)[source]#
Compute the forward return of each bar, the label factors are judged against.
The label at bar
tis the return of a position entereddelaybars later and heldspanbars, both at thepricecolumn:price[t + delay + span] / price[t + delay] - 1
delay + spanis the label’s lookahead: the lastdelay + spanbars of the frame have no later bars to read and are NaN. The defaults match the library’sReturnlabel, a signal at bartfilled at bart + 1’s open. The whole frame is computed at once.- Parameters:
- framepandas.DataFrame or polars.DataFrame
Bars in long form:
timestamp,symboland thepricecolumn.- pricestr, default “open”
The column the return is computed on, after
columnsrenames. There is no fallback to another column: use the column a backtest fills at, so the label and the fill agree.- spanint, default 1
Bars the return is held over; at least 1.
- delayint, default 1
Bars between the signal bar and the entry bar; at least 0. With 0 the position is entered at the signal bar’s own price.
- binarybool, default False
Return 1.0 where the forward return is positive and 0.0 elsewhere (the library’s
BinaryReturn) instead of the return (Return).- columnsmapping of str to str, optional
Renames the frame’s columns onto the canonical names,
{"date": "timestamp", "ticker": "symbol", "Open": "open"}.- as_xarraybool, default False
Return the
xarray.Datasetpanel instead of a frame.
- Returns:
- pandas.DataFrame, polars.DataFrame or xarray.Dataset
One row per
(timestamp, symbol)of the frame’s full grid, columnstimestamp,symbolandret_{span}(ret_binary_{span}withbinary=True), in the frame’s library; or the panel on(timestamp, symbol)withas_xarray=True. The values are float32, computed by the library’s KunQuant label path, so compare them with float64 returns at a float32-level tolerance, not exactly.
- Raises:
- ValueError
If the
pricecolumn is missing (the message lists the columns present and suggests one asprice=),spanis below 1,delayis below 0,columnsnames an absent column, or a(timestamp, symbol)pair repeats.- TypeError
If
frameis not a pandas or polars DataFrame,priceis not a string,spanordelayis not an int, orbinaryis not a bool.
Examples
>>> import pandas as pd >>> import quantlab.api as qa >>> frame = pd.DataFrame({ ... "timestamp": pd.to_datetime(["2024-01-02", "2024-01-03", "2024-01-04"] * 2), ... "symbol": ["AAA"] * 3 + ["BBB"] * 3, ... "open": [10.0, 11.0, 12.1, 20.0, 19.0, 19.0], ... "close": [10.5, 11.5, 12.5, 19.5, 19.2, 18.0], ... }) >>> qa.forward_returns(frame) timestamp symbol ret_1 0 2024-01-02 AAA 0.1 1 2024-01-02 BBB 0.0 2 2024-01-03 AAA NaN 3 2024-01-03 BBB NaN 4 2024-01-04 AAA NaN 5 2024-01-04 BBB NaN >>> qa.forward_returns(frame, price="close", delay=0, binary=True) timestamp symbol ret_binary_1 0 2024-01-02 AAA 1.0 1 2024-01-02 BBB 0.0 2 2024-01-03 AAA 1.0 3 2024-01-03 BBB 0.0 4 2024-01-04 AAA NaN 5 2024-01-04 BBB NaN