Fix Gate/Binance memory regression and roll stop offset from avg.
Stop fetch_tickers fallback for volume rank and keep stale cache on failed refresh. Compute roll unified stop as merge-average plus offset percent instead of break-even. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
from typing import Any, Optional
|
||||
|
||||
from fib_key_monitor_lib import fib_invalidate_by_mark
|
||||
from strategy_roll_lib import unified_stop_from_avg
|
||||
from strategy_db import init_strategy_tables
|
||||
|
||||
ROLL_LEG_STATUS_LABELS = {
|
||||
@@ -218,11 +219,31 @@ def _finalize_roll_leg_fill(
|
||||
leg_id = int(leg["id"])
|
||||
gid = int(group["id"])
|
||||
new_sl = float(leg.get("new_stop_loss") or 0)
|
||||
stop_offset_pct = leg.get("stop_offset_pct")
|
||||
tp0 = float(group.get("initial_take_profit") or 0)
|
||||
fill_px = float(leg.get("limit_price") or mark)
|
||||
add_qty = float(leg.get("amount") or 0)
|
||||
if stop_offset_pct not in (None, ""):
|
||||
try:
|
||||
offset_pct = float(stop_offset_pct)
|
||||
except (TypeError, ValueError):
|
||||
offset_pct = 0.0
|
||||
if offset_pct > 0:
|
||||
pos = cfg["get_position"](ex_sym, direction) or {}
|
||||
avg = float(pos.get("entry_price") or 0)
|
||||
if avg <= 0 and add_qty > 0:
|
||||
avg = fill_px
|
||||
if avg > 0:
|
||||
new_sl = unified_stop_from_avg(direction, avg, offset_pct)
|
||||
px_fn = cfg.get("price_to_precision")
|
||||
if callable(px_fn):
|
||||
try:
|
||||
new_sl = float(px_fn(ex_sym, new_sl) or new_sl)
|
||||
except Exception:
|
||||
pass
|
||||
conn.execute(
|
||||
"UPDATE roll_legs SET status='filled', fill_price=? WHERE id=? AND status='pending'",
|
||||
(fill_px, leg_id),
|
||||
"UPDATE roll_legs SET status='filled', fill_price=?, new_stop_loss=? WHERE id=? AND status='pending'",
|
||||
(fill_px, new_sl, leg_id),
|
||||
)
|
||||
if new_sl > 0:
|
||||
conn.execute(
|
||||
|
||||
Reference in New Issue
Block a user