fix: max_trades=99, leverage per symbol, crash recovery, test_live.py
- max_trades=99 в config/backtest — стратегия находит ВСЕ входы, лимит позиций контролирует executor (Phase 2) - leverage setup per symbol (set[str] вместо bool) - crash recovery: get_today_trade_keys() восстанавливает last_trade_ids - flash trade exit по new_positions[-1] (slippage-safe) - last_trade_ids.add() только в Phase 2 для исполненных трейдов - test_live.py: cfg.strategy → per-symbol strat_cfg - backtest_wf: risk 5%, max_trades=[99] - cli daemon: per-symbol params display Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+10
-10
@@ -23,8 +23,8 @@ 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,
|
||||
get_state_val, log_order, open_position, save_session,
|
||||
set_state_val, update_equity,
|
||||
get_state_val, get_today_trade_keys, log_order, open_position,
|
||||
save_session, set_state_val, update_equity,
|
||||
)
|
||||
from strategy import Trade, run_day
|
||||
|
||||
@@ -150,17 +150,15 @@ def run_cycle(conn: sqlite3.Connection, cfg: Config) -> list[str]:
|
||||
key = _trade_key(best_trade)
|
||||
|
||||
if best_trade.exit_price > 0:
|
||||
# Flash trade
|
||||
# Flash trade — entry + exit in same cycle
|
||||
_execute_entry(conn, cfg, best_trade, best_symbol,
|
||||
get_equity(conn), logs)
|
||||
sess.last_trade_ids.add(key)
|
||||
# Find the just-opened position (last open for this symbol)
|
||||
new_positions = get_open_positions(conn, best_symbol)
|
||||
for pos in new_positions:
|
||||
pos_key = f"{pos['direction']}_{pos['entry_price']:.2f}_{pos['stop_price']:.2f}"
|
||||
if pos_key == key:
|
||||
_execute_exit(conn, cfg, pos, best_symbol,
|
||||
best_trade.exit_price, best_trade.result, logs)
|
||||
break
|
||||
if new_positions:
|
||||
_execute_exit(conn, cfg, new_positions[-1], best_symbol,
|
||||
best_trade.exit_price, best_trade.result, logs)
|
||||
else:
|
||||
_execute_entry(conn, cfg, best_trade, best_symbol,
|
||||
get_equity(conn), logs)
|
||||
@@ -182,7 +180,7 @@ def _run_symbol_phase1(conn: sqlite3.Connection, cfg: Config, symbol: str,
|
||||
candidates: list[tuple[str, Trade]] = []
|
||||
sess = _get_session(symbol)
|
||||
|
||||
# Try restore OR from DB on first cycle
|
||||
# Try restore state from DB on first cycle (crash recovery)
|
||||
if not sess.or_computed:
|
||||
saved = get_session(conn, today, symbol)
|
||||
if saved:
|
||||
@@ -190,6 +188,8 @@ def _run_symbol_phase1(conn: sqlite3.Connection, cfg: Config, symbol: str,
|
||||
sess.or_low = saved["or_low"]
|
||||
sess.or_computed = True
|
||||
logs.append(f"Session restored: OR {sess.or_high:.2f}/{sess.or_low:.2f}")
|
||||
# Restore trade keys to prevent duplicates after restart
|
||||
sess.last_trade_ids = get_today_trade_keys(conn, today, symbol)
|
||||
|
||||
# Fetch 5m candles from 9:30 NY to now
|
||||
futures = _get_futures(cfg)
|
||||
|
||||
Reference in New Issue
Block a user