Skip to content

"Vibe-Trading: Your Personal Trading Agent"

feat(portfolio): decouple currency identity from FX availability

zeus22947a2ffa78190
diff --git a/agent/src/portfolio/compatibility.py b/agent/src/portfolio/compatibility.py
index 178ecb96b..d14bc6f5e 100644
--- a/agent/src/portfolio/compatibility.py
+++ b/agent/src/portfolio/compatibility.py
@@ -12,13 +12,12 @@ from __future__ import annotations
from dataclasses import asdict, dataclass
from decimal import Decimal, InvalidOperation
-from typing import Any, Literal
+from typing import Any, Literal, Mapping
+from src.portfolio.iso4217 import is_iso_currency
from src.trading.types import TradingProfile
CompatibilityLevel = Literal["native", "contract_tested", "experimental"]
-
-SUPPORTED_VALUE_CURRENCIES = frozenset({"USD", "HKD", "CNY"})
_SYMBOL_FIELDS = ("symbol", "code", "ticker")
_QUANTITY_FIELDS = (
"quantity",
@@ -211,19 +210,37 @@ def _require_equity_only_robinhood_account(account_payload: dict[str, Any]) -> N
)
-def ensure_supported_currencies(rows: list[dict[str, Any]], account_payload: dict[str, Any] | None = None) -> None:
- """Fail closed when the current portfolio FX model cannot value a source.
+def ensure_supported_currencies(
+ rows: list[dict[str, Any]],
+ account_payload: dict[str, Any] | None = None,
+ rates: Mapping[str, Decimal] | None = None,
+) -> None:
+ """Validate currency identity separately from current FX availability.
- Account currency is checked as well as position currency so a cash-only
- account cannot accidentally be reported as USD.
+ A real ISO-4217 currency is valid portfolio metadata even when the current
+ rates map cannot convert it. Valuation callers pass a rates map to fail
+ closed on such gaps instead of silently assuming a 1:1 USD rate.
"""
- currencies = {str(row.get("price_currency") or row.get("currency") or "USD").upper() for row in rows}
+ currencies = {
+ str(row.get("price_currency") or row.get("currency") or "USD").upper()
+ for row in rows
+ }
account_currency = _account_currency(account_payload or {})
if account_currency:
currencies.add(account_currency)
- unsupported = sorted(currencies - SUPPORTED_VALUE_CURRENCIES)
- if unsupported:
- raise PortfolioContractError("portfolio FX conversion is not available for: " + ", ".join(unsupported))
+
+ invalid = sorted(code for code in currencies if not is_iso_currency(code))
+ if invalid:
+ raise PortfolioContractError(
+ "portfolio currency is not a valid ISO-4217 code: " + ", ".join(invalid)
+ )
+
+ if rates is not None:
+ missing = sorted(code for code in currencies if rates.get(code) is None)
+ if missing:
+ raise PortfolioContractError(
+ "portfolio FX conversion is not available for: " + ", ".join(missing)
+ )
def _account_currency(payload: dict[str, Any]) -> str | None: