When to Use
Invoke this whenever a model is trained offline on historical bars and served online on a
live market data stream. Writing one feature path for the backtest (Pandas vectorization)
and a second for live trading (a stateful streaming loop) produces training-serving skew:
the two agree on the day they are written and drift apart with every subsequent edit.
Divergence in NaN handling, rolling-window boundaries, standard-deviation conventions, or
warm-up behavior moves live predictions away from the ones the backtest was validated on,
and the backtest keeps reporting the old, better numbers.
The remedy is structural, not a reconciliation report: one shared calculation core called
by both runtimes, plus an automated replay assertion that the two matrices agree.
When NOT to Use
- Path-dependent features that are not window-derivable. Wilder's RSI, EWMA
volatility, and Kalman filters carry recursive state whose value depends on where the
history was started. They cannot be reproduced from a bounded ring buffer. Serve them by
checkpointing the smoothed state across the batch/online boundary — see
offline-train-online-infer-deployment — not by widening the lookback until the
difference looks small.
- Point-in-time correctness of the underlying data. This skill makes two code paths
agree; it does not make either one as-of correct. Fundamentals, index membership, and
restated vendor history are
point-in-time-database-for-ml-training-data and
point-in-time-fundamentals-data-joins.
- Cross-sectional or portfolio-level features. The engine here is per-instrument and
single-window; a rank across a universe needs the whole universe present at each
timestamp, which a per-symbol ring buffer cannot supply.
Prerequisites
- One shared feature-calculation module imported by both the backtest and the live runtime
— not copied between them.
- A bounded rolling buffer sized to the lookback for the online path.
- A de-duplicated, chronologically ordered bar source. Duplicate or late bars are a data
problem the feature store must reject, not absorb.
- An automated parity test wired into CI, so the assertion runs on every change to the
feature code rather than at promotion time only.
Workflow
- Implement the shared core (
compute_features_from_window) as a pure function of
one window of closes. Both runtimes must call this function; neither may hold a private
copy of the arithmetic. Fix the conventions here explicitly rather than inheriting a
library default:
- Standard deviation ddof.
pandas.rolling().std() defaults to ddof=1; TA-Lib
and pandas_ta.bbands use the population divisor (ddof=0). Pick one, name it in
code, and assert it. Unmatched ddof is the most common concrete cause of the skew
this skill exists to prevent, and it is silent — the bands are merely slightly wrong.
- Undefined cases. A flat window makes RSI a 0/0 division and %B a zero-width
band. Decide the value and write it down; the reference engine returns neutral
(RSI 50, %B 0.5). TA-Lib returns RSI 0.0 for the same input, so a pipeline diffed
against TA-Lib will disagree on flat windows and only on flat windows.
- No rounding inside the core. Quantizing to four decimals makes any parity
tolerance tighter than about 5e-5 unenforceable — the check passes because the
evidence was thrown away.
- Run the batch pipeline (
compute_batch_features) over the historical bars. The
window at index i must close on the right at bar i: bars[i-lookback+1 : i+1].
Reject an unsorted or duplicated series up front — a batch matrix built from unordered
bars has no live stream that can reproduce it.
- Run the online pipeline (
compute_online_feature) over the live stream. Before the
first inference, seed the buffer with the bars the batch pipeline would have held
(warm_up). Reject any bar whose timestamp is not strictly newer than the last one
ingested; a websocket replaying its last bar after a reconnect is the normal case, not
an exotic one.
- Mask warm-up rows. A window shorter than the full lookback yields a different
estimator, and a window shorter than the minimum yields a placeholder. Both are flagged
is_warm=False. Drop or mask them before training and before inference — a placeholder
fed to a model is indistinguishable from a real reading once it is in the matrix.
- Assert parity (
validate_parity). Replay one bar series through both pipelines and
compare every feature. With a genuinely shared core the difference is exactly 0.0, so
run it at tolerance=0.0 first; reserve a non-zero tolerance for a caller who has
substituted a vectorized batch implementation whose float accumulation order differs.
Run the validator on a throwaway engine so a live engine's buffer is never touched.
- Close the data-side gap that replay cannot reach. Replay proves the two code paths
agree on identical input. It says nothing about the input differing in production —
vendor bar revisions, late or dropped ticks, a different consolidation rule, an
adjustment applied to history but not to the live feed. Log the feature vectors
actually served and reconcile them against the batch recomputation for the same
timestamps (Rules of ML #29).
Full step-by-step procedure with implementation detail: see references/workflows.md.
Convention and standards table for this skill: see references/standards.md.
Printable pre-flight checklist: see assets/checklist.md.
Common Pitfalls
- Dual code paths. Two feature implementations agree on day one and drift with every
edit afterwards. Share the function; do not reconcile the outputs.
- A parity check that mutates live state. A validator that clears the engine's own
ring buffer to run its replay resets the live warm-up and leaves the validation series
in the buffer, so the next live inference runs on the wrong history — the self-check
causes the outage. Replay on a separate instance.
- Rounding inside the shared core. Rounding makes the parity assertion pass by
destroying the digits that would have failed it. Round for display, never before the
comparison.
- Unmatched standard-deviation ddof. A pandas batch path and a TA-Lib-shaped live path
disagree on every Bollinger value, with no error and no NaN — just bands a few basis
points off, all day, in one direction.
- Duplicate bar replay after a reconnect. Appending a re-delivered bar as a fresh
observation fabricates a 0.0 return and, because the buffer is bounded, evicts a real
bar — permanently shortening the history behind every later feature. De-duplicate on
timestamp and reject anything not strictly newer.
- Cold live start. Beginning a session with an empty buffer means the first
lookback
inferences run on short-window estimators the model never saw in training. They look
like ordinary feature values.
- Undefined values silently rendered as extremes. RSI on a flat window is 0/0. Mapping
it to 100 tells the model an illiquid, non-trading instrument is maximally overbought.
- Look-ahead reintroduced downstream. Features at timestamp t include bar t's close,
so they are actionable at or after that close. Using them to size bar t's open is
look-ahead the feature store cannot detect — see
lookahead-bias-elimination.
- Treating replay parity as proof of production parity. It is a code-parity test. Feed
differences are invisible to it.
Verification
- Run
python -m unittest discover -s skills/feature-store-for-live-and-backtest-parity/scripts and confirm all tests pass.
- Compute the batch matrix over a historical series, replay the same series through
compute_online_feature(), and confirm validate_parity(bars, tolerance=0.0) returns
True — with a shared core the agreement is exact, not merely within epsilon.
- Confirm
validate_parity() raises FeatureParityMismatchError when the online path is
genuinely perturbed, and that the message names the diverging feature and index. A test
that raises the error itself verifies nothing.
- Confirm that running
validate_parity() on a warmed engine leaves online_ring_buffer
byte-identical and the engine still warm.
- Confirm a re-delivered bar and an out-of-order bar both raise
BarSequenceError, and
that the buffer depth is unchanged afterwards.
- Confirm the first
lookback_period - 1 batch rows report is_warm=False, and that a
flat window returns RSI 50.0 rather than 100.0.
- Confirm a non-finite or non-positive close is rejected with an actionable error rather
than a
ZeroDivisionError from inside the return calculation.
Related Skills
feature-engineering-without-leakage — leakage inside the feature definition itself, which parity preserves rather than fixes
offline-train-online-infer-deployment — serving path-dependent models whose state is not window-derivable
research-environment-vs-production-environment-parity — the same divergence problem one level up, at the environment
point-in-time-database-for-ml-training-data — as-of correctness of the inputs this engine consumes
websocket-reconnection-with-state-recovery — the reconnect that delivers the duplicate bars this engine rejects
backtest-determinism-and-reproducibility — reproducibility of the batch side in isolation
lookahead-bias-elimination — the look-ahead a caller can still introduce downstream
1---2name: feature-store-for-live-and-backtest-parity3description: >-4 Use when a model trains offline on bars and serves online on a stream, and two5 separate feature code paths would drift apart; one shared computation engine plus a6 numeric parity check between batch and streaming output.7license: Apache-2.08metadata:9 source: https://skillmd.com/skills/himanshuj16/feature-store-for-live-and-backtest-parity10 domain: algorithmic-trading11 subdomain: financial-ml12 tags: financial-ml, feature-store, backtest-live-parity, train-test-skew, feature-engineering13 brokers_frameworks: "Feast; Hopsworks; Pandas; NumPy; TA-Lib; Custom Feature Engines"14 version: "1.1.0"15 author: algo-trading-skills-contributors16---1718## When to Use1920Invoke this whenever a model is trained offline on historical bars and served online on a21live market data stream. Writing one feature path for the backtest (Pandas vectorization)22and a second for live trading (a stateful streaming loop) produces training-serving skew:23the two agree on the day they are written and drift apart with every subsequent edit.24Divergence in NaN handling, rolling-window boundaries, standard-deviation conventions, or25warm-up behavior moves live predictions away from the ones the backtest was validated on,26and the backtest keeps reporting the old, better numbers.2728The remedy is structural, not a reconciliation report: one shared calculation core called29by both runtimes, plus an automated replay assertion that the two matrices agree.3031## When NOT to Use3233- **Path-dependent features that are not window-derivable.** Wilder's RSI, EWMA34 volatility, and Kalman filters carry recursive state whose value depends on where the35 history was started. They cannot be reproduced from a bounded ring buffer. Serve them by36 checkpointing the smoothed state across the batch/online boundary — see37 `offline-train-online-infer-deployment` — not by widening the lookback until the38 difference looks small.39- **Point-in-time correctness of the underlying data.** This skill makes two code paths40 agree; it does not make either one as-of correct. Fundamentals, index membership, and41 restated vendor history are `point-in-time-database-for-ml-training-data` and42 `point-in-time-fundamentals-data-joins`.43- **Cross-sectional or portfolio-level features.** The engine here is per-instrument and44 single-window; a rank across a universe needs the whole universe present at each45 timestamp, which a per-symbol ring buffer cannot supply.4647## Prerequisites4849- One shared feature-calculation module imported by both the backtest and the live runtime50 — not copied between them.51- A bounded rolling buffer sized to the lookback for the online path.52- A de-duplicated, chronologically ordered bar source. Duplicate or late bars are a data53 problem the feature store must reject, not absorb.54- An automated parity test wired into CI, so the assertion runs on every change to the55 feature code rather than at promotion time only.5657## Workflow58591. **Implement the shared core** (`compute_features_from_window`) as a pure function of60 one window of closes. Both runtimes must call this function; neither may hold a private61 copy of the arithmetic. Fix the conventions here explicitly rather than inheriting a62 library default:63 - **Standard deviation ddof.** `pandas.rolling().std()` defaults to `ddof=1`; TA-Lib64 and `pandas_ta.bbands` use the population divisor (`ddof=0`). Pick one, name it in65 code, and assert it. Unmatched ddof is the most common concrete cause of the skew66 this skill exists to prevent, and it is silent — the bands are merely slightly wrong.67 - **Undefined cases.** A flat window makes RSI a 0/0 division and %B a zero-width68 band. Decide the value and write it down; the reference engine returns neutral69 (RSI 50, %B 0.5). TA-Lib returns RSI 0.0 for the same input, so a pipeline diffed70 against TA-Lib will disagree on flat windows and only on flat windows.71 - **No rounding inside the core.** Quantizing to four decimals makes any parity72 tolerance tighter than about 5e-5 unenforceable — the check passes because the73 evidence was thrown away.742. **Run the batch pipeline** (`compute_batch_features`) over the historical bars. The75 window at index `i` must close on the right at bar `i`: `bars[i-lookback+1 : i+1]`.76 Reject an unsorted or duplicated series up front — a batch matrix built from unordered77 bars has no live stream that can reproduce it.783. **Run the online pipeline** (`compute_online_feature`) over the live stream. Before the79 first inference, seed the buffer with the bars the batch pipeline would have held80 (`warm_up`). Reject any bar whose timestamp is not strictly newer than the last one81 ingested; a websocket replaying its last bar after a reconnect is the normal case, not82 an exotic one.834. **Mask warm-up rows.** A window shorter than the full lookback yields a *different*84 estimator, and a window shorter than the minimum yields a placeholder. Both are flagged85 `is_warm=False`. Drop or mask them before training and before inference — a placeholder86 fed to a model is indistinguishable from a real reading once it is in the matrix.875. **Assert parity** (`validate_parity`). Replay one bar series through both pipelines and88 compare every feature. With a genuinely shared core the difference is exactly 0.0, so89 run it at `tolerance=0.0` first; reserve a non-zero tolerance for a caller who has90 substituted a vectorized batch implementation whose float accumulation order differs.91 Run the validator on a throwaway engine so a live engine's buffer is never touched.926. **Close the data-side gap that replay cannot reach.** Replay proves the two code paths93 agree on identical input. It says nothing about the input differing in production —94 vendor bar revisions, late or dropped ticks, a different consolidation rule, an95 adjustment applied to history but not to the live feed. Log the feature vectors96 actually served and reconcile them against the batch recomputation for the same97 timestamps (Rules of ML #29).9899> Full step-by-step procedure with implementation detail: see `references/workflows.md`.100> Convention and standards table for this skill: see `references/standards.md`.101> Printable pre-flight checklist: see `assets/checklist.md`.102103## Common Pitfalls104105- **Dual code paths.** Two feature implementations agree on day one and drift with every106 edit afterwards. Share the function; do not reconcile the outputs.107- **A parity check that mutates live state.** A validator that clears the engine's own108 ring buffer to run its replay resets the live warm-up and leaves the validation series109 in the buffer, so the next live inference runs on the wrong history — the self-check110 causes the outage. Replay on a separate instance.111- **Rounding inside the shared core.** Rounding makes the parity assertion pass by112 destroying the digits that would have failed it. Round for display, never before the113 comparison.114- **Unmatched standard-deviation ddof.** A pandas batch path and a TA-Lib-shaped live path115 disagree on every Bollinger value, with no error and no NaN — just bands a few basis116 points off, all day, in one direction.117- **Duplicate bar replay after a reconnect.** Appending a re-delivered bar as a fresh118 observation fabricates a 0.0 return and, because the buffer is bounded, evicts a real119 bar — permanently shortening the history behind every later feature. De-duplicate on120 timestamp and reject anything not strictly newer.121- **Cold live start.** Beginning a session with an empty buffer means the first `lookback`122 inferences run on short-window estimators the model never saw in training. They look123 like ordinary feature values.124- **Undefined values silently rendered as extremes.** RSI on a flat window is 0/0. Mapping125 it to 100 tells the model an illiquid, non-trading instrument is maximally overbought.126- **Look-ahead reintroduced downstream.** Features at timestamp t include bar t's close,127 so they are actionable at or after that close. Using them to size bar t's open is128 look-ahead the feature store cannot detect — see `lookahead-bias-elimination`.129- **Treating replay parity as proof of production parity.** It is a code-parity test. Feed130 differences are invisible to it.131132## Verification133134- Run `python -m unittest discover -s skills/feature-store-for-live-and-backtest-parity/scripts` and confirm all tests pass.135- Compute the batch matrix over a historical series, replay the same series through136 `compute_online_feature()`, and confirm `validate_parity(bars, tolerance=0.0)` returns137 True — with a shared core the agreement is exact, not merely within epsilon.138- Confirm `validate_parity()` raises `FeatureParityMismatchError` when the online path is139 genuinely perturbed, and that the message names the diverging feature and index. A test140 that raises the error itself verifies nothing.141- Confirm that running `validate_parity()` on a warmed engine leaves `online_ring_buffer`142 byte-identical and the engine still warm.143- Confirm a re-delivered bar and an out-of-order bar both raise `BarSequenceError`, and144 that the buffer depth is unchanged afterwards.145- Confirm the first `lookback_period - 1` batch rows report `is_warm=False`, and that a146 flat window returns RSI 50.0 rather than 100.0.147- Confirm a non-finite or non-positive close is rejected with an actionable error rather148 than a `ZeroDivisionError` from inside the return calculation.149150## Related Skills151152- `feature-engineering-without-leakage` — leakage inside the feature definition itself, which parity preserves rather than fixes153- `offline-train-online-infer-deployment` — serving path-dependent models whose state is not window-derivable154- `research-environment-vs-production-environment-parity` — the same divergence problem one level up, at the environment155- `point-in-time-database-for-ml-training-data` — as-of correctness of the inputs this engine consumes156- `websocket-reconnection-with-state-recovery` — the reconnect that delivers the duplicate bars this engine rejects157- `backtest-determinism-and-reproducibility` — reproducibility of the batch side in isolation158- `lookahead-bias-elimination` — the look-ahead a caller can still introduce downstream