"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(alpaca): sign position quantity by side for the mandate gate
Yufeng He2f5f844da228
diff --git a/agent/src/trading/connectors/alpaca/sdk.py b/agent/src/trading/connectors/alpaca/sdk.pyindex 931cf8d3d..81ec40502 100644--- a/agent/src/trading/connectors/alpaca/sdk.py+++ b/agent/src/trading/connectors/alpaca/sdk.py@@ -847,10 +847,17 @@ def _obj_get(obj: Any, name: str, default: Any = None) -> Any:def _position_to_dict(item: Any) -> dict[str, Any]:+ side = str(_obj_get(item, "side", ""))+ quantity = _obj_get(item, "qty")+ if side == "short" and quantity is not None:+ # Alpaca reports qty as an absolute magnitude and carries direction in+ # side. The mandate gate (like the tiger connector) reads quantity as+ # signed, so an unsigned short would book as positive exposure.+ quantity = -float(quantity)return {"symbol": _obj_get(item, "symbol"),- "side": str(_obj_get(item, "side", "")),- "quantity": _obj_get(item, "qty"),+ "side": side,+ "quantity": quantity,"average_cost": _obj_get(item, "avg_entry_price"),"market_value": _obj_get(item, "market_value"),"current_price": _obj_get(item, "current_price"),diff --git a/agent/tests/test_alpaca_position_sign.py b/agent/tests/test_alpaca_position_sign.pynew file mode 100644index 000000000..7c2c4a288--- /dev/null+++ b/agent/tests/test_alpaca_position_sign.py@@ -0,0 +1,46 @@+"""Tests for the Alpaca position mapping's quantity sign.++Alpaca reports ``qty`` as an absolute magnitude and carries direction in+``side`` (``"long"``/``"short"``). The mandate gate reads position quantity as+signed (its own fixtures use negative quantities for shorts), so an unsigned+short would book as positive exposure and every sell would move the computed+exposure toward zero. The connector now negates qty for short positions.+"""++from __future__ import annotations++from types import SimpleNamespace++import pytest++from src.live.enforcement import _position_signed_market_value+from src.trading.connectors.alpaca.sdk import _position_to_dict++pytestmark = pytest.mark.unit+++def test_short_position_gets_negative_quantity() -> None:+ row = _position_to_dict({"symbol": "AAPL", "side": "short", "qty": "400", "market_value": "-50000"})+ assert row["quantity"] == -400.0+ assert row["side"] == "short"+++def test_long_position_quantity_passes_through() -> None:+ row = _position_to_dict({"symbol": "AAPL", "side": "long", "qty": "400", "market_value": "50000"})+ assert row["quantity"] == "400"+++def test_short_position_without_qty_stays_none() -> None:+ row = _position_to_dict({"symbol": "AAPL", "side": "short", "qty": None})+ assert row["quantity"] is None+++def test_object_positions_get_the_same_sign() -> None:+ item = SimpleNamespace(symbol="AAPL", side="short", qty="400", market_value="-50000")+ row = _position_to_dict(item)+ assert row["quantity"] == -400.0+++def test_gate_reads_alpaca_short_as_negative_exposure() -> None:+ row = _position_to_dict({"symbol": "AAPL", "side": "short", "qty": "400", "market_value": "-50000"})+ assert _position_signed_market_value(row) == -50000.0