"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(metrics): add missing annualisation entries for 15 data sources (#891)
卫斯李348a25b8f85b
diff --git a/agent/backtest/metrics.py b/agent/backtest/metrics.pyindex 55712b976..21a8b6b07 100644--- a/agent/backtest/metrics.py+++ b/agent/backtest/metrics.py@@ -20,16 +20,97 @@ from backtest.models import TradeRecord# sessions are marginally longer (~330 min) — an approximation in line with the# rest of this annualisation table; the key fix is that intraday mootdx/futu no# longer fall back to the bars_per_day=1 default, which mis-annualised vol/Sharpe.-_TRADING_DAYS = {"tushare": 252, "yfinance": 252, "okx": 365, "akshare": 252, "ccxt": 365, "mootdx": 252, "futu": 252, "mt5": 260}+_TRADING_DAYS = {+ # existing+ "tushare": 252, "yfinance": 252, "okx": 365, "akshare": 252, "ccxt": 365,+ "mootdx": 252, "futu": 252, "mt5": 260,+ # crypto+ "binance": 365,+ # A-share equity+ "baostock": 252, "tencent": 252, "eastmoney": 252, "sina": 252,+ # US / international equity+ "yahoo": 252, "finnhub": 252, "alphavantage": 252, "tiingo": 252,+ "fmp": 252, "stooq": 252, "longbridge": 252,+ # resampling sources (local files / paid data — interval depends on source+ # data granularity; model as US equity session as a conservative default)+ "local": 252, "qveris": 252,+ # Indian equity+ "india_broker": 252,+}# mt5 is a forex/CFD feed: 24x5 sessions → 260 trading days, 24h intraday bars.+# US equity (yfinance-style): 6.5h sessions → 390 1m bars/day.+# A-share equity (tushare-style): 4.0h sessions → 240 1m bars/day.+# Crypto (okx/ccxt-style): 24h sessions → 1440 1m bars/day.+# Indian equity: 6.25h sessions → 375 1m bars/day._BARS_PER_DAY = {- "1m": {"tushare": 240, "okx": 1440, "yfinance": 390, "akshare": 240, "ccxt": 1440, "mootdx": 240, "futu": 240, "mt5": 1440},- "5m": {"tushare": 48, "okx": 288, "yfinance": 78, "akshare": 48, "ccxt": 288, "mootdx": 48, "futu": 48, "mt5": 288},- "15m": {"tushare": 16, "okx": 96, "yfinance": 26, "akshare": 16, "ccxt": 96, "mootdx": 16, "futu": 16, "mt5": 96},- "30m": {"tushare": 8, "okx": 48, "yfinance": 13, "akshare": 8, "ccxt": 48, "mootdx": 8, "futu": 8, "mt5": 48},- "1H": {"tushare": 4, "okx": 24, "yfinance": 7, "akshare": 4, "ccxt": 24, "mootdx": 4, "futu": 4, "mt5": 24},- "4H": {"tushare": 1, "okx": 6, "yfinance": 2, "akshare": 1, "ccxt": 6, "mootdx": 1, "futu": 1, "mt5": 6},- "1D": {"tushare": 1, "okx": 1, "yfinance": 1, "akshare": 1, "ccxt": 1, "mootdx": 1, "futu": 1, "mt5": 1},+ # --- US/international equity (6.5h session) ---+ "1m": {"yfinance": 390, "yahoo": 390, "finnhub": 390, "alphavantage": 390,+ "tiingo": 390, "fmp": 390, "stooq": 390, "longbridge": 390,+ "local": 390, "qveris": 390,+ # A-share equity (4.0h session)+ "tushare": 240, "akshare": 240, "baostock": 240, "tencent": 240,+ "eastmoney": 240, "sina": 240, "mootdx": 240, "futu": 240,+ # crypto (24h)+ "okx": 1440, "ccxt": 1440, "binance": 1440,+ # forex/CFD (24h intraday)+ "mt5": 1440,+ # Indian equity (6.25h session)+ "india_broker": 375,+ },+ "5m": {"yfinance": 78, "yahoo": 78, "finnhub": 78, "alphavantage": 78,+ "tiingo": 78, "fmp": 78, "stooq": 78, "longbridge": 78,+ "local": 78, "qveris": 78,+ "tushare": 48, "akshare": 48, "baostock": 48, "tencent": 48,+ "eastmoney": 48, "sina": 48, "mootdx": 48, "futu": 48,+ "okx": 288, "ccxt": 288, "binance": 288,+ "mt5": 288,+ "india_broker": 75,+ },+ "15m": {"yfinance": 26, "yahoo": 26, "finnhub": 26, "alphavantage": 26,+ "tiingo": 26, "fmp": 26, "stooq": 26, "longbridge": 26,+ "local": 26, "qveris": 26,+ "tushare": 16, "akshare": 16, "baostock": 16, "tencent": 16,+ "eastmoney": 16, "sina": 16, "mootdx": 16, "futu": 16,+ "okx": 96, "ccxt": 96, "binance": 96,+ "mt5": 96,+ "india_broker": 25,+ },+ "30m": {"yfinance": 13, "yahoo": 13, "finnhub": 13, "alphavantage": 13,+ "tiingo": 13, "fmp": 13, "stooq": 13, "longbridge": 13,+ "local": 13, "qveris": 13,+ "tushare": 8, "akshare": 8, "baostock": 8, "tencent": 8,+ "eastmoney": 8, "sina": 8, "mootdx": 8, "futu": 8,+ "okx": 48, "ccxt": 48, "binance": 48,+ "mt5": 48,+ "india_broker": 13,+ },+ "1H": {"yfinance": 7, "yahoo": 7, "finnhub": 7, "alphavantage": 7,+ "tiingo": 7, "fmp": 7, "stooq": 7, "longbridge": 7,+ "local": 7, "qveris": 7,+ "tushare": 4, "akshare": 4, "baostock": 4, "tencent": 4,+ "eastmoney": 4, "sina": 4, "mootdx": 4, "futu": 4,+ "okx": 24, "ccxt": 24, "binance": 24,+ "mt5": 24,+ "india_broker": 7,+ },+ "4H": {"yfinance": 2, "yahoo": 2, "finnhub": 2, "alphavantage": 2,+ "tiingo": 2, "fmp": 2, "stooq": 2, "longbridge": 2,+ "local": 2, "qveris": 2,+ "tushare": 1, "akshare": 1, "baostock": 1, "tencent": 1,+ "eastmoney": 1, "sina": 1, "mootdx": 1, "futu": 1,+ "okx": 6, "ccxt": 6, "binance": 6,+ "mt5": 6,+ "india_broker": 2,+ },+ "1D": {"yfinance": 1, "yahoo": 1, "finnhub": 1, "alphavantage": 1,+ "tiingo": 1, "fmp": 1, "stooq": 1, "longbridge": 1,+ "local": 1, "qveris": 1,+ "tushare": 1, "akshare": 1, "baostock": 1, "tencent": 1,+ "eastmoney": 1, "sina": 1, "mootdx": 1, "futu": 1,+ "okx": 1, "ccxt": 1, "binance": 1,+ "mt5": 1,+ "india_broker": 1,+ },}# Runner/loaders also emit these aliases; map them onto the table keys above.@@ -57,15 +138,16 @@ def calc_bars_per_year(interval: str = "1D", source: str = "tushare") -> int:Args:interval: Bar size (1m / 5m / 15m / 30m / 1H / 4H / 1D), case-insensitive- for hour/day tokens.- source: Data source (tushare / yfinance / okx). ``yahoo`` aliases to- ``yfinance``; ``binance`` aliases to ``ccxt``.+ like loaders accept (``1h`` → ``1H``, ``4h`` → ``4H``, ``1d`` → ``1D``).+ source: Data source (any VALID_SOURCES entry). Defaults to 252 days, 1 bar/day+ when source is missing from the table.Returns:Bars per year."""- source_key = _SOURCE_ALIASES.get(str(source or "").strip().lower(), source)interval_key = _normalize_interval(interval)+ source_key = str(source or "").strip().lower()+ source_key = _SOURCE_ALIASES.get(source_key, source_key)trading_days = _TRADING_DAYS.get(source_key, 252)bars_per_day = _BARS_PER_DAY.get(interval_key, {}).get(source_key, 1)return trading_days * bars_per_daydiff --git a/agent/tests/test_metrics.py b/agent/tests/test_metrics.pyindex 15c1aa33d..fddcbb4f3 100644--- a/agent/tests/test_metrics.py+++ b/agent/tests/test_metrics.py@@ -92,6 +92,105 @@ class TestBarsPerYear:# Falls back to 1 bar/dayassert calc_bars_per_year("2H", "tushare") == 252+ # ── newly covered sources (#884) ──++ # resampling sources (local files / paid data, default to US equity session)+ def test_resampling_qveris(self) -> None:+ assert calc_bars_per_year("1D", "qveris") == 252+ assert calc_bars_per_year("1m", "qveris") == 252 * 390+ assert calc_bars_per_year("1H", "qveris") == 252 * 7++ def test_resampling_local(self) -> None:+ assert calc_bars_per_year("1D", "local") == 252+ assert calc_bars_per_year("1m", "local") == 252 * 390++ # crypto (365-day) sources+ def test_crypto_binance(self) -> None:+ assert calc_bars_per_year("1D", "binance") == 365+ assert calc_bars_per_year("1m", "binance") == 365 * 1440+ assert calc_bars_per_year("1H", "binance") == 365 * 24++ # A-share equity (252-day, 240-min session) sources+ def test_ashare_baostock(self) -> None:+ assert calc_bars_per_year("1D", "baostock") == 252+ assert calc_bars_per_year("1m", "baostock") == 252 * 240++ def test_ashare_tencent(self) -> None:+ assert calc_bars_per_year("1D", "tencent") == 252+ assert calc_bars_per_year("1m", "tencent") == 252 * 240++ def test_ashare_eastmoney(self) -> None:+ assert calc_bars_per_year("1D", "eastmoney") == 252+ assert calc_bars_per_year("1m", "eastmoney") == 252 * 240++ def test_ashare_sina(self) -> None:+ assert calc_bars_per_year("1D", "sina") == 252+ assert calc_bars_per_year("1m", "sina") == 252 * 240++ # US equity (252-day, 390-min session) sources+ def test_us_yahoo(self) -> None:+ assert calc_bars_per_year("1D", "yahoo") == 252+ assert calc_bars_per_year("1m", "yahoo") == 252 * 390+ # same as yfinance+ assert calc_bars_per_year("1m", "yahoo") == calc_bars_per_year("1m", "yfinance")++ def test_us_finnhub(self) -> None:+ assert calc_bars_per_year("1D", "finnhub") == 252+ assert calc_bars_per_year("1m", "finnhub") == 252 * 390++ def test_us_alphavantage(self) -> None:+ assert calc_bars_per_year("1D", "alphavantage") == 252+ assert calc_bars_per_year("1m", "alphavantage") == 252 * 390++ def test_us_tiingo(self) -> None:+ assert calc_bars_per_year("1D", "tiingo") == 252+ assert calc_bars_per_year("1m", "tiingo") == 252 * 390++ def test_us_fmp(self) -> None:+ assert calc_bars_per_year("1D", "fmp") == 252+ assert calc_bars_per_year("1m", "fmp") == 252 * 390++ def test_us_stooq(self) -> None:+ assert calc_bars_per_year("1D", "stooq") == 252+ assert calc_bars_per_year("1m", "stooq") == 252 * 390++ def test_us_longbridge(self) -> None:+ assert calc_bars_per_year("1D", "longbridge") == 252+ assert calc_bars_per_year("1m", "longbridge") == 252 * 390++ # Indian equity (252-day, 375-min session)+ def test_india_broker(self) -> None:+ assert calc_bars_per_year("1D", "india_broker") == 252+ assert calc_bars_per_year("1m", "india_broker") == 252 * 375+ assert calc_bars_per_year("5m", "india_broker") == 252 * 75+ assert calc_bars_per_year("1H", "india_broker") == 252 * 7++ # Verify the default for every VALID_SOURCES entry is not the misleading 252×1 at intraday+ def test_no_intraday_source_falls_to_default(self) -> None:+ """Every entry in VALID_SOURCES must have a trading-days lookup."""+ from backtest.loaders.registry import VALID_SOURCES+ from backtest.metrics import _TRADING_DAYS, _BARS_PER_DAY++ # A source is "covered" if it has an entry in _TRADING_DAYS AND in at+ # least one _BARS_PER_DAY interval layer. Both tables must be kept in+ # sync when new loaders are registered.+ trading_covered = set(_TRADING_DAYS)+ bars_covered = set()+ for interval_layer in _BARS_PER_DAY.values():+ bars_covered |= set(interval_layer)+ covered = trading_covered & bars_covered++ unregistered = (VALID_SOURCES - {"auto"}) - covered+ assert not unregistered, (+ f"sources missing annualisation entries: {sorted(unregistered)}"+ )++ for src in sorted(VALID_SOURCES - {"auto"}):+ bp = calc_bars_per_year("1D", src)+ assert bp > 0, f"source {src!r} 1D returned {bp}"+ bp_1m = calc_bars_per_year("1m", src)+ assert bp_1m > 0, f"source {src!r} 1m returned {bp_1m}"+def test_lowercase_hour_matches_uppercase(self) -> None:# Loaders accept 1h/4h after interval-map fixes; annualisation must too.assert calc_bars_per_year("1h", "okx") == calc_bars_per_year("1H", "okx")@@ -106,6 +205,15 @@ class TestBarsPerYear:assert calc_bars_per_year("1H", "yahoo") == calc_bars_per_year("1H", "yfinance")assert calc_bars_per_year("1H", "yahoo") == 252 * 7+ def test_source_is_case_insensitive(self) -> None:+ # source is normalised to lowercase before lookup, mirroring interval+ # normalisation — regression guard for the alias layer that used to+ # skip .strip().lower() on the fallback path.+ assert calc_bars_per_year("1m", "Yahoo") == calc_bars_per_year("1m", "yahoo")+ assert calc_bars_per_year("1m", "OKX") == calc_bars_per_year("1m", "okx")+ assert calc_bars_per_year("1m", "Yahoo") == 252 * 390+ assert calc_bars_per_year("1m", "OKX") == 365 * 1440+# ---------------------------------------------------------------------------# win_rate_and_stats