Auto-reconnect CTP with new front-end addresses after saving settings
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1715,20 +1715,26 @@ def settings():
|
|||||||
from ctp_settings import save_ctp_settings_from_form
|
from ctp_settings import save_ctp_settings_from_form
|
||||||
|
|
||||||
save_ctp_settings_from_form(request.form, set_setting)
|
save_ctp_settings_from_form(request.form, set_setting)
|
||||||
|
flash_msg = "CTP 配置已保存,正在使用新地址重连…"
|
||||||
try:
|
try:
|
||||||
from vnpy_bridge import get_bridge, _persist_last_error
|
from vnpy_bridge import get_bridge
|
||||||
|
from trading_context import get_trading_mode
|
||||||
|
|
||||||
b = get_bridge()
|
b = get_bridge()
|
||||||
b.mark_disconnected()
|
|
||||||
if (request.form.get("simnow_password") or "").strip() or (
|
if (request.form.get("simnow_password") or "").strip() or (
|
||||||
request.form.get("ctp_live_password") or ""
|
request.form.get("ctp_live_password") or ""
|
||||||
).strip():
|
).strip():
|
||||||
b._clear_login_cooldown()
|
b._clear_login_cooldown()
|
||||||
b._last_error = ""
|
mode = get_trading_mode(get_setting)
|
||||||
_persist_last_error("")
|
info = b.reconnect_after_settings_saved(mode)
|
||||||
except Exception:
|
if info.get("cooldown"):
|
||||||
pass
|
flash_msg = "CTP 配置已保存;当前处于登录冷却,请稍后再连"
|
||||||
flash("CTP 配置已保存,请在持仓监控页重连 CTP")
|
elif not info.get("started") and info.get("connected"):
|
||||||
|
flash_msg = "CTP 配置已保存,当前连接正常"
|
||||||
|
except Exception as exc:
|
||||||
|
app.logger.warning("CTP reconnect after settings save: %s", exc)
|
||||||
|
flash_msg = "CTP 配置已保存,请稍后在持仓监控页重连"
|
||||||
|
flash(flash_msg)
|
||||||
elif action == "nav":
|
elif action == "nav":
|
||||||
items = {k: request.form.get(f"nav_{k}") == "on" for k in NAV_TOGGLES}
|
items = {k: request.form.get(f"nav_{k}") == "on" for k in NAV_TOGGLES}
|
||||||
save_nav_items(set_setting, items)
|
save_nav_items(set_setting, items)
|
||||||
|
|||||||
@@ -131,7 +131,7 @@
|
|||||||
<h2>CTP 连接</h2>
|
<h2>CTP 连接</h2>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<p class="hint" style="margin-bottom:.85rem">
|
<p class="hint" style="margin-bottom:.85rem">
|
||||||
投资者代码、密码、前置地址在此维护(优先于 <code>.env</code>)。保存后请在持仓监控页点击「重连 CTP」。
|
投资者代码、密码、前置地址在此维护(优先于 <code>.env</code>)。保存后将自动断开并用新地址重连 CTP。
|
||||||
{% if ctp_status.connected %}
|
{% if ctp_status.connected %}
|
||||||
<span class="badge profit" style="margin-left:.35rem">已连接</span>
|
<span class="badge profit" style="margin-left:.35rem">已连接</span>
|
||||||
{% elif ctp_status.connecting %}
|
{% elif ctp_status.connecting %}
|
||||||
|
|||||||
@@ -546,6 +546,13 @@ class CtpBridge:
|
|||||||
def mark_disconnected(self) -> None:
|
def mark_disconnected(self) -> None:
|
||||||
self._connected_mode = None
|
self._connected_mode = None
|
||||||
|
|
||||||
|
def reconnect_after_settings_saved(self, mode: str) -> dict[str, Any]:
|
||||||
|
"""保存前置/账号后关闭旧连接,并用数据库中的新配置重连。"""
|
||||||
|
self._close_gateway()
|
||||||
|
self._last_error = ""
|
||||||
|
_persist_last_error("")
|
||||||
|
return self.start_connect_async(mode, force=True)
|
||||||
|
|
||||||
def _schedule_fee_sync(self, mode: str) -> None:
|
def _schedule_fee_sync(self, mode: str) -> None:
|
||||||
"""连接成功后触发每日同步检查(非每次全量)。"""
|
"""连接成功后触发每日同步检查(非每次全量)。"""
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user