Add daily loss force-flatten at configurable equity limit

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-03 12:42:13 +08:00
parent b6c3266a9e
commit 2081bf2da9
17 changed files with 850 additions and 97 deletions
+32
View File
@@ -240,6 +240,25 @@ def _on_ctp_connected(mode: str) -> None:
get_bridge().request_position_snapshot(force=True)
get_bridge().calibrate_trading_state()
_persist_snapshot(mode)
conn = connect_db(DB_PATH)
try:
_init_worker_tables(conn)
capital = _capital(conn)
if capital <= 0:
acc = ctp_get_account(mode) or {}
capital = float(acc.get("balance") or 0)
if capital > 0:
from modules.risk.daily_loss_guard import check_daily_loss_and_flatten
check_daily_loss_and_flatten(
conn,
mode,
equity=capital,
notify_fn=_send_wechat_msg,
get_setting=get_setting,
)
finally:
conn.close()
except Exception as exc:
logger.debug("worker ctp connected callback: %s", exc)
@@ -287,6 +306,16 @@ def _start_background_workers() -> None:
get_be_tick_buffer_fn=lambda: get_trailing_be_tick_buffer(get_setting),
notify_fn=_send_wechat_msg,
)
from modules.risk.daily_loss_guard import start_daily_loss_guard_worker
start_daily_loss_guard_worker(
db_path=DB_PATH,
get_mode_fn=_mode,
init_tables_fn=_init_worker_tables,
get_capital_fn=_capital,
get_setting_fn=get_setting,
notify_fn=_send_wechat_msg,
)
def _snapshot_loop() -> None:
time.sleep(3)
@@ -432,6 +461,9 @@ def api_order():
price=float(data.get("price") or 0),
settings=data.get("settings") or {},
order_type=data.get("order_type") or "limit",
urgency=data.get("urgency") or "normal",
equity=data.get("equity"),
slippage_buffer_pct=data.get("slippage_buffer_pct"),
)
_persist_snapshot(mode)
return _json_ok(**result)