Fix all audit issues: survival check performance, display consistency, atomic config write, credential validation, TP null handling, highest_open preservation, stale docstring

This commit is contained in:
2026-05-27 07:45:25 +01:00
parent 1b505e0b90
commit ea428b5025
5 changed files with 56 additions and 31 deletions
+18 -10
View File
@@ -409,9 +409,6 @@ class Calibrator:
f"{gap:.0f} at £{daily_rate:.2f}/day)"
)
# ══════════════════════════════════════
# 3. GRID (survival-gated — spacing first, then depth)
# ══════════════════════════════════════
# ══════════════════════════════════════
# 3. GRID (frozen until 60% survival achieved)
# Spacing tightens only when full 60% drop simulation passes.
@@ -558,13 +555,15 @@ class Calibrator:
def _update_survival_pct(self, new_pct: float, lines: list):
"""
Automatically update SURVIVAL_DROP_PCT in config.py.
Does a safe in-place string replacement so all other
config values and comments are preserved.
Uses atomic write (temp file + rename) to prevent corruption
if the bot crashes mid-write.
"""
try:
config_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "config.py"
)
tmp_path = config_path + ".tmp"
with open(config_path, "r") as f:
content = f.read()
@@ -572,14 +571,15 @@ class Calibrator:
new_line = f"SURVIVAL_DROP_PCT = {new_pct:.1f}"
if old_line not in content:
# Try without decimal
old_line = f"SURVIVAL_DROP_PCT = {int(config.SURVIVAL_DROP_PCT)}"
if old_line in content:
new_content = content.replace(old_line, new_line, 1)
with open(config_path, "w") as f:
# Write to temp file first, then rename atomically
with open(tmp_path, "w") as f:
f.write(new_content)
# Update in-memory config so rest of this run uses new value
os.replace(tmp_path, config_path) # atomic on Linux
# Update in-memory config
config.SURVIVAL_DROP_PCT = new_pct
lines.append(
f" ✓ config.py updated: SURVIVAL_DROP_PCT = {new_pct:.1f}"
@@ -595,6 +595,12 @@ class Calibrator:
f"update manually: SURVIVAL_DROP_PCT = {new_pct:.1f}"
)
except Exception as e:
# Clean up temp file if it exists
try:
if os.path.exists(tmp_path):
os.remove(tmp_path)
except Exception:
pass
lines.append(f" ⚠ Auto-update failed: {e}")
lines.append(
f" Update manually in config.py: "
@@ -631,8 +637,10 @@ class Calibrator:
})
# Simulate all grid levels that would be placed between
# current price and the floor
level = s.current_price * (1.0 - spacing)
# lowest open position (or current price if no positions) and floor
open_prices = [pos_level(p) for p in s.positions]
start_price = min(open_prices) if open_prices else s.current_price
level = start_price * (1.0 - spacing)
while level >= floor:
size = self.calculator.get_size(level, s.highest_open_price)
simulated_levels.append({"price": level, "size": size})