From 1b505e0b90cd9abd0e1b20c73576e13daecacbbf Mon Sep 17 00:00:00 2001 From: George Date: Wed, 27 May 2026 07:39:12 +0100 Subject: [PATCH] Fix: run survival check once before order loop, not per order --- grid.py | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/grid.py b/grid.py index 5503c2b..c47990c 100644 --- a/grid.py +++ b/grid.py @@ -292,6 +292,23 @@ class GridManager: f"{[f'${p:.2f}' for p in missing]}" ) + # Run survival check ONCE before placing any orders. + # No need to repeat for each order — the grid doesn't change + # between placements in the same loop. + full_sim = calc._simulate_grid( + s.highest_open_price, spacing_pct, s.gbpusd, s.all_levels() + ) + survival = calc.survival_check( + s.equity, s.highest_open_price, full_sim, s.gbpusd, + override_pct=60.0 + ) + if not survival["safe"]: + log.warning( + f"Survival check failed — no orders placed. " + f"Margin at floor: {survival['margin_level_at_floor_pct']:.1f}%" + ) + return + for target_price in missing: # Safety: never place at or above current price if target_price >= s.current_price: @@ -308,24 +325,6 @@ class GridManager: log.info(f"Skipping ${target_price:.2f} — at/above no-TP position.") continue - # Survival check — simulate FULL grid at 60% drop - # (not just current orders — honest worst case) - full_sim = calc._simulate_grid( - s.highest_open_price, spacing_pct, s.gbpusd, - s.all_levels() - ) - survival = calc.survival_check( - s.equity, s.highest_open_price, - full_sim, s.gbpusd, - override_pct=60.0 - ) - if not survival["safe"]: - log.warning( - f"Survival check failed — stopping. " - f"Margin at floor: {survival['margin_level_at_floor_pct']:.1f}%" - ) - break - size = calc.get_size(target_price, s.highest_open_price) tp_price = calc.get_tp_price(target_price, size, s.gbpusd)