修复趋势回调顺势加仓的交易记录
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
"""策略交易写入 trade_records 时的类型与复盘开仓类型标注。"""
|
||||
from __future__ import annotations
|
||||
|
||||
MONITOR_TYPE_TREND_PULLBACK = "趋势回调"
|
||||
MONITOR_TYPE_ROLL = "顺势加仓"
|
||||
|
||||
ENTRY_REASON_TREND_PULLBACK = "趋势回调"
|
||||
ENTRY_REASON_ROLL = "顺势加仓"
|
||||
|
||||
STRATEGY_ENTRY_REASON_OPTIONS = (
|
||||
ENTRY_REASON_TREND_PULLBACK,
|
||||
ENTRY_REASON_ROLL,
|
||||
)
|
||||
|
||||
|
||||
def order_had_roll_fills(conn, order_monitor_id) -> bool:
|
||||
try:
|
||||
oid = int(order_monitor_id)
|
||||
except (TypeError, ValueError):
|
||||
return False
|
||||
if oid <= 0:
|
||||
return False
|
||||
try:
|
||||
row = conn.execute(
|
||||
"""SELECT 1 FROM roll_legs l
|
||||
INNER JOIN roll_groups g ON g.id = l.roll_group_id
|
||||
WHERE g.order_monitor_id=? AND l.status='filled'
|
||||
LIMIT 1""",
|
||||
(oid,),
|
||||
).fetchone()
|
||||
return row is not None
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def _row_monitor_type(row, default_manual: str) -> str:
|
||||
if row is None:
|
||||
return default_manual
|
||||
try:
|
||||
keys = row.keys() if hasattr(row, "keys") else []
|
||||
except Exception:
|
||||
keys = []
|
||||
if "monitor_type" in keys:
|
||||
mt = (row["monitor_type"] or "").strip()
|
||||
if mt:
|
||||
return mt
|
||||
return default_manual
|
||||
|
||||
|
||||
def trade_record_monitor_type(conn, order_row, *, default_manual: str = "下单监控") -> str:
|
||||
"""平仓写入 trade_records 时:曾顺势加仓则标「顺势加仓」,否则沿用监控单类型。"""
|
||||
oid = None
|
||||
try:
|
||||
keys = order_row.keys() if hasattr(order_row, "keys") else []
|
||||
if "id" in keys and order_row["id"] is not None:
|
||||
oid = int(order_row["id"])
|
||||
except Exception:
|
||||
oid = None
|
||||
if oid and order_had_roll_fills(conn, oid):
|
||||
return MONITOR_TYPE_ROLL
|
||||
return _row_monitor_type(order_row, default_manual)
|
||||
|
||||
|
||||
def entry_reason_for_monitor_type(monitor_type: str | None) -> str:
|
||||
mt = (monitor_type or "").strip()
|
||||
if mt == MONITOR_TYPE_TREND_PULLBACK:
|
||||
return ENTRY_REASON_TREND_PULLBACK
|
||||
if mt == MONITOR_TYPE_ROLL:
|
||||
return ENTRY_REASON_ROLL
|
||||
return ""
|
||||
Reference in New Issue
Block a user