Add CTP auto-connect toggle to stop off-hours reconnect attempts.

When disabled, disconnect immediately and skip auto-reconnect, premarket connect, and TCP probes that fail outside SimNow trading hours.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-26 18:53:49 +08:00
parent 3a150dd3d6
commit 631aa2c0ab
9 changed files with 231 additions and 18 deletions
+24 -5
View File
@@ -34,6 +34,7 @@ from recommend_store import (
)
from recommend_stream import recommend_hub, schedule_recommend_refresh, start_recommend_worker
from position_stream import position_hub, start_position_worker
from ctp_settings import is_ctp_auto_connect_enabled
from ctp_reconnect import start_ctp_reconnect_worker
from ctp_premarket_connect import start_ctp_premarket_connect_worker
from ctp_fee_worker import start_ctp_fee_worker
@@ -1562,9 +1563,10 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
threading.Thread(target=_warm, daemon=True, name="position-bootstrap").start()
try:
from vnpy_bridge import ctp_start_connect
mode = get_trading_mode(get_setting)
ctp_start_connect(mode, force=False)
if is_ctp_auto_connect_enabled(get_setting):
from vnpy_bridge import ctp_start_connect
mode = get_trading_mode(get_setting)
ctp_start_connect(mode, force=False)
except Exception as exc:
logger.debug("bootstrap ctp connect: %s", exc)
@@ -1617,6 +1619,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
risk_percent=get_risk_percent(get_setting),
max_margin_pct=get_max_margin_pct(get_setting),
pending_order_timeout_min=get_pending_order_timeout_min(get_setting),
ctp_auto_connect=is_ctp_auto_connect_enabled(get_setting),
recommend_rows=rec_cache.get("rows") or [],
recommend_updated_at=rec_cache.get("updated_at"),
product_categories=PRODUCT_CATEGORIES,
@@ -2258,7 +2261,17 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
@login_required
def api_ctp_connect():
from vnpy_bridge import ctp_start_connect
from ctp_settings import CTP_DISABLED_HINT
if not is_ctp_auto_connect_enabled(get_setting):
mode = get_trading_mode(get_setting)
st = ctp_status(mode)
return jsonify({
"ok": False,
"disabled": True,
"error": CTP_DISABLED_HINT,
"status": st,
}), 400
mode = get_trading_mode(get_setting)
body = request.get_json(silent=True) or {}
force = bool(body.get("force"))
@@ -2723,8 +2736,14 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
get_sizing_mode_fn=lambda: get_sizing_mode(get_setting),
get_fixed_lots_fn=lambda: get_fixed_lots(get_setting),
)
start_ctp_reconnect_worker(get_mode_fn=lambda: get_trading_mode(get_setting))
start_ctp_premarket_connect_worker(get_mode_fn=lambda: get_trading_mode(get_setting))
start_ctp_reconnect_worker(
get_mode_fn=lambda: get_trading_mode(get_setting),
get_setting_fn=get_setting,
)
start_ctp_premarket_connect_worker(
get_mode_fn=lambda: get_trading_mode(get_setting),
get_setting_fn=get_setting,
)
start_sl_tp_guard_worker(
db_path=DB_PATH,
get_mode_fn=lambda: get_trading_mode(get_setting),