Allow scheduled CTP connect when auto-connect setting is off.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+26
-13
@@ -575,10 +575,10 @@ class CtpBridge:
|
||||
"td_address": st.get("交易服务器", ""),
|
||||
}
|
||||
|
||||
def connect(self, mode: str, *, force: bool = False) -> None:
|
||||
from ctp_settings import CTP_DISABLED_HINT, is_ctp_auto_connect_enabled
|
||||
def connect(self, mode: str, *, force: bool = False, scheduled: bool = False) -> None:
|
||||
from ctp_settings import CTP_DISABLED_HINT
|
||||
|
||||
if not is_ctp_auto_connect_enabled():
|
||||
if not _ctp_connect_permitted(scheduled=scheduled):
|
||||
self._last_error = CTP_DISABLED_HINT
|
||||
_persist_last_error(CTP_DISABLED_HINT)
|
||||
raise RuntimeError(CTP_DISABLED_HINT)
|
||||
@@ -680,11 +680,13 @@ class CtpBridge:
|
||||
finally:
|
||||
self._connect_in_progress = False
|
||||
|
||||
def start_connect_async(self, mode: str, *, force: bool = False) -> dict[str, Any]:
|
||||
def start_connect_async(
|
||||
self, mode: str, *, force: bool = False, scheduled: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""后台连接,不阻塞 HTTP 请求。"""
|
||||
from ctp_settings import CTP_DISABLED_HINT, is_ctp_auto_connect_enabled
|
||||
from ctp_settings import CTP_DISABLED_HINT
|
||||
|
||||
if not is_ctp_auto_connect_enabled():
|
||||
if not _ctp_connect_permitted(scheduled=scheduled):
|
||||
self._last_error = CTP_DISABLED_HINT
|
||||
_persist_last_error(CTP_DISABLED_HINT)
|
||||
return {
|
||||
@@ -709,7 +711,7 @@ class CtpBridge:
|
||||
|
||||
def _run() -> None:
|
||||
try:
|
||||
self.connect(mode, force=force)
|
||||
self.connect(mode, force=force, scheduled=scheduled)
|
||||
except Exception as exc:
|
||||
logger.warning("CTP 后台连接失败: %s", exc)
|
||||
|
||||
@@ -1915,6 +1917,19 @@ def vnpy_available() -> bool:
|
||||
return get_bridge().available()
|
||||
|
||||
|
||||
def _ctp_connect_permitted(*, scheduled: bool = False) -> bool:
|
||||
"""scheduled=True:盘前/交易时段计划连接,不受「自动连接」开关限制。"""
|
||||
from ctp_settings import is_ctp_auto_connect_enabled
|
||||
|
||||
if is_ctp_auto_connect_enabled():
|
||||
return True
|
||||
if not scheduled:
|
||||
return False
|
||||
from ctp_premarket_connect import should_auto_connect_now
|
||||
|
||||
return should_auto_connect_now()
|
||||
|
||||
|
||||
def ctp_disconnect(*, set_disabled_hint: bool = False) -> None:
|
||||
"""主动断开 CTP 并清理内存状态。"""
|
||||
from ctp_settings import CTP_DISABLED_HINT
|
||||
@@ -1935,19 +1950,17 @@ def ctp_connect(mode: str, *, force: bool = False) -> dict[str, Any]:
|
||||
return b.status(mode)
|
||||
|
||||
|
||||
def ctp_start_connect(mode: str, *, force: bool = False) -> dict[str, Any]:
|
||||
def ctp_start_connect(mode: str, *, force: bool = False, scheduled: bool = False) -> dict[str, Any]:
|
||||
"""非阻塞发起连接,供 Web API 使用。"""
|
||||
b = get_bridge()
|
||||
info = b.start_connect_async(mode, force=force)
|
||||
info = b.start_connect_async(mode, force=force, scheduled=scheduled)
|
||||
st = b.status(mode)
|
||||
return {**info, "status": st}
|
||||
|
||||
|
||||
def ctp_try_auto_reconnect(mode: str) -> bool:
|
||||
"""断线时静默异步重连;已连接且交易通道正常则不再重复 connect。"""
|
||||
from ctp_settings import is_ctp_auto_connect_enabled
|
||||
|
||||
if not is_ctp_auto_connect_enabled():
|
||||
if not _ctp_connect_permitted(scheduled=True):
|
||||
return False
|
||||
b = get_bridge()
|
||||
if not b.available():
|
||||
@@ -1974,7 +1987,7 @@ def ctp_try_auto_reconnect(mode: str) -> bool:
|
||||
"请更新 SIMNOW_TD_ADDRESS 并确认服务器出网。"
|
||||
)
|
||||
return False
|
||||
info = b.start_connect_async(mode, force=False)
|
||||
info = b.start_connect_async(mode, force=False, scheduled=True)
|
||||
return bool(
|
||||
info.get("connected")
|
||||
or info.get("connecting")
|
||||
|
||||
Reference in New Issue
Block a user