Add martingale mode for risk-based percent sizing.

Enable in settings (default off): after N consecutive loss days, double the effective risk_loss_pct up to a configurable max; blocked when base pct > 3%.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-07 09:55:29 +08:00
parent 3d1f9f3d50
commit 0f552eb50e
9 changed files with 476 additions and 10 deletions
+28
View File
@@ -187,6 +187,23 @@ class StrategyEngine:
"perp_qty_eth": perp_qty,
"option_qty_eth": opt_qty,
}
try:
from .risk_sizing import resolve_martingale
martingale = resolve_martingale(
self.db, ledger=self.ledger, base_pct=risk_loss_pct
)
except Exception:
logger.exception("resolve_martingale for state() failed")
martingale = {
"enabled": False,
"eligible": False,
"doubles": 0,
"loss_days": 0,
"start_after_loss_days": 2,
"max_doubles": 3,
"effective_pct": risk_loss_pct,
}
rest_until = row["rest_until_ms"]
rest_left = 0
if rest_until:
@@ -235,6 +252,17 @@ class StrategyEngine:
"risk_last_k": risk_last_k if risk_last_k > 0 else None,
"risk_sizing_preview": risk_preview,
"risk_sizing_locked": bool(trade_locked and sizing_mode == "risk_based"),
"martingale_enabled": bool(martingale.get("enabled")),
"martingale_eligible": bool(martingale.get("eligible")),
"martingale_doubles": int(martingale.get("doubles") or 0),
"martingale_loss_days": int(martingale.get("loss_days") or 0),
"martingale_start_after_loss_days": int(
martingale.get("start_after_loss_days") or 2
),
"martingale_max_doubles": int(martingale.get("max_doubles") or 3),
"risk_effective_loss_pct": float(
martingale.get("effective_pct") or risk_loss_pct
),
"min_option_hours": min_hours,
"min_option_leverage": min_opt_lev,
"atm_open_offset_enabled": atm_off_on,