Forward-return labels
Build the returns a signal is judged against, with explicit entry, exit and horizon timing, and the evidence of what was dropped.
Every signal diagnostic compares a signal with what happened next. The forward return is that “what happened next”, and most look-ahead bugs live in how it is built: entering at a price the signal could not have seen, measuring a horizon in calendar days across a holiday, quietly dropping the instruments that delisted.
Lacuna builds labels with lc.labels.forward_returns, and SignalStudy calls it for you. Either way, every timing choice is a parameter you state, and the result records it.
A first example
Two instruments, five sessions. The signal is computed from each session’s close, so the earliest you can trade on it is the next open.
from datetime import date
import lacuna as lc
import polars as pl
prices = pl.DataFrame(
{
"time": [date(2025, 3, day) for day in (3, 4, 5, 6, 7)] * 2,
"instrument": ["AAA"] * 5 + ["BBB"] * 5,
"open": [10.0, 10.2, 10.1, 10.6, 10.9, 50.0, 49.0, 48.5, 47.0, 46.0],
"close": [10.1, 10.3, 10.4, 10.8, 11.0, 49.5, 48.8, 48.0, 46.5, None],
"delisting_return": [None] * 9 + [-0.35],
}
)
labels = lc.labels.forward_returns(
prices,
horizons=("1D", "2D"),
signal_time="close",
entry="next_open",
price_adjustment="split_adjusted",
)
print(labels.frame.filter(pl.col("instrument") == "AAA")) shape: (7, 7)
┌──────────────────┬─────────────┬────────────┬────────────┬────────────┬─────────┬────────────────┐
│ observation_time ┆ label_start ┆ entry_time ┆ label_end ┆ instrument ┆ horizon ┆ forward_return │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ date ┆ date ┆ date ┆ date ┆ str ┆ str ┆ f64 │
╞══════════════════╪═════════════╪════════════╪════════════╪════════════╪═════════╪════════════════╡
│ 2025-03-03 ┆ 2025-03-03 ┆ 2025-03-04 ┆ 2025-03-04 ┆ AAA ┆ 1D ┆ 0.009804 │
│ 2025-03-04 ┆ 2025-03-04 ┆ 2025-03-05 ┆ 2025-03-05 ┆ AAA ┆ 1D ┆ 0.029703 │
│ 2025-03-05 ┆ 2025-03-05 ┆ 2025-03-06 ┆ 2025-03-06 ┆ AAA ┆ 1D ┆ 0.018868 │
│ 2025-03-06 ┆ 2025-03-06 ┆ 2025-03-07 ┆ 2025-03-07 ┆ AAA ┆ 1D ┆ 0.009174 │
│ 2025-03-03 ┆ 2025-03-03 ┆ 2025-03-04 ┆ 2025-03-05 ┆ AAA ┆ 2D ┆ 0.019608 │
│ 2025-03-04 ┆ 2025-03-04 ┆ 2025-03-05 ┆ 2025-03-06 ┆ AAA ┆ 2D ┆ 0.069307 │
│ 2025-03-05 ┆ 2025-03-05 ┆ 2025-03-06 ┆ 2025-03-07 ┆ AAA ┆ 2D ┆ 0.037736 │
└──────────────────┴─────────────┴────────────┴────────────┴────────────┴─────────┴────────────────┘ Take the first row. The signal belongs to the observation on 3 March. The position is entered at the open on 4 March, and the 1D label exits at the close on 4 March: 10.3 / 10.2 − 1 = 0.98%.
The last observation has no 1D label and the last two have no 2D label, because the exit lies beyond the data. Those rows are dropped, never filled.
Horizons count observations
A horizon is a number of observations per instrument: "5D" or the integer 5. The label ends at the fifth observation after the one the signal belongs to, whatever dates those are. Lacuna does not infer a calendar from your dates, so weekends and holidays never stretch or shrink a horizon. If your panel is daily, "5D" is five trading sessions. If it is hourly, it is five hours. A row missing from one instrument’s history does change its calendar span: that instrument’s label then ends one session later, with no warning. Keep each instrument’s rows complete, using a null price for a day with no trade, which is handled under Missing prices.
The D is the only suffix. "1W" or "1M" raise an error, because a week or a month of observations depends on a calendar Lacuna will not guess.
Entry and exit
entry | Entry price | Entry observation |
|---|---|---|
None (default) | the price column | the signal’s own observation |
"current_close" | the price column | the signal’s own observation |
"next_open" | the open column | the next observation |
"next_close" | the price column | the next observation |
The exit is always at the horizon’s last observation. By default it reads the price column, which is close unless you rename it. Pass exit="vwap", or any other column name, to exit at a different price.
The return is simple: exit price divided by entry price, minus one. Prices must be strictly positive. Infinite prices raise an error.
entry="next_close" with a 1D horizon is rejected: it would enter and exit at the same close, a holding period of zero length. Use a horizon of at least 2D. entry="next_open" with 1D is fine; it enters at the next open and exits at that session’s close.
Signals observed at the close
signal_time says when the signal becomes known: "open" or "close", in lowercase. It exists for one check. The value is not validated, so "Close" or a typo silently turns the check off. A signal computed from a session’s close cannot be traded at that same close, so this raises:
lc.labels.forward_returns(prices, horizon="1D", signal_time="close", entry="current_close") MethodContractError: a close-observed signal cannot use the same close; use entry='next_open' or entry='next_close' (horizons of at least 2D), or explicitly set allow_same_close=True If your process really does trade in the closing auction on a signal formed just before it, pass allow_same_close=True. The flag itself is not stored in the result; a reviewer sees signal_time="close" with entry="current_close" in the parameters, which only this flag allows.
SignalStudy calls this parameter signal_observed_at.
Label intervals
Each label carries four times:
| Column | Meaning |
|---|---|
observation_time | The observation the signal belongs to. |
label_start | When the label’s information window opens: the observation time. |
entry_time | When the position is entered. |
label_end | The exit observation. |
The label covers the half-open interval [label_start, label_end). Purged cross-validation uses exactly this interval: a training label is removed when its interval overlaps a test label’s. That is why labels expose their boundaries instead of a bare return column. See Cross-validation.
Consecutive labels overlap whenever the horizon is longer than one observation. A 5D label at each session shares four of its five daily returns with its neighbour, so neighbouring returns are not independent. The audit’s IC bootstrap accounts for this by keeping only non-overlapping periods of one horizon. The IC tables and their t-statistic do not, and your own statistics should.
Price adjustment
price_adjustment records what your prices include:
| Value | Meaning |
|---|---|
"total_return_adjusted" | Adjusted for splits and for dividends and other distributions. |
"split_adjusted" | Adjusted for splits only; dividends are missing from returns. |
"raw" | Unadjusted prices; a split shows up as a huge return. |
"unknown" | Not declared. This is the default. |
Lacuna cannot detect adjustment from prices, so it takes your word and records it; the audit checks that you declared it, not that it is right. Leaving it as "unknown" adds a PRICE_ADJUSTMENT_UNKNOWN finding to the labels and makes the audit’s PRICE_ADJUSTMENT_DECLARED check UNKNOWN.
Delisted instruments
An instrument that delists stops having prices, so its last labels have no exit price. Dropping them removes exactly the outcomes that matter: the bankruptcies and the forced sales.
Pass the name of a column holding each instrument’s delisting return in delisting_return. When a label’s exit price is null, Lacuna uses the delisting return on the exit row as the label’s return. That row has to exist: give the instrument a final row on its delisting date with a null price and the delisting return, as BBB has below. If an instrument’s rows simply stop, its last labels are still dropped. A NaN exit price does not take the delisting return; the label is dropped. Naming the column is enough for the audit’s DELISTING_HANDLING_DECLARED check to pass, even if the column is empty.
labels = lc.labels.forward_returns(
prices,
horizon="2D",
delisting_return="delisting_return",
price_adjustment="split_adjusted",
)
print(labels.frame.filter(pl.col("instrument") == "BBB").select("observation_time", "label_end", "forward_return")) shape: (3, 3)
┌──────────────────┬────────────┬────────────────┐
│ observation_time ┆ label_end ┆ forward_return │
│ --- ┆ --- ┆ --- │
│ date ┆ date ┆ f64 │
╞══════════════════╪════════════╪════════════════╡
│ 2025-03-03 ┆ 2025-03-05 ┆ -0.030303 │
│ 2025-03-04 ┆ 2025-03-06 ┆ -0.047131 │
│ 2025-03-05 ┆ 2025-03-07 ┆ -0.35 │
└──────────────────┴────────────┴────────────────┘ BBB has no close on 7 March, so the label ending there takes the −35% delisting return instead of being dropped. The delisting return replaces the whole label return; it is not compounded with the price move before it.
Without delisting_return, the labels carry a DELISTING_RETURNS_UNKNOWN finding, and the audit’s DELISTING_HANDLING_DECLARED check stays UNKNOWN.
Missing prices
missing="drop", the default, removes labels whose entry or exit price is null or NaN, or whose exit lies past the end of an instrument’s data. missing="raise" raises a DataContractError if any entry or exit price in the input is null or NaN, except a null exit price on a row that carries a delisting return, or if a label has both an entry and an exit observation but no defined return. It does not raise for labels that lose their entry or exit to the end of an instrument’s history, which every finite panel has; those are dropped under either policy and counted in censored_rows. A null close on a delisting row, as BBB has above, is the case delisting_return exists for, so missing="raise" accepts it when you pass delisting_return, and rejects it when you don’t.
That delisting row still counts in source_missing_rows and as excluded in the first row of data_attrition, under either policy.
Either way, the result counts what happened:
labels = lc.labels.forward_returns(
prices,
horizons=("1D", "2D"),
signal_time="close",
entry="next_open",
price_adjustment="split_adjusted",
)
print(labels.evidence.metrics)
print(labels.evidence.table("coverage_by_horizon")) {'n_labels': 12, 'n_instruments': 2, 'n_horizons': 2, 'source_missing_rows': 1, 'censored_rows': 8}
[{'horizon': '1D', 'eligible_rows': 7, 'censored_rows': 3}, {'horizon': '2D', 'eligible_rows': 5, 'censored_rows': 5}] source_missing_rows counts input rows with a missing entry or exit price. censored_rows counts, across horizons, the labels that could not be built. The data_attrition table breaks the same numbers down step by step.
The result
forward_returns returns a LabelResult:
labels.frameis a Polars DataFrame with one row per instrument, observation and horizon, sorted by horizon (as text, so10Dcomes before5D), observation time and instrument. The input need not be sorted, but duplicate or null(time, instrument)keys raise.labels.evidenceis anAnalysisResultholding the counts above, the findings, and the resolved settings inlabels.evidence.metadata.parameters: horizons, the entry and exit columns, the signal time, price adjustment, the delisting column and the missing-data policy.
Pass a LabelResult straight to the signal functions, such as lc.signal.ic(signal, labels). They read the intervals and horizons from it. The label frame always names its identifier column instrument, whatever you passed as instrument=. When you pass the LabelResult, the signal functions read that canonical column, so their instrument= names only the signal table’s identifier column. If you pass labels.frame instead, it is read with the instrument= name when it has that column, and with instrument otherwise, which is the case for a frame from forward_returns; the label input’s execution_operations then include canonical_label_instrument. The same rule applies to a label table you build yourself: name its identifier column as in the signal, or instrument.
Parameters
| Parameter | Default | Meaning |
|---|---|---|
prices | required | A long-form price table: Polars, pandas, Arrow or anything to_polars accepts. |
horizons | One or more horizons, such as ("1D", "5D") or (1, 5). Pass this or horizon. | |
horizon | A single horizon. | |
time | "time" | The observation-time column. |
instrument | "instrument" | The instrument identifier column. Use a stable ID, not a ticker that can be reused. The output column is always called instrument. |
price | "close" | The price column for entries at the close and for the default exit. |
entry | None | None, "current_close", "next_open" or "next_close". See Entry and exit. |
exit | "close" | "close" exits on price; any other value names the exit column. |
signal_time | None | "open" or "close": when the signal becomes known. |
allow_same_close | False | Allow a close-observed signal to enter at that same close. |
price_adjustment | "unknown" | "raw", "split_adjusted", "total_return_adjusted" or "unknown". |
delisting_return | None | A column of delisting returns, used when the exit row’s price is null. |
missing | "drop" | "drop" or "raise" for missing prices and censored labels. "raise" rejects null or NaN prices, and labels with both observations but no return; labels past the end of the data are still dropped and counted. |
schema | None | Column names for a two-dimensional NumPy array, which has none. Ignored for other inputs. |
The full signature is in the API reference.