diff --git a/momentum_cli.py b/momentum_cli.py index d935542..a131bb6 100644 --- a/momentum_cli.py +++ b/momentum_cli.py @@ -17,7 +17,6 @@ exits non-zero if any check FAILs, so it is scriptable / CI-able. from __future__ import annotations import json -import sys from datetime import datetime, timezone import typer diff --git a/runtime/exit_manager.py b/runtime/exit_manager.py index 9a3c209..ba1079f 100644 --- a/runtime/exit_manager.py +++ b/runtime/exit_manager.py @@ -30,7 +30,6 @@ class _Tracked: entry_price: float init_stop: float # original opening-range-low stop (for the R math) entry_ts: object # timestamp we first saw the position (~entry) - last_stop: float # last stop we set at the broker scaled: bool = False @@ -120,6 +119,10 @@ class LiveExitManager: below market when underwater (a sell-stop can't sit above the last price). Returns the stop price placed.""" qty = int(abs(float(p.get("qty") or 0))) + if qty < 1: # degenerate qty: a 0-qty submit is + return round(entry, 2) # rejected and would churn — bail + # cur falsy (price missing / 0 after-hours) falls to the breakeven arm; a stop + # at entry is below market when in profit and a safe floor otherwise. if cur and cur <= entry: pct = self.cfg.catastrophe_pct or 0.10 stop_px = round(cur * (1 - pct), 2) @@ -276,16 +279,17 @@ class LiveExitManager: # the move since entry, not since we first noticed the position) entry_ts = self._entry_time(sym, orders) or _last_ts(bars) if cur_stop >= entry: - # already at BREAKEVEN+ with no real R to trail from (the original - # stop was stripped/invalid). Trail on a SYNTHETIC R so the step-trail - # / profit-lock still ratchet the broker stop UP as it rises (only ever - # up, never below the live breakeven stop). 0 = leave it frozen. + # stop is at/above entry (breakeven already reached on a healthy + # trade, OR the original R was lost — stop stripped/invalid), so there + # is no real risk distance to trail from. Trail on a SYNTHETIC R so the + # step-trail / profit-lock still ratchet the broker stop UP as it rises + # (only ever up, never below the live stop). 0 = leave it frozen. if not self.cfg.default_trail_r_pct: continue init = entry * (1.0 - self.cfg.default_trail_r_pct) - self._tracked[sym] = _Tracked(entry, init, entry_ts, cur_stop) + self._tracked[sym] = _Tracked(entry, init, entry_ts) else: - self._tracked[sym] = _Tracked(entry, cur_stop, entry_ts, cur_stop) + self._tracked[sym] = _Tracked(entry, cur_stop, entry_ts) tr = self._tracked[sym] since = _bars_since(bars, tr.entry_ts) @@ -338,7 +342,6 @@ class LiveExitManager: elif leg_id: try: self.client.replace_order(leg_id, stop_price=round(d.desired_stop, 2)) - tr.last_stop = d.desired_stop self._emit(sym, f"stop {sym} {cur_stop:.2f} -> {d.desired_stop:.2f}", "stop_moved", {"from": cur_stop, "to": d.desired_stop}) actions.append(f"{sym} stop->{d.desired_stop:.2f}") diff --git a/storage/db_pg.py b/storage/db_pg.py index c33d468..0a59e44 100644 --- a/storage/db_pg.py +++ b/storage/db_pg.py @@ -180,6 +180,10 @@ class PgConnection: # at a known LAN endpoint, try the configured host first and then the alternate, so the # loop self-heals on boot regardless of the current tunnel state (this is the recurring # crash-loop landmine). "connection refused" returns instantly, so the fallback is cheap. +# _LAN_DB_HOSTS is the GATE (superset, incl. localhost): only a DSN pointing at one of +# these gets host-swapped, so a non-LAN DSN (e.g. a cloud RDS host) is never redirected +# to the wrong database. _LAN_DB_FALLBACKS is the TRY-ORDER (the two real endpoints). +# Keep the gate a superset of the fallbacks. _LAN_DB_HOSTS = ("127.0.0.1", "localhost", "192.168.1.5") _LAN_DB_FALLBACKS = ("192.168.1.5", "127.0.0.1") diff --git a/strategy/exits.py b/strategy/exits.py index bdc54aa..09fe865 100644 --- a/strategy/exits.py +++ b/strategy/exits.py @@ -127,7 +127,9 @@ class ExitConfig: catastrophe_pct=f("TRADING_CATASTROPHE_STOP_PCT", "0.10"), catastrophe_risk_mult=f("TRADING_CATASTROPHE_RISK_MULT", "1.5"), enforce_stop_grace_passes=int(f("TRADING_ENFORCE_STOP_GRACE_PASSES", "2")), - default_trail_r_pct=f("TRADING_EXIT_DEFAULT_TRAIL_R_PCT", "0.10"), + # clamp to [0, 1): a synthetic risk distance must be a positive fraction of + # entry below it; >=1 or negative would put the synthetic stop at/above entry. + default_trail_r_pct=min(max(f("TRADING_EXIT_DEFAULT_TRAIL_R_PCT", "0.10"), 0.0), 0.95), ) def describe(self) -> str: @@ -152,6 +154,8 @@ class ExitConfig: parts.append(f"catastrophe@-{self.catastrophe_pct:.0%}/{self.catastrophe_risk_mult:g}xR") if self.enforce_stop_grace_passes: parts.append(f"enforce-stop({self.enforce_stop_grace_passes}p)") + if self.default_trail_r_pct: + parts.append(f"synth-trail {self.default_trail_r_pct:.0%}") return ", ".join(parts)