fix: skip close на бирже после SL/TP, dynamic TF в OR check

- Не вызывать close_position/cancel_orders если SL/TP уже сработал
- or_minutes делить на фактический timeframe, не хардкод 5
- Cleanup: unused Params import, redundant local import

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-13 17:03:39 +10:00
co-authored by Claude Opus 4.6
parent c0678fba2f
commit 70a855c588
+16 -11
View File
@@ -23,10 +23,10 @@ from exchange import FuturesExecutor
from notify import notify_entry, notify_exit, notify_or from notify import notify_entry, notify_exit, notify_or
from state import ( from state import (
close_position, get_equity, get_open_positions, get_session, close_position, get_equity, get_open_positions, get_session,
log_order, open_position, save_session, get_state_val, log_order, open_position, save_session,
set_state_val, update_equity, set_state_val, update_equity,
) )
from strategy import Params, Trade, run_day from strategy import Trade, run_day
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
NY = ZoneInfo("America/New_York") NY = ZoneInfo("America/New_York")
@@ -128,8 +128,9 @@ def run_cycle(conn: sqlite3.Connection, cfg: Config) -> list[str]:
params = cfg.strategy.to_params() params = cfg.strategy.to_params()
strategy_trades = run_day(df, params, cfg.strategy.or_minutes) strategy_trades = run_day(df, params, cfg.strategy.or_minutes)
# Track OR computation (or_minutes on 5m TF = or_minutes/5 candles) # Track OR computation (convert or_minutes to candle count)
or_candles_needed = max(1, cfg.strategy.or_minutes // 5) tf_minutes = {"1m": 1, "5m": 5, "15m": 15}.get(cfg.timeframe, 5)
or_candles_needed = max(1, cfg.strategy.or_minutes // tf_minutes)
if not _session.or_computed and len(df) >= or_candles_needed: if not _session.or_computed and len(df) >= or_candles_needed:
from strategy import get_opening_range from strategy import get_opening_range
rng = get_opening_range(df, cfg.strategy.or_minutes) rng = get_opening_range(df, cfg.strategy.or_minutes)
@@ -265,13 +266,18 @@ def _execute_exit(conn: sqlite3.Connection, cfg: Config, pos: dict,
symbol = cfg.symbol symbol = cfg.symbol
side = "buy" if pos["direction"] == "long" else "sell" side = "buy" if pos["direction"] == "long" else "sell"
# Cancel SL/TP orders # If SL/TP already fired on exchange, skip close + cancel
futures.cancel_orders(symbol, already_closed = reason == "sl_tp_exchange"
pos.get("sl_order_id", ""),
pos.get("tp_order_id", ""))
# Close on exchange if not already_closed:
order = futures.close_position(symbol, side, pos["pos_size"]) futures.cancel_orders(symbol,
pos.get("sl_order_id", ""),
pos.get("tp_order_id", ""))
# Close on exchange (skip if already closed by exchange SL/TP)
order = None
if not already_closed:
order = futures.close_position(symbol, side, pos["pos_size"])
# PnL # PnL
if pos["direction"] == "long": if pos["direction"] == "long":
@@ -288,7 +294,6 @@ def _execute_exit(conn: sqlite3.Connection, cfg: Config, pos: dict,
update_equity(conn, new_equity) update_equity(conn, new_equity)
# Update stats # Update stats
from state import get_state_val, set_state_val
trades_count = get_state_val(conn, "trades_count") + 1 trades_count = get_state_val(conn, "trades_count") + 1
set_state_val(conn, "trades_count", trades_count) set_state_val(conn, "trades_count", trades_count)
if pnl_net > 0: if pnl_net > 0: