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:
+36
-12
@@ -1528,6 +1528,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)
|
||||
@@ -3527,16 +3549,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"],))
|
||||
if result in ("止盈", "止损", "保本止盈", "移动止盈", "手动平仓", "强制清仓"):
|
||||
send_wechat_msg(
|
||||
@@ -8126,10 +8138,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,
|
||||
@@ -8190,6 +8203,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()
|
||||
@@ -8849,6 +8872,7 @@ try:
|
||||
ohlcv_fn=_hub_fetch_ohlcv,
|
||||
volume_rank_fn=_hub_fetch_volume_rank,
|
||||
risk_status_fn=hub_account_risk_status,
|
||||
user_close_fn=hub_user_initiated_close,
|
||||
)
|
||||
except Exception as _hub_err:
|
||||
print(f"[hub_bridge] okx: {_hub_err}")
|
||||
|
||||
Reference in New Issue
Block a user