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:
+18
-12
@@ -60,8 +60,10 @@ def test_or_computation():
|
||||
|
||||
def test_strategy_signals(day_df, or_high, or_low):
|
||||
"""Test that run_day produces trades from cached data."""
|
||||
params = Params(rr_ratio=2.5, max_trades=2, fvg_expiry=12, session_end_h=11)
|
||||
trades = run_day(day_df, params, or_minutes=15)
|
||||
cfg = load_config()
|
||||
strat_cfg = list(cfg.symbols.values())[0] # BTC params from config.yaml
|
||||
params = strat_cfg.to_params()
|
||||
trades = run_day(day_df, params, or_minutes=strat_cfg.or_minutes)
|
||||
|
||||
print(f"\nStrategy trades ({len(trades)}):")
|
||||
for t in trades:
|
||||
@@ -78,7 +80,7 @@ def test_strategy_signals(day_df, or_high, or_low):
|
||||
|
||||
def test_run_cycle_simulation():
|
||||
"""Simulate run_cycle by feeding cached candles as if live."""
|
||||
from executor import SessionState, _execute_entry, _execute_exit, _is_nyse_hours, _session
|
||||
from executor import SessionState, _execute_entry, _execute_exit, _is_nyse_hours
|
||||
|
||||
cfg = load_config()
|
||||
|
||||
@@ -97,12 +99,16 @@ def test_run_cycle_simulation():
|
||||
day_df = df[ny.dt.date == test_date].reset_index(drop=True)
|
||||
|
||||
# Simulate incremental candle feed (as daemon would do every 5 min)
|
||||
params = cfg.strategy.to_params()
|
||||
# Use first symbol's strategy config for test
|
||||
test_symbol = list(cfg.symbols.keys())[0]
|
||||
strat_cfg = cfg.symbols[test_symbol]
|
||||
params = strat_cfg.to_params()
|
||||
from state import save_session, update_equity, set_state_val
|
||||
|
||||
# Init equity
|
||||
update_equity(conn, cfg.capital)
|
||||
set_state_val(conn, "peak_equity", cfg.capital)
|
||||
# Init equity (capital=0 means auto-fetch, use 1000 for test)
|
||||
capital = cfg.capital if cfg.capital > 0 else 1000.0
|
||||
update_equity(conn, capital)
|
||||
set_state_val(conn, "peak_equity", capital)
|
||||
|
||||
ny_times = day_df["timestamp"].dt.tz_convert(NY)
|
||||
# Filter from 9:30 to 16:00 — matches real executor (fetches from 9:30)
|
||||
@@ -133,17 +139,17 @@ def test_run_cycle_simulation():
|
||||
cycle_count += 1
|
||||
|
||||
# Run strategy on partial data
|
||||
trades = run_day(partial, params, cfg.strategy.or_minutes)
|
||||
trades = run_day(partial, params, strat_cfg.or_minutes)
|
||||
|
||||
# Check OR
|
||||
tf_minutes = 5
|
||||
or_candles_needed = max(1, cfg.strategy.or_minutes // tf_minutes)
|
||||
or_candles_needed = max(1, strat_cfg.or_minutes // tf_minutes)
|
||||
if not or_computed and i >= or_candles_needed:
|
||||
rng = get_opening_range(partial, cfg.strategy.or_minutes)
|
||||
rng = get_opening_range(partial, strat_cfg.or_minutes)
|
||||
if rng:
|
||||
or_h, or_l = rng
|
||||
or_computed = True
|
||||
save_session(conn, test_date, or_h, or_l)
|
||||
save_session(conn, test_date, or_h, or_l, test_symbol)
|
||||
print(f" [{now_ny.strftime('%H:%M')}] OR computed: "
|
||||
f"high={or_h:.2f} low={or_l:.2f}")
|
||||
|
||||
@@ -192,7 +198,7 @@ def test_run_cycle_simulation():
|
||||
opened_keys.discard(key)
|
||||
|
||||
# Verify session was saved
|
||||
saved = get_session(conn, test_date)
|
||||
saved = get_session(conn, test_date, test_symbol)
|
||||
assert saved is not None, "Session should be saved"
|
||||
print(f"\nDB session: OR {saved['or_high']:.2f}/{saved['or_low']:.2f}")
|
||||
print(f"Cycles simulated: {cycle_count}")
|
||||
|
||||
Reference in New Issue
Block a user