"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(shadow-account): seed Wilder RSI with a simple average, not the first observation
Lanre Shittu85c5079f9dc0
diff --git a/agent/src/shadow_account/extractor.py b/agent/src/shadow_account/extractor.pyindex dbbb8b7e6..dd32b5ba9 100644--- a/agent/src/shadow_account/extractor.py+++ b/agent/src/shadow_account/extractor.py@@ -147,8 +147,50 @@ def extract_shadow_profile(# ---------------- Feature engineering ----------------+def _wilder_average(values: pd.Series, period: int) -> pd.Series:+ """True Wilder recursive smoothing: SMA-seeded, then exponential recursion.++ Wilder's original formula seeds the first average as a simple mean of the+ first ``period`` valid values, then recurses as+ ``avg[t] = (avg[t-1] * (period - 1) + values[t]) / period`` for every bar+ after that. This is *not* the same as+ ``values.ewm(alpha=1/period, adjust=False).mean()``, which seeds its+ recursion from the single first observation instead of that average —+ measurably further from Wilder during the warmup-adjacent bars, not+ closer, even though the two converge for data far enough past warmup.++ Vectorized as an ``ewm`` over the seed plus the remaining values, which is+ mathematically identical to the explicit recursion: seeding an+ ``adjust=False`` ewm with the average as its first "observation" and+ dropping that duplicate first row reproduces the same recursive update at+ every following step.++ Args:+ values: Series to smooth (e.g. per-bar gains or losses); may lead+ with NaN (as ``close.diff()`` does).+ period: Wilder lookback period.++ Returns:+ A series aligned with ``values``, NaN before the seed index.+ """+ result = pd.Series(np.nan, index=values.index, dtype=float)+ valid = values.dropna()+ if len(valid) < period:+ return result++ seed = valid.iloc[:period].mean()+ result.loc[valid.index[period - 1]] = seed++ remaining = valid.iloc[period:]+ if len(remaining) > 0:+ tail = pd.concat([pd.Series([seed]), remaining])+ smoothed_tail = tail.ewm(alpha=1 / period, adjust=False).mean().iloc[1:]+ result.loc[remaining.index] = smoothed_tail.to_numpy()+ return result++def _compute_rsi(close: pd.Series, period: int = _RSI_PERIOD) -> pd.Series:- """Causal Wilder-EWM RSI.+ """Causal Wilder RSI.Mirrors the shape of ``compute_rsi`` in``agent/src/skills/technical-basic/example_signal_engine.py:13`` — that@@ -166,8 +208,8 @@ def _compute_rsi(close: pd.Series, period: int = _RSI_PERIOD) -> pd.Series:delta = close.diff()gain = delta.clip(lower=0)loss = (-delta).clip(lower=0)- avg_gain = gain.ewm(alpha=1 / period, adjust=False, min_periods=period).mean()- avg_loss = loss.ewm(alpha=1 / period, adjust=False, min_periods=period).mean()+ avg_gain = _wilder_average(gain, period)+ avg_loss = _wilder_average(loss, period)rs = avg_gain / avg_lossreturn 100 - 100 / (1 + rs)diff --git a/agent/src/shadow_account/scanner.py b/agent/src/shadow_account/scanner.pyindex d3bb3ba6e..88e7aa546 100644--- a/agent/src/shadow_account/scanner.py+++ b/agent/src/shadow_account/scanner.py@@ -9,6 +9,7 @@ from __future__ import annotationsfrom datetime import date, datetimefrom typing import Any, Callable+import numpy as npimport pandas as pdfrom src.shadow_account.backtester import _LIQUID_BASKETS, SUPPORTED_MARKETS@@ -200,8 +201,33 @@ def _compute_features(bars: pd.DataFrame, rule: ShadowRule) -> dict[str, float |return features+def _wilder_average(values: pd.Series, period: int) -> pd.Series:+ """True Wilder recursive smoothing: SMA-seeded, then exponential recursion.++ Kept byte-identical to ``_wilder_average`` in ``extractor.py`` — see that+ copy's docstring for the full rationale. Duplicated rather than imported+ for the same reason ``_compute_rsi`` already was: this module's+ ``_compute_rsi`` must evaluate the same RSI that produced the extracted+ ``entry_rsi14`` bounds, so the two copies cannot drift apart.+ """+ result = pd.Series(np.nan, index=values.index, dtype=float)+ valid = values.dropna()+ if len(valid) < period:+ return result++ seed = valid.iloc[:period].mean()+ result.loc[valid.index[period - 1]] = seed++ remaining = valid.iloc[period:]+ if len(remaining) > 0:+ tail = pd.concat([pd.Series([seed]), remaining])+ smoothed_tail = tail.ewm(alpha=1 / period, adjust=False).mean().iloc[1:]+ result.loc[remaining.index] = smoothed_tail.to_numpy()+ return result++def _compute_rsi(close: pd.Series, period: int = _RSI_PERIOD) -> pd.Series:- """Causal Wilder-EWM RSI.+ """Causal Wilder RSI.Mirrors ``_compute_rsi`` in ``extractor.py`` so the scanner evaluates thesame RSI that produced the extracted ``entry_rsi14`` bounds. Causal by@@ -210,8 +236,8 @@ def _compute_rsi(close: pd.Series, period: int = _RSI_PERIOD) -> pd.Series:delta = close.diff()gain = delta.clip(lower=0)loss = (-delta).clip(lower=0)- avg_gain = gain.ewm(alpha=1 / period, min_periods=period).mean()- avg_loss = loss.ewm(alpha=1 / period, min_periods=period).mean()+ avg_gain = _wilder_average(gain, period)+ avg_loss = _wilder_average(loss, period)rs = avg_gain / avg_lossreturn 100 - 100 / (1 + rs)diff --git a/agent/tests/test_shadow_account.py b/agent/tests/test_shadow_account.pyindex a6820e9b2..7f0170c82 100644--- a/agent/tests/test_shadow_account.py+++ b/agent/tests/test_shadow_account.py@@ -1214,11 +1214,15 @@ def test_compute_rsi_is_causal_and_bounded() -> None:@pytest.mark.unitdef test_compute_rsi_matches_wilder_recursive_smoothing() -> None:"""On a mixed gain/loss series, must match true Wilder recursive- smoothing, not pandas' default weight-normalized ewm (which diverges- materially from Wilder on short warmup-adjacent windows)."""+ smoothing: the first average is a simple mean of the first `period`+ gains/losses (the seed), and every bar after that applies the recursive+ update ``avg[t] = (avg[t-1] * (period - 1) + x[t]) / period``. Seeding+ from the single first observation instead (pandas' ewm with+ adjust=False, taken alone) diverges materially from this on the bars+ right after warmup, even though the two converge much later."""closes = [100.0, 101.0, 99.5, 102.0, 101.0, 103.0, 100.0, 104.0, 101.5, 105.0,- 102.0, 106.0, 103.0, 107.0, 104.5,+ 102.0, 106.0, 103.0, 107.0, 104.5, 106.0, 108.0, 103.5, 107.0, 109.0,]close = pd.Series(closes)@@ -1226,16 +1230,39 @@ def test_compute_rsi_matches_wilder_recursive_smoothing() -> None:gains = [max(d, 0.0) for d in deltas]losses = [max(-d, 0.0) for d in deltas]period = 14- alpha = 1 / period- avg_gain, avg_loss = gains[0], losses[0]- for g, loss_val in zip(gains[1:], losses[1:]):- avg_gain = alpha * g + (1 - alpha) * avg_gain- avg_loss = alpha * loss_val + (1 - alpha) * avg_loss++ avg_gain = sum(gains[:period]) / period+ avg_loss = sum(losses[:period]) / period+ for g, loss_val in zip(gains[period:], losses[period:]):+ avg_gain = (avg_gain * (period - 1) + g) / period+ avg_loss = (avg_loss * (period - 1) + loss_val) / periodexpected = 100 - 100 / (1 + avg_gain / avg_loss)assert float(_compute_rsi(close, period=period).iloc[-1]) == pytest.approx(expected)+def test_compute_rsi_seed_is_a_simple_average_not_first_value() -> None:+ """At exactly `period` valid deltas (the boundary case), the seed value+ itself is the whole answer: a simple mean of every gain/loss so far, not+ the single-observation seed pandas' ewm(adjust=False) would use."""+ closes = [+ 100.0, 101.0, 99.5, 102.0, 101.0, 103.0, 100.0, 104.0, 101.5, 105.0,+ 102.0, 106.0, 103.0, 107.0, 104.5,+ ]+ close = pd.Series(closes)++ deltas = [closes[i] - closes[i - 1] for i in range(1, len(closes))]+ gains = [max(d, 0.0) for d in deltas]+ losses = [max(-d, 0.0) for d in deltas]+ period = 14+ assert len(gains) == period++ expected = 100 - 100 / (1 + (sum(gains) / period) / (sum(losses) / period))++ assert float(_compute_rsi(close, period=period).iloc[-1]) == pytest.approx(expected)++@pytest.mark.unitdef test_price_features_as_of_reads_only_past_bars() -> None:dates = [f"2026-02-{d:02d}" for d in range(1, 21)]