fix(risk): trigger cooldown only on user-initiated closes
Remove external-close risk hooks; register user_instance, user_hub, and user_trend_stop via hub API and trend stop; update docs and tests. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -838,7 +838,34 @@ def _bump_session_capital_no_commit(
|
||||
return None
|
||||
|
||||
|
||||
def _finalize_plan(cfg: dict, conn, row, result_label: str, exit_price: float) -> None:
|
||||
def _apply_trend_user_risk_close(cfg: dict, conn, *, trade_record_id=None, closed_at_ms=None) -> None:
|
||||
m = _m(cfg)
|
||||
fn = getattr(m, "hub_user_initiated_close", None)
|
||||
from account_risk_lib import CLOSE_SOURCE_USER_TREND_STOP
|
||||
|
||||
if callable(fn):
|
||||
fn(
|
||||
conn,
|
||||
source=CLOSE_SOURCE_USER_TREND_STOP,
|
||||
count=1,
|
||||
trade_record_id=trade_record_id,
|
||||
closed_at_ms=closed_at_ms,
|
||||
)
|
||||
return
|
||||
from account_risk_lib import on_user_initiated_close
|
||||
|
||||
on_user_initiated_close(
|
||||
conn,
|
||||
source=CLOSE_SOURCE_USER_TREND_STOP,
|
||||
trade_record_id=trade_record_id,
|
||||
closed_at_ms=closed_at_ms,
|
||||
trading_day=m.get_trading_day(),
|
||||
now=m.app_now(),
|
||||
count=1,
|
||||
)
|
||||
|
||||
|
||||
def _finalize_plan(cfg: dict, conn, row, result_label: str, exit_price: float, *, user_initiated_risk: bool = False) -> None:
|
||||
m = _m(cfg)
|
||||
plan_id = int(row["id"])
|
||||
active = conn.execute(
|
||||
@@ -897,6 +924,7 @@ def _finalize_plan(cfg: dict, conn, row, result_label: str, exit_price: float) -
|
||||
except Exception:
|
||||
pass
|
||||
session_capital = None
|
||||
trade_record_id = None
|
||||
if not _trend_plan_trade_exists(conn, plan_id):
|
||||
session_date = row["session_date"] or m.get_trading_day()
|
||||
session_capital = _bump_session_capital_no_commit(
|
||||
@@ -928,6 +956,26 @@ def _finalize_plan(cfg: dict, conn, row, result_label: str, exit_price: float) -
|
||||
entry_reason=ENTRY_REASON_TREND_PULLBACK,
|
||||
),
|
||||
)
|
||||
try:
|
||||
from account_risk_lib import insert_trade_record_id
|
||||
|
||||
trade_record_id = insert_trade_record_id(conn)
|
||||
except Exception:
|
||||
trade_record_id = None
|
||||
if user_initiated_risk:
|
||||
closed_ms = None
|
||||
to_ms = getattr(m, "_to_ms_with_fallback", None)
|
||||
if callable(to_ms):
|
||||
try:
|
||||
closed_ms = to_ms(None, closed_at)
|
||||
except Exception:
|
||||
closed_ms = None
|
||||
_apply_trend_user_risk_close(
|
||||
cfg,
|
||||
conn,
|
||||
trade_record_id=trade_record_id,
|
||||
closed_at_ms=closed_ms,
|
||||
)
|
||||
conn.commit()
|
||||
try:
|
||||
from strategy_wechat_notify import notify_trend_plan_ended
|
||||
@@ -1845,7 +1893,7 @@ def register_trend_routes(app: Flask, cfg: dict) -> None:
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
_finalize_plan(cfg, conn, row, "手动平仓", exit_p)
|
||||
_finalize_plan(cfg, conn, row, "手动平仓", exit_p, user_initiated_risk=True)
|
||||
except Exception as e:
|
||||
conn.execute(
|
||||
"UPDATE trend_pullback_plans SET status='stopped_manual', message=? "
|
||||
|
||||
Reference in New Issue
Block a user