fix: show box/convergence breakout labels in key level trade records
Persist 箱体突破/收敛突破 as key_signal_type and backfill historical records on startup. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -43,6 +43,7 @@ from ai_review_lib import (
|
||||
from form_submit_lib import check_duplicate_submit, submit_scope_add_key, submit_scope_add_order
|
||||
from fib_key_monitor_lib import (
|
||||
FIB_KEY_MONITOR_TYPES,
|
||||
backfill_missing_key_signal_types,
|
||||
calc_fib_plan,
|
||||
entry_reason_from_key_signal,
|
||||
fib_invalidate_by_mark,
|
||||
@@ -1460,6 +1461,7 @@ def init_db():
|
||||
from strategy_db import init_strategy_tables
|
||||
|
||||
init_strategy_tables(conn)
|
||||
backfill_missing_key_signal_types(conn, monitor_type=ORDER_MONITOR_TYPE_KEY_AUTO)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
@@ -44,6 +44,7 @@ from form_submit_lib import check_duplicate_submit, submit_scope_add_key, submit
|
||||
from fib_key_monitor_lib import (
|
||||
FIB_KEY_MONITOR_TYPES,
|
||||
KEY_ENTRY_REASON_BY_SIGNAL,
|
||||
backfill_missing_key_signal_types,
|
||||
calc_fib_plan,
|
||||
entry_reason_from_key_signal,
|
||||
fib_invalidate_by_mark,
|
||||
@@ -1454,6 +1455,7 @@ def init_db():
|
||||
from strategy_db import init_strategy_tables
|
||||
|
||||
init_strategy_tables(conn)
|
||||
backfill_missing_key_signal_types(conn, monitor_type=ORDER_MONITOR_TYPE_KEY_AUTO)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
@@ -44,6 +44,7 @@ from form_submit_lib import check_duplicate_submit, submit_scope_add_key, submit
|
||||
from fib_key_monitor_lib import (
|
||||
FIB_KEY_MONITOR_TYPES,
|
||||
KEY_ENTRY_REASON_BY_SIGNAL,
|
||||
backfill_missing_key_signal_types,
|
||||
calc_fib_plan,
|
||||
entry_reason_from_key_signal,
|
||||
fib_invalidate_by_mark,
|
||||
@@ -1454,6 +1455,7 @@ def init_db():
|
||||
from strategy_db import init_strategy_tables
|
||||
|
||||
init_strategy_tables(conn)
|
||||
backfill_missing_key_signal_types(conn, monitor_type=ORDER_MONITOR_TYPE_KEY_AUTO)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@ from ai_review_lib import (
|
||||
from form_submit_lib import check_duplicate_submit, submit_scope_add_key, submit_scope_add_order
|
||||
from fib_key_monitor_lib import (
|
||||
FIB_KEY_MONITOR_TYPES,
|
||||
backfill_missing_key_signal_types,
|
||||
calc_fib_plan,
|
||||
entry_reason_from_key_signal,
|
||||
fib_invalidate_by_mark,
|
||||
@@ -1440,6 +1441,7 @@ def init_db():
|
||||
from strategy_db import init_strategy_tables
|
||||
|
||||
init_strategy_tables(conn)
|
||||
backfill_missing_key_signal_types(conn, monitor_type=ORDER_MONITOR_TYPE_KEY_AUTO)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
"""斐波关键位监控:纯计算与类型判断(Gate / Binance 主站共用)。"""
|
||||
|
||||
from key_monitor_lib import KEY_MONITOR_AUTO_TYPES
|
||||
|
||||
FIB_KEY_MONITOR_TYPES = frozenset({"斐波回调0.618", "斐波回调0.786"})
|
||||
KEY_MONITOR_TRADE_TYPE = "关键位监控"
|
||||
|
||||
FIB_RATIO_BY_TYPE = {
|
||||
"斐波回调0.618": 0.618,
|
||||
@@ -47,6 +50,8 @@ def stored_key_signal_type(monitor_type):
|
||||
return mt
|
||||
if mt == "假突破":
|
||||
return mt
|
||||
if mt in KEY_MONITOR_AUTO_TYPES:
|
||||
return mt
|
||||
return None
|
||||
|
||||
|
||||
@@ -76,6 +81,47 @@ def key_signal_type_for_trade_record(key_signal_type, box_auto_types):
|
||||
return None
|
||||
|
||||
|
||||
def backfill_missing_key_signal_types(conn, *, monitor_type: str = KEY_MONITOR_TRADE_TYPE) -> int:
|
||||
"""补全历史 trade_records / order_monitors 中缺失的箱体/收敛 key_signal_type。"""
|
||||
mt = (monitor_type or KEY_MONITOR_TRADE_TYPE).strip()
|
||||
updated = 0
|
||||
for signal in KEY_MONITOR_AUTO_TYPES:
|
||||
entry_reason = KEY_ENTRY_REASON_BY_SIGNAL.get(signal)
|
||||
if entry_reason:
|
||||
cur = conn.execute(
|
||||
"""UPDATE trade_records SET key_signal_type=?
|
||||
WHERE monitor_type=? AND (key_signal_type IS NULL OR TRIM(key_signal_type)='')
|
||||
AND TRIM(COALESCE(entry_reason, ''))=?""",
|
||||
(signal, mt, entry_reason),
|
||||
)
|
||||
updated += int(cur.rowcount or 0)
|
||||
rows = conn.execute(
|
||||
"""SELECT id, symbol, opened_at FROM trade_records
|
||||
WHERE monitor_type=? AND (key_signal_type IS NULL OR TRIM(key_signal_type)='')""",
|
||||
(mt,),
|
||||
).fetchall()
|
||||
for row in rows:
|
||||
sym = row["symbol"]
|
||||
opened = (row["opened_at"] or "").strip()
|
||||
for signal in KEY_MONITOR_AUTO_TYPES:
|
||||
hist = conn.execute(
|
||||
"""SELECT monitor_type FROM key_monitor_history
|
||||
WHERE symbol=? AND monitor_type=? AND close_reason='auto_opened'
|
||||
AND (?='' OR closed_at <= ?)
|
||||
ORDER BY closed_at DESC LIMIT 1""",
|
||||
(sym, signal, opened, opened),
|
||||
).fetchone()
|
||||
if not hist:
|
||||
continue
|
||||
conn.execute(
|
||||
"UPDATE trade_records SET key_signal_type=? WHERE id=?",
|
||||
(signal, row["id"]),
|
||||
)
|
||||
updated += 1
|
||||
break
|
||||
return updated
|
||||
|
||||
|
||||
def fib_invalidate_by_mark(direction, mark_price, upper, lower):
|
||||
"""先触达止盈侧(标记价)则失效。多:mark>=H;空:mark<=L。"""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user