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:
@@ -135,7 +135,6 @@ DAILY_OPEN_HARD_LIMIT=0
|
||||
# =============================================================================
|
||||
# RISK_CONTROL_ENABLED=true
|
||||
# RISK_COOLING_HOURS_MANUAL=4
|
||||
# RISK_COOLING_HOURS_EXTERNAL=4
|
||||
# RISK_COOLING_HOURS_MANUAL_JOURNAL=1
|
||||
# RISK_MANUAL_CLOSE_DAILY_LIMIT=2
|
||||
# RISK_MOOD_ISSUES_DAILY_FREEZE=true
|
||||
|
||||
@@ -1539,6 +1539,28 @@ def hub_account_risk_status(conn):
|
||||
)
|
||||
|
||||
|
||||
def hub_user_initiated_close(
|
||||
conn,
|
||||
*,
|
||||
source,
|
||||
count=1,
|
||||
trade_record_id=None,
|
||||
closed_at_ms=None,
|
||||
):
|
||||
from account_risk_lib import CLOSE_SOURCE_USER_HUB, on_user_initiated_close
|
||||
|
||||
src = (source or "").strip() or CLOSE_SOURCE_USER_HUB
|
||||
on_user_initiated_close(
|
||||
conn,
|
||||
source=src,
|
||||
trade_record_id=trade_record_id,
|
||||
closed_at_ms=closed_at_ms,
|
||||
trading_day=get_trading_day(),
|
||||
now=app_now(),
|
||||
count=count,
|
||||
)
|
||||
|
||||
|
||||
def app_now():
|
||||
"""应用本地时区当前墙钟时间(无时区的 datetime,便于与库中字符串直接比较)。"""
|
||||
return datetime.now(APP_TZ).replace(tzinfo=None)
|
||||
@@ -4162,16 +4184,6 @@ def reconcile_external_closes(conn, days=None):
|
||||
opened_at=opened_at,
|
||||
closed_at=closed_at,
|
||||
)
|
||||
from account_risk_lib import on_external_close, should_apply_external_close_risk
|
||||
|
||||
if should_apply_external_close_risk(result):
|
||||
close_ms = _to_ms_with_fallback(None, closed_at)
|
||||
on_external_close(
|
||||
conn,
|
||||
closed_at_ms=close_ms,
|
||||
trading_day=session_date,
|
||||
now=app_now(),
|
||||
)
|
||||
conn.execute("UPDATE order_monitors SET status='stopped' WHERE id=?", (r["id"],))
|
||||
clear_key_sizing_snapshot_if_flat(conn, r["session_date"] or get_trading_day())
|
||||
if result in ("止盈", "止损", "保本止盈", "移动止盈", "手动平仓", "强制清仓"):
|
||||
@@ -8563,10 +8575,11 @@ def del_order(id):
|
||||
opened_at=opened_at,
|
||||
closed_at=closed_at,
|
||||
)
|
||||
from account_risk_lib import insert_trade_record_id, on_manual_close
|
||||
from account_risk_lib import CLOSE_SOURCE_USER_INSTANCE, insert_trade_record_id, on_user_initiated_close
|
||||
|
||||
on_manual_close(
|
||||
on_user_initiated_close(
|
||||
conn,
|
||||
source=CLOSE_SOURCE_USER_INSTANCE,
|
||||
trade_record_id=insert_trade_record_id(conn),
|
||||
closed_at_ms=_to_ms_with_fallback(None, closed_at),
|
||||
trading_day=session_date,
|
||||
@@ -8630,6 +8643,16 @@ def del_order(id):
|
||||
opened_at=opened_at,
|
||||
closed_at=closed_at,
|
||||
)
|
||||
from account_risk_lib import CLOSE_SOURCE_USER_INSTANCE, insert_trade_record_id, on_user_initiated_close
|
||||
|
||||
on_user_initiated_close(
|
||||
conn,
|
||||
source=CLOSE_SOURCE_USER_INSTANCE,
|
||||
trade_record_id=insert_trade_record_id(conn),
|
||||
closed_at_ms=_to_ms_with_fallback(None, closed_at),
|
||||
trading_day=session_date,
|
||||
now=app_now(),
|
||||
)
|
||||
conn.execute("UPDATE order_monitors SET status='stopped' WHERE id=?", (id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
@@ -9311,6 +9334,7 @@ try:
|
||||
volume_rank_fn=_hub_fetch_volume_rank,
|
||||
reconcile_hub_flat_fn=reconcile_hub_external_close,
|
||||
risk_status_fn=hub_account_risk_status,
|
||||
user_close_fn=hub_user_initiated_close,
|
||||
)
|
||||
except Exception as _hub_err:
|
||||
print(f"[hub_bridge] gate_bot: {_hub_err}")
|
||||
|
||||
Reference in New Issue
Block a user