"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
feat(shadow-account): carry price-condition bounds into extracted rules (#314)
卫斯李632a6778f296
diff --git a/agent/src/shadow_account/codegen.py b/agent/src/shadow_account/codegen.pyindex af768491f..3c1cc17be 100644--- a/agent/src/shadow_account/codegen.py+++ b/agent/src/shadow_account/codegen.py@@ -75,7 +75,7 @@ def _rule_to_context(rule: ShadowRule) -> dict[str, Any]:hour = rule.entry_condition.get("entry_hour") or {}hold_lo, hold_hi = rule.holding_days_rangehold_days = max(1, int(round((hold_lo + hold_hi) / 2)))- return {+ ctx: dict[str, Any] = {"rule_id": rule.rule_id,"market": market,"entry_hour_min": int(hour.get("min", 0)),@@ -83,6 +83,15 @@ def _rule_to_context(rule: ShadowRule) -> dict[str, Any]:"hold_days": hold_days,"weight": float(rule.weight),}+ for feature in ("entry_rsi14", "prior_5d_return"):+ bounds = rule.entry_condition.get(feature)+ if isinstance(bounds, dict):+ lo = bounds.get("min")+ hi = bounds.get("max")+ if isinstance(lo, (int, float)) and isinstance(hi, (int, float)):+ ctx[feature + "_min"] = float(lo)+ ctx[feature + "_max"] = float(hi)+ return ctxdef render_signal_engine(profile: ShadowProfile) -> str:diff --git a/agent/src/shadow_account/extractor.py b/agent/src/shadow_account/extractor.pyindex cbef792de..576187114 100644--- a/agent/src/shadow_account/extractor.py+++ b/agent/src/shadow_account/extractor.py@@ -354,10 +354,23 @@ def _extract_rules(llm_translator: Any | None,) -> list[ShadowRule]:"""Cluster profitable roundtrips, derive one rule per dense cluster."""+ available_price_features = tuple(+ f for f in _PRICE_FEATURES if f in features_df.columns+ )if len(features_df) < min_support:- return [_heuristic_single_rule(features_df, min_support, llm_translator)]+ return [+ _heuristic_single_rule(+ features_df,+ min_support,+ llm_translator,+ price_features=available_price_features,+ )+ ]numeric_features = _promoted_numeric_features(features_df, min_support=min_support)+ promoted_price_features = tuple(+ f for f in numeric_features if f in _PRICE_FEATURES+ )cluster_labels = _auto_cluster(features_df, max_k=min(max_rules, 5), numeric_features=numeric_features,)@@ -375,6 +388,7 @@ def _extract_rules(rule_index=len(rules) + 1,total_profitable=total_profitable,llm_translator=llm_translator,+ price_features=promoted_price_features,)# Deduplicate near-identical rules (same market + same holding band)key = (rule.entry_condition.get("market"), rule.holding_days_range)@@ -386,7 +400,14 @@ def _extract_rules(breakif not rules:- rules = [_heuristic_single_rule(features_df, min_support, llm_translator)]+ rules = [+ _heuristic_single_rule(+ features_df,+ min_support,+ llm_translator,+ price_features=promoted_price_features,+ )+ ]return rules@@ -439,6 +460,7 @@ def _cluster_to_rule(rule_index: int,total_profitable: int,llm_translator: Any | None,+ price_features: tuple[str, ...] = (),) -> ShadowRule:"""Summarize a cluster as one ShadowRule.@@ -458,6 +480,13 @@ def _cluster_to_rule("market": market,"entry_hour": {"min": hour_lo, "max": hour_hi},}+ for feature in price_features:+ if feature in cluster_df.columns:+ series = cluster_df[feature].dropna()+ if len(series) >= 2:+ lo = float(round(series.quantile(0.10), 2))+ hi = float(round(series.quantile(0.90), 2))+ entry_condition[feature] = {"min": lo, "max": hi}exit_condition: dict[str, Any] = {"holding_days": {"min": hold_lo, "max": hold_hi},}@@ -492,13 +521,22 @@ def _heuristic_single_rule(features_df: pd.DataFrame,min_support: int,llm_translator: Any | None,+ *,+ price_features: tuple[str, ...] = (),) -> ShadowRule:- """Degenerate fallback when clustering/tree yield nothing usable."""+ """Degenerate fallback when clustering/tree yield nothing usable.++ Forwards ``price_features`` so the single-rule path carries the same+ RSI/return bounds as the multi-cluster path; ``_cluster_to_rule`` already+ guards on column presence and ``len(series) >= 2``, so sparse data simply+ yields a behavior-only rule.+ """return _cluster_to_rule(cluster_df=features_df,rule_index=1,total_profitable=max(len(features_df), min_support),llm_translator=llm_translator,+ price_features=price_features,)diff --git a/agent/src/shadow_account/scanner.py b/agent/src/shadow_account/scanner.pyindex 4b0aa4ef7..d3bb3ba6e 100644--- a/agent/src/shadow_account/scanner.py+++ b/agent/src/shadow_account/scanner.py@@ -16,6 +16,8 @@ from src.shadow_account.models import ShadowProfile, ShadowRulePriceFetcher = Callable[..., pd.DataFrame | None]+_RSI_PERIOD = 14+def scan_today_signals(profile: ShadowProfile,@@ -190,9 +192,30 @@ def _compute_features(bars: pd.DataFrame, rule: ShadowRule) -> dict[str, float |baseline = volume.iloc[-volume_window - 1:-1].mean()if baseline > 0:features["volume_ratio"] = float(volume.iloc[-1] / baseline)++ if len(close) >= _RSI_PERIOD:+ rsi = _compute_rsi(close).iloc[-1]+ if pd.notna(rsi):+ features["entry_rsi14"] = float(rsi)return features+def _compute_rsi(close: pd.Series, period: int = _RSI_PERIOD) -> pd.Series:+ """Causal Wilder-EWM RSI.++ Mirrors ``_compute_rsi`` in ``extractor.py`` so the scanner evaluates the+ same RSI that produced the extracted ``entry_rsi14`` bounds. Causal by+ construction: ``RSI[t]`` depends only on closes dated ``<= t``.+ """+ 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()+ rs = avg_gain / avg_loss+ return 100 - 100 / (1 + rs)++def _window_from_rule(rule: ShadowRule, default: int) -> int:"""Infer a recent-return window from common condition keys."""for key, value in rule.entry_condition.items():@@ -211,6 +234,8 @@ def _feature_for_condition_key(key: str) -> str | None:key_lower = str(key).lower()if key_lower in {"market", "ma_window", "volume_window"}:return None+ if "rsi" in key_lower:+ return "entry_rsi14"if "above_ma" in key_lower or "price_above" in key_lower or "close_gt_ma" in key_lower:return "price_above_ma"if "volume" in key_lower or "turnover" in key_lower:@@ -229,6 +254,14 @@ def _compare(value: float | bool, condition: Any) -> bool:return value == (condition.strip().lower() not in {"false", "0", "no"})return value == bool(condition)+ if isinstance(condition, dict):+ lo, hi = _to_float(condition.get("min")), _to_float(condition.get("max"))+ if lo is not None and value < lo:+ return False+ if hi is not None and value > hi:+ return False+ return True+if isinstance(condition, (tuple, list)) and len(condition) >= 2:op, threshold = str(condition[0]), _to_float(condition[1])else:diff --git a/agent/src/shadow_account/templates/signal_engine.py.j2 b/agent/src/shadow_account/templates/signal_engine.py.j2index ca00f3d27..aa14c6787 100644--- a/agent/src/shadow_account/templates/signal_engine.py.j2+++ b/agent/src/shadow_account/templates/signal_engine.py.j2@@ -30,21 +30,43 @@ def _market_of(code: str) -> str:class SignalEngine:- """Shadow Account signal engine (rule-replay, daily timeframe).+ """Shadow Account signal engine (conditional entry, daily timeframe).- For each code we pick the first rule whose market matches, then emit- a long signal (= rule.weight) for ``hold_days`` bars every- ``2 * hold_days`` bars — a deliberate period-replay of the user's- profitable holding cadence. Non-matching codes yield a zero series.+ For each code we pick the first rule whose market matches, then check+ per-bar entry conditions: hour window, plus optionally RSI range and+ prior-return range (read from the rule dict — absent keys skip the+ check). When all conditions pass we enter a position and hold for the+ rule's holding days. Non-matching codes yield a zero series."""+ @staticmethod+ def _compute_rsi(close: pd.Series, period: int = 14) -> pd.Series:+ """Causal Wilder-EWM RSI (mirrors ``compute_rsi`` in example_signal_engine.py:13)."""+ 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()+ rs = avg_gain / avg_loss+ return 100 - 100 / (1 + rs)++ @staticmethod+ def _compute_prior_return(close: pd.Series, window: int = 5) -> pd.Series:+ """Trailing close-to-close return over *window* bars."""+ return close.pct_change(window)+def generate(self, data_map: dict) -> dict:signals: dict[str, pd.Series] = {}for code, df in data_map.items():sig = pd.Series(0.0, index=df.index)rule = self._pick_rule(code)if rule is not None:- sig = self._replay_rule(df.index, rule)+ close = df["close"]+ rsi = self._compute_rsi(close)+ prior_ret = self._compute_prior_return(close)+ sig = self._conditional_entry(+ df.index, rsi, prior_ret, rule,+ )signals[code] = sigreturn signals@@ -53,18 +75,49 @@ class SignalEngine:for rule in RULES:if rule["market"] == market or rule["market"] == "other":return rule- # Fallback: first rule, regardless of market match.return RULES[0] if RULES else None@staticmethod- def _replay_rule(index: pd.Index, rule: dict) -> pd.Series:- hold = max(1, int(rule["hold_days"]))- period = max(2, hold * 2)+ def _conditional_entry(+ index: pd.Index,+ rsi: pd.Series,+ prior_ret: pd.Series,+ rule: dict,+ ) -> pd.Series:+ hold_days = max(1, int(rule["hold_days"]))+ hour_min = int(rule["entry_hour_min"])+ hour_max = int(rule["entry_hour_max"])weight = float(rule["weight"])++ rsi_min = rule.get("entry_rsi14_min")+ rsi_max = rule.get("entry_rsi14_max")+ ret_min = rule.get("prior_5d_return_min")+ ret_max = rule.get("prior_5d_return_max")+values = [0.0] * len(index)- i = 0- while i < len(index):- for j in range(i, min(i + hold, len(index))):- values[j] = weight- i += period+ remaining_hold = 0++ for i in range(len(index)):+ if remaining_hold > 0:+ values[i] = weight+ remaining_hold -= 1+ continue++ bar_hour = pd.Timestamp(index[i]).hour+ if bar_hour < hour_min or bar_hour > hour_max:+ continue++ if rsi_min is not None and rsi_max is not None:+ rsi_val = rsi.iloc[i]+ if pd.isna(rsi_val) or rsi_val < rsi_min or rsi_val > rsi_max:+ continue++ if ret_min is not None and ret_max is not None:+ ret_val = prior_ret.iloc[i]+ if pd.isna(ret_val) or ret_val < ret_min or ret_val > ret_max:+ continue++ values[i] = weight+ remaining_hold = hold_days - 1+return pd.Series(values, index=index)diff --git a/agent/tests/test_shadow_account.py b/agent/tests/test_shadow_account.pyindex abedda190..33af5163b 100644--- a/agent/tests/test_shadow_account.py+++ b/agent/tests/test_shadow_account.py@@ -830,6 +830,37 @@ def test_promoted_features_threshold() -> None:assert set(_MARKET_KEY_MAP) == {"china_a", "us", "hk", "crypto"}+def test_single_cluster_fallback_carries_price_bounds() -> None:+ """The degenerate single-rule path must still emit RSI/return bounds.++ Regression for PR #314 review: ``_heuristic_single_rule`` previously called+ ``_cluster_to_rule`` without ``price_features``, so small/homogeneous+ journals (len < min_support) silently produced behavior-only rules even+ when price data was present.+ """+ from src.shadow_account.extractor import _extract_rules++ df = pd.DataFrame({+ "market": ["us", "us", "us"],+ "holding_days": [3.0, 4.0, 5.0],+ "entry_hour": [10, 11, 10],+ "symbol": ["AAPL", "MSFT", "NVDA"],+ "buy_dt": pd.to_datetime(["2026-01-01", "2026-01-02", "2026-01-03"]),+ "entry_rsi14": [55.0, 60.0, 65.0],+ "prior_5d_return": [0.01, 0.02, 0.03],+ })++ # min_support above len(df) forces the len < min_support fallback branch.+ rules = _extract_rules(df, min_support=10, max_rules=3, llm_translator=None)++ assert len(rules) == 1+ entry = rules[0].entry_condition+ assert isinstance(entry.get("entry_rsi14"), dict)+ assert isinstance(entry.get("prior_5d_return"), dict)+ assert entry["entry_rsi14"]["min"] <= entry["entry_rsi14"]["max"]++# ---- Degradation branches in the price-fetch / attach path ----from src.shadow_account.extractor import ( # noqa: E402@@ -958,3 +989,601 @@ def test_auto_cluster_all_nan_feature_is_dropped() -> None:assert len(labels) == len(df)+# ---- M4: Price conditions in extracted rules ----+++def test_extracted_rules_carry_price_condition_bounds(+ profitable_journal: Path, with_price_loader,+) -> None:+ """When price features are promoted, each rule's entry_condition carries bounds."""+ dates = pd.bdate_range("2025-12-01", periods=60).strftime("%Y-%m-%d").tolist()+ closes = [10.0 + 0.05 * i for i in range(60)]+ with_price_loader(_price_frame(dates, closes))++ profile = extract_shadow_profile(profitable_journal, min_support=2)+ assert len(profile.rules) >= 1++ for rule in profile.rules:+ ec = rule.entry_condition+ assert "market" in ec+ assert "entry_hour" in ec+ # Price features were promoted → should appear in every rule.+ assert "entry_rsi14" in ec, f"rule {rule.rule_id} missing entry_rsi14 bounds"+ assert isinstance(ec["entry_rsi14"], dict)+ assert "min" in ec["entry_rsi14"] and "max" in ec["entry_rsi14"]+ assert "prior_5d_return" in ec, f"rule {rule.rule_id} missing prior_5d_return bounds"+ assert isinstance(ec["prior_5d_return"], dict)+++def test_extracted_rules_no_price_conditions_when_sparse(+ profitable_journal: Path,+) -> None:+ """With prices offline (autouse), rules carry only behavioral conditions."""+ profile = extract_shadow_profile(profitable_journal, min_support=2)+ assert len(profile.rules) >= 1++ for rule in profile.rules:+ ec = rule.entry_condition+ assert "market" in ec+ assert "entry_hour" in ec+ assert "entry_rsi14" not in ec+ assert "prior_5d_return" not in ec+++def test_price_condition_bounds_are_p10_p90() -> None:+ """The p10/p90 values are the actual quantiles of the cluster."""+ from src.shadow_account.extractor import _cluster_to_rule++ cluster_df = pd.DataFrame({+ "symbol": ["A"] * 4,+ "market": ["china_a"] * 4,+ "holding_days": [3.0, 4.0, 3.0, 4.0],+ "pnl_pct": [0.1, 0.2, 0.1, 0.2],+ "entry_hour": [10, 10, 10, 10],+ "entry_weekday": [1, 1, 1, 1],+ "entry_rsi14": [25.0, 30.0, 35.0, 45.0],+ "buy_dt": [pd.Timestamp("2026-01-10")] * 4,+ "sell_dt": [pd.Timestamp("2026-01-14")] * 4,+ })+ rule = _cluster_to_rule(+ cluster_df=cluster_df,+ rule_index=1,+ total_profitable=4,+ llm_translator=None,+ price_features=("entry_rsi14",),+ )+ bounds = rule.entry_condition["entry_rsi14"]+ # p10 of [25, 30, 35, 45] = 25 + 0.3*5 = 26.5; p90 = 30 + 0.9*10 = 39.0+ # Actually with 4 data points, p10 is between 1st and 2nd:+ # With default linear interpolation: p10 ~ 26.5, p90 ~ 42.0+ assert "min" in bounds and "max" in bounds+ assert bounds["min"] > 24.0 # roughly p10+ assert bounds["max"] < 46.0 # roughly p90+ assert bounds["min"] < bounds["max"]+++def test_price_condition_nan_rows_excluded_from_bounds() -> None:+ """NaN rows in the cluster do not shift p10/p90 quantiles."""+ from src.shadow_account.extractor import _cluster_to_rule++ cluster_df = pd.DataFrame({+ "symbol": ["A"] * 4,+ "market": ["china_a"] * 4,+ "holding_days": [3.0, 4.0, 3.0, 4.0],+ "pnl_pct": [0.1, 0.2, 0.1, 0.2],+ "entry_hour": [10, 10, 10, 10],+ "entry_weekday": [1, 1, 1, 1],+ "entry_rsi14": [30.0, float("nan"), 40.0, float("nan")],+ "buy_dt": [pd.Timestamp("2026-01-10")] * 4,+ "sell_dt": [pd.Timestamp("2026-01-14")] * 4,+ })+ rule = _cluster_to_rule(+ cluster_df=cluster_df,+ rule_index=1,+ total_profitable=4,+ llm_translator=None,+ price_features=("entry_rsi14",),+ )+ # Only two non-NaN values (30, 40) — bounds from those.+ bounds = rule.entry_condition["entry_rsi14"]+ assert bounds["min"] == pytest.approx(30.0, abs=2.0)+ assert bounds["max"] == pytest.approx(40.0, abs=2.0)+++# ---- M5: Codegen with price conditions ----+++from src.shadow_account.codegen import _rule_to_context # noqa: E402+++def test_rule_to_context_with_price_conditions() -> None:+ rule = ShadowRule(+ rule_id="R1",+ human_text="Enter when RSI 25-45",+ entry_condition={+ "market": "china_a",+ "entry_hour": {"min": 9, "max": 11},+ "entry_rsi14": {"min": 25.0, "max": 45.0},+ "prior_5d_return": {"min": -0.05, "max": 0.02},+ },+ exit_condition={"holding_days": {"min": 2, "max": 5}},+ holding_days_range=(2, 5),+ support_count=10,+ coverage_rate=0.5,+ sample_trades=("600519@2026-01-10",),+ )+ ctx = _rule_to_context(rule)+ assert ctx["entry_rsi14_min"] == 25.0+ assert ctx["entry_rsi14_max"] == 45.0+ assert ctx["prior_5d_return_min"] == -0.05+ assert ctx["prior_5d_return_max"] == 0.02+++def test_rule_to_context_without_price_conditions() -> None:+ rule = ShadowRule(+ rule_id="R1",+ human_text="Enter at 9-11am",+ entry_condition={+ "market": "china_a",+ "entry_hour": {"min": 9, "max": 11},+ },+ exit_condition={"holding_days": {"min": 2, "max": 5}},+ holding_days_range=(2, 5),+ support_count=10,+ coverage_rate=0.5,+ sample_trades=("600519@2026-01-10",),+ )+ ctx = _rule_to_context(rule)+ assert "entry_rsi14_min" not in ctx+ assert "entry_rsi14_max" not in ctx+ assert "prior_5d_return_min" not in ctx+ assert "prior_5d_return_max" not in ctx+++def test_render_signal_engine_with_price_conditions() -> None:+ rule = ShadowRule(+ rule_id="R1",+ human_text="Enter when RSI 25-45",+ entry_condition={+ "market": "china_a",+ "entry_hour": {"min": 9, "max": 11},+ "entry_rsi14": {"min": 25.0, "max": 45.0},+ },+ exit_condition={"holding_days": {"min": 2, "max": 5}},+ holding_days_range=(2, 5),+ support_count=10,+ coverage_rate=0.5,+ sample_trades=("600519@2026-01-10",),+ )+ profile = ShadowProfile(+ shadow_id="shadow_test123",+ created_at="2026-01-01T00:00:00",+ journal_hash="abc",+ source_market="china_a",+ profitable_roundtrips=10,+ total_roundtrips=20,+ date_range=("2025-01-01", "2026-01-01"),+ profile_text="test",+ rules=(rule,),+ preferred_markets=("china_a",),+ typical_holding_days=(3.0, 5.0),+ )+ source = render_signal_engine(profile)+ ok, err = validate_generated(source)+ assert ok, f"validation failed: {err}"+ assert "_compute_rsi" in source+ assert "_compute_prior_return" in source+ assert "_conditional_entry" in source+ assert "entry_rsi14_min" in source+ assert "entry_rsi14_max" in source+ assert "class SignalEngine" in source+ assert "def generate" in source+++def test_render_signal_engine_without_price_conditions_still_valid() -> None:+ rule = ShadowRule(+ rule_id="R1",+ human_text="Enter at 9-11am",+ entry_condition={+ "market": "china_a",+ "entry_hour": {"min": 9, "max": 11},+ },+ exit_condition={"holding_days": {"min": 2, "max": 5}},+ holding_days_range=(2, 5),+ support_count=10,+ coverage_rate=0.5,+ sample_trades=("600519@2026-01-10",),+ )+ profile = ShadowProfile(+ shadow_id="shadow_test456",+ created_at="2026-01-01T00:00:00",+ journal_hash="def",+ source_market="china_a",+ profitable_roundtrips=10,+ total_roundtrips=20,+ date_range=("2025-01-01", "2026-01-01"),+ profile_text="test",+ rules=(rule,),+ preferred_markets=("china_a",),+ typical_holding_days=(3.0, 5.0),+ )+ source = render_signal_engine(profile)+ ok, err = validate_generated(source)+ assert ok, f"validation failed: {err}"+ # Conditional entry method still present (uses runtime checks)+ assert "_conditional_entry" in source+ # Rule dict in RULES literal has only behavioral keys, no price keys.+ import ast+ tree = ast.parse(source)+ rules_found = False+ for node in ast.walk(tree):+ if isinstance(node, ast.Assign) and getattr(node.targets[0], "id", "") == "RULES":+ rules_found = True+ val = node.value+ if isinstance(val, ast.List) and val.elts:+ first_rule = ast.literal_eval(val.elts[0])+ assert "entry_rsi14_min" not in first_rule+ assert "prior_5d_return_min" not in first_rule+ assert rules_found+++# ---- M6: Template behavior — conditional entry logic ----+++def _generate_signals(profile: ShadowProfile, data_map: dict) -> dict:+ """Render + load + execute a SignalEngine against a data_map."""+ import importlib.util+ import tempfile++ source = render_signal_engine(profile)+ ok, err = validate_generated(source)+ assert ok, f"generated source failed validation: {err}"++ with tempfile.TemporaryDirectory() as tmp:+ path = Path(tmp) / "signal_engine.py"+ path.write_text(source, encoding="utf-8")+ spec = importlib.util.spec_from_file_location("gen_se", path)+ module = importlib.util.module_from_spec(spec)+ assert spec and spec.loader+ spec.loader.exec_module(module)+ engine = module.SignalEngine()+ return engine.generate(data_map)+++def _rule_with_rsi(+ rsi_min: float, rsi_max: float, hour_min: int = 0, hour_max: int = 23,+) -> ShadowRule:+ return ShadowRule(+ rule_id="R1",+ human_text="RSI rule",+ entry_condition={+ "market": "china_a",+ "entry_hour": {"min": hour_min, "max": hour_max},+ "entry_rsi14": {"min": rsi_min, "max": rsi_max},+ },+ exit_condition={"holding_days": {"min": 2, "max": 5}},+ holding_days_range=(3, 3),+ support_count=10,+ coverage_rate=0.5,+ sample_trades=("600519@2026-01-10",),+ )+++def _rule_without_price() -> ShadowRule:+ return ShadowRule(+ rule_id="R1",+ human_text="Time-only rule",+ entry_condition={+ "market": "china_a",+ "entry_hour": {"min": 0, "max": 23},+ },+ exit_condition={"holding_days": {"min": 2, "max": 5}},+ holding_days_range=(3, 3),+ support_count=10,+ coverage_rate=0.5,+ sample_trades=("600519@2026-01-10",),+ )+++def _profile(rule: ShadowRule) -> ShadowProfile:+ return ShadowProfile(+ shadow_id="shadow_test",+ created_at="2026-01-01T00:00:00",+ journal_hash="test",+ source_market="china_a",+ profitable_roundtrips=10,+ total_roundtrips=20,+ date_range=("2025-01-01", "2026-01-01"),+ profile_text="test",+ rules=(rule,),+ preferred_markets=("china_a",),+ typical_holding_days=(3.0, 5.0),+ )+++def _hourly_index(start: str = "2026-01-02", periods: int = 24) -> pd.DatetimeIndex:+ return pd.date_range(start, periods=periods, freq="h")+++def _daily_index(start: str = "2026-01-02", periods: int = 30) -> pd.DatetimeIndex:+ return pd.date_range(start, periods=periods, freq="B")+++def test_conditional_entry_emits_signal_when_rsi_in_range() -> None:+ """RSI in [25, 45] → signal fires."""+ rule = _rule_with_rsi(25.0, 45.0)+ idx = _hourly_index(periods=40)+ sideways = pd.Series(+ [10.0 + (i % 6) * 0.5 for i in range(40)], index=idx, dtype=float,+ )+ s2 = _generate_signals(+ _profile(rule),+ {"600519.SH": pd.DataFrame({"close": sideways}, index=idx)},+ )["600519.SH"]+ assert (s2 >= 0).all()+ # With a 3-day hold, at least some 4-hour entry windows should fire.+ assert (s2 > 0).any(), "signal should fire when RSI is in range"+++def test_conditional_entry_skips_when_rsi_out_of_range() -> None:+ """RSI strictly below min → no entry. Use a steep uptrend to push RSI high."""+ rule = _rule_with_rsi(5.0, 15.0) # very low range+ idx = _hourly_index(periods=40)+ # Steep uptrend → RSI very high (well above 15)+ close = pd.Series(+ [10.0 + 0.5 * i for i in range(40)], index=idx, dtype=float,+ )+ signals = _generate_signals(+ _profile(rule),+ {"600519.SH": pd.DataFrame({"close": close}, index=idx)},+ )+ series = signals["600519.SH"]+ # RSI should be far above the 5-15 range → zero entries.+ assert (series == 0.0).all()+++def test_conditional_entry_respects_hour_window() -> None:+ """Entry only allowed when bar hour is in [9, 11]; outside → zero."""+ rule = _rule_with_rsi(0.0, 100.0, hour_min=9, hour_max=11)+ idx = _hourly_index(periods=48) # 2 full days of hours+ close = pd.Series(+ [10.0 + (i % 6) * 0.5 for i in range(48)], index=idx, dtype=float,+ )+ signals = _generate_signals(+ _profile(rule),+ {"600519.SH": pd.DataFrame({"close": close}, index=idx)},+ )+ series = signals["600519.SH"]+ for i, ts in enumerate(idx):+ h = pd.Timestamp(ts).hour+ if h < 9 or h > 11:+ assert series.iloc[i] == 0.0, f"signal at hour {h} should be zero"+++def test_conditional_entry_holds_for_full_duration() -> None:+ """Once entered, signal persists for hold_days bars."""+ rule = _rule_with_rsi(0.0, 100.0) # always passes RSI check+ idx = _hourly_index(periods=24)+ close = pd.Series(+ [10.0 + (i % 6) * 0.5 for i in range(24)], index=idx, dtype=float,+ )+ signals = _generate_signals(+ _profile(rule),+ {"600519.SH": pd.DataFrame({"close": close}, index=idx)},+ )+ series = signals["600519.SH"]++ # Find first entry bar.+ entry_idx = None+ for i in range(len(series)):+ if series.iloc[i] > 0:+ entry_idx = i+ break+ assert entry_idx is not None+ # Signal should persist for hold_days (3) consecutive bars.+ for j in range(entry_idx, min(entry_idx + 3, len(series))):+ assert series.iloc[j] == 1.0, f"hold broken at bar {j}"+++def test_conditional_entry_no_reentry_while_holding() -> None:+ """remaining_hold > 0 must prevent re-entry during a hold."""+ # Use daily bars with all-hours window so RSI warmup is the only gate.+ rule = _rule_with_rsi(0.0, 100.0, hour_min=0, hour_max=23)+ idx = _daily_index(periods=30)+ # A price series that yields a moderate RSI value (not extreme).+ close = pd.Series(+ [10.0 + (i % 6) * 0.5 for i in range(30)], index=idx, dtype=float,+ )+ signals = _generate_signals(+ _profile(rule),+ {"600519.SH": pd.DataFrame({"close": close}, index=idx)},+ )+ series = signals["600519.SH"]++ # Find the first entry bar.+ entry_bar = None+ for i in range(len(series)):+ if series.iloc[i] > 0:+ entry_bar = i+ break+ assert entry_bar is not None, "expected at least one entry"+ # The first hold run after entry should last hold_days=3 consecutive bars.+ for j in range(entry_bar, min(entry_bar + 3, len(series))):+ assert series.iloc[j] == 1.0, f"hold broken at bar {j} (entry at {entry_bar})"+++def test_conditional_entry_falls_back_to_time_only_when_no_price_keys() -> None:+ """Rule without price conditions uses time-window-only entry."""+ rule = _rule_without_price()+ idx = _hourly_index(periods=24)+ close = pd.Series(+ [10.0 + (i % 6) * 0.5 for i in range(24)], index=idx, dtype=float,+ )+ signals = _generate_signals(+ _profile(rule),+ {"600519.SH": pd.DataFrame({"close": close}, index=idx)},+ )+ series = signals["600519.SH"]+ assert isinstance(series, pd.Series)+ assert len(series) == len(idx)+ # With hour_min=0, hour_max=23 and no price gates, at least first bar enters.+ assert (series > 0).any()+++def test_conditional_entry_prior_return_gates_entry() -> None:+ """Prior return outside bounds → no entry."""+ rule = ShadowRule(+ rule_id="R1",+ human_text="Return rule",+ entry_condition={+ "market": "china_a",+ "entry_hour": {"min": 0, "max": 23},+ "prior_5d_return": {"min": -0.03, "max": 0.03},+ },+ exit_condition={"holding_days": {"min": 2, "max": 5}},+ holding_days_range=(3, 3),+ support_count=10,+ coverage_rate=0.5,+ sample_trades=("600519@2026-01-10",),+ )+ idx = _daily_index(periods=40)+ # Very large returns (+50% over 5 bars) → well outside [-0.03, 0.03]+ close = pd.Series(+ [10.0 + 0.5 * i for i in range(40)], index=idx, dtype=float,+ )+ signals = _generate_signals(+ _profile(rule),+ {"600519.SH": pd.DataFrame({"close": close}, index=idx)},+ )+ series = signals["600519.SH"]+ # Strong trend → prior_5d_return far outside narrow [-3%, 3%] → all zero.+ assert (series == 0.0).all()+++def test_conditional_entry_both_rsi_and_return_must_pass() -> None:+ """Both RSI and prior-return conditions must pass (AND logic)."""+ rule = ShadowRule(+ rule_id="R1",+ human_text="Both rule",+ entry_condition={+ "market": "china_a",+ "entry_hour": {"min": 0, "max": 23},+ "entry_rsi14": {"min": 0.0, "max": 100.0}, # always passes+ "prior_5d_return": {"min": -0.02, "max": 0.02}, # very narrow+ },+ exit_condition={"holding_days": {"min": 2, "max": 5}},+ holding_days_range=(3, 3),+ support_count=10,+ coverage_rate=0.5,+ sample_trades=("600519@2026-01-10",),+ )+ idx = _daily_index(periods=40)+ # Strong uptrend → prior_5d_return >> 2%.+ close = pd.Series(+ [10.0 + 1.0 * i for i in range(40)], index=idx, dtype=float,+ )+ signals = _generate_signals(+ _profile(rule),+ {"600519.SH": pd.DataFrame({"close": close}, index=idx)},+ )+ series = signals["600519.SH"]+ # RSI passes, but prior_5d_return fails (too positive) → no entry.+ assert (series == 0.0).all()+++def test_conditional_entry_only_prior_return_condition() -> None:+ """Rule with only prior_5d_return (no RSI) gates on return alone."""+ rule = ShadowRule(+ rule_id="R1",+ human_text="Return-only rule",+ entry_condition={+ "market": "china_a",+ "entry_hour": {"min": 0, "max": 23},+ "prior_5d_return": {"min": -0.02, "max": 0.02},+ },+ exit_condition={"holding_days": {"min": 2, "max": 5}},+ holding_days_range=(3, 3),+ support_count=10,+ coverage_rate=0.5,+ sample_trades=("600519@2026-01-10",),+ )+ idx = _daily_index(periods=40)+ # Sideways prices → prior_5d_return ~0, within [-0.02, 0.02].+ close = pd.Series(+ [10.0 + (i % 3) * 0.005 for i in range(40)], index=idx, dtype=float,+ )+ signals = _generate_signals(+ _profile(rule),+ {"600519.SH": pd.DataFrame({"close": close}, index=idx)},+ )+ series = signals["600519.SH"]+ assert (series >= 0).all()+ assert (series > 0).any(), "entry should fire when return in range"+++def test_cluster_to_rule_skips_all_nan_price_feature() -> None:+ """When a promoted price feature is all-NaN in the cluster, it is skipped."""+ from src.shadow_account.extractor import _cluster_to_rule++ cluster_df = pd.DataFrame({+ "symbol": ["A"] * 4,+ "market": ["china_a"] * 4,+ "holding_days": [3.0, 4.0, 3.0, 4.0],+ "pnl_pct": [0.1, 0.2, 0.1, 0.2],+ "entry_hour": [10, 10, 10, 10],+ "entry_weekday": [1, 1, 1, 1],+ "entry_rsi14": [float("nan")] * 4, # all NaN+ "buy_dt": [pd.Timestamp("2026-01-10")] * 4,+ "sell_dt": [pd.Timestamp("2026-01-14")] * 4,+ })+ rule = _cluster_to_rule(+ cluster_df=cluster_df,+ rule_index=1,+ total_profitable=4,+ llm_translator=None,+ price_features=("entry_rsi14",),+ )+ # All-NaN → dropna() leaves < 2 values → feature not added to entry_condition.+ assert "entry_rsi14" not in rule.entry_condition+ assert "market" in rule.entry_condition+ assert "entry_hour" in rule.entry_condition+++def test_conditional_entry_rsi_nan_bars_are_skipped() -> None:+ """Bars where RSI is NaN (warmup) are skipped, then entry fires after warmup."""+ rule = _rule_with_rsi(0.0, 100.0)+ idx = _daily_index(periods=30)+ close = pd.Series(+ [10.0 + (i % 6) * 0.5 for i in range(30)], index=idx, dtype=float,+ )+ signals = _generate_signals(+ _profile(rule),+ {"600519.SH": pd.DataFrame({"close": close}, index=idx)},+ )+ series = signals["600519.SH"]+ # First 14 bars have NaN RSI → must be zero.+ for i in range(14):+ assert series.iloc[i] == 0.0, f"bar {i} should be zero (RSI warmup)"+ # At least one entry after warmup.+ assert (series.iloc[14:] > 0).any(), "no entry after RSI warmup"++diff --git a/agent/tests/test_shadow_scanner.py b/agent/tests/test_shadow_scanner.pyindex 6a9ab9766..5bcb8be9a 100644--- a/agent/tests/test_shadow_scanner.py+++ b/agent/tests/test_shadow_scanner.py@@ -119,3 +119,66 @@ def test_scan_today_signals_respects_per_market_cap() -> None:)assert [match["symbol"] for match in matches] == ["AAPL", "MSFT"]+++def _ramp(n: int, start: float, step: float) -> list[float]:+ """Monotonic close series of length n — drives RSI toward an extreme."""+ return [start + step * i for i in range(n)]+++def test_scan_today_signals_matches_rsi_range_condition() -> None:+ """An RSI ``{min,max}`` bound must be honored by the scanner (PR #314).++ Before the fix, ``entry_rsi14`` mapped to no feature and the bound was+ silently dropped; a steady uptrend pins RSI near 100 and should match a+ wide [50, 100] band.+ """+ profile = _profile({"market": "us", "entry_rsi14": {"min": 50.0, "max": 100.0}})+ closes = _ramp(20, 10.0, 0.2) # >= 14 bars so RSI is defined+ target = pd.Timestamp("2026-04-01") + pd.Timedelta(days=len(closes) - 1)+ frames = {"AAPL": _bars(closes)}++ matches = scan_today_signals(profile, target_date=target.date(), price_frames=frames)++ assert [m["symbol"] for m in matches] == ["AAPL"]+++def test_scan_today_signals_rejects_out_of_band_rsi() -> None:+ """A steady uptrend (RSI ~100) must fail a low-RSI [0, 30] band."""+ profile = _profile({"market": "us", "entry_rsi14": {"min": 0.0, "max": 30.0}})+ closes = _ramp(20, 10.0, 0.2)+ target = pd.Timestamp("2026-04-01") + pd.Timedelta(days=len(closes) - 1)+ frames = {"AAPL": _bars(closes)}++ assert scan_today_signals(profile, target_date=target.date(), price_frames=frames) == []+++def test_scan_today_signals_honors_prior_return_dict_band() -> None:+ """``prior_5d_return`` as a ``{min,max}`` dict must range-check momentum.++ Before the fix the dict reached ``_to_float`` and returned ``None`` → the+ bound was skipped. The 5-day return here is 11.4/10 - 1 = 0.14, inside the+ band; a too-high floor must reject it.+ """+ closes = [10, 10.2, 10.4, 10.5, 10.7, 11.4]+ frames = {"AAPL": _bars(closes)}++ inside = _profile({"market": "us", "prior_5d_return": {"min": 0.10, "max": 0.20}})+ assert [m["symbol"] for m in scan_today_signals(+ inside, target_date="2026-04-06", price_frames=frames)] == ["AAPL"]++ outside = _profile({"market": "us", "prior_5d_return": {"min": 0.50, "max": 1.0}})+ assert scan_today_signals(outside, target_date="2026-04-06", price_frames=frames) == []+++def test_scan_today_signals_no_match_when_rsi_history_insufficient() -> None:+ """Too few bars to compute RSI → the RSI-bearing rule cannot match."""+ profile = _profile({"market": "us", "entry_rsi14": {"min": 0.0, "max": 100.0}})+ frames = {"AAPL": _bars(_ramp(6, 10.0, 0.2))} # < 14 bars, RSI undefined++ assert scan_today_signals(profile, target_date="2026-04-06", price_frames=frames) == []+