Track open orders as pending until CTP fill, with cancel and timeout.

Add configurable pending timeout in settings and clearer CTP password save feedback.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-26 00:05:45 +08:00
parent 7ea8fb6301
commit a23f2c80ca
10 changed files with 567 additions and 41 deletions
+41 -7
View File
@@ -390,6 +390,8 @@ def init_db():
set_setting("max_margin_pct", "30")
if not get_setting("trailing_be_tick_buffer"):
set_setting("trailing_be_tick_buffer", "2")
if not get_setting("pending_order_timeout_min"):
set_setting("pending_order_timeout_min", "5")
if not get_setting("fee_source_mode"):
set_setting("fee_source_mode", "ctp")
set_setting("fee_source_mode", "ctp")
@@ -1710,30 +1712,61 @@ def settings():
except ValueError:
flash("移动保本缓冲无效")
return redirect(url_for("settings"))
try:
pt = int(float(request.form.get("pending_order_timeout_min", "5") or 5))
set_setting("pending_order_timeout_min", str(max(1, min(60, pt))))
except ValueError:
flash("挂单超时无效")
return redirect(url_for("settings"))
flash("交易模式已保存")
elif action == "ctp":
from ctp_settings import save_ctp_settings_from_form
save_ctp_settings_from_form(request.form, set_setting)
save_result = save_ctp_settings_from_form(request.form, set_setting)
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())
live_pwd_len = len((request.form.get("ctp_live_password") or "").strip())
print(
f"CTP settings save: simnow_password_len={simnow_pwd_len} "
f"live_password_len={live_pwd_len} updated={pwd_updated}",
flush=True,
)
app.logger.info(
"CTP settings save: simnow_password_len=%s live_password_len=%s updated=%s",
simnow_pwd_len,
live_pwd_len,
pwd_updated,
)
if "simnow_password" in pwd_updated:
pwd_note = f"SimNow 交易密码已更新({simnow_pwd_len} 位)"
elif "simnow_password" in pwd_empty:
pwd_note = "SimNow 交易密码未改:提交为空,请在「交易密码」框手打后再保存"
elif "ctp_live_password" in pwd_updated:
pwd_note = "实盘交易密码已更新"
elif "ctp_live_password" in pwd_empty:
pwd_note = "实盘交易密码未改(提交为空)"
else:
pwd_note = ""
flash_msg = "CTP 配置已保存,正在使用新地址重连…"
if pwd_note:
flash_msg = f"CTP 配置已保存;{pwd_note},正在重连…"
try:
from vnpy_bridge import get_bridge
from trading_context import get_trading_mode
b = get_bridge()
if (request.form.get("simnow_password") or "").strip() or (
request.form.get("ctp_live_password") or ""
).strip():
if pwd_updated:
b._clear_login_cooldown()
mode = get_trading_mode(get_setting)
info = b.reconnect_after_settings_saved(mode)
if info.get("cooldown"):
flash_msg = "CTP 配置已保存;当前处于登录冷却,请稍后再连"
flash_msg = f"CTP 配置已保存;{pwd_note or '请稍后再连'}"
elif not info.get("started") and info.get("connected"):
flash_msg = "CTP 配置已保存,当前连接正常"
flash_msg = f"CTP 配置已保存{pwd_note or '当前连接正常'}"
except Exception as exc:
app.logger.warning("CTP reconnect after settings save: %s", exc)
flash_msg = "CTP 配置已保存请稍后在持仓监控页重连"
flash_msg = f"CTP 配置已保存{pwd_note or '请稍后在持仓监控页重连'}"
flash(flash_msg)
elif action == "nav":
items = {k: request.form.get(f"nav_{k}") == "on" for k in NAV_TOGGLES}
@@ -1781,6 +1814,7 @@ def settings():
risk_percent=get_setting("risk_percent", "1"),
max_margin_pct=get_setting("max_margin_pct", "30"),
trailing_be_tick_buffer=get_setting("trailing_be_tick_buffer", "2"),
pending_order_timeout_min=get_setting("pending_order_timeout_min", "5"),
nav_items=get_nav_items(get_setting),
nav_toggles=NAV_TOGGLES,
)