feat: CTP 断线重连、下单卡片优化、手数自动计算
后台每 30s 检测并重连;以损定仓填止损后自动算手数;开仓/平仓按钮并排对齐。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
"""CTP 断线自动重连(后台线程)。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
from typing import Callable
|
||||
|
||||
from vnpy_bridge import ctp_try_auto_reconnect
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
RECONNECT_INTERVAL_SEC = 30
|
||||
|
||||
|
||||
def _auto_reconnect_enabled() -> bool:
|
||||
return (os.getenv("CTP_AUTO_RECONNECT", "true") or "true").strip().lower() in (
|
||||
"1",
|
||||
"true",
|
||||
"yes",
|
||||
)
|
||||
|
||||
|
||||
def start_ctp_reconnect_worker(*, get_mode_fn: Callable[[], str], interval: int = RECONNECT_INTERVAL_SEC) -> None:
|
||||
"""定时检测 CTP 连接,断线后自动重连。"""
|
||||
|
||||
def _loop() -> None:
|
||||
time.sleep(5)
|
||||
while True:
|
||||
try:
|
||||
if _auto_reconnect_enabled():
|
||||
mode = get_mode_fn()
|
||||
if ctp_try_auto_reconnect(mode):
|
||||
logger.debug("CTP 连接正常 [%s]", mode)
|
||||
except Exception as exc:
|
||||
logger.warning("CTP reconnect worker: %s", exc)
|
||||
time.sleep(max(15, interval))
|
||||
|
||||
threading.Thread(target=_loop, daemon=True, name="ctp-reconnect-worker").start()
|
||||
Reference in New Issue
Block a user