fix: 持仓接口实时拉取并回写本地监控,修复有仓不显示
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+29
-9
@@ -105,6 +105,7 @@ class CtpBridge:
|
||||
self._commission_lists: dict[int, list] = {}
|
||||
self._commission_hooked = False
|
||||
self._subscribed: set[str] = set()
|
||||
self._last_position_query_ts: float = 0.0
|
||||
self._tick_hooked = False
|
||||
self._bar_generators: dict[str, Any] = {}
|
||||
self._bars_1m: dict[str, deque] = {}
|
||||
@@ -698,7 +699,7 @@ class CtpBridge:
|
||||
"accountid": getattr(acc, "accountid", ""),
|
||||
}
|
||||
|
||||
def list_positions(self) -> list[dict[str, Any]]:
|
||||
def _collect_positions(self) -> list[dict[str, Any]]:
|
||||
if not self._engine:
|
||||
return []
|
||||
out: list[dict[str, Any]] = []
|
||||
@@ -706,12 +707,7 @@ class CtpBridge:
|
||||
vol = int(getattr(pos, "volume", 0) or 0)
|
||||
if vol <= 0:
|
||||
continue
|
||||
direction = getattr(pos, "direction", None)
|
||||
d = "long"
|
||||
if direction is not None and str(direction).endswith("SHORT"):
|
||||
d = "short"
|
||||
elif direction is not None and "空" in str(direction):
|
||||
d = "short"
|
||||
d = "long" if _is_long_direction(getattr(pos, "direction", None)) else "short"
|
||||
sym = getattr(pos, "symbol", "") or ""
|
||||
exchange = getattr(pos, "exchange", None)
|
||||
ex_name = str(exchange.value if hasattr(exchange, "value") else exchange or "")
|
||||
@@ -726,6 +722,30 @@ class CtpBridge:
|
||||
})
|
||||
return out
|
||||
|
||||
def refresh_positions(self) -> None:
|
||||
"""向柜台查询持仓(内存为空时补拉)。"""
|
||||
if not self._engine:
|
||||
return
|
||||
now = time.time()
|
||||
if now - self._last_position_query_ts < 1.0:
|
||||
return
|
||||
self._last_position_query_ts = now
|
||||
try:
|
||||
gw = self._engine.get_gateway(GATEWAY_NAME)
|
||||
td = getattr(gw, "td_api", None)
|
||||
if td and hasattr(td, "query_position"):
|
||||
td.query_position()
|
||||
time.sleep(0.4)
|
||||
except Exception as exc:
|
||||
logger.debug("refresh_positions: %s", exc)
|
||||
|
||||
def list_positions(self, *, refresh_if_empty: bool = True) -> list[dict[str, Any]]:
|
||||
out = self._collect_positions()
|
||||
if not out and refresh_if_empty:
|
||||
self.refresh_positions()
|
||||
out = self._collect_positions()
|
||||
return out
|
||||
|
||||
def list_active_orders(self) -> list[dict[str, Any]]:
|
||||
if not self._engine:
|
||||
return []
|
||||
@@ -907,10 +927,10 @@ def ctp_get_account(mode: str) -> dict[str, Any]:
|
||||
return b.get_account()
|
||||
|
||||
|
||||
def ctp_list_positions(mode: str) -> list[dict[str, Any]]:
|
||||
def ctp_list_positions(mode: str, *, refresh_if_empty: bool = True) -> list[dict[str, Any]]:
|
||||
b = get_bridge()
|
||||
b.ensure_connected(mode)
|
||||
return b.list_positions()
|
||||
return b.list_positions(refresh_if_empty=refresh_if_empty)
|
||||
|
||||
|
||||
def ctp_list_active_orders(mode: str) -> list[dict[str, Any]]:
|
||||
|
||||
Reference in New Issue
Block a user