Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(loaders): a one-bar plateau at the window edge refuses the conversion

Yufeng He85118116e12e
diff --git a/agent/backtest/loaders/additive_conversion.py b/agent/backtest/loaders/additive_conversion.py
index 716dc7f5c..21374e76a 100644
--- a/agent/backtest/loaders/additive_conversion.py
+++ b/agent/backtest/loaders/additive_conversion.py
@@ -36,20 +36,23 @@ import pandas as pd
# one corporate-action segment into two.
_OFFSET_TOL = 1e-6
-# A one-bar plateau inside (or at the edge of) a longer level is a quote
-# wobble, not a dividend: fold it only when the neighboring level(s) match
-# within this tolerance. Anything else that short cannot be classified and
-# refuses the window (a wrong series with a clean stamp is the worst outcome).
+# A one-bar plateau strictly inside a longer level is a quote wobble, not a
+# dividend: fold it only when both neighboring levels match within this
+# tolerance. At the window edge a lone bar cannot be told from a dividend
+# landing on the first or last bar, so it refuses the window instead (a wrong
+# series with a clean stamp is the worst outcome).
_WOBBLE_TOL = 0.05
def _plateau_spans(offset: pd.Series) -> list[tuple[int, int, float]] | None:
"""Group the offset series into [start, end) spans of one constant value.
- One-bar spans are folded into a matching neighbor within ``_WOBBLE_TOL``
- (quote wobble); a one-bar span that cannot be classified that way returns
- None, since dropping a step we cannot classify would mint a wrong series
- with a clean stamp.
+ One-bar spans are folded into a matching level when both neighbors agree
+ within ``_WOBBLE_TOL`` (quote wobble). A one-bar span at either window
+ edge has only one neighbor and cannot be told from a dividend landing on
+ the first or last bar, and any other one-bar span that cannot be
+ classified returns None, since dropping a step we cannot classify would
+ mint a wrong series with a clean stamp.
"""
spans: list[tuple[int, int, float]] = []
start = 0
@@ -79,13 +82,6 @@ def _plateau_spans(offset: pd.Series) -> list[tuple[int, int, float]] | None:
):
foldable_idx = i
break
- # Edge wobble: the lone bar at either end matches its neighbor.
- if i == 0 and next_value is not None and abs(value - next_value) <= _WOBBLE_TOL:
- foldable_idx = i
- break
- if i == len(spans) - 1 and prev_value is not None and abs(value - prev_value) <= _WOBBLE_TOL:
- foldable_idx = i
- break
if foldable_idx is None:
# Any one-bar plateau left cannot be classified as noise; refuse
# rather than guess.
diff --git a/agent/backtest/loaders/akshare_loader.py b/agent/backtest/loaders/akshare_loader.py
index 56c55a7b2..4d69f0692 100644
--- a/agent/backtest/loaders/akshare_loader.py
+++ b/agent/backtest/loaders/akshare_loader.py
@@ -34,7 +34,9 @@ _DAILY_ONLY_ALIASES = frozenset({"1d", "d", "day", "daily"})
def _require_daily_interval(interval: str, market: str) -> None:
if str(interval).strip().lower() not in _DAILY_ONLY_ALIASES:
- raise ValueError(f"Unsupported interval {interval!r}; akshare {market} supports daily bars only")
+ raise ValueError(
+ f"Unsupported interval {interval!r}; akshare {market} supports daily bars only"
+ )
def _is_a_share(code: str) -> bool:
@@ -65,6 +67,9 @@ def _sina_contract(code: str) -> str:
_CN_FUTURES_MAIN_RE = re.compile(r"^[A-Z]{1,2}0$")
+
+
+
def _is_forex(code: str) -> bool:
"""Detect forex pairs by matching against AKShare's symbol_market_map.
@@ -98,7 +103,6 @@ class DataLoader:
"""Available if akshare is installed."""
try:
import akshare # noqa: F401
-
return True
except ImportError:
return False
@@ -177,11 +181,7 @@ class DataLoader:
return result
def _fetch_one(
- self,
- code: str,
- start_date: str,
- end_date: str,
- interval: str,
+ self, code: str, start_date: str, end_date: str, interval: str,
) -> Optional[pd.DataFrame]:
"""Fetch a single symbol."""
import akshare as ak
@@ -209,25 +209,23 @@ class DataLoader:
# Returning None hands the symbol to the next link in the chain;
# letting it reach the A-share default below priced a USD-quoted
# global contract off ``stock_zh_a_hist`` without erroring (#1395).
- logger.warning("akshare serves Chinese futures only; %s has no akshare source", code)
+ logger.warning(
+ "akshare serves Chinese futures only; %s has no akshare source", code
+ )
return None
# Default: try A-share
return self._fetch_a_share(ak, code, start_date, end_date, interval)
def _fetch_a_share(
- self,
- ak,
- code: str,
- start_date: str,
- end_date: str,
- interval: str,
+ self, ak, code: str, start_date: str, end_date: str, interval: str,
) -> Optional[pd.DataFrame]:
"""Fetch A-share via stock_zh_a_hist."""
symbol = code.split(".")[0]
period = _INTERVAL_MAP_DAILY.get(interval)
if period is None:
raise ValueError(
- f"Unsupported interval {interval!r}; akshare a-share supports {sorted(_INTERVAL_MAP_DAILY)}"
+ f"Unsupported interval {interval!r}; akshare a-share supports "
+ f"{sorted(_INTERVAL_MAP_DAILY)}"
)
sd = start_date.replace("-", "")
ed = end_date.replace("-", "")
@@ -256,7 +254,8 @@ class DataLoader:
period = _INTERVAL_MAP_DAILY.get(interval)
if period is None:
raise ValueError(
- f"Unsupported interval {interval!r}; akshare a-share supports {sorted(_INTERVAL_MAP_DAILY)}"
+ f"Unsupported interval {interval!r}; akshare a-share supports "
+ f"{sorted(_INTERVAL_MAP_DAILY)}"
)
df = ak.stock_zh_a_hist(
symbol=symbol,
@@ -315,21 +314,21 @@ class DataLoader:
df = ak.forex_hist_em(symbol=symbol)
if df is None or df.empty:
return None
- df = df.rename(
- columns={
- "日期": "trade_date",
- "今开": "open",
- "最新价": "close",
- "最高": "high",
- "最低": "low",
- }
- )
+ df = df.rename(columns={
+ "日期": "trade_date",
+ "今开": "open",
+ "最新价": "close",
+ "最高": "high",
+ "最低": "low",
+ })
df["trade_date"] = pd.to_datetime(df["trade_date"])
df = df.set_index("trade_date").sort_index()
df["volume"] = 0.0
for col in ("open", "high", "low", "close"):
df[col] = pd.to_numeric(df[col], errors="coerce")
- df = df[["open", "high", "low", "close", "volume"]].dropna(subset=["open", "high", "low", "close"])
+ df = df[["open", "high", "low", "close", "volume"]].dropna(
+ subset=["open", "high", "low", "close"]
+ )
return df.loc[start_date:end_date]
def _fetch_hk(self, ak, code: str, start_date: str, end_date: str) -> Optional[pd.DataFrame]:
@@ -347,11 +346,7 @@ class DataLoader:
return self._normalize(df, date_col="日期")
def _fetch_china_futures(
- self,
- ak,
- code: str,
- start_date: str,
- end_date: str,
+ self, ak, code: str, start_date: str, end_date: str,
) -> Optional[pd.DataFrame]:
"""Fetch a Chinese futures contract from Sina's token-free endpoints.
@@ -386,14 +381,10 @@ class DataLoader:
)
if raw is None or raw.empty:
return None
- raw = raw.rename(
- columns={
- "开盘价": "开盘",
- "最高价": "最高",
- "最低价": "最低",
- "收盘价": "收盘",
- }
- )
+ raw = raw.rename(columns={
+ "开盘价": "开盘", "最高价": "最高",
+ "最低价": "最低", "收盘价": "收盘",
+ })
return self._normalize(raw, date_col="日期")
raw = ak.futures_zh_daily_sina(symbol=symbol)
@@ -409,7 +400,7 @@ class DataLoader:
df = self._normalize(raw, date_col="date")
# The dated endpoint ignores the window, so slice it here; without this
# a one-month request came back with the contract's entire history.
- return df.loc[str(start_date) : str(end_date)]
+ return df.loc[str(start_date):str(end_date)]
@staticmethod
def _normalize(df: pd.DataFrame, date_col: str = "日期") -> pd.DataFrame:
@@ -419,14 +410,7 @@ class DataLoader:
AKShare English column names: date, open, high, low, close, volume
"""
col_map_cn = {"开盘": "open", "最高": "high", "最低": "low", "收盘": "close", "成交量": "volume"}
- col_map_en = {
- "date": "trade_date",
- "open": "open",
- "high": "high",
- "low": "low",
- "close": "close",
- "volume": "volume",
- }
+ col_map_en = {"date": "trade_date", "open": "open", "high": "high", "low": "low", "close": "close", "volume": "volume"}
if date_col in df.columns:
df = df.rename(columns={date_col: "trade_date"})
diff --git a/agent/backtest/loaders/eastmoney_loader.py b/agent/backtest/loaders/eastmoney_loader.py
index 71699e20b..93ead6705 100644
--- a/agent/backtest/loaders/eastmoney_loader.py
+++ b/agent/backtest/loaders/eastmoney_loader.py
@@ -107,7 +107,9 @@ class DataLoader:
start_date=start_date,
end_date=end_date,
fields=None,
- fetch=lambda code=code: self._fetch_one(code, start_date, end_date, interval),
+ fetch=lambda code=code: self._fetch_one(
+ code, start_date, end_date, interval
+ ),
)
if df is not None and not df.empty and _is_a_share(code) and not _is_etf_listed(code):
# #1541: fqt=1 is dividend-additive and cannot serve
@@ -122,7 +124,9 @@ class DataLoader:
start_date=start_date,
end_date=end_date,
fields=["raw"],
- fetch=lambda code=code: self._fetch_one(code, start_date, end_date, interval, fqt=0),
+ fetch=lambda code=code: self._fetch_one(
+ code, start_date, end_date, interval, fqt=0
+ ),
)
except Exception as exc: # noqa: BLE001 - degrade to additive
logger.warning(
@@ -145,12 +149,7 @@ class DataLoader:
return result
def _fetch_one(
- self,
- code: str,
- start_date: str,
- end_date: str,
- interval: str,
- fqt: int = 1,
+ self, code: str, start_date: str, end_date: str, interval: str, fqt: int = 1,
) -> Optional[pd.DataFrame]:
"""Resolve one symbol and build its OHLCV frame, or ``None`` on a miss.
diff --git a/agent/backtest/loaders/registry.py b/agent/backtest/loaders/registry.py
index f3197e28a..e7ccf7fe4 100644
--- a/agent/backtest/loaders/registry.py
+++ b/agent/backtest/loaders/registry.py
@@ -326,7 +326,9 @@ _A_SHARE_INDEX_CODE = re.compile(r"^(?:000\d{3}\.SH|399\d{3}\.SZ|899\d{3}\.BJ)$"
#: "split_dividend_additive" does participate — it is the whole point of the
#: caliber: an additive level and a multiplicative one are two different scales,
#: so a basket holding both must be warned about (#1493).
-_COMPARABLE_CALIBERS = frozenset({"raw", "split", "split_dividend", "split_dividend_additive"})
+_COMPARABLE_CALIBERS = frozenset(
+ {"raw", "split", "split_dividend", "split_dividend_additive"}
+)
#: Calibers whose daily moves cannot be read as returns at all. Cash dividends
#: enter the price *level* as an offset rather than scaling the series, so a
@@ -336,20 +338,6 @@ _COMPARABLE_CALIBERS = frozenset({"raw", "split", "split_dividend", "split_divid
_ADDITIVE_CALIBERS = frozenset({"split_dividend_additive"})
-def frame_caliber(frame: object, source: str, market: str | None = None, symbol: str | None = None) -> str:
- """Caliber of one served frame, honoring what the loader actually served.
-
- A loader that converted its series (e.g. the additive-to-multiplicative
- path from #1541) reports the caliber on the frame itself; the static
- (source, market) table is the fallback for unconverted data.
- """
- attrs = getattr(frame, "attrs", None)
- adjustment = attrs.get("adjustment") if isinstance(attrs, dict) else None
- if isinstance(adjustment, str) and adjustment:
- return adjustment
- return price_caliber(source, market, symbol)
-
-
def market_has_corporate_actions(market: str) -> bool:
"""Whether prices in ``market`` depend on a split/dividend adjustment.
@@ -363,6 +351,19 @@ def market_has_corporate_actions(market: str) -> bool:
return market not in _NA_CALIBER_MARKETS and market not in _INDEX_CALIBER_MARKETS
+def frame_caliber(frame: object, source: str, market: str | None = None, symbol: str | None = None) -> str:
+ """Caliber of one served frame, honoring what the loader actually served.
+ A loader that converted its series (e.g. the additive-to-multiplicative
+ path from #1541) reports the caliber on the frame itself; the static
+ (source, market) table is the fallback for unconverted data.
+ """
+ attrs = getattr(frame, "attrs", None)
+ adjustment = attrs.get("adjustment") if isinstance(attrs, dict) else None
+ if isinstance(adjustment, str) and adjustment:
+ return adjustment
+ return price_caliber(source, market, symbol)
+
+
def price_caliber(source: str, market: str | None = None, symbol: str | None = None) -> str:
"""Return the adjustment caliber of ``source``'s served prices.
@@ -383,7 +384,9 @@ def price_caliber(source: str, market: str | None = None, symbol: str | None = N
market == "a_share" and symbol is not None and _A_SHARE_INDEX_CODE.match(symbol)
):
return "raw"
- return PRICE_CALIBER_BY_SOURCE_MARKET.get((source, market), PRICE_CALIBER_BY_SOURCE.get(source, "unknown"))
+ return PRICE_CALIBER_BY_SOURCE_MARKET.get(
+ (source, market), PRICE_CALIBER_BY_SOURCE.get(source, "unknown")
+ )
def mixed_caliber_warning(stamps: dict[str, tuple[str, str]]) -> str | None:
@@ -423,10 +426,16 @@ def additive_caliber_warning(stamps: dict[str, tuple[str, str]]) -> str | None:
dividends added back as a flat offset. That is the common A-share case,
since ``tencent`` heads the chain (#1493).
"""
- additive = {symbol: source for symbol, (source, caliber) in stamps.items() if caliber in _ADDITIVE_CALIBERS}
+ additive = {
+ symbol: source
+ for symbol, (source, caliber) in stamps.items()
+ if caliber in _ADDITIVE_CALIBERS
+ }
if not additive:
return None
- shown = ", ".join(f"{symbol} ({source})" for symbol, source in sorted(additive.items())[:4])
+ shown = ", ".join(
+ f"{symbol} ({source})" for symbol, source in sorted(additive.items())[:4]
+ )
if len(additive) > 4:
shown += f", +{len(additive) - 4} more"
return (
@@ -455,7 +464,9 @@ _SOURCE_ORDER_ENV_PREFIX = "MARKET_DATA_ORDER_"
# Snapshot of the chains as written above. refresh_source_order_overrides()
# restores from here; entries must never leak into FALLBACK_CHAINS by
# aliasing (always copy on restore), or a restore would mutate the snapshot.
-_DEFAULT_CHAINS: dict[str, list[str]] = {market: chain[:] for market, chain in FALLBACK_CHAINS.items()}
+_DEFAULT_CHAINS: dict[str, list[str]] = {
+ market: chain[:] for market, chain in FALLBACK_CHAINS.items()
+}
# market -> override currently in effect. Populated only by
# refresh_source_order_overrides(); absent key = default chain in effect.
@@ -529,7 +540,8 @@ def refresh_source_order_overrides() -> None:
from src.config.accessor import get_env_value
snapshot = {
- source_order_env_var(market): get_env_value(source_order_env_var(market), "") for market in _DEFAULT_CHAINS
+ source_order_env_var(market): get_env_value(source_order_env_var(market), "")
+ for market in _DEFAULT_CHAINS
}
if snapshot == _LAST_ORDER_ENV_SNAPSHOT:
return
@@ -544,7 +556,8 @@ def refresh_source_order_overrides() -> None:
continue
if order: # non-empty but invalid — warn, keep default order
logger.warning(
- "Ignoring invalid %s=%r: value must be a permutation of the default chain %s; keeping default order",
+ "Ignoring invalid %s=%r: value must be a permutation of the"
+ " default chain %s; keeping default order",
source_order_env_var(market),
raw,
default,
@@ -591,7 +604,8 @@ def resolve_loader(market: str) -> Any:
if loader.is_available():
return loader
raise NoAvailableSourceError(
- f"No available data source for market '{market}'. Tried: {tried or chain}. Check network and API token config."
+ f"No available data source for market '{market}'. "
+ f"Tried: {tried or chain}. Check network and API token config."
)
@@ -640,7 +654,8 @@ def get_loader_cls_with_fallback(source: str) -> Type[Any]:
"to api.wallex.ir.",
}.get(source, "")
raise NoAvailableSourceError(
- f"Data source '{source}' is unavailable and does not fall back to a network source. {hint}".rstrip()
+ f"Data source '{source}' is unavailable and does not fall back to a "
+ f"network source. {hint}".rstrip()
)
# Source unavailable — try same-market fallback
@@ -657,4 +672,6 @@ def get_loader_cls_with_fallback(source: str) -> Type[Any]:
except NoAvailableSourceError:
continue
- raise NoAvailableSourceError(f"Data source '{source}' is unavailable and no fallback found.")
+ raise NoAvailableSourceError(
+ f"Data source '{source}' is unavailable and no fallback found."
+ )
diff --git a/agent/backtest/loaders/tencent_loader.py b/agent/backtest/loaders/tencent_loader.py
index 5f8690453..8365be265 100644
--- a/agent/backtest/loaders/tencent_loader.py
+++ b/agent/backtest/loaders/tencent_loader.py
@@ -49,7 +49,6 @@ _PAGE_BACKOFF = 0.6
_SSL_CONTEXT = ssl.create_default_context(cafile=certifi.where())
-
def _is_a_share(code: str) -> bool:
return code.upper().endswith((".SZ", ".SH"))
@@ -122,7 +121,9 @@ class DataLoader:
start_date=start_date,
end_date=end_date,
fields=["raw"],
- fetch=lambda code=code: self._fetch_one(code, start_date, end_date, forward_adjusted=False),
+ fetch=lambda code=code: self._fetch_one(
+ code, start_date, end_date, forward_adjusted=False
+ ),
)
except Exception as exc: # noqa: BLE001 - degrade to additive
logger.warning(
@@ -145,11 +146,7 @@ class DataLoader:
return result
def _request_page(
- self,
- code: str,
- start_date: str,
- end_date: str,
- forward_adjusted: bool = True,
+ self, code: str, start_date: str, end_date: str, forward_adjusted: bool = True,
) -> Optional[pd.DataFrame]:
"""Fetch up to `_PAGE_SIZE` bars in [start_date, end_date]."""
if not _is_a_share(code) and not _is_hk_equity(code):
@@ -175,13 +172,10 @@ class DataLoader:
f"{',qfq' if forward_adjusted else ''}"
)
- req = urllib.request.Request(
- url,
- headers={
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
- "Referer": "https://web.ifzq.gtimg.cn/",
- },
- )
+ req = urllib.request.Request(url, headers={
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
+ "Referer": "https://web.ifzq.gtimg.cn/",
+ })
with urllib.request.urlopen(req, timeout=15, context=_SSL_CONTEXT) as resp:
raw = resp.read().decode("utf-8")
@@ -213,16 +207,14 @@ class DataLoader:
rows = []
for k in klines:
if len(k) >= 6:
- rows.append(
- {
- "trade_date": k[0],
- "open": float(k[1]),
- "close": float(k[2]),
- "high": float(k[3]),
- "low": float(k[4]),
- "volume": float(k[5]),
- }
- )
+ rows.append({
+ "trade_date": k[0],
+ "open": float(k[1]),
+ "close": float(k[2]),
+ "high": float(k[3]),
+ "low": float(k[4]),
+ "volume": float(k[5]),
+ })
if not rows:
return None
@@ -230,15 +222,13 @@ class DataLoader:
df = pd.DataFrame(rows)
df["trade_date"] = pd.to_datetime(df["trade_date"])
df = df.set_index("trade_date").sort_index()
- df = df[["open", "high", "low", "close", "volume"]].dropna(subset=["open", "high", "low", "close"])
+ df = df[["open", "high", "low", "close", "volume"]].dropna(
+ subset=["open", "high", "low", "close"]
+ )
return df
def _fetch_one(
- self,
- code: str,
- start_date: str,
- end_date: str,
- forward_adjusted: bool = True,
+ self, code: str, start_date: str, end_date: str, forward_adjusted: bool = True,
) -> Optional[pd.DataFrame]:
"""Paginate [start_date, end_date] in `_PAGE_SIZE`-bar windows.
@@ -264,7 +254,8 @@ class DataLoader:
for _ in range(_MAX_PAGES):
if cursor_end in seen_ends:
raise ValueError(
- f"incomplete tencent history: {code} stopped advancing at {cursor_end} before reaching {start_date}"
+ f"incomplete tencent history: {code} stopped advancing at "
+ f"{cursor_end} before reaching {start_date}"
)
seen_ends.add(cursor_end)
@@ -278,7 +269,7 @@ class DataLoader:
except Exception as exc: # noqa: BLE001 - transient network jitter
last_error = exc
if attempt < _PAGE_RETRIES - 1:
- time.sleep(_PAGE_BACKOFF * (2**attempt))
+ time.sleep(_PAGE_BACKOFF * (2 ** attempt))
if last_error is not None:
# Partial pages already collected would read as a complete
# history downstream; a failed page is an error, not a series.
@@ -300,7 +291,8 @@ class DataLoader:
page = self._request_page(code, start_date, cursor_end, forward_adjusted)
except Exception as exc: # noqa: BLE001 - transient network jitter
raise ValueError(
- f"incomplete tencent history: {code} re-request at {cursor_end} failed: {exc}"
+ f"incomplete tencent history: {code} re-request at "
+ f"{cursor_end} failed: {exc}"
) from exc
if page is None or page.empty:
break
@@ -315,7 +307,10 @@ class DataLoader:
break
cursor_end = next_end
else:
- raise ValueError(f"incomplete tencent history: {code} hit {_MAX_PAGES} pages without reaching {start_date}")
+ raise ValueError(
+ f"incomplete tencent history: {code} hit {_MAX_PAGES} pages "
+ f"without reaching {start_date}"
+ )
if not chunks:
return None
diff --git a/agent/backtest/runner.py b/agent/backtest/runner.py
index 7b3435935..e1411397f 100644
--- a/agent/backtest/runner.py
+++ b/agent/backtest/runner.py
@@ -39,7 +39,6 @@ from backtest.loaders.base import (
source_interval,
validate_ohlc,
)
-
# Symbol classification lives in ``_market_hooks`` so runner.py and
# composite.py share a single source of truth (audit-2026-05-18 B1+C1+C2).
# ``_detect_market`` is also re-exported here for back-compat with
@@ -139,7 +138,9 @@ class BacktestConfigSchema(BaseModel):
try:
pd.Timestamp(v)
except Exception:
- raise ValueError(f"invalid evaluation_start_date: {v!r} (expected YYYY-MM-DD)") from None
+ raise ValueError(
+ f"invalid evaluation_start_date: {v!r} (expected YYYY-MM-DD)"
+ ) from None
return v
@field_validator("rebalance_mask")
@@ -190,7 +191,9 @@ class BacktestConfigSchema(BaseModel):
return v
for entry in v:
if not isinstance(entry, dict):
- raise ValueError("each event_feeds entry must be an object with name/route_template/event_type")
+ raise ValueError(
+ "each event_feeds entry must be an object with name/route_template/event_type"
+ )
for key in ("name", "route_template", "event_type"):
if not str(entry.get(key, "")).strip():
raise ValueError(f"event_feeds entry missing required field: {key}")
@@ -199,9 +202,13 @@ class BacktestConfigSchema(BaseModel):
@model_validator(mode="after")
def start_before_end(self) -> "BacktestConfigSchema":
if self.rebalance_mask is not None and self.position_adjustment != "rebalance":
- raise ValueError("rebalance_mask requires position_adjustment='rebalance'")
+ raise ValueError(
+ "rebalance_mask requires position_adjustment='rebalance'"
+ )
if pd.Timestamp(self.start_date) > pd.Timestamp(self.end_date):
- raise ValueError(f"start_date ({self.start_date}) must be <= end_date ({self.end_date})")
+ raise ValueError(
+ f"start_date ({self.start_date}) must be <= end_date ({self.end_date})"
+ )
return self
@@ -309,7 +316,9 @@ def _validate_class_body(node: ast.ClassDef) -> None:
continue
if isinstance(child, ast.Pass):
continue
- raise ValueError(f"Executable class-level statement {type(child).__name__} is not allowed")
+ raise ValueError(
+ f"Executable class-level statement {type(child).__name__} is not allowed"
+ )
# --- Runtime-reachable operation scrubber (VT-001 defense-in-depth) ---
@@ -421,9 +430,10 @@ def _is_forbidden_module_path(name: str) -> bool:
return False
if name.split(".")[0] in _FORBIDDEN_IMPORT_MODULES:
return True
- return any(name == prefix or name.startswith(prefix + ".") for prefix in _FORBIDDEN_IMPORT_PREFIXES)
-
-
+ return any(
+ name == prefix or name.startswith(prefix + ".")
+ for prefix in _FORBIDDEN_IMPORT_PREFIXES
+ )
# ``os`` itself is allowed (os.path etc.), but these attributes shell out, spawn,
# or read the process environment — none has a place in a signal engine.
_FORBIDDEN_OS_ATTRS = frozenset(
@@ -488,7 +498,9 @@ _FORBIDDEN_DUNDER_ATTRS = frozenset(
"__subclasshook__",
}
)
-_FORBIDDEN_BUILTINS = frozenset({"eval", "exec", "compile", "__import__", "globals", "locals", "vars", "breakpoint"})
+_FORBIDDEN_BUILTINS = frozenset(
+ {"eval", "exec", "compile", "__import__", "globals", "locals", "vars", "breakpoint"}
+)
# getattr/setattr/delattr can indirect around the attribute scanner
# (``getattr(os, "system")("id")``). We reject them ONLY when the target object
# is ``os`` or a forbidden module — keyed off the target, not the attribute
@@ -743,8 +755,14 @@ def _scan_runtime_reachable(tree: ast.Module) -> None:
if engine_cls is None:
return
- module_funcs = {n.name: n for n in tree.body if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef))}
- worklist: list[ast.AST] = [m for m in engine_cls.body if isinstance(m, (ast.FunctionDef, ast.AsyncFunctionDef))]
+ module_funcs = {
+ n.name: n
+ for n in tree.body
+ if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef))
+ }
+ worklist: list[ast.AST] = [
+ m for m in engine_cls.body if isinstance(m, (ast.FunctionDef, ast.AsyncFunctionDef))
+ ]
aliases = _module_level_forbidden_aliases(tree)
visited: set[int] = set()
while worklist:
@@ -755,7 +773,10 @@ def _scan_runtime_reachable(tree: ast.Module) -> None:
for node in ast.walk(fn):
_reject_forbidden_node(node)
if isinstance(node, ast.Name) and node.id in aliases:
- raise ValueError(f"Use of {node.id!r}, bound at module level to {aliases[node.id]!r}, {_SCRUB_MSG}")
+ raise ValueError(
+ f"Use of {node.id!r}, bound at module level to "
+ f"{aliases[node.id]!r}, {_SCRUB_MSG}"
+ )
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
target = module_funcs.get(node.func.id)
if target is not None:
@@ -787,7 +808,9 @@ def _validate_signal_engine_source(file_path: Path) -> None:
continue
if _is_safe_constant_assignment(node):
continue
- raise ValueError(f"Executable top-level statement {type(node).__name__} is not allowed")
+ raise ValueError(
+ f"Executable top-level statement {type(node).__name__} is not allowed"
+ )
# Deep pass: the structural loop above only guards import-time execution;
# this walks the code that runs on SignalEngine().generate() (VT-001).
@@ -798,10 +821,8 @@ def _validate_signal_engine_class(engine_cls) -> None:
"""Pre-flight check: SignalEngine can be instantiated with no args and has generate()."""
sig = inspect.signature(engine_cls.__init__)
required = [
- p.name
- for p in sig.parameters.values()
- if p.name != "self"
- and p.default is inspect.Parameter.empty
+ p.name for p in sig.parameters.values()
+ if p.name != "self" and p.default is inspect.Parameter.empty
and p.kind not in (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD)
]
if required:
@@ -1029,7 +1050,11 @@ def _build_price_panel(data_map: dict[str, pd.DataFrame]) -> dict[str, pd.DataFr
"""
panel: dict[str, pd.DataFrame] = {}
for column in _PRICE_PANEL_COLUMNS:
- series_by_symbol = {symbol: frame[column] for symbol, frame in data_map.items() if column in frame.columns}
+ series_by_symbol = {
+ symbol: frame[column]
+ for symbol, frame in data_map.items()
+ if column in frame.columns
+ }
if series_by_symbol:
panel[column] = pd.DataFrame(series_by_symbol)
return panel
@@ -1071,7 +1096,7 @@ def _inject_fundamental_panel(
Returns:
The same panel dictionary with ``fund:<field>`` frames added.
"""
- fields = [column[len(_FUND_PREFIX) :] for column in fund_columns if column.startswith(_FUND_PREFIX)]
+ fields = [column[len(_FUND_PREFIX):] for column in fund_columns if column.startswith(_FUND_PREFIX)]
fields = list(dict.fromkeys(fields))
if not fields:
return panel
@@ -1163,7 +1188,6 @@ def _maybe_inject_fundamentals_for_factor_panel(
# --- Main entry ---
-
def main(run_dir: Path) -> None:
"""Load config, fetch data, run the selected backtest engine.
@@ -1196,7 +1220,6 @@ def main(run_dir: Path) -> None:
# below blocks executable top-level statements but a method body still
# runs on instantiation. See ``safe_run_dir`` for the policy.
from src.tools.path_utils import safe_run_dir
-
try:
run_dir = safe_run_dir(str(run_dir))
except ValueError as exc:
@@ -1288,7 +1311,6 @@ def main(run_dir: Path) -> None:
if engine_type == "options":
from backtest.engines.options_portfolio import run_options_backtest
-
run_options_backtest(config, loader, signal_engine, run_dir, bars_per_year=bars_per_year)
else:
market_engine = _create_market_engine(effective_source, config, codes)
@@ -1343,7 +1365,9 @@ def _observed_spacing(data_map: dict, codes: List[str]) -> float | None:
fundamental panel cannot decide the annualisation.
"""
indexes = [
- data_map[code].index for code in codes if code in data_map and len(data_map[code]) >= _MIN_BARS_FOR_SPACING
+ data_map[code].index
+ for code in codes
+ if code in data_map and len(data_map[code]) >= _MIN_BARS_FOR_SPACING
]
if not indexes:
return None
@@ -1418,7 +1442,8 @@ def _annualisation_bars(
matched = min(
_INTERVAL_SECONDS,
- key=lambda name: max(_INTERVAL_SECONDS[name], observed) / min(_INTERVAL_SECONDS[name], observed),
+ key=lambda name: max(_INTERVAL_SECONDS[name], observed)
+ / min(_INTERVAL_SECONDS[name], observed),
)
matched_spacing = _INTERVAL_SECONDS[matched]
if max(matched_spacing, observed) / min(matched_spacing, observed) >= _SPACING_MISMATCH_RATIO:
@@ -1488,7 +1513,6 @@ def _create_market_engine(source: str, config: dict, codes: List[str]):
# Cross-market -> CompositeEngine
if len(markets) > 1:
from backtest.engines.composite import CompositeEngine
-
return CompositeEngine(config, codes)
# Futures routing (Wave 2)
@@ -1496,16 +1520,13 @@ def _create_market_engine(source: str, config: dict, codes: List[str]):
# Distinguish China vs global futures by exchange suffix
if any(_is_china_futures(c) for c in codes):
from backtest.engines.china_futures import ChinaFuturesEngine
-
return ChinaFuturesEngine(config)
from backtest.engines.global_futures import GlobalFuturesEngine
-
return GlobalFuturesEngine(config)
# Forex routing (Wave 2)
if "forex" in markets:
from backtest.engines.forex import ForexEngine
-
return ForexEngine(config)
# India equity routing — must precede source-based routing because India's
@@ -1513,14 +1534,12 @@ def _create_market_engine(source: str, config: dict, codes: List[str]):
# otherwise fall through to the crypto default.
if "india_equity" in markets:
from backtest.engines.india_equity import IndiaEquityEngine
-
return IndiaEquityEngine(config)
# Korea equity routing — same reason as India: its effective source
# (``pykrx``) has no Wave-1 branch and would fall through to the default.
if "kr_equity" in markets:
from backtest.engines.korea_equity import KoreaEquityEngine
-
return KoreaEquityEngine(config)
# Vietnam equity routing — same reason as India and Korea: its effective
@@ -1528,7 +1547,6 @@ def _create_market_engine(source: str, config: dict, codes: List[str]):
# default.
if "vietnam_equity" in markets:
from backtest.engines.vietnam_equity import VietnamEquityEngine
-
return VietnamEquityEngine(config)
# Argentina market-data routing is supported, but BYMA execution rules
# are not modeled yet. Fail closed instead of silently applying US/crypto
@@ -1544,22 +1562,18 @@ def _create_market_engine(source: str, config: dict, codes: List[str]):
# default the source-based fallback would pick.
if "index" in markets:
from backtest.engines.global_equity import GlobalEquityEngine
-
return GlobalEquityEngine(config, market=_detect_submarket(codes))
# Original routing (Wave 1)
if source in ("okx", "ccxt"):
from backtest.engines.crypto import CryptoEngine
-
return CryptoEngine(config)
elif source in ("tushare", "akshare"):
if markets & {"us_equity", "hk_equity", "ca_equity", "uk_equity"}:
from backtest.engines.global_equity import GlobalEquityEngine
-
market = _detect_submarket(codes)
return GlobalEquityEngine(config, market=market)
from backtest.engines.china_a import ChinaAEngine
-
return ChinaAEngine(config)
elif source == "yfinance":
# yfinance serves crypto pairs (BTC-USDT, BTC-USD) next to equities,
@@ -1569,10 +1583,8 @@ def _create_market_engine(source: str, config: dict, codes: List[str]):
# in the config, and nothing warns.
if "crypto" in markets:
from backtest.engines.crypto import CryptoEngine
-
return CryptoEngine(config)
from backtest.engines.global_equity import GlobalEquityEngine
-
market = _detect_submarket(codes)
return GlobalEquityEngine(config, market=market)
else:
@@ -1581,7 +1593,6 @@ def _create_market_engine(source: str, config: dict, codes: List[str]):
# local AAPL.US dataset gets US-equity execution rules instead of crypto.
if markets & {"us_equity", "hk_equity", "ca_equity", "uk_equity"}:
from backtest.engines.global_equity import GlobalEquityEngine
-
market = _detect_submarket(codes)
return GlobalEquityEngine(config, market=market)
# A-shares need the same treatment. Every branchless source that serves
@@ -1592,10 +1603,8 @@ def _create_market_engine(source: str, config: dict, codes: List[str]):
# position. The run still succeeds, which is what makes it dangerous.
if "a_share" in markets:
from backtest.engines.china_a import ChinaAEngine
-
return ChinaAEngine(config)
from backtest.engines.crypto import CryptoEngine
-
return CryptoEngine(config)
@@ -1653,7 +1662,9 @@ def _fetch_auto(codes: List[str], config: dict, interval: str = "1D") -> dict:
local_result = local_loader.fetch(stripped, start_date, end_date, interval=interval)
missing_local = [code for code in local_codes if strip_local_prefix(code) not in local_result]
if missing_local:
- raise NoAvailableSourceError(f"incomplete data for source=local; missing symbols: {missing_local}")
+ raise NoAvailableSourceError(
+ f"incomplete data for source=local; missing symbols: {missing_local}"
+ )
local_name = str(getattr(local_loader, "name", "local") or "local")
served_by.add(local_name)
for code in local_result:
@@ -1684,7 +1695,9 @@ def _fetch_auto(codes: List[str], config: dict, interval: str = "1D") -> dict:
fields=fields,
interval=interval,
)
- market_result = _restore_original_codes(result, market_codes, normalized_codes)
+ market_result = _restore_original_codes(
+ result, market_codes, normalized_codes
+ )
if market_result:
served_by.add(src_name)
for code in market_result:
@@ -1702,7 +1715,9 @@ def _fetch_auto(codes: List[str], config: dict, interval: str = "1D") -> dict:
if not fb_loader.is_available():
continue
fb_codes = _normalize_codes(missing, fb_name)
- fallback_result = fb_loader.fetch(fb_codes, start_date, end_date, interval=interval)
+ fallback_result = fb_loader.fetch(
+ fb_codes, start_date, end_date, interval=interval
+ )
mapped = _restore_original_codes(fallback_result, missing, fb_codes)
if mapped:
market_result.update(mapped)
@@ -1711,14 +1726,17 @@ def _fetch_auto(codes: List[str], config: dict, interval: str = "1D") -> dict:
served_by.add(fb_served_by)
for code in mapped:
caliber_stamps[code] = (fb_served_by, frame_caliber(mapped[code], fb_served_by, market, code))
- logger.info("Runtime fallback: %s -> %s for %s", src_name, fb_name, market)
+ logger.info(
+ "Runtime fallback: %s -> %s for %s", src_name, fb_name, market
+ )
if missing:
served = sorted(set(merged) - set(missing))
raise NoAvailableSourceError(
f"incomplete data for {market}; missing symbols: {missing}; "
f"served symbols: {served}"
- ) merged.update(market_result)
+ )
+ merged.update(market_result)
config["_actual_sources"] = sorted(served_by)
config["_caliber_stamps"] = caliber_stamps
@@ -1758,7 +1776,9 @@ def fetch_data_map(config: dict) -> DataFetchResult:
# a contradictory request is refused here rather than half-served.
prefixed = [code for code in codes if strip_local_prefix(code) != code]
if prefixed and source not in ("local", "auto"):
- raise ValueError(f"local: codes need source='local' or 'auto', not {source!r}: {prefixed}")
+ raise ValueError(
+ f"local: codes need source='local' or 'auto', not {source!r}: {prefixed}"
+ )
bare = [strip_local_prefix(code) for code in codes]
repeated = sorted({symbol for symbol in bare if bare.count(symbol) > 1})
if repeated:
@@ -1773,9 +1793,9 @@ def fetch_data_map(config: dict) -> DataFetchResult:
# is only a fallback for a stubbed/patched fetcher that recorded nothing.
recorded = config.pop("_actual_sources", None)
caliber_stamps = config.pop("_caliber_stamps", None) or {}
- used_sources: list[str] = [str(name) for name in recorded or [] if str(name).strip()] or sorted(
- _group_codes_by_source(codes)
- )
+ used_sources: list[str] = [
+ str(name) for name in recorded or [] if str(name).strip()
+ ] or sorted(_group_codes_by_source(codes))
else:
codes = _normalize_codes(codes, source)
primary_source = source
@@ -1814,7 +1834,9 @@ def fetch_data_map(config: dict) -> DataFetchResult:
# as if the dataset held it. A ``local:`` code is the dataset's or nothing.
unserved_local = [code for code in prefixed if strip_local_prefix(code) in missing]
if unserved_local:
- raise NoAvailableSourceError(f"incomplete data for source=local; missing symbols: {unserved_local}")
+ raise NoAvailableSourceError(
+ f"incomplete data for source=local; missing symbols: {unserved_local}"
+ )
if missing:
logger.warning(
"source=%s returned data for %d/%d symbols; missing: %s",
@@ -1861,7 +1883,8 @@ def fetch_data_map(config: dict) -> DataFetchResult:
market_missing = [code for code in market_missing if code not in mapped]
fb_served_by = str(
getattr(fallback_loader, "name", fallback_source)
- or fallback_source )
+ or fallback_source
+ )
for code in mapped:
caliber_stamps[code] = (
fb_served_by,
@@ -1875,13 +1898,20 @@ def fetch_data_map(config: dict) -> DataFetchResult:
"Runtime fallback: %s -> %s", primary_source, fb_served_by
)
missing = [code for code in missing if code not in data_map]
+
if missing:
raise NoAvailableSourceError(
f"incomplete data for source={primary_source}; missing symbols: {missing}; "
f"served symbols: {sorted(data_map)}"
)
- data_map = {code: resample_bars(frame, requested_interval) for code, frame in _sanitize_data_map(data_map).items()}
- caliber_stamps = {code: stamp for code, stamp in caliber_stamps.items() if code in data_map}
+
+ data_map = {
+ code: resample_bars(frame, requested_interval)
+ for code, frame in _sanitize_data_map(data_map).items()
+ }
+ caliber_stamps = {
+ code: stamp for code, stamp in caliber_stamps.items() if code in data_map
+ }
# Both warnings can apply at once (a tencent+baostock basket mixes calibers
# *and* serves an additive one), and each says something the other does not,
# so they are reported together rather than one shadowing the other.
diff --git a/agent/src/market_data.py b/agent/src/market_data.py
index 4ec0f81bf..f2864a7de 100644
--- a/agent/src/market_data.py
+++ b/agent/src/market_data.py
@@ -72,6 +72,7 @@ def detect_source(code: str) -> str:
return "tushare"
+
def get_loader(source: str):
"""Get loader class via registry with fallback support."""
from backtest.loaders.registry import get_loader_cls_with_fallback
@@ -88,46 +89,10 @@ def get_loader(source: str):
#: search, fetch and grounding all agree.
FIAT_CODES = frozenset(
{
- "USD",
- "EUR",
- "GBP",
- "JPY",
- "CHF",
- "CNY",
- "CNH",
- "HKD",
- "AUD",
- "NZD",
- "CAD",
- "KRW",
- "INR",
- "SGD",
- "SEK",
- "NOK",
- "DKK",
- "MXN",
- "BRL",
- "ZAR",
- "TRY",
- "RUB",
- "PLN",
- "THB",
- "MYR",
- "IDR",
- "PHP",
- "VND",
- "ILS",
- "AED",
- "SAR",
- "EGP",
- "CZK",
- "HUF",
- "RON",
- "CLP",
- "COP",
- "PEN",
- "TWD",
- "CUP",
+ "USD", "EUR", "GBP", "JPY", "CHF", "CNY", "CNH", "HKD", "AUD", "NZD",
+ "CAD", "KRW", "INR", "SGD", "SEK", "NOK", "DKK", "MXN", "BRL", "ZAR",
+ "TRY", "RUB", "PLN", "THB", "MYR", "IDR", "PHP", "VND", "ILS", "AED",
+ "SAR", "EGP", "CZK", "HUF", "RON", "CLP", "COP", "PEN", "TWD", "CUP",
}
)
@@ -257,7 +222,12 @@ def fetch_market_data(
results: dict[str, Any] = {}
provenance: dict[str, dict[str, Any]] = {}
- result_aliases = {code: code.split(":", 1)[1] if code.lower().startswith("local:") else code for code in codes}
+ result_aliases = {
+ code: code.split(":", 1)[1]
+ if code.lower().startswith("local:")
+ else code
+ for code in codes
+ }
groups: dict[tuple[str, str], list[str]] = {}
for code in codes:
@@ -313,9 +283,16 @@ def fetch_market_data(
override = None
else:
override = (
- get_source_order_override(market) if source == "auto" and fallback_chain_provider is None else None
+ get_source_order_override(market)
+ if source == "auto"
+ and fallback_chain_provider is None
+ else None
+ )
+ candidates = (
+ list(override)
+ if override is not None and src in override
+ else [src, *chain]
)
- candidates = list(override) if override is not None and src in override else [src, *chain]
# Deduplicate (preserving order), then cap the attempt budget.
attempts: list[str] = []
for candidate in candidates:
@@ -342,7 +319,9 @@ def fetch_market_data(
try:
loader = loader_cls()
# Weekly and monthly bars are built from daily ones (#1479).
- partial = loader.fetch(remaining, start_date, end_date, interval=source_interval(interval))
+ partial = loader.fetch(
+ remaining, start_date, end_date, interval=source_interval(interval)
+ )
partial = {
code: resample_bars(frame, interval) if hasattr(frame, "groupby") else frame
for code, frame in (partial or {}).items()
@@ -350,9 +329,7 @@ def fetch_market_data(
except Exception as exc: # noqa: BLE001 — contained per-symbol fallback
logger.error(
"market-data loader %r failed for %s; trying next source in chain: %s",
- attempt_src,
- remaining,
- exc,
+ attempt_src, remaining, exc,
)
continue
if not partial:
@@ -371,16 +348,15 @@ def fetch_market_data(
if used_source and used_source != src:
logger.info(
"market-data source %r unavailable for %s; fell back to %r",
- src,
- src_codes,
- used_source,
+ src, src_codes, used_source,
)
- served_elsewhere = sorted({serve_src for serve_src, _ in symbol_sources.values()} - {used_source})
+ served_elsewhere = sorted(
+ {serve_src for serve_src, _ in symbol_sources.values()} - {used_source}
+ )
if served_elsewhere:
logger.info(
"market-data per-symbol fallback: %s served the remaining symbols %r could not",
- served_elsewhere,
- src,
+ served_elsewhere, src,
)
return data_map, used_source, provider_cls, symbol_sources
@@ -407,10 +383,6 @@ def fetch_market_data(
currency_conversion = frame_attrs.get("currency_conversion")
if not isinstance(currency_conversion, str) or not currency_conversion:
currency_conversion = "none"
- # A loader that converted its series reports the caliber it actually
- # served on the frame; the static (source, market) table is the
- # fallback for unconverted data (#1541).
- adjustment = frame_caliber(df, used_source or src, market, symbol)
entry: dict[str, Any] = {
"source": used_source or src,
"requested_source": source,
@@ -418,7 +390,10 @@ def fetch_market_data(
"fallback_used": bool(used_source and used_source != src),
"currency_conversion": currency_conversion,
"volume_unit": volume_units.get(market),
- "adjustment": adjustment,
+ # A loader that converted its series reports the caliber it
+ # actually served on the frame; the static (source, market)
+ # table is the fallback for unconverted data (#1541).
+ "adjustment": frame_caliber(df, used_source or src, market, symbol),
}
quote_currency = frame_attrs.get("quote_currency")
if isinstance(quote_currency, str) and quote_currency:
@@ -428,19 +403,24 @@ def fetch_market_data(
provenance[symbol] = entry
for (src, market), src_codes in groups.items():
- data_map, used_source, provider_cls, symbol_sources = _fetch_via_chain(src, market, src_codes)
+ data_map, used_source, provider_cls, symbol_sources = _fetch_via_chain(
+ src, market, src_codes
+ )
for symbol, df in data_map.items():
- symbol_source, symbol_provider_cls = symbol_sources.get(symbol, (used_source, provider_cls))
+ symbol_source, symbol_provider_cls = symbol_sources.get(
+ symbol, (used_source, provider_cls)
+ )
_emit(
- symbol,
- df,
- src=src,
- used_source=symbol_source,
- provider_cls=symbol_provider_cls,
+ symbol, df,
+ src=src, used_source=symbol_source, provider_cls=symbol_provider_cls,
market=market,
)
- unresolved = [code for code in codes if code not in results and result_aliases[code] not in results]
+ unresolved = [
+ code
+ for code in codes
+ if code not in results and result_aliases[code] not in results
+ ]
# Canadian venue-alias fallback: TSX (.TO) <-> TSX Venture (.V).
#
@@ -479,25 +459,21 @@ def fetch_market_data(
"resolved_symbol": sibling,
}
logger.info(
- "market-data venue alias %s -> %s (source=%s)",
- code,
- sibling,
- src,
+ "market-data venue alias %s -> %s (source=%s)", code, sibling, src,
)
unresolved.remove(code)
continue
# Sibling not already resolved — targeted re-fetch of just that
# symbol through the same market's source chain.
- sibling_data, used_source, provider_cls, _ = _fetch_via_chain(src, market, [sibling])
+ sibling_data, used_source, provider_cls, _ = _fetch_via_chain(
+ src, market, [sibling]
+ )
if sibling_data:
df = next(iter(sibling_data.values()))
if df is not None and not df.empty:
_emit(
- code,
- df,
- src=src,
- used_source=used_source,
- provider_cls=provider_cls,
+ code, df,
+ src=src, used_source=used_source, provider_cls=provider_cls,
market=market,
extra_provenance={
"venue_fallback": True,
@@ -506,9 +482,7 @@ def fetch_market_data(
)
logger.info(
"market-data venue fallback %s -> %s (source=%s)",
- code,
- sibling,
- src,
+ code, sibling, src,
)
unresolved.remove(code)
diff --git a/agent/tests/test_additive_conversion.py b/agent/tests/test_additive_conversion.py
index 18d87e3f0..aa00f5d1e 100644
--- a/agent/tests/test_additive_conversion.py
+++ b/agent/tests/test_additive_conversion.py
@@ -113,6 +113,22 @@ class TestConvertAdditiveToMultiplicative:
additive = _frame([95.0, 95.0, 98.0, 96.0, 96.0])
assert convert_additive_to_multiplicative(raw, additive) is None
+ def test_dividend_on_the_last_bar_refuses_the_window(self):
+ # Dividends of 0.30 at bar 50 and 0.03 on the final bar of a flat
+ # 10.00 series. The final bar is its own offset plateau, and with no
+ # successor to classify it against the fold cannot tell it from a
+ # wobble; converting would silently drop the 0.03.
+ raw = _frame([10.0] * 50 + [9.7] * 49 + [9.67])
+ additive = _frame([9.67] * 100)
+ assert convert_additive_to_multiplicative(raw, additive) is None
+
+ def test_dividend_on_the_second_bar_refuses_the_window(self):
+ # The mirror at the window start: dividends of 0.03 at bar 1 and 0.30
+ # at bar 50 leave bar 0 as a one-bar plateau with no predecessor.
+ raw = _frame([10.0] + [9.97] * 49 + [9.67] * 50)
+ additive = _frame([9.67] * 100)
+ assert convert_additive_to_multiplicative(raw, additive) is None
+
def test_misaligned_calendars_fail_closed(self):
raw, additive = _two_action_series()
converted = convert_additive_to_multiplicative(raw.iloc[:-1], additive)