Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(upbit): wire positions to the portfolio contract correctly

heonjinjeong27314e517400
diff --git a/agent/src/trading/connectors/upbit/sdk.py b/agent/src/trading/connectors/upbit/sdk.py
index efa1eebc2..ae8cf0d2b 100644
--- a/agent/src/trading/connectors/upbit/sdk.py
+++ b/agent/src/trading/connectors/upbit/sdk.py
@@ -239,7 +239,15 @@ def get_account_snapshot(config: UpbitConfig | None = None) -> dict[str, Any]:
def get_positions(config: UpbitConfig | None = None) -> dict[str, Any]:
- """Fetch current crypto holdings (every non-KRW balance)."""
+ """Fetch current crypto holdings (every non-KRW balance).
+
+ ``currency`` here is the currency ``average_cost`` (and a market price,
+ were one attached) is quoted in -- Upbit's own ``unit_currency`` -- not
+ the coin itself, which is ``symbol``. ``normalize_position`` in
+ ``src/portfolio/normalization.py`` reads exactly these two fields as the
+ quote currency and the traded symbol respectively, so getting them
+ backwards here would price a BTC position in BTC.
+ """
cfg = config or load_config()
accounts = _accounts(cfg)
@@ -253,17 +261,13 @@ def get_positions(config: UpbitConfig | None = None) -> dict[str, Any]:
continue
unit_currency = str(item.get("unit_currency") or "KRW")
rows.append({
- "symbol": f"{unit_currency}-{currency}",
- "currency": currency,
+ "symbol": currency,
+ "asset_type": "crypto",
+ "currency": unit_currency,
"quantity": quantity,
"available": _as_float(item.get("balance")),
"locked": _as_float(item.get("locked")),
- # avg_buy_price is a price per unit of `currency`, denominated in
- # `unit_currency` (e.g. a KRW price for a BTC position) -- not in
- # `currency` itself, which is what `average_cost` alone would imply.
"average_cost": _as_float(item.get("avg_buy_price")),
- "average_cost_currency": unit_currency,
- "unit_currency": unit_currency,
})
return {"status": "ok", "profile": cfg.profile, "is_paper": cfg.is_paper, "positions": rows}
diff --git a/agent/tests/test_sdk_connectors_upbit.py b/agent/tests/test_sdk_connectors_upbit.py
index 95a1cedba..cca4915bf 100644
--- a/agent/tests/test_sdk_connectors_upbit.py
+++ b/agent/tests/test_sdk_connectors_upbit.py
@@ -12,6 +12,7 @@ from __future__ import annotations
import pytest
from src.live.classification import ToolClass
+from src.portfolio.normalization import normalize_position
from src.trading import profiles, service
from src.trading.connectors.upbit import sdk as up
from src.trading.connectors.upbit.classification import UPBIT_TOOL_CLASS
@@ -186,6 +187,52 @@ def test_upbit_service_unconfigured(monkeypatch, tmp_path) -> None:
assert result["transport"] == "broker_sdk"
+def test_upbit_positions_report_the_coin_as_symbol_and_krw_as_currency(monkeypatch) -> None:
+ """currency must be the currency average_cost is quoted in (unit_currency),
+ not the coin -- otherwise the portfolio layer prices a BTC position in BTC."""
+ monkeypatch.setattr(
+ up,
+ "_accounts",
+ lambda cfg: [
+ {"currency": "BTC", "balance": "0.5", "locked": "0", "avg_buy_price": "140000000", "unit_currency": "KRW"}
+ ],
+ )
+ cfg = up.UpbitConfig(access_key="ak", secret_key="sk", profile="paper")
+ result = up.get_positions(cfg)
+
+ assert result["positions"] == [
+ {
+ "symbol": "BTC",
+ "asset_type": "crypto",
+ "currency": "KRW",
+ "quantity": 0.5,
+ "available": 0.5,
+ "locked": 0.0,
+ "average_cost": 140_000_000.0,
+ }
+ ]
+
+
+def test_upbit_position_row_normalizes_as_a_krw_priced_crypto_position(monkeypatch) -> None:
+ """End-to-end through the portfolio contract: src/portfolio/normalization.py
+ must see a crypto position priced in KRW, not a stock priced in BTC."""
+ monkeypatch.setattr(
+ up,
+ "_accounts",
+ lambda cfg: [
+ {"currency": "BTC", "balance": "0.5", "locked": "0", "avg_buy_price": "140000000", "unit_currency": "KRW"}
+ ],
+ )
+ cfg = up.UpbitConfig(access_key="ak", secret_key="sk", profile="paper")
+ row = up.get_positions(cfg)["positions"][0]
+
+ normalized = normalize_position("upbit", row)
+ assert normalized["symbol"] == "BTC"
+ assert normalized["asset_type"] == "crypto"
+ assert normalized["currency"] == "KRW"
+ assert normalized["cost_price"] == 140_000_000.0
+
+
def test_upbit_order_ops_classified_write() -> None:
for name in ("place_order", "cancel_order"):
assert UPBIT_TOOL_CLASS[name] is ToolClass.WRITE