feat(risk): add account cooldown and daily freeze after manual/external close
Implements shared account_risk_lib with 4h/1h cooloff and daily freeze rules, wires hooks into all four exchange apps and hub monitor UI, with tests and docs. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1480,6 +1480,9 @@ def init_db():
|
||||
from strategy_db import init_strategy_tables
|
||||
|
||||
init_strategy_tables(conn)
|
||||
from account_risk_lib import ensure_account_risk_schema
|
||||
|
||||
ensure_account_risk_schema(conn)
|
||||
backfill_missing_key_signal_types(conn, monitor_type=ORDER_MONITOR_TYPE_KEY_AUTO)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
@@ -1513,6 +1516,18 @@ def get_db():
|
||||
return conn
|
||||
|
||||
|
||||
def hub_account_risk_status(conn):
|
||||
from account_risk_lib import compute_account_risk_status, ensure_account_risk_schema
|
||||
|
||||
ensure_account_risk_schema(conn)
|
||||
return compute_account_risk_status(
|
||||
conn,
|
||||
trading_day=get_trading_day(),
|
||||
now=app_now(),
|
||||
fmt_local_ms=ms_to_app_local_str,
|
||||
)
|
||||
|
||||
|
||||
def app_now():
|
||||
"""应用本地时区当前墙钟时间(无时区的 datetime,便于与库中字符串直接比较)。"""
|
||||
return datetime.now(APP_TZ).replace(tzinfo=None)
|
||||
@@ -2509,6 +2524,16 @@ def trading_day_reset_allows_new_open(now, conn=None):
|
||||
|
||||
def precheck_risk(conn, symbol, direction):
|
||||
now = app_now()
|
||||
from account_risk_lib import account_risk_blocks_trading
|
||||
|
||||
ok_risk, risk_reason = account_risk_blocks_trading(
|
||||
conn,
|
||||
trading_day=get_trading_day(now),
|
||||
now=now,
|
||||
fmt_local_ms=ms_to_app_local_str,
|
||||
)
|
||||
if not ok_risk:
|
||||
return False, risk_reason
|
||||
if not trading_day_reset_allows_new_open(now):
|
||||
return False, f"北京时间 {TRADING_DAY_RESET_HOUR}:00 前不允许持仓"
|
||||
active_count = get_active_position_count(conn)
|
||||
@@ -3502,6 +3527,16 @@ 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(
|
||||
@@ -6317,12 +6352,14 @@ def render_main_page(page="trade"):
|
||||
open_guard_enabled = get_trading_day_reset_open_guard_enabled(conn)
|
||||
open_guard_blocks_now = open_guard_enabled and now.hour < TRADING_DAY_RESET_HOUR
|
||||
opens_today = count_opens_for_trading_day(conn, trading_day)
|
||||
risk_status = hub_account_risk_status(conn)
|
||||
can_trade = can_trade_new_open(
|
||||
time_allows=trading_day_reset_allows_new_open(now, conn),
|
||||
active_count=active_count,
|
||||
max_active_positions=MAX_ACTIVE_POSITIONS,
|
||||
opens_today=opens_today,
|
||||
hard_limit=DAILY_OPEN_HARD_LIMIT,
|
||||
extra_blocks=not risk_status.get("can_trade", True),
|
||||
)
|
||||
key_rule_ctx = key_monitor_rule_template_context(
|
||||
kline_timeframe=KLINE_TIMEFRAME,
|
||||
@@ -6416,6 +6453,7 @@ def render_main_page(page="trade"):
|
||||
key_rule_ctx=key_rule_ctx,
|
||||
funds_fmt=format_funds_u,
|
||||
exchange_display=EXCHANGE_DISPLAY_NAME,
|
||||
risk_status=risk_status,
|
||||
max_active_positions=MAX_ACTIVE_POSITIONS,
|
||||
manual_min_planned_rr=MANUAL_MIN_PLANNED_RR,
|
||||
key_auto_min_planned_rr=KEY_AUTO_MIN_PLANNED_RR,
|
||||
@@ -6484,6 +6522,7 @@ def api_account_snapshot():
|
||||
active_count = get_active_position_count(conn)
|
||||
open_guard_enabled = get_trading_day_reset_open_guard_enabled(conn)
|
||||
opens_today = count_opens_for_trading_day(conn, trading_day)
|
||||
risk_status = hub_account_risk_status(conn)
|
||||
conn.close()
|
||||
open_guard_blocks_now = open_guard_enabled and now.hour < TRADING_DAY_RESET_HOUR
|
||||
can_trade = can_trade_new_open(
|
||||
@@ -6492,6 +6531,7 @@ def api_account_snapshot():
|
||||
max_active_positions=MAX_ACTIVE_POSITIONS,
|
||||
opens_today=opens_today,
|
||||
hard_limit=DAILY_OPEN_HARD_LIMIT,
|
||||
extra_blocks=not risk_status.get("can_trade", True),
|
||||
)
|
||||
available_trading_usdt = get_available_trading_usdt()
|
||||
return jsonify({
|
||||
@@ -6510,6 +6550,7 @@ def api_account_snapshot():
|
||||
"reset_hour": TRADING_DAY_RESET_HOUR,
|
||||
"manual_min_planned_rr": MANUAL_MIN_PLANNED_RR,
|
||||
"trading_day": trading_day,
|
||||
"risk_status": risk_status,
|
||||
})
|
||||
|
||||
|
||||
@@ -8085,6 +8126,15 @@ def del_order(id):
|
||||
opened_at=opened_at,
|
||||
closed_at=closed_at,
|
||||
)
|
||||
from account_risk_lib import insert_trade_record_id, on_manual_close
|
||||
|
||||
on_manual_close(
|
||||
conn,
|
||||
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', exchange_close_order_id=? WHERE id=?", (close_order_id, id))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
@@ -8297,6 +8347,16 @@ def add_journal():
|
||||
d.get("post_breakeven_stare"), d.get("new_trade_while_occupied"), d.get("note"), image_filename
|
||||
)
|
||||
)
|
||||
from account_risk_lib import on_journal_saved
|
||||
|
||||
on_journal_saved(
|
||||
conn,
|
||||
early_exit_trigger=early_exit_trigger,
|
||||
early_exit_note=early_exit_note,
|
||||
mood_issues_raw=mood_issues,
|
||||
trading_day=get_trading_day(),
|
||||
now=app_now(),
|
||||
)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
if chart_msg:
|
||||
@@ -8788,6 +8848,7 @@ try:
|
||||
views={"add_order": add_order, "add_key": add_key},
|
||||
ohlcv_fn=_hub_fetch_ohlcv,
|
||||
volume_rank_fn=_hub_fetch_volume_rank,
|
||||
risk_status_fn=hub_account_risk_status,
|
||||
)
|
||||
except Exception as _hub_err:
|
||||
print(f"[hub_bridge] okx: {_hub_err}")
|
||||
|
||||
Reference in New Issue
Block a user