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:
+16
-11
@@ -23,10 +23,10 @@ from exchange import FuturesExecutor
|
||||
from notify import notify_entry, notify_exit, notify_or
|
||||
from state import (
|
||||
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,
|
||||
)
|
||||
from strategy import Params, Trade, run_day
|
||||
from strategy import Trade, run_day
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
NY = ZoneInfo("America/New_York")
|
||||
@@ -128,8 +128,9 @@ def run_cycle(conn: sqlite3.Connection, cfg: Config) -> list[str]:
|
||||
params = cfg.strategy.to_params()
|
||||
strategy_trades = run_day(df, params, cfg.strategy.or_minutes)
|
||||
|
||||
# Track OR computation (or_minutes on 5m TF = or_minutes/5 candles)
|
||||
or_candles_needed = max(1, cfg.strategy.or_minutes // 5)
|
||||
# Track OR computation (convert or_minutes to candle count)
|
||||
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:
|
||||
from strategy import get_opening_range
|
||||
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
|
||||
side = "buy" if pos["direction"] == "long" else "sell"
|
||||
|
||||
# Cancel SL/TP orders
|
||||
futures.cancel_orders(symbol,
|
||||
pos.get("sl_order_id", ""),
|
||||
pos.get("tp_order_id", ""))
|
||||
# If SL/TP already fired on exchange, skip close + cancel
|
||||
already_closed = reason == "sl_tp_exchange"
|
||||
|
||||
# Close on exchange
|
||||
order = futures.close_position(symbol, side, pos["pos_size"])
|
||||
if not already_closed:
|
||||
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
|
||||
if pos["direction"] == "long":
|
||||
@@ -288,7 +294,6 @@ def _execute_exit(conn: sqlite3.Connection, cfg: Config, pos: dict,
|
||||
update_equity(conn, new_equity)
|
||||
|
||||
# Update stats
|
||||
from state import get_state_val, set_state_val
|
||||
trades_count = get_state_val(conn, "trades_count") + 1
|
||||
set_state_val(conn, "trades_count", trades_count)
|
||||
if pnl_net > 0:
|
||||
|
||||
Reference in New Issue
Block a user