Count expiry settlements as martingale losses.
Even small-profit expiry closes count toward consecutive loss days; only non-expiry profitable days break the streak. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -269,10 +269,26 @@ def resolve_budget(db: Database | None = None) -> tuple[float | None, str, float
|
||||
MARTINGALE_MAX_BASE_PCT = 3.0
|
||||
|
||||
|
||||
def _is_expiry_close_reason(reason: str | None) -> bool:
|
||||
r = str(reason or "").strip().lower()
|
||||
return r in ("expiry", "到期", "到期结算", "到期结算全平")
|
||||
|
||||
|
||||
def _martingale_day_pnl_contrib(realized_pnl: float, close_reason: str | None) -> float:
|
||||
"""
|
||||
倍投连亏日口径:到期结算无论实际盈亏(含小盈利)一律按亏损计入;
|
||||
其它平仓按真实 realized_pnl。
|
||||
"""
|
||||
if _is_expiry_close_reason(close_reason):
|
||||
return -1.0
|
||||
return float(realized_pnl or 0.0)
|
||||
|
||||
|
||||
def consecutive_loss_days(db: Database | None = None) -> int:
|
||||
"""
|
||||
按上海日历「平仓日」汇总净盈亏,从最近有平仓的一天往前数连续亏损天数。
|
||||
某日净盈亏 < 0 计为亏损日;无平仓的日历日不计入、不打断(按有成交日序列)。
|
||||
按上海日历「平仓日」汇总倍投口径盈亏,从最近有平仓的一天往前数连续亏损天数。
|
||||
某日合计 < 0 计为亏损日;到期结算组无论盈亏均按亏损计入。
|
||||
无平仓的日历日不计入、不打断(按有成交日序列)。
|
||||
"""
|
||||
from collections import defaultdict
|
||||
from datetime import datetime, timezone
|
||||
@@ -280,7 +296,7 @@ def consecutive_loss_days(db: Database | None = None) -> int:
|
||||
|
||||
database = db or get_db()
|
||||
rows = database.fetchall(
|
||||
"""SELECT realized_pnl, close_at_ms FROM groups
|
||||
"""SELECT realized_pnl, close_at_ms, close_reason FROM groups
|
||||
WHERE status='closed' AND close_at_ms IS NOT NULL
|
||||
ORDER BY close_at_ms ASC"""
|
||||
)
|
||||
@@ -300,7 +316,10 @@ def consecutive_loss_days(db: Database | None = None) -> int:
|
||||
.astimezone(sh)
|
||||
.strftime("%Y-%m-%d")
|
||||
)
|
||||
day_pnl[day] += float(r["realized_pnl"] or 0)
|
||||
day_pnl[day] += _martingale_day_pnl_contrib(
|
||||
float(r["realized_pnl"] or 0),
|
||||
r["close_reason"],
|
||||
)
|
||||
if not day_pnl:
|
||||
return 0
|
||||
streak = 0
|
||||
|
||||
Reference in New Issue
Block a user