"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(etoro): browse crypto via market-data instrumentTypeId
Ofek Shakedaf142ae0041d
diff --git a/agent/src/tools/trading_connector_tool.py b/agent/src/tools/trading_connector_tool.pyindex 1b2e5f1d5..3f53923e3 100644--- a/agent/src/tools/trading_connector_tool.py+++ b/agent/src/tools/trading_connector_tool.py@@ -550,19 +550,31 @@ class EtoroSearchInstrumentsTool(BaseTool):name = "etoro_search_instruments"description = (- "Search eToro instruments by ticker (BTC, AAPL) or free text. "- "Tickers use exact internalSymbolFull lookup; discovery uses fuzzy search."+ "Search eToro instruments by ticker (BTC, AAPL), free text, or asset class "+ "(crypto, stocks, forex). Tickers use exact internalSymbolFull lookup; "+ "asset-class labels browse via instrumentTypeId on /market-data/instruments.")parameters = {"type": "object","properties": {**ETORO_TOOL_PARAMETERS,- "query": {"type": "string", "description": "Ticker or search text (e.g. BTC, Apple)."},+ "query": {+ "type": "string",+ "description": "Ticker, search text (e.g. Apple), or asset class (e.g. crypto).",+ },"limit": {"type": "integer", "description": "Max results (default 10, max 50)."},"mode": {"type": "string",- "enum": ["auto", "symbol", "discover"],- "description": "auto: ticker exact lookup then fuzzy; symbol: exact only; discover: fuzzy only.",+ "enum": ["auto", "symbol", "discover", "type"],+ "description": "auto: ticker exact lookup then fuzzy; symbol: exact only; discover: fuzzy only; type: browse by instrument_type_id.",+ },+ "instrument_type_id": {+ "type": "integer",+ "description": "eToro instrumentTypeID (1=Forex … 10=Crypto). Browse mode when set.",+ },+ "include_rates": {+ "type": "boolean",+ "description": "When browsing by type, attach bid/ask/last from /market-data/instruments/rates.",},},"required": ["query"],@@ -575,6 +587,8 @@ class EtoroSearchInstrumentsTool(BaseTool):overrides = _etoro_overrides(kwargs)limit = _int_or_none(kwargs.get("limit"), "limit") or 10mode = str(kwargs.get("mode") or "auto")+ instrument_type_id = _int_or_none(kwargs.get("instrument_type_id"), "instrument_type_id")+ include_rates = bool(kwargs.get("include_rates", False))except InvalidTradingArgument as exc:return _json_result({"status": "error", "error": str(exc)})try:@@ -584,6 +598,8 @@ class EtoroSearchInstrumentsTool(BaseTool):_connection(kwargs.get("connection")),limit=limit,mode=mode,+ instrument_type_id=instrument_type_id,+ include_rates=include_rates,**overrides,))diff --git a/agent/src/trading/connectors/etoro/classification.py b/agent/src/trading/connectors/etoro/classification.pyindex 20b5de63f..58ede74ed 100644--- a/agent/src/trading/connectors/etoro/classification.py+++ b/agent/src/trading/connectors/etoro/classification.py@@ -18,6 +18,7 @@ ETORO_TOOL_CLASS: dict[str, ToolClass] = {"get_quote": ToolClass.READ,"get_historical_bars": ToolClass.READ,"search_instruments": ToolClass.READ,+ "list_instruments_by_type": ToolClass.READ,"get_instrument_metadata": ToolClass.READ,"copy_poll": ToolClass.READ,"copy_precheck": ToolClass.READ,diff --git a/agent/src/trading/connectors/etoro/client.py b/agent/src/trading/connectors/etoro/client.pyindex e1234bd52..afe275ad8 100644--- a/agent/src/trading/connectors/etoro/client.py+++ b/agent/src/trading/connectors/etoro/client.py@@ -28,6 +28,11 @@ CONFIG_FILENAME = "etoro.json"BASE_URL = "https://public-api.etoro.com"PAPER_GUARD = "path_separated_key_bound"+# Market-data routes are profile-neutral (no demo/real path prefix).+MARKET_DATA_INSTRUMENTS_PATH = "/api/v1/market-data/instruments"+MARKET_DATA_RATES_PATH = "/api/v1/market-data/instruments/rates"+MARKET_DATA_SEARCH_PATH = "/api/v1/market-data/search"+PROFILE_ENVIRONMENTS = {"paper": "paper","live-readonly": "live",diff --git a/agent/src/trading/connectors/etoro/instruments.py b/agent/src/trading/connectors/etoro/instruments.pyindex 5f3a75de0..86d214545 100644--- a/agent/src/trading/connectors/etoro/instruments.py+++ b/agent/src/trading/connectors/etoro/instruments.py@@ -3,8 +3,10 @@eToro Public API conventions (see eToro builders docs):- Exact ticker lookup: ``GET /market-data/search?internalSymbolFull=BTC``- Fuzzy discovery: ``GET /market-data/search?search=<query>&limit=<n>``+- Asset-class browse: ``GET /market-data/instruments?instrumentTypeId=<n>``- Search responses use ``items[]`` with ``instrumentId`` (lowercase ``d``).- Metadata enrichment: ``GET /market-data/instruments?instrumentIds=...``+- Quotes (all profiles): ``GET /market-data/instruments/rates?instrumentIds=...``"""from __future__ import annotations@@ -12,7 +14,14 @@ from __future__ import annotationsimport refrom typing import Any-from src.trading.connectors.etoro.client import EtoroAPIError, EtoroConfig, make_client+from src.trading.connectors.etoro.client import (+ EtoroAPIError,+ EtoroConfig,+ MARKET_DATA_INSTRUMENTS_PATH,+ MARKET_DATA_RATES_PATH,+ MARKET_DATA_SEARCH_PATH,+ make_client,+)# Common aliases → eToro ``internalSymbolFull`` tickers._SYMBOL_ALIASES: dict[str, str] = {@@ -29,6 +38,47 @@ _INVALID_INSTRUMENT_IDS = frozenset({-100000})_TICKER_RE = re.compile(r"^[A-Za-z0-9./_-]{1,24}$")+# Official eToro ``instrumentTypeID`` values (Public API market-data catalog).+INSTRUMENT_TYPE_IDS: dict[int, str] = {+ 1: "forex",+ 2: "commodity",+ 3: "cfd",+ 4: "indices",+ 5: "stocks",+ 6: "etf",+ 7: "bonds",+ 8: "trust_funds",+ 9: "options",+ 10: "crypto",+}++_INSTRUMENT_TYPE_ALIASES: dict[str, int] = {+ "forex": 1,+ "fx": 1,+ "commodity": 2,+ "commodities": 2,+ "cfd": 3,+ "cfds": 3,+ "indices": 4,+ "index": 4,+ "stocks": 5,+ "stock": 5,+ "equity": 5,+ "equities": 5,+ "etf": 6,+ "etfs": 6,+ "bond": 7,+ "bonds": 7,+ "trustfund": 8,+ "trustfunds": 8,+ "trust_funds": 8,+ "option": 9,+ "options": 9,+ "crypto": 10,+ "cryptocurrency": 10,+ "cryptocurrencies": 10,+}+def _base_payload(cfg: EtoroConfig) -> dict[str, Any]:return {@@ -44,15 +94,23 @@ def search_instruments(*,limit: int = 10,mode: str = "auto",+ instrument_type_id: int | None = None,+ include_rates: bool = False,) -> dict[str, Any]:- """Search eToro instruments by ticker or free-text query.+ """Search eToro instruments by ticker, free-text query, or asset class.Args:- query: Symbol (``BTC``, ``AAPL``) or discovery text (``bitcoin``, sector name).+ query: Symbol (``BTC``, ``AAPL``), discovery text, or asset-class label+ (``crypto``, ``stocks``, ``forex``).config: Optional connector config.limit: Max results (capped at 50).mode: ``auto`` (ticker → exact lookup then fuzzy), ``symbol`` (exact only),- or ``discover`` (fuzzy ``search`` param only).+ ``discover`` (fuzzy ``search`` param only), or ``type`` (browse by+ ``instrument_type_id`` / asset-class alias).+ instrument_type_id: Optional eToro ``instrumentTypeID`` filter (e.g. ``10``+ for crypto). When set, uses ``GET /market-data/instruments``.+ include_rates: When browsing by type, attach bid/ask/last from the flat+ ``/market-data/instruments/rates`` endpoint (works on all profiles)."""from src.trading.connectors.etoro.client import load_config@@ -63,8 +121,25 @@ def search_instruments(clean_limit = max(1, min(int(limit), 50))clean_mode = str(mode or "auto").strip().lower()- if clean_mode not in ("auto", "symbol", "discover"):- raise EtoroAPIError("mode must be 'auto', 'symbol', or 'discover'")+ if clean_mode not in ("auto", "symbol", "discover", "type"):+ raise EtoroAPIError("mode must be 'auto', 'symbol', 'discover', or 'type'")++ resolved_type_id = instrument_type_id+ if resolved_type_id is None:+ resolved_type_id = _instrument_type_id_from_query(token)++ if resolved_type_id is not None or clean_mode == "type":+ if resolved_type_id is None:+ raise EtoroAPIError(+ "instrument_type_id is required for type browse "+ "(e.g. 10 for crypto) or use an asset-class query like 'crypto'"+ )+ return list_instruments_by_type(+ resolved_type_id,+ cfg,+ limit=clean_limit,+ include_rates=include_rates,+ )rows: list[dict[str, Any]] = []lookup_mode = clean_mode@@ -98,6 +173,51 @@ def search_instruments(}+def list_instruments_by_type(+ instrument_type_id: int,+ config: EtoroConfig | None = None,+ *,+ limit: int = 10,+ include_rates: bool = False,+) -> dict[str, Any]:+ """List tradable instruments for an eToro ``instrumentTypeID`` (e.g. ``10`` = crypto)."""+ from src.trading.connectors.etoro.client import load_config++ cfg = config or load_config()+ type_id = int(instrument_type_id)+ if type_id not in INSTRUMENT_TYPE_IDS:+ raise EtoroAPIError(f"unsupported instrument_type_id {type_id}")++ clean_limit = max(1, min(int(limit), 50))+ payload = make_client(cfg).request(+ "GET",+ MARKET_DATA_INSTRUMENTS_PATH,+ params={"instrumentTypeId": type_id},+ allow_retry=True,+ )+ items = _extract_metadata_items(payload)+ filtered = [+ item+ for item in items+ if isinstance(item, dict) and _instrument_type_id(item) == type_id+ ]+ normalized = [_normalize_metadata_row(item) for item in filtered]+ normalized = [row for row in normalized if row.get("instrument_id") is not None]+ normalized.sort(key=lambda row: str(row.get("symbol") or row.get("display_name") or ""))+ instruments = normalized[:clean_limit]+ if include_rates:+ _attach_rates(cfg, instruments)++ return {+ "status": "ok",+ **_base_payload(cfg),+ "instrument_type_id": type_id,+ "instrument_type": INSTRUMENT_TYPE_IDS[type_id],+ "mode": "type",+ "instruments": instruments,+ }++def resolve_instrument_id(symbol: str, cfg: EtoroConfig) -> int:"""Resolve a user symbol to an eToro ``instrumentId`` for trading APIs."""token = str(symbol or "").strip()@@ -153,7 +273,7 @@ def get_instrument_metadata(payload = make_client(cfg).request("GET",- "/api/v1/market-data/instruments",+ MARKET_DATA_INSTRUMENTS_PATH,params={"instrumentIds": ",".join(str(i) for i in ids)},allow_retry=True,)@@ -185,7 +305,7 @@ def _lookup_variants(token: str, canonical: str) -> list[str]:def _search_by_symbol(cfg: EtoroConfig, symbol: str, *, limit: int) -> list[dict[str, Any]]:payload = make_client(cfg).request("GET",- "/api/v1/market-data/search",+ MARKET_DATA_SEARCH_PATH,params={"internalSymbolFull": symbol},allow_retry=True,)@@ -196,7 +316,7 @@ def _search_by_symbol(cfg: EtoroConfig, symbol: str, *, limit: int) -> list[dictdef _search_by_text(cfg: EtoroConfig, query: str, *, limit: int) -> list[dict[str, Any]]:payload = make_client(cfg).request("GET",- "/api/v1/market-data/search",+ MARKET_DATA_SEARCH_PATH,params={"search": query, "limit": limit},allow_retry=True,)@@ -204,6 +324,88 @@ def _search_by_text(cfg: EtoroConfig, query: str, *, limit: int) -> list[dict[streturn [item for item in items if isinstance(item, dict)]+def _instrument_type_id(item: dict[str, Any]) -> int | None:+ for key in ("instrumentTypeID", "instrumentTypeId", "instrument_type_id"):+ value = item.get(key)+ if value is None:+ continue+ try:+ return int(value)+ except (TypeError, ValueError):+ continue+ return None+++def _instrument_type_id_from_query(query: str) -> int | None:+ token = str(query or "").strip().lower().replace("-", "_")+ if not token:+ return None+ if token.isdigit():+ type_id = int(token)+ return type_id if type_id in INSTRUMENT_TYPE_IDS else None+ return _INSTRUMENT_TYPE_ALIASES.get(token)+++def _attach_rates(cfg: EtoroConfig, instruments: list[dict[str, Any]]) -> None:+ ids: list[int] = []+ for row in instruments:+ instrument_id = row.get("instrument_id")+ if instrument_id is None:+ continue+ try:+ ids.append(int(instrument_id))+ except (TypeError, ValueError):+ continue+ if not ids:+ return+ payload = make_client(cfg).request(+ "GET",+ MARKET_DATA_RATES_PATH,+ params={"instrumentIds": ",".join(str(i) for i in ids)},+ allow_retry=True,+ )+ rates = _rates_by_instrument_id(payload)+ for row in instruments:+ instrument_id = row.get("instrument_id")+ if instrument_id is None:+ continue+ try:+ quote = rates.get(int(instrument_id))+ except (TypeError, ValueError):+ quote = None+ if quote:+ row["quote"] = quote+++def _rates_by_instrument_id(payload: Any) -> dict[int, dict[str, Any]]:+ items: list[Any] = []+ if isinstance(payload, list):+ items = payload+ elif isinstance(payload, dict):+ for key in ("rates", "items", "data"):+ value = payload.get(key)+ if isinstance(value, list):+ items = value+ break+ result: dict[int, dict[str, Any]] = {}+ for item in items:+ if not isinstance(item, dict):+ continue+ instrument_id = item.get("instrumentID") or item.get("instrumentId")+ if instrument_id is None:+ continue+ try:+ key = int(instrument_id)+ except (TypeError, ValueError):+ continue+ result[key] = {+ "bid": item.get("bid") or item.get("bidRate"),+ "ask": item.get("ask") or item.get("askRate"),+ "last": item.get("lastExecution") or item.get("last") or item.get("rate"),+ }+ return result++def _instrument_id(item: dict[str, Any]) -> int | None:for key in ("instrumentId", "instrumentID", "internalInstrumentId"):value = item.get(key)@@ -278,7 +480,7 @@ def _normalize_metadata_row(item: dict[str, Any]) -> dict[str, Any]:"instrument_id": instrument_id,"symbol": _symbol_full(item) or item.get("symbolFull"),"display_name": _display_name(item) or item.get("instrumentDisplayName"),- "instrument_type_id": item.get("instrumentTypeID") or item.get("instrumentTypeId"),+ "instrument_type_id": _instrument_type_id(item),"exchange_id": item.get("exchangeID") or item.get("exchangeId"),"raw": item,}diff --git a/agent/src/trading/connectors/etoro/sdk.py b/agent/src/trading/connectors/etoro/sdk.pyindex 2e6b40120..64179a5fd 100644--- a/agent/src/trading/connectors/etoro/sdk.py+++ b/agent/src/trading/connectors/etoro/sdk.py@@ -9,6 +9,7 @@ from src.trading.connectors.etoro.client import (EtoroAPIError,EtoroConfig,EtoroConfigError,+ MARKET_DATA_RATES_PATH,PAPER_GUARD,aggregate_portfolio_path,build_config,@@ -27,6 +28,7 @@ from src.trading.connectors.etoro.copy_trading import ()from src.trading.connectors.etoro.instruments import (get_instrument_metadata,+ list_instruments_by_type,resolve_instrument_id,search_instruments,)@@ -59,6 +61,7 @@ __all__ = ["copy_poll","copy_close","search_instruments",+ "list_instruments_by_type","get_instrument_metadata","EtoroConfig","EtoroConfigError",@@ -165,11 +168,12 @@ def get_open_orders(config: EtoroConfig | None = None, *, include_executions: bodef get_quote(symbol: str, *, config: EtoroConfig | None = None, **_: Any) -> dict[str, Any]:+ """Fetch a quote via the flat market-data rates route (all profiles)."""cfg = config or load_config()instrument_id = resolve_instrument_id(symbol, cfg)payload = _client(cfg).request("GET",- "/api/v1/market-data/instruments/rates",+ MARKET_DATA_RATES_PATH,params={"instrumentIds": instrument_id},allow_retry=True,)diff --git a/agent/src/trading/service.py b/agent/src/trading/service.pyindex ce21bb04d..a9d88d391 100644--- a/agent/src/trading/service.py+++ b/agent/src/trading/service.py@@ -153,6 +153,8 @@ def search_instruments(*,limit: int = 10,mode: str = "auto",+ instrument_type_id: int | None = None,+ include_rates: bool = False,**overrides: Any,) -> dict[str, Any]:"""Search tradable instruments (eToro Public API)."""@@ -167,6 +169,8 @@ def search_instruments(module.build_config(profile.config, overrides),limit=limit,mode=mode,+ instrument_type_id=instrument_type_id,+ include_rates=include_rates,),)diff --git a/agent/tests/test_etoro_connector.py b/agent/tests/test_etoro_connector.pyindex 109b5afad..2bb1d5193 100644--- a/agent/tests/test_etoro_connector.py+++ b/agent/tests/test_etoro_connector.py@@ -245,6 +245,7 @@ def test_classification_registered() -> None:assert classify_tool("unknown_etoro_op", None, curated) is ToolClass.UNKNOWNassert ETORO_TOOL_CLASS["copy_close"] is ToolClass.WRITEassert ETORO_TOOL_CLASS["get_instrument_metadata"] is ToolClass.READ+ assert ETORO_TOOL_CLASS["list_instruments_by_type"] is ToolClass.READdef test_resolve_btc_uses_internal_symbol_full(monkeypatch) -> None:@@ -297,6 +298,103 @@ def test_fuzzy_search_filters_sentinel_ids(monkeypatch) -> None:assert result["instruments"][0]["instrument_id"] == 100000+def test_crypto_query_browses_instrument_type_catalog(monkeypatch) -> None:+ cfg = EtoroConfig(profile="paper", api_key="k", user_key="u")+ captured: list[tuple[str, dict[str, Any]]] = []++ def _transport(method: str, url: str, **kwargs: Any) -> _FakeResponse:+ captured.append((url, dict(kwargs.get("params") or {})))+ if "/market-data/instruments" in url and kwargs.get("params", {}).get("instrumentTypeId") == 10:+ return _FakeResponse(+ 200,+ {+ "instrumentDisplayDatas": [+ {+ "instrumentID": 100000,+ "symbolFull": "BTC",+ "instrumentDisplayName": "Bitcoin",+ "instrumentTypeID": 10,+ },+ {+ "instrumentID": 5,+ "symbolFull": "00001.HK",+ "instrumentDisplayName": "CK Hutchison",+ "instrumentTypeID": 5,+ },+ ],+ },+ )+ raise AssertionError(f"unexpected request {method} {url} {kwargs.get('params')}")++ set_client_factory(lambda c: EtoroClient(cfg, transport=_transport))+ from src.trading.connectors.etoro.instruments import search_instruments++ result = search_instruments("crypto", cfg, limit=5)+ assert result["status"] == "ok"+ assert result["mode"] == "type"+ assert result["instrument_type_id"] == 10+ assert result["instruments"][0]["symbol"] == "BTC"+ assert all(row.get("instrument_type_id") == 10 for row in result["instruments"])+ assert not any("/market-data/search" in url for url, _ in captured)+ assert any(params.get("instrumentTypeId") == 10 for _, params in captured)+++def test_crypto_browse_can_attach_flat_market_data_rates(monkeypatch) -> None:+ cfg = EtoroConfig(profile="live-readonly", api_key="k", user_key="u")+ captured_urls: list[str] = []++ def _transport(method: str, url: str, **kwargs: Any) -> _FakeResponse:+ captured_urls.append(url)+ if "/market-data/instruments" in url and "rates" not in url:+ return _FakeResponse(+ 200,+ {+ "instrumentDisplayDatas": [+ {+ "instrumentID": 100000,+ "symbolFull": "BTC",+ "instrumentTypeID": 10,+ },+ ],+ },+ )+ if "/market-data/instruments/rates" in url:+ return _FakeResponse(+ 200,+ {"rates": [{"instrumentID": 100000, "bid": 1.0, "ask": 2.0, "lastExecution": 1.5}]},+ )+ raise AssertionError(f"unexpected request {method} {url}")++ set_client_factory(lambda c: EtoroClient(cfg, transport=_transport))+ from src.trading.connectors.etoro.instruments import search_instruments++ result = search_instruments("crypto", cfg, limit=5, include_rates=True)+ assert result["instruments"][0]["quote"] == {"bid": 1.0, "ask": 2.0, "last": 1.5}+ assert any("/market-data/instruments/rates" in url for url in captured_urls)+ assert not any(url.endswith("/api/v1/instruments") for url in captured_urls)+++def test_get_quote_uses_flat_rates_on_readonly_profile(monkeypatch) -> None:+ cfg = EtoroConfig(profile="live-readonly", api_key="k", user_key="u")++ def _transport(method: str, url: str, **kwargs: Any) -> _FakeResponse:+ if "/market-data/search" in url:+ return _FakeResponse(200, {"items": [{"instrumentId": 100000, "internalSymbolFull": "BTC"}]})+ if "/market-data/instruments/rates" in url:+ return _FakeResponse(200, {"rates": [{"instrumentID": 100000, "bid": 1.0, "ask": 2.0}]})+ raise AssertionError(f"unexpected request {method} {url}")++ set_client_factory(lambda c: EtoroClient(cfg, transport=_transport))+ result = etoro_sdk.get_quote("BTC", config=cfg)+ assert result["status"] == "ok"+ assert result["quote"]["bid"] == 1.0++ service_result = service.get_quote("BTC", "etoro-live-sdk-readonly", api_key="k", user_key="u")+ assert service_result["status"] == "ok"+ assert service_result["profile_id"] == "etoro-live-sdk-readonly"+ assert service_result["quote"]["ask"] == 2.0++def test_get_open_orders_reads_portfolio_orders(monkeypatch) -> None:cfg = EtoroConfig(profile="paper", api_key="k", user_key="u")