INNER CODE UNIT · Python
_persist_closed_trade
aaryansinha16/AI-trader · backend/app.py:108
def _persist_closed_trade(pos: dict):
"""Append a closed trade (with journey) to the JSONL file for its mode."""
mode = pos.get("mode", "test")
_closed_trades_by_mode.setdefault(mode, []).append(pos)
fpath = _paper_trades_file(mode)
try:
with open(fpath, "a") as f:
f.write(json.dumps(pos, default=str) + "\n")
except Exception as e:
logger.warning(f"Failed to persist trade {pos.get('id')}: {e}")
# ── Consecutive SL tracking (ported from backtest 2026-04-15) ────────
# Count consecutive SL_HIT exits. After 2 in a row, pause new entries
# for 30 minutes. Any non-SL exit (trailing, target, RL, timeout, manual)
# resets the counter.
global _consecutive_sl_hits, _sl_pause_until
reason = pos.get("exit_reason", "")
if reason == "SL_HIT":