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
+23 -1
View File
@@ -1788,9 +1788,24 @@ def settings():
return redirect(url_for("settings"))
flash("交易模式已保存")
elif action == "ctp":
from ctp_settings import save_ctp_auto_connect, is_ctp_auto_connect_enabled
from ctp_settings import save_ctp_settings_from_form
from vnpy_bridge import ctp_disconnect
was_enabled = is_ctp_auto_connect_enabled(get_setting)
auto_enabled = save_ctp_auto_connect(request.form, set_setting)
save_result = save_ctp_settings_from_form(request.form, set_setting)
if not auto_enabled:
ctp_disconnect(set_disabled_hint=True)
elif not was_enabled and auto_enabled:
try:
from vnpy_bridge import get_bridge
from trading_context import get_trading_mode
mode = get_trading_mode(get_setting)
get_bridge().reconnect_after_settings_saved(mode)
except Exception as exc:
app.logger.debug("CTP connect after enable auto: %s", exc)
pwd_updated = save_result.get("passwords_updated") or []
pwd_empty = save_result.get("passwords_submitted_empty") or []
simnow_pwd_len = len((request.form.get("simnow_password") or "").strip())
@@ -1816,6 +1831,12 @@ def settings():
pwd_note = "实盘交易密码未改(提交为空)"
else:
pwd_note = ""
if not auto_enabled:
flash("CTP 配置已保存;自动连接已关闭,所有 CTP 连接已断开")
return redirect(url_for("settings"))
if not was_enabled:
flash("CTP 配置已保存;自动连接已开启,正在连接…")
return redirect(url_for("settings"))
flash_msg = "CTP 配置已保存,正在使用新地址重连…"
if pwd_note:
flash_msg = f"CTP 配置已保存;{pwd_note},正在重连…"
@@ -1864,7 +1885,7 @@ def settings():
ctp_st = ctp_status(get_trading_mode(get_setting))
except Exception:
pass
from ctp_settings import get_ctp_settings_for_ui
from ctp_settings import get_ctp_settings_for_ui, is_ctp_auto_connect_enabled
return render_template(
"settings.html",
@@ -1873,6 +1894,7 @@ def settings():
quote_label=get_quote_source_label(ctp_connected=bool(ctp_st.get("connected"))),
ctp_status=ctp_st,
ctp_cfg=get_ctp_settings_for_ui(),
ctp_auto_connect=is_ctp_auto_connect_enabled(get_setting),
trading_mode=get_setting("trading_mode", "simulation"),
position_sizing_mode=get_setting("position_sizing_mode", "fixed"),
fixed_lots=get_setting("fixed_lots", "1"),