fix: OR check, stale equity, exchange reconciliation в executor
- OR candles guard: or_minutes/5 вместо or_minutes (15 свечей → 3) - Re-read equity перед каждым entry (был stale после exit в том же цикле) - Проверка exchange position: если SL/TP сработал между циклами — фиксируем exit - Удалён dead code (strat_keys, unused import) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+17
-10
@@ -23,7 +23,7 @@ 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,
|
||||||
get_today_trade_count, log_order, open_position, save_session,
|
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 Params, Trade, run_day
|
||||||
@@ -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
|
# Track OR computation (or_minutes on 5m TF = or_minutes/5 candles)
|
||||||
if not _session.or_computed and len(df) >= cfg.strategy.or_minutes:
|
or_candles_needed = max(1, cfg.strategy.or_minutes // 5)
|
||||||
|
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)
|
||||||
if rng:
|
if rng:
|
||||||
@@ -139,8 +140,6 @@ def run_cycle(conn: sqlite3.Connection, cfg: Config) -> list[str]:
|
|||||||
logs.append(f"OR computed: {_session.or_high:.2f}/{_session.or_low:.2f}")
|
logs.append(f"OR computed: {_session.or_high:.2f}/{_session.or_low:.2f}")
|
||||||
notify_or(cfg.telegram, str(today), _session.or_high, _session.or_low)
|
notify_or(cfg.telegram, str(today), _session.or_high, _session.or_low)
|
||||||
|
|
||||||
# Build set of strategy trade keys
|
|
||||||
strat_keys = {_trade_key(t) for t in strategy_trades}
|
|
||||||
logs.append(f"Strategy signals: {len(strategy_trades)} trades")
|
logs.append(f"Strategy signals: {len(strategy_trades)} trades")
|
||||||
|
|
||||||
# Get DB open positions
|
# Get DB open positions
|
||||||
@@ -150,7 +149,17 @@ def run_cycle(conn: sqlite3.Connection, cfg: Config) -> list[str]:
|
|||||||
for pos in db_positions:
|
for pos in db_positions:
|
||||||
key = f"{pos['direction']}_{pos['entry_price']:.2f}_{pos['stop_price']:.2f}"
|
key = f"{pos['direction']}_{pos['entry_price']:.2f}_{pos['stop_price']:.2f}"
|
||||||
|
|
||||||
# Find matching strategy trade
|
# First: check if exchange position is gone (SL/TP fired between cycles)
|
||||||
|
if cfg.mode == "live":
|
||||||
|
ex_pos = futures.get_position(cfg.symbol)
|
||||||
|
if ex_pos is None:
|
||||||
|
# Exchange has no position — SL/TP fired
|
||||||
|
price = futures.get_price(cfg.symbol) or pos["entry_price"]
|
||||||
|
logs.append(f"Exchange position gone — SL/TP fired between cycles")
|
||||||
|
_execute_exit(conn, cfg, pos, price, "sl_tp_exchange", logs)
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Second: check if strategy replay says trade is closed
|
||||||
matched_trade = None
|
matched_trade = None
|
||||||
for t in strategy_trades:
|
for t in strategy_trades:
|
||||||
if _trade_key(t) == key and t.exit_price > 0:
|
if _trade_key(t) == key and t.exit_price > 0:
|
||||||
@@ -158,11 +167,9 @@ def run_cycle(conn: sqlite3.Connection, cfg: Config) -> list[str]:
|
|||||||
break
|
break
|
||||||
|
|
||||||
if matched_trade:
|
if matched_trade:
|
||||||
# Strategy says this trade is closed
|
|
||||||
_execute_exit(conn, cfg, pos, matched_trade.exit_price,
|
_execute_exit(conn, cfg, pos, matched_trade.exit_price,
|
||||||
matched_trade.result, logs)
|
matched_trade.result, logs)
|
||||||
elif _is_eod(now_ny):
|
elif _is_eod(now_ny):
|
||||||
# EOD: close all remaining
|
|
||||||
price = futures.get_price(cfg.symbol) or pos["entry_price"]
|
price = futures.get_price(cfg.symbol) or pos["entry_price"]
|
||||||
_execute_exit(conn, cfg, pos, price, "eod", logs)
|
_execute_exit(conn, cfg, pos, price, "eod", logs)
|
||||||
|
|
||||||
@@ -178,8 +185,8 @@ def run_cycle(conn: sqlite3.Connection, cfg: Config) -> list[str]:
|
|||||||
# Already closed in strategy — skip (historical trade)
|
# Already closed in strategy — skip (historical trade)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# New active trade → execute entry
|
# New active trade → execute entry (re-read equity for fresh value)
|
||||||
_execute_entry(conn, cfg, t, equity, logs)
|
_execute_entry(conn, cfg, t, get_equity(conn), logs)
|
||||||
_session.last_trade_ids.add(key)
|
_session.last_trade_ids.add(key)
|
||||||
|
|
||||||
return logs
|
return logs
|
||||||
|
|||||||
Reference in New Issue
Block a user