Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(etoro): read account PnL from aggregate-portfolio route

Ofek Shaked582f1a07ff3a
diff --git a/agent/src/trading/connectors/etoro/client.py b/agent/src/trading/connectors/etoro/client.py
index bba48a51a..e44b53f59 100644
--- a/agent/src/trading/connectors/etoro/client.py
+++ b/agent/src/trading/connectors/etoro/client.py
@@ -203,6 +203,10 @@ def trade_history_root(cfg: EtoroConfig) -> str:
return "/api/v1/trading/info/trade/demo" if cfg.is_paper else "/api/v1/trading/info/trade"
+def aggregate_portfolio_path(cfg: EtoroConfig) -> str:
+ return f"{info_root(cfg)}/aggregate-portfolio"
+
+
def positions_root(cfg: EtoroConfig) -> str:
return "/api/v2/trading/demo" if cfg.is_paper else "/api/v2/trading"
diff --git a/agent/src/trading/connectors/etoro/sdk.py b/agent/src/trading/connectors/etoro/sdk.py
index 05e82db63..2e6b40120 100644
--- a/agent/src/trading/connectors/etoro/sdk.py
+++ b/agent/src/trading/connectors/etoro/sdk.py
@@ -10,6 +10,7 @@ from src.trading.connectors.etoro.client import (
EtoroConfig,
EtoroConfigError,
PAPER_GUARD,
+ aggregate_portfolio_path,
build_config,
info_root,
load_config,
@@ -129,7 +130,9 @@ def get_account_snapshot(config: EtoroConfig | None = None) -> dict[str, Any]:
portfolio = client.request("GET", f"{info_root(cfg)}/portfolio", allow_retry=True)
account: dict[str, Any] = {"portfolio": portfolio}
try:
- account["pnl"] = client.request("GET", f"{info_root(cfg)}/pnl", allow_retry=True)
+ aggregated = client.request("GET", aggregate_portfolio_path(cfg), allow_retry=True)
+ account["aggregated_portfolio"] = aggregated
+ account["pnl"] = _pnl_from_aggregate_portfolio(aggregated)
except EtoroAPIError as exc:
account["pnl_error"] = str(exc)
return {
@@ -285,6 +288,27 @@ def _position_row(item: dict[str, Any]) -> dict[str, Any]:
}
+def _pnl_from_aggregate_portfolio(payload: Any) -> dict[str, Any]:
+ """Normalize account-level PnL from ``GET …/aggregate-portfolio``."""
+ if not isinstance(payload, dict):
+ return {"source": "aggregate-portfolio", "raw": payload}
+ totals = payload.get("accountTotals")
+ if not isinstance(totals, dict):
+ return {"source": "aggregate-portfolio", "raw": payload}
+ return {
+ "source": "aggregate-portfolio",
+ "account_current_pnl": totals.get("accountCurrentPnl"),
+ "account_total_value": totals.get("accountTotalValue"),
+ "account_balance": totals.get("accountBalance"),
+ "account_available_cash": totals.get("accountAvailableCash"),
+ "account_frozen_cash": totals.get("accountFrozenCash"),
+ "account_total_used_margin": totals.get("accountTotalUsedMargin"),
+ "account_currency": payload.get("accountCurrency"),
+ "timestamp": payload.get("timestamp"),
+ "account_totals": totals,
+ }
+
+
def _client_portfolio(payload: Any) -> dict[str, Any] | None:
if not isinstance(payload, dict):
return None
diff --git a/agent/tests/test_etoro_connector.py b/agent/tests/test_etoro_connector.py
index 604683a12..180e7af9d 100644
--- a/agent/tests/test_etoro_connector.py
+++ b/agent/tests/test_etoro_connector.py
@@ -15,6 +15,7 @@ from src.trading.connectors.etoro.client import (
EtoroClient,
EtoroConfig,
build_config,
+ aggregate_portfolio_path,
copy_root,
execution_root,
execution_v1_root,
@@ -88,6 +89,8 @@ def test_demo_and_real_paths_separated() -> None:
assert execution_v1_root(paper).endswith("/demo")
assert "/demo" not in execution_v1_root(live)
assert info_root(paper).endswith("/demo")
+ assert aggregate_portfolio_path(paper).endswith("/demo/aggregate-portfolio")
+ assert aggregate_portfolio_path(live) == "/api/v1/trading/info/aggregate-portfolio"
assert copy_root(paper).endswith("/demo")
assert positions_root(paper).endswith("/demo")
assert paper.api_key == live.api_key
@@ -133,6 +136,39 @@ def test_credentials_redacted_in_public_config() -> None:
assert payload["api_key_configured"] is True
+def test_get_account_snapshot_uses_aggregate_portfolio_for_pnl(monkeypatch) -> None:
+ cfg = EtoroConfig(profile="live", api_key="k", user_key="u")
+ captured_urls: list[str] = []
+
+ def _transport(method: str, url: str, **kwargs: Any) -> _FakeResponse:
+ captured_urls.append(url)
+ if "/portfolio" in url and "aggregate" not in url:
+ return _FakeResponse(200, {"clientPortfolio": {"credit": 1.25, "positions": []}})
+ if "/aggregate-portfolio" in url:
+ return _FakeResponse(
+ 200,
+ {
+ "accountCurrency": "USD",
+ "accountTotals": {
+ "accountCurrentPnl": -26.94,
+ "accountTotalValue": 615.48,
+ "accountBalance": 1.25,
+ },
+ },
+ )
+ raise AssertionError(f"unexpected {method} {url}")
+
+ set_client_factory(lambda c: EtoroClient(cfg, transport=_transport))
+ result = etoro_sdk.get_account_snapshot(cfg)
+ assert result["status"] == "ok"
+ pnl = result["account"]["pnl"]
+ assert pnl["source"] == "aggregate-portfolio"
+ assert pnl["account_current_pnl"] == -26.94
+ assert "pnl_error" not in result["account"]
+ assert any("/aggregate-portfolio" in url for url in captured_urls)
+ assert not any(url.endswith("/pnl") for url in captured_urls)
+
+
def test_place_order_uses_demo_execution_path(monkeypatch) -> None:
cfg = EtoroConfig(profile="paper", api_key="k", user_key="u", readonly=False)
captured: dict[str, Any] = {}