fix: K线新浪历史补齐与手续费页布局及CTP批量同步

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-24 13:26:53 +08:00
parent 3fe4add8e1
commit de6815d481
8 changed files with 178 additions and 47 deletions
+36 -14
View File
@@ -88,7 +88,7 @@ class CtpBridge:
self._last_error: str = ""
self._connect_lock = threading.Lock()
self._commission_waiters: dict[int, threading.Event] = {}
self._commission_results: dict[int, dict] = {}
self._commission_lists: dict[int, list] = {}
self._commission_hooked = False
self._subscribed: set[str] = set()
self._tick_hooked = False
@@ -251,7 +251,7 @@ class CtpBridge:
error.get("ErrorMsg") or error,
)
if data and data.get("InstrumentID"):
bridge._commission_results[reqid] = dict(data)
bridge._commission_lists.setdefault(reqid, []).append(dict(data))
ev = bridge._commission_waiters.get(reqid)
if last and ev:
ev.set()
@@ -259,21 +259,26 @@ class CtpBridge:
td.onRspQryInstrumentCommissionRate = on_rsp # type: ignore[method-assign]
self._commission_hooked = True
def query_instrument_commission(self, ths_code: str, *, mode: str) -> dict:
"""查询单合约 CTP 手续费率(需已连接)。"""
def _query_commission(
self,
*,
mode: str,
instrument_id: str = "",
exchange_id: str = "",
timeout: float = 8,
) -> list[dict]:
if self._connected_mode != mode or not self._engine:
return {}
return []
try:
sym, ex_name = ths_to_vnpy_symbol(ths_code)
gw = self._engine.get_gateway(GATEWAY_NAME)
td = gw.td_api
except Exception as exc:
logger.debug("commission query init: %s", exc)
return {}
return []
if not getattr(td, "login_status", False):
return {}
return []
if not hasattr(td, "reqQryInstrumentCommissionRate"):
return {}
return []
self._ensure_commission_callback()
reqid = int(getattr(td, "reqid", 0)) + 1
td.reqid = reqid
@@ -282,16 +287,33 @@ class CtpBridge:
req = {
"BrokerID": td.brokerid,
"InvestorID": td.userid,
"InstrumentID": sym,
"ExchangeID": ex_name,
"InstrumentID": instrument_id or "",
"ExchangeID": exchange_id or "",
}
ret = td.reqQryInstrumentCommissionRate(req, reqid)
if ret != 0:
self._commission_waiters.pop(reqid, None)
return {}
ev.wait(timeout=8)
return []
ev.wait(timeout=timeout)
self._commission_waiters.pop(reqid, None)
return self._commission_results.pop(reqid, {})
return self._commission_lists.pop(reqid, [])
def query_instrument_commission(self, ths_code: str, *, mode: str) -> dict:
"""查询单合约 CTP 手续费率(需已连接)。"""
try:
sym, ex_name = ths_to_vnpy_symbol(ths_code)
except Exception:
return {}
rows = self._query_commission(
mode=mode,
instrument_id=sym,
exchange_id=ex_name,
)
return rows[-1] if rows else {}
def query_all_commissions(self, *, mode: str) -> list[dict]:
"""批量查询全部合约手续费(InstrumentID 留空)。"""
return self._query_commission(mode=mode, timeout=45)
def _tick_key(self, symbol: str, ex_name: str) -> str:
return f"{symbol.lower()}:{ex_name.upper()}"