Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(live): recognize current_price/ltp/market_val so direct-SDK positions price correctly

Lanre Shittue4e0d9bc2354
diff --git a/agent/src/live/enforcement.py b/agent/src/live/enforcement.py
index 5b6a2d4fa..c451616cd 100644
--- a/agent/src/live/enforcement.py
+++ b/agent/src/live/enforcement.py
@@ -369,11 +369,17 @@ def _coerce_position_rows(positions: object) -> list[dict] | None:
def _position_market_value(row: dict) -> float | None:
"""Extract one position's USD market value, fail-closed.
- Prefers an explicit ``market_value`` field; otherwise derives it from
- ``quantity`` × (``price`` | ``last_price`` | ``mark_price``). Returns
- ``None`` if neither is parseable.
+ Prefers an explicit market-value field (including Futu's ``market_val``);
+ otherwise derives it from ``quantity`` × a current-price field. The price
+ keys include ``current_price``/``ltp`` alongside ``price``/``last_price``/
+ ``mark_price``/``market_price`` because several direct-SDK connectors
+ (Toss, Dhan, KIS, Zerodha, Shoonya) report the live quote under one of
+ those names rather than the generic ones -- without them, every position
+ from those brokers was unpriceable, and one unpriceable position fails
+ the whole mandate exposure check closed, blocking every subsequent order
+ regardless of size. Returns ``None`` if neither is parseable.
"""
- for key in ("market_value", "marketValue", "value_usd", "value"):
+ for key in ("market_value", "marketValue", "value_usd", "value", "market_val"):
if key in row:
parsed = _as_float(row[key])
return parsed # may be None → fail-closed upstream
@@ -383,7 +389,14 @@ def _position_market_value(row: dict) -> float | None:
qty = _as_float(row[key])
break
price = None
- for key in ("price", "last_price", "mark_price", "market_price"):
+ for key in (
+ "price",
+ "last_price",
+ "mark_price",
+ "market_price",
+ "current_price",
+ "ltp",
+ ):
if key in row:
price = _as_float(row[key])
break
diff --git a/agent/tests/test_mandate_enforcement.py b/agent/tests/test_mandate_enforcement.py
index 3b0cfd7c1..1c17ea054 100644
--- a/agent/tests/test_mandate_enforcement.py
+++ b/agent/tests/test_mandate_enforcement.py
@@ -223,6 +223,25 @@ def test_unreadable_positions_fail_closed() -> None:
assert breach.limit == "max_total_exposure_usd"
+ "row",
+ [
+ # Toss / Dhan / KIS shape: quantity + current_price.
+ {"symbol": "AAPL", "quantity": 10, "current_price": 25.0},
+ # Zerodha / Shoonya shape: quantity + ltp.
+ {"symbol": "AAPL", "quantity": 10, "ltp": 25.0},
+ # Futu shape: already a market value, spelled market_val.
+ {"code": "AAPL", "qty": 10, "market_val": 250.0},
+ ],
+)
+def test_direct_sdk_position_shapes_are_priced_not_fail_closed(row: dict) -> None:
+ """Connector-shaped rows that only carry current_price/ltp/market_val must
+ price correctly instead of tripping the fail-closed exposure denial every
+ other order would then hit regardless of size (#position-pricing-gap)."""
+ breach = _check(_intent(notional_usd=100.0), _mandate(), positions=[row])
+ assert breach is None
+
+
def test_universe_market_cap_floor_denies_when_below(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(enforcement, "market_cap_usd", lambda s, ac: 1.0e8)
mandate = _mandate()