Skip to content

"Vibe-Trading: Your Personal Trading Agent"

feat(data): add explicit Binance USD-M symbol contract

zygoradc8337d6c315
diff --git a/agent/backtest/loaders/ccxt_loader.py b/agent/backtest/loaders/ccxt_loader.py
index e59ca1bd7..c01d0f149 100644
--- a/agent/backtest/loaders/ccxt_loader.py
+++ b/agent/backtest/loaders/ccxt_loader.py
@@ -9,6 +9,7 @@ from __future__ import annotations
import logging
import os
+import re
import time
from typing import Dict, List, Optional
@@ -40,6 +41,19 @@ _CCXT_TIMEOUT_MS = positive_env_int("CCXT_TIMEOUT_MS", 15_000)
_CCXT_FETCH_BUDGET_S = positive_env_float("CCXT_FETCH_BUDGET_S", 60.0)
+def _parse_ccxt_symbol(code: str) -> tuple[str, str]:
+ """Return the canonical CCXT symbol and instrument type for ``code``."""
+ normalized = code.strip().upper()
+ if normalized.endswith("-PERP"):
+ match = re.fullmatch(r"([A-Z0-9]+)-USDT-PERP", normalized)
+ if match is None:
+ raise ValueError(
+ "USD-M perpetual symbol must use BASE-USDT-PERP, e.g. BTC-USDT-PERP"
+ )
+ return f"{match.group(1)}/USDT:USDT", "swap"
+ return normalized.replace("-", "/"), "spot"
+
+
def _first_proxy_env(*names: str) -> str:
for name in names:
value = os.getenv(name, "").strip() # noqa: env-gate — system proxy vars
@@ -137,7 +151,7 @@ class DataLoader:
result: Dict[str, pd.DataFrame] = {}
for code in codes:
try:
- ccxt_symbol = code.replace("-", "/").upper()
+ ccxt_symbol, _instrument_type = _parse_ccxt_symbol(code)
df = cached_loader_fetch(
source=self.name,
symbol=code,
diff --git a/agent/tests/test_ccxt_perpetual_loader.py b/agent/tests/test_ccxt_perpetual_loader.py
new file mode 100644
index 000000000..8c313b53c
--- /dev/null
+++ b/agent/tests/test_ccxt_perpetual_loader.py
@@ -0,0 +1,35 @@
+"""Historical Binance USD-M data-contract tests."""
+
+from __future__ import annotations
+
+import pytest
+
+from backtest.loaders.base import make_loader_cache_key
+from backtest.loaders.ccxt_loader import _parse_ccxt_symbol
+
+
+def test_spot_symbol_keeps_existing_ccxt_contract() -> None:
+ assert _parse_ccxt_symbol("BTC-USDT") == ("BTC/USDT", "spot")
+
+
+def test_perpetual_symbol_maps_to_binance_usdm_contract() -> None:
+ assert _parse_ccxt_symbol("BTC-USDT-PERP") == ("BTC/USDT:USDT", "swap")
+
+
[email protected]("code", ["BTC-PERP", "-USDT-PERP", "BTC--PERP"])
+def test_malformed_perpetual_symbol_is_rejected(code: str) -> None:
+ with pytest.raises(ValueError, match="USD-M perpetual symbol"):
+ _parse_ccxt_symbol(code)
+
+
+def test_spot_and_perpetual_cache_keys_cannot_collide() -> None:
+ common = {
+ "source": "ccxt",
+ "timeframe": "1H",
+ "start_date": "2024-01-01",
+ "end_date": "2024-01-31",
+ "fields": None,
+ }
+ spot = make_loader_cache_key(symbol="BTC-USDT", **common)
+ perpetual = make_loader_cache_key(symbol="BTC-USDT-PERP", **common)
+ assert spot != perpetual