Riy777 commited on
Commit
e15b993
·
verified ·
1 Parent(s): 9f5257f

Update backtest_engine.py

Browse files
Files changed (1) hide show
  1. backtest_engine.py +174 -186
backtest_engine.py CHANGED
@@ -1,5 +1,5 @@
1
  # ============================================================
2
- # 🧪 backtest_engine.py (V159.0 - GEM-Architect: Hyper-Speed Jump Logic)
3
  # ============================================================
4
 
5
  import asyncio
@@ -13,7 +13,7 @@ import glob
13
  import gc
14
  import sys
15
  import traceback
16
- from datetime import datetime, timezone
17
  from typing import Dict, Any, List
18
 
19
  # محاولة استيراد المكتبات
@@ -60,7 +60,7 @@ class HeavyDutyBacktester:
60
  self.proc = processor
61
 
62
  # 🎛️ الكثافة (Density): عدد الخطوات في النطاق
63
- self.GRID_DENSITY = 3 # 3 is enough for quick checks, 5 for deep dive
64
 
65
  self.INITIAL_CAPITAL = 10.0
66
  self.TRADING_FEES = 0.001
@@ -68,32 +68,31 @@ class HeavyDutyBacktester:
68
 
69
  # 🎛️ CONTROL PANEL - DYNAMIC RANGES
70
  self.GRID_RANGES = {
71
- 'TITAN': np.linspace(0.10, 0.50, self.GRID_DENSITY),
72
- 'ORACLE': np.linspace(0.40, 0.80, self.GRID_DENSITY),
73
  'SNIPER': np.linspace(0.30, 0.70, self.GRID_DENSITY),
74
- 'PATTERN': np.linspace(0.10, 0.50, self.GRID_DENSITY),
75
- 'L1_SCORE': [10.0],
76
- # Guardians
77
- 'HYDRA_CRASH': np.linspace(0.60, 0.85, self.GRID_DENSITY),
78
- 'HYDRA_GIVEBACK': np.linspace(0.60, 0.85, self.GRID_DENSITY),
79
- 'LEGACY_V2': np.linspace(0.85, 0.98, self.GRID_DENSITY),
80
  }
81
 
82
  self.TARGET_COINS = [
83
- 'SOL/USDT', 'XRP/USDT', 'DOGE/USDT', 'ADA/USDT', 'AVAX/USDT', 'LINK/USDT',
84
- 'TON/USDT', 'INJ/USDT', 'APT/USDT', 'OP/USDT', 'ARB/USDT', 'SUI/USDT',
85
- 'SEI/USDT', 'MINA/USDT', 'MATIC/USDT', 'NEAR/USDT', 'RUNE/USDT', 'API3/USDT',
86
- 'FLOKI/USDT', 'BABYDOGE/USDT', 'SHIB/USDT', 'TRX/USDT', 'DOT/USDT', 'UNI/USDT',
87
- 'ONDO/USDT', 'SNX/USDT', 'HBAR/USDT', 'XLM/USDT', 'AGIX/USDT', 'IMX/USDT',
88
- 'LRC/USDT', 'KCS/USDT', 'ICP/USDT', 'SAND/USDT', 'AXS/USDT', 'APE/USDT',
89
- 'GMT/USDT', 'CHZ/USDT', 'CFX/USDT', 'LDO/USDT', 'FET/USDT', 'RPL/USDT',
90
- 'MNT/USDT', 'RAY/USDT', 'CAKE/USDT', 'SRM/USDT', 'PENDLE/USDT', 'ATOM/USDT'
91
- ]
92
- self.force_start_date = None
93
- self.force_end_date = None
 
 
 
 
94
 
95
  if not os.path.exists(CACHE_DIR): os.makedirs(CACHE_DIR)
96
- print(f"🧪 [Backtest V159.0] Hyper-Speed Jump Engine (CPU Optimized).")
97
 
98
  def set_date_range(self, start_str, end_str):
99
  self.force_start_date = start_str
@@ -132,14 +131,14 @@ class HeavyDutyBacktester:
132
  return df.values.tolist()
133
 
134
  # ----------------------------------------------------------------------
135
- # 🏎️ VECTORIZED INDICATORS
136
  # ----------------------------------------------------------------------
137
  def _calculate_indicators_vectorized(self, df, timeframe='1m'):
138
  if df.empty: return df
139
  cols = ['close', 'high', 'low', 'volume', 'open']
140
  for c in cols: df[c] = df[c].astype(np.float64)
141
 
142
- # EMAs
143
  df['ema9'] = df['close'].ewm(span=9, adjust=False).mean()
144
  df['ema20'] = df['close'].ewm(span=20, adjust=False).mean()
145
  df['ema21'] = df['close'].ewm(span=21, adjust=False).mean()
@@ -150,12 +149,20 @@ class HeavyDutyBacktester:
150
  df['RSI'] = ta.rsi(df['close'], length=14).fillna(50)
151
  df['ATR'] = ta.atr(df['high'], df['low'], df['close'], length=14).fillna(0)
152
  bb = ta.bbands(df['close'], length=20, std=2.0)
153
- df['bb_width'] = bb.iloc[:, 3].fillna(0) if bb is not None else 0.0
 
 
 
 
 
 
154
  macd = ta.macd(df['close'])
155
  if macd is not None:
156
  df['MACD'] = macd.iloc[:, 0].fillna(0)
 
157
  df['MACD_h'] = macd.iloc[:, 1].fillna(0)
158
- else: df['MACD'] = 0; df['MACD_h'] = 0
 
159
  df['ADX'] = ta.adx(df['high'], df['low'], df['close'], length=14).iloc[:, 0].fillna(0)
160
  df['CCI'] = ta.cci(df['high'], df['low'], df['close'], length=20).fillna(0)
161
  df['MFI'] = ta.mfi(df['high'], df['low'], df['close'], df['volume'], length=14).fillna(50)
@@ -164,6 +171,11 @@ class HeavyDutyBacktester:
164
  df['vwap'] = vwap.fillna(df['close']) if vwap is not None else df['close']
165
 
166
  c = df['close'].values
 
 
 
 
 
167
  df['EMA_9_dist'] = (c / df['ema9'].values) - 1
168
  df['EMA_21_dist'] = (c / df['ema21'].values) - 1
169
  df['EMA_50_dist'] = (c / df['ema50'].values) - 1
@@ -177,47 +189,9 @@ class HeavyDutyBacktester:
177
  df['rel_vol'] = df['volume'] / (df['volume'].rolling(50).mean() + 1e-9)
178
  df['log_ret'] = np.concatenate([[0], np.diff(np.log(c + 1e-9))])
179
 
180
- roll_min = df['low'].rolling(50).min(); roll_max = df['high'].rolling(50).max()
181
- df['fib_pos'] = (c - roll_min) / (roll_max - roll_min + 1e-9)
182
- df['volatility'] = df['ATR_pct']
183
-
184
- e20 = df['ema20'].values
185
- e20_s = np.roll(e20, 5); e20_s[:5] = e20[0]
186
- df['trend_slope'] = (e20 - e20_s) / (e20_s + 1e-9)
187
-
188
- fib618 = roll_max - ((roll_max - roll_min) * 0.382)
189
- df['dist_fib618'] = (c - fib618) / (c + 1e-9)
190
- df['dist_ema50'] = df['EMA_50_dist']
191
- df['dist_ema200'] = df['EMA_200_dist']
192
-
193
  if timeframe == '1m':
194
  df['return_1m'] = df['log_ret']
195
- df['rsi_14'] = df['RSI']
196
- e9 = df['ema9'].values; e9_s = np.roll(e9, 1); e9_s[0] = e9[0]
197
- df['ema_9_slope'] = (e9 - e9_s) / (e9_s + 1e-9)
198
- df['ema_21_dist'] = df['EMA_21_dist']
199
-
200
  df['atr_z'] = _z_roll_np(df['ATR'].values, 100)
201
- df['vol_zscore_50'] = _z_roll_np(df['volume'].values, 50)
202
- rng = df['high'].values - df['low'].values
203
- df['candle_range'] = _z_roll_np(rng, 500)
204
- df['close_pos_in_range'] = (c - df['low'].values) / (rng + 1e-9)
205
-
206
- dollar_vol = c * df['volume'].values
207
- amihud = np.abs(df['log_ret']) / (dollar_vol + 1e-9)
208
- df['amihud'] = _z_roll_np(amihud, 500)
209
-
210
- sign = np.sign(np.diff(c, prepend=c[0]))
211
- signed_vol = sign * df['volume'].values
212
- ofi = pd.Series(signed_vol).rolling(30).sum().fillna(0).values
213
- df['ofi'] = _z_roll_np(ofi, 500)
214
- df['vwap_dev'] = _z_roll_np(c - df['vwap'].values, 500)
215
-
216
- for lag in [1, 2, 3, 5, 10, 20]:
217
- df[f'log_ret_lag_{lag}'] = df['log_ret'].shift(lag).fillna(0)
218
- df[f'rsi_lag_{lag}'] = df['RSI'].shift(lag).fillna(50)/100.0
219
- df[f'fib_pos_lag_{lag}'] = df['fib_pos'].shift(lag).fillna(0.5)
220
- df[f'volatility_lag_{lag}'] = df['volatility'].shift(lag).fillna(0)
221
 
222
  df.fillna(0, inplace=True)
223
  return df
@@ -260,19 +234,87 @@ class HeavyDutyBacktester:
260
  if tf not in numpy_htf or 'timestamp' not in numpy_htf[tf]: return np.zeros(len(arr_ts_1m), dtype=int)
261
  return np.clip(np.searchsorted(numpy_htf[tf]['timestamp'], arr_ts_1m), 0, len(numpy_htf[tf]['timestamp']) - 1)
262
 
263
- map_5m = get_map('5m'); map_15m = get_map('15m'); map_1h = get_map('1h'); map_4h = get_map('4h')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265
  titan_model = getattr(self.proc.titan, 'model', None)
266
  oracle_dir = getattr(self.proc.oracle, 'model_direction', None)
267
  oracle_cols = getattr(self.proc.oracle, 'feature_cols', [])
268
  sniper_models = getattr(self.proc.sniper, 'models', [])
269
  sniper_cols = getattr(self.proc.sniper, 'feature_names', [])
270
- hydra_models = getattr(self.proc.guardian_hydra, 'models', {}) if self.proc.guardian_hydra else {}
271
- legacy_v2 = getattr(self.proc.guardian_legacy, 'model_v2', None)
272
-
273
- # --- BATCH PREDICTIONS ---
274
  global_titan_scores = np.full(len(arr_ts_1m), 0.5, dtype=np.float32)
275
  if titan_model:
 
276
  titan_cols = [
277
  '5m_open', '5m_high', '5m_low', '5m_close', '5m_volume', '5m_RSI', '5m_MACD', '5m_MACD_h',
278
  '5m_CCI', '5m_ADX', '5m_EMA_9_dist', '5m_EMA_21_dist', '5m_EMA_50_dist', '5m_EMA_200_dist',
@@ -284,6 +326,12 @@ class HeavyDutyBacktester:
284
  '1d_RSI', '1d_EMA_200_dist', '1d_Trend_Strong'
285
  ]
286
  try:
 
 
 
 
 
 
287
  t_vecs = []
288
  for col in titan_cols:
289
  parts = col.split('_', 1); tf = parts[0]; feat = parts[1]
@@ -300,6 +348,8 @@ class HeavyDutyBacktester:
300
  global_oracle_scores = np.full(len(arr_ts_1m), 0.5, dtype=np.float32)
301
  if oracle_dir:
302
  try:
 
 
303
  o_vecs = []
304
  for col in oracle_cols:
305
  if col.startswith('1h_'): o_vecs.append(numpy_htf['1h'].get(col[3:], np.zeros(len(arr_ts_1m)))[map_1h])
@@ -328,47 +378,13 @@ class HeavyDutyBacktester:
328
  global_sniper_scores = _revive_score_distribution(np.mean(preds, axis=0))
329
  except: pass
330
 
331
- global_v2_scores = np.zeros(len(arr_ts_1m), dtype=np.float32)
332
- if legacy_v2:
333
- try:
334
- l_log = fast_1m['log_ret']; l_rsi = fast_1m['RSI'] / 100.0; l_fib = fast_1m['fib_pos']; l_vol = fast_1m['volatility']
335
- l5_log = numpy_htf['5m']['log_ret'][map_5m]; l5_rsi = numpy_htf['5m']['RSI'][map_5m] / 100.0; l5_fib = numpy_htf['5m']['fib_pos'][map_5m]; l5_trd = numpy_htf['5m']['trend_slope'][map_5m]
336
- l15_log = numpy_htf['15m']['log_ret'][map_15m]; l15_rsi = numpy_htf['15m']['RSI'][map_15m] / 100.0; l15_fib618 = numpy_htf['15m']['dist_fib618'][map_15m]; l15_trd = numpy_htf['15m']['trend_slope'][map_15m]
337
- lags = []
338
- for lag in [1, 2, 3, 5, 10, 20]:
339
- lags.extend([fast_1m[f'log_ret_lag_{lag}'], fast_1m[f'rsi_lag_{lag}'], fast_1m[f'fib_pos_lag_{lag}'], fast_1m[f'volatility_lag_{lag}']])
340
- X_V2 = np.column_stack([l_log, l_rsi, l_fib, l_vol, l5_log, l5_rsi, l5_fib, l5_trd, l15_log, l15_rsi, l15_fib618, l15_trd, *lags])
341
- preds = legacy_v2.predict(xgb.DMatrix(X_V2))
342
- global_v2_scores = preds[:, 2] if len(preds.shape) > 1 else preds
343
- global_v2_scores = global_v2_scores.flatten()
344
- except: pass
345
-
346
- global_hydra_crash = np.zeros(len(arr_ts_1m), dtype=np.float32)
347
- global_hydra_give = np.zeros(len(arr_ts_1m), dtype=np.float32)
348
- if hydra_models:
349
- try:
350
- zeros = np.zeros(len(arr_ts_1m))
351
- h_static = np.column_stack([
352
- fast_1m['RSI'], numpy_htf['5m']['RSI'][map_5m], numpy_htf['15m']['RSI'][map_15m],
353
- fast_1m['bb_width'], fast_1m['rel_vol'], fast_1m['atr'], fast_1m['close']
354
- ])
355
- X_H = np.column_stack([
356
- h_static[:,0], h_static[:,1], h_static[:,2], h_static[:,3], h_static[:,4],
357
- zeros, fast_1m['ATR_pct'], zeros, zeros, zeros, zeros, zeros, zeros,
358
- global_oracle_scores, np.full(len(arr_ts_1m), 0.7), np.full(len(arr_ts_1m), 3.0)
359
- ])
360
-
361
- probs_c = hydra_models['crash'].predict_proba(X_H)[:, 1]
362
- global_hydra_crash = probs_c.astype(np.float32)
363
-
364
- probs_g = hydra_models['giveback'].predict_proba(X_H)[:, 1]
365
- global_hydra_give = probs_g.astype(np.float32)
366
- except: pass
367
-
368
- # Filter
369
- rsi_1h = numpy_htf['1h'].get('RSI', np.zeros(len(arr_ts_1m)))[map_1h]
370
- # Keep candles where at least minimal promise exists (reduces size)
371
- is_candidate_mask = (rsi_1h <= 70) & (global_titan_scores > 0.3) & (global_oracle_scores > 0.3)
372
  candidate_indices = np.where(is_candidate_mask)[0]
373
  end_limit = len(arr_ts_1m) - 60
374
  candidate_indices = candidate_indices[candidate_indices < end_limit]
@@ -383,12 +399,8 @@ class HeavyDutyBacktester:
383
  'real_titan': global_titan_scores[candidate_indices],
384
  'oracle_conf': global_oracle_scores[candidate_indices],
385
  'sniper_score': global_sniper_scores[candidate_indices],
386
- 'pattern_score': np.full(len(candidate_indices), 0.5),
387
- 'risk_hydra_crash': global_hydra_crash[candidate_indices],
388
- 'risk_hydra_giveback': global_hydra_give[candidate_indices],
389
- 'risk_legacy_v2': global_v2_scores[candidate_indices],
390
- 'time_hydra_crash': np.zeros(len(candidate_indices), dtype=int),
391
- 'l1_score': 50.0
392
  })
393
 
394
  dt = time.time() - t0
@@ -398,18 +410,28 @@ class HeavyDutyBacktester:
398
  gc.collect()
399
 
400
  async def generate_truth_data(self):
401
- if self.force_start_date:
402
- dt_s = datetime.strptime(self.force_start_date, "%Y-%m-%d").replace(tzinfo=timezone.utc)
403
- dt_e = datetime.strptime(self.force_end_date, "%Y-%m-%d").replace(tzinfo=timezone.utc)
404
- ms_s = int(dt_s.timestamp()*1000); ms_e = int(dt_e.timestamp()*1000)
405
- print(f"\n🚜 [Phase 1] Processing Era: {self.force_start_date} -> {self.force_end_date}")
406
- for sym in self.TARGET_COINS:
407
- c = await self._fetch_all_data_fast(sym, ms_s, ms_e)
408
- if c: await self._process_data_in_memory(sym, c, ms_s, ms_e)
 
 
 
 
 
 
 
 
 
 
409
 
410
  @staticmethod
411
  def _worker_optimize(combinations_batch, scores_files, initial_capital, fees_pct, max_slots):
412
- """🚀 HYPER-SPEED JUMP LOGIC (NO LOOPING OVER IDLE CANDLES)"""
413
  print(f" ⏳ [System] Loading {len(scores_files)} datasets...", flush=True)
414
  data = []
415
  for f in scores_files:
@@ -418,7 +440,7 @@ class HeavyDutyBacktester:
418
  if not data: return []
419
  df = pd.concat(data).sort_values('timestamp').reset_index(drop=True)
420
 
421
- # Pre-load arrays for max speed
422
  ts = df['timestamp'].values
423
  close = df['close'].values.astype(float)
424
  sym = df['symbol'].values
@@ -427,74 +449,47 @@ class HeavyDutyBacktester:
427
  oracle = df['oracle_conf'].values
428
  sniper = df['sniper_score'].values
429
  titan = df['real_titan'].values
430
- pattern = df['pattern_score'].values
431
- l1 = df['l1_score'].values
432
- hydra = df['risk_hydra_crash'].values
433
- hydra_give = df['risk_hydra_giveback'].values
434
- legacy = df['risk_legacy_v2'].values
435
 
436
  N = len(ts)
437
  print(f" 🚀 [System] Testing {len(combinations_batch)} configs on {N} candidates...", flush=True)
438
 
439
  res = []
440
  for cfg in combinations_batch:
441
- # 1. Vectorized Entry Mask (The Speed Secret)
442
- # Instead of checking every candle, we calculate ALL valid entries at once
443
- entry_mask = (l1 >= cfg['L1_SCORE']) & \
444
  (oracle >= cfg['ORACLE']) & \
445
  (sniper >= cfg['SNIPER']) & \
446
- (titan >= cfg['TITAN']) & \
447
- (pattern >= cfg.get('PATTERN', 0.10))
448
 
449
- # Get only the indices where entry is possible
450
  valid_entry_indices = np.where(entry_mask)[0]
451
 
452
- # Extract thresholds locally to avoid dictionary lookups in inner loop
453
- h_crash_thresh = cfg['HYDRA_CRASH']
454
- h_give_thresh = cfg['HYDRA_GIVEBACK']
455
- leg_thresh = cfg['LEGACY_V2']
456
-
457
  # Simulation State
458
  pos = {} # sym_id -> (entry_price, size)
459
  bal = float(initial_capital)
460
  alloc = 0.0
461
  log = []
462
 
463
- # Iterate ONLY on relevant indices (Jump!)
464
- # But we must respect time. So we iterate valid indices,
465
- # and check exits for OPEN positions at that time step?
466
- # Problem: If we jump, we miss exits between entries.
467
- # Fix: We must iterate all rows for exits, but we can skip logic if no pos.
468
- # OR: Since df is filtered candidates only, gaps exist.
469
- # We assume candidates are frequent enough or we only check exits on candidate candles.
470
- # *Refinement*: The dataframe `df` only contains ~30k candidates out of 100k candles.
471
- # Exiting only on candidate candles is an approximation, but acceptable for optimization speed.
472
-
473
  for i in range(N):
474
  s = sym_id[i]; p = float(close[i])
475
 
476
- # A. Check Exits (If holding this symbol)
477
  if s in pos:
478
  entry_p, size_val = pos[s]
479
  pnl = (p - entry_p) / entry_p
480
 
481
- # Guardian Logic (Local vars)
482
- is_guard = (hydra[i] > h_crash_thresh) or \
483
- (hydra_give[i] > h_give_thresh) or \
484
- (legacy[i] > leg_thresh)
485
-
486
- # VETO (Price Confirmation)
487
- confirmed = is_guard and (pnl < -0.0015)
488
-
489
- if confirmed or (pnl > 0.04) or (pnl < -0.02):
490
  realized = pnl - (fees_pct * 2)
491
  bal += size_val * (1.0 + realized)
492
  alloc -= size_val
493
  del pos[s]
494
  log.append({'pnl': realized})
495
- continue # Can't buy same candle we sold
496
 
497
- # B. Check Entries (Only if mask is True)
498
  if entry_mask[i] and len(pos) < max_slots:
499
  if s not in pos and bal >= 5.0:
500
  size = min(10.0, bal * 0.98)
@@ -515,7 +510,6 @@ class HeavyDutyBacktester:
515
  gross_l = abs(sum([x['pnl'] for x in losing]))
516
  profit_factor = (gross_p / gross_l) if gross_l > 0 else 99.9
517
 
518
- # Simple streaks
519
  max_win_s = 0; max_loss_s = 0; curr_w = 0; curr_l = 0
520
  for t in log:
521
  if t['pnl'] > 0: curr_w +=1; curr_l = 0; max_win_s = max(max_win_s, curr_w)
@@ -526,8 +520,7 @@ class HeavyDutyBacktester:
526
  'total_trades': tot, 'win_rate': win_rate, 'profit_factor': profit_factor,
527
  'win_count': len(winning), 'loss_count': len(losing),
528
  'avg_win': avg_win, 'avg_loss': avg_loss,
529
- 'max_win_streak': max_win_s, 'max_loss_streak': max_loss_s,
530
- 'consensus_agreement_rate': 0.0, 'high_consensus_win_rate': 0.0
531
  })
532
  return res
533
 
@@ -547,12 +540,13 @@ class HeavyDutyBacktester:
547
 
548
  mapped_config = {
549
  'w_titan': best['config']['TITAN'],
550
- 'w_struct': best['config']['PATTERN'],
551
- 'thresh': best['config']['L1_SCORE'],
552
  'oracle_thresh': best['config']['ORACLE'],
553
  'sniper_thresh': best['config']['SNIPER'],
554
- 'hydra_thresh': best['config']['HYDRA_CRASH'],
555
- 'legacy_thresh': best['config']['LEGACY_V2']
 
556
  }
557
 
558
  # Diagnosis
@@ -560,7 +554,6 @@ class HeavyDutyBacktester:
560
  if best['total_trades'] > 2000 and best['net_profit'] < 10: diag.append("⚠️ Overtrading")
561
  if best['win_rate'] > 55 and best['net_profit'] < 0: diag.append("⚠️ Fee Burn")
562
  if abs(best['avg_loss']) > best['avg_win'] and best['win_count'] > 0: diag.append("⚠️ Risk/Reward Inversion")
563
- if best['max_loss_streak'] > 10: diag.append("⚠️ Consecutive Loss Risk")
564
  if not diag: diag.append("✅ System Healthy")
565
 
566
  print("\n" + "="*60)
@@ -570,15 +563,8 @@ class HeavyDutyBacktester:
570
  print("-" * 60)
571
  print(f" 📊 Total Trades: {best['total_trades']}")
572
  print(f" 📈 Win Rate: {best['win_rate']:.1f}%")
573
- print(f" ✅ Winning Trades: {best['win_count']} (Avg: {best['avg_win']*100:.2f}%)")
574
- print(f" ❌ Losing Trades: {best['loss_count']} (Avg: {best['avg_loss']*100:.2f}%)")
575
- print(f" 🌊 Max Streaks: Win {best['max_win_streak']} | Loss {best['max_loss_streak']}")
576
  print(f" ⚖️ Profit Factor: {best['profit_factor']:.2f}")
577
  print("-" * 60)
578
- print(f" 🧠 CONSENSUS ANALYTICS:")
579
- print(f" 🤝 Model Agreement Rate: {best.get('consensus_agreement_rate', 0.0):.1f}%")
580
- print(f" 🌟 High-Consensus Win Rate: {best.get('high_consensus_win_rate', 0.0):.1f}%")
581
- print("-" * 60)
582
  print(f" 🩺 DIAGNOSIS: {' '.join(diag)}")
583
 
584
  p_str = ""
@@ -598,16 +584,18 @@ async def run_strategic_optimization_task():
598
  if proc.guardian_hydra: proc.guardian_hydra.set_silent_mode(True)
599
  hub = AdaptiveHub(r2); await hub.initialize()
600
  opt = HeavyDutyBacktester(dm, proc)
 
 
601
  scenarios = [
602
- {"regime": "DEAD", "start": "2023-06-01", "end": "2023-08-01"},
603
- {"regime": "RANGE", "start": "2024-07-01", "end": "2024-09-30"},
604
- {"regime": "BULL", "start": "2024-01-01", "end": "2024-03-30"},
605
- {"regime": "BEAR", "start": "2023-08-01", "end": "2023-09-15"},
606
- ]
607
  for s in scenarios:
608
- opt.set_date_range(s["start"], s["end"])
609
  best_cfg, best_stats = await opt.run_optimization(s["regime"])
610
  if best_cfg: hub.submit_challenger(s["regime"], best_cfg, best_stats)
 
611
  await hub._save_state_to_r2()
612
  print("✅ [System] DNA Updated.")
613
  finally:
 
1
  # ============================================================
2
+ # 🧪 backtest_engine.py (V160.0 - GEM-Architect: Gov + L1 Update)
3
  # ============================================================
4
 
5
  import asyncio
 
13
  import gc
14
  import sys
15
  import traceback
16
+ from datetime import datetime, timezone, timedelta
17
  from typing import Dict, Any, List
18
 
19
  # محاولة استيراد المكتبات
 
60
  self.proc = processor
61
 
62
  # 🎛️ الكثافة (Density): عدد الخطوات في النطاق
63
+ self.GRID_DENSITY = 3
64
 
65
  self.INITIAL_CAPITAL = 10.0
66
  self.TRADING_FEES = 0.001
 
68
 
69
  # 🎛️ CONTROL PANEL - DYNAMIC RANGES
70
  self.GRID_RANGES = {
71
+ 'TITAN': np.linspace(0.20, 0.50, self.GRID_DENSITY),
72
+ 'ORACLE': np.linspace(0.50, 0.80, self.GRID_DENSITY),
73
  'SNIPER': np.linspace(0.30, 0.70, self.GRID_DENSITY),
74
+ 'GOV_SCORE': np.linspace(50.0, 80.0, self.GRID_DENSITY), # ✅ Added Governance Threshold
 
 
 
 
 
75
  }
76
 
77
  self.TARGET_COINS = [
78
+ 'SOL/USDT', 'XRP/USDT', 'DOGE/USDT', 'ADA/USDT', 'AVAX/USDT', 'LINK/USDT',
79
+ 'TON/USDT', 'INJ/USDT', 'APT/USDT', 'OP/USDT', 'ARB/USDT', 'SUI/USDT',
80
+ 'SEI/USDT', 'MINA/USDT', 'MATIC/USDT', 'NEAR/USDT', 'RUNE/USDT', 'API3/USDT',
81
+ 'FLOKI/USDT', 'BABYDOGE/USDT', 'SHIB/USDT', 'TRX/USDT', 'DOT/USDT', 'UNI/USDT',
82
+ 'ONDO/USDT', 'SNX/USDT', 'HBAR/USDT', 'XLM/USDT', 'AGIX/USDT', 'IMX/USDT',
83
+ 'LRC/USDT', 'KCS/USDT', 'ICP/USDT', 'SAND/USDT', 'AXS/USDT', 'APE/USDT',
84
+ 'GMT/USDT', 'CHZ/USDT', 'CFX/USDT', 'LDO/USDT', 'FET/USDT', 'RPL/USDT',
85
+ 'MNT/USDT', 'RAY/USDT', 'CAKE/USDT', 'SRM/USDT', 'PENDLE/USDT', 'ATOM/USDT'
86
+ ]
87
+
88
+ # DATE SETTINGS
89
+ self.USE_FIXED_DATES = False # Set to True to use force_start_date
90
+ self.LOOKBACK_DAYS = 30 # Default lookback
91
+ self.force_start_date = "2024-01-01"
92
+ self.force_end_date = "2024-02-01"
93
 
94
  if not os.path.exists(CACHE_DIR): os.makedirs(CACHE_DIR)
95
+ print(f"🧪 [Backtest V160.0] Hyper-Speed Jump Engine (Gov + L1 Update).")
96
 
97
  def set_date_range(self, start_str, end_str):
98
  self.force_start_date = start_str
 
131
  return df.values.tolist()
132
 
133
  # ----------------------------------------------------------------------
134
+ # 🏎️ VECTORIZED INDICATORS (Enhanced for L1 & Gov)
135
  # ----------------------------------------------------------------------
136
  def _calculate_indicators_vectorized(self, df, timeframe='1m'):
137
  if df.empty: return df
138
  cols = ['close', 'high', 'low', 'volume', 'open']
139
  for c in cols: df[c] = df[c].astype(np.float64)
140
 
141
+ # Basic EMAs (Required for L1 & Gov)
142
  df['ema9'] = df['close'].ewm(span=9, adjust=False).mean()
143
  df['ema20'] = df['close'].ewm(span=20, adjust=False).mean()
144
  df['ema21'] = df['close'].ewm(span=21, adjust=False).mean()
 
149
  df['RSI'] = ta.rsi(df['close'], length=14).fillna(50)
150
  df['ATR'] = ta.atr(df['high'], df['low'], df['close'], length=14).fillna(0)
151
  bb = ta.bbands(df['close'], length=20, std=2.0)
152
+ if bb is not None:
153
+ df['lower_bb'] = bb.iloc[:, 0].fillna(0)
154
+ df['upper_bb'] = bb.iloc[:, 2].fillna(0)
155
+ df['bb_width'] = bb.iloc[:, 3].fillna(0)
156
+ else:
157
+ df['lower_bb'] = 0; df['upper_bb'] = 0; df['bb_width'] = 0
158
+
159
  macd = ta.macd(df['close'])
160
  if macd is not None:
161
  df['MACD'] = macd.iloc[:, 0].fillna(0)
162
+ df['MACD_s'] = macd.iloc[:, 2].fillna(0) # Signal line
163
  df['MACD_h'] = macd.iloc[:, 1].fillna(0)
164
+ else: df['MACD'] = 0; df['MACD_h'] = 0; df['MACD_s'] = 0
165
+
166
  df['ADX'] = ta.adx(df['high'], df['low'], df['close'], length=14).iloc[:, 0].fillna(0)
167
  df['CCI'] = ta.cci(df['high'], df['low'], df['close'], length=20).fillna(0)
168
  df['MFI'] = ta.mfi(df['high'], df['low'], df['close'], df['volume'], length=14).fillna(50)
 
171
  df['vwap'] = vwap.fillna(df['close']) if vwap is not None else df['close']
172
 
173
  c = df['close'].values
174
+ # Distances for L1 Logic
175
+ df['dist_ema50'] = (df['ema50'] - c) / df['ema50'] # Positive if price below ema50
176
+ df['dist_upper'] = (df['upper_bb'] - c) / c
177
+
178
+ # Features for models
179
  df['EMA_9_dist'] = (c / df['ema9'].values) - 1
180
  df['EMA_21_dist'] = (c / df['ema21'].values) - 1
181
  df['EMA_50_dist'] = (c / df['ema50'].values) - 1
 
189
  df['rel_vol'] = df['volume'] / (df['volume'].rolling(50).mean() + 1e-9)
190
  df['log_ret'] = np.concatenate([[0], np.diff(np.log(c + 1e-9))])
191
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  if timeframe == '1m':
193
  df['return_1m'] = df['log_ret']
 
 
 
 
 
194
  df['atr_z'] = _z_roll_np(df['ATR'].values, 100)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
 
196
  df.fillna(0, inplace=True)
197
  return df
 
234
  if tf not in numpy_htf or 'timestamp' not in numpy_htf[tf]: return np.zeros(len(arr_ts_1m), dtype=int)
235
  return np.clip(np.searchsorted(numpy_htf[tf]['timestamp'], arr_ts_1m), 0, len(numpy_htf[tf]['timestamp']) - 1)
236
 
237
+ map_1h = get_map('1h'); map_15m = get_map('15m')
238
+
239
+ # ============================================================
240
+ # ✅ 2. NEW L1 LOGIC (Vectorized Implementation of DataManager)
241
+ # ============================================================
242
+ # Logic is applied on 1H frame, mapped to 1m
243
+ h1_rsi = numpy_htf['1h']['RSI'][map_1h]
244
+ h1_close = numpy_htf['1h']['close'][map_1h]
245
+ h1_ema20 = numpy_htf['1h']['ema20'][map_1h]
246
+ h1_ema50 = numpy_htf['1h']['ema50'][map_1h]
247
+ h1_ema200 = numpy_htf['1h']['ema200'][map_1h]
248
+ h1_lower_bb = numpy_htf['1h']['lower_bb'][map_1h]
249
+ h1_upper_bb = numpy_htf['1h']['upper_bb'][map_1h]
250
+ h1_bb_width = numpy_htf['1h']['bb_width'][map_1h]
251
+
252
+ # TYPE 1: SAFE_BOTTOM
253
+ # (rsi < 45) & (close <= lower_bb * 1.05) & (dist_from_ema50 > 0.015)
254
+ dist_from_ema50 = (h1_ema50 - h1_close) / h1_ema50
255
+ mask_safe_bottom = (h1_rsi < 45) & (h1_close <= h1_lower_bb * 1.05) & (dist_from_ema50 > 0.015)
256
+
257
+ # TYPE 2: ACCUMULATION_SQUEEZE
258
+ # (45 <= rsi <= 60) & (bb_width < 0.12) & (close > ema20 * 0.995)
259
+ mask_acc_squeeze = (h1_rsi >= 45) & (h1_rsi <= 60) & (h1_bb_width < 0.12) & (h1_close > h1_ema20 * 0.995)
260
+
261
+ # TYPE 3: MOMENTUM_LAUNCH
262
+ # (60 < rsi < 80) & (close > ema50) & (close > ema200) & (dist_to_upper < 0.08)
263
+ dist_to_upper = (h1_upper_bb - h1_close) / h1_close
264
+ mask_mom_launch = (h1_rsi > 60) & (h1_rsi < 80) & (h1_close > h1_ema50) & (h1_close > h1_ema200) & (dist_to_upper < 0.08)
265
 
266
+ # Combine Masks (Any Valid L1)
267
+ valid_l1_mask = mask_safe_bottom | mask_acc_squeeze | mask_mom_launch
268
+
269
+ # ============================================================
270
+ # ✅ 4. GOVERNANCE PROXY (Vectorized Scoring)
271
+ # ============================================================
272
+ # Mimics governance_engine.py logic (Simplified for speed)
273
+ gov_points = np.zeros(len(arr_ts_1m), dtype=np.float32)
274
+
275
+ # Trend (30%)
276
+ # EMA9 > EMA21 on 15m
277
+ m15_ema9 = numpy_htf['15m']['ema9'][map_15m]
278
+ m15_ema21 = numpy_htf['15m']['ema21'][map_15m]
279
+ m15_ema50 = numpy_htf['15m']['ema50'][map_15m]
280
+ m15_close = numpy_htf['15m']['close'][map_15m]
281
+
282
+ gov_points += np.where(m15_ema9 > m15_ema21, 15.0, 0.0)
283
+ gov_points += np.where(m15_ema21 > m15_ema50, 10.0, 0.0)
284
+ gov_points += np.where(m15_close > numpy_htf['15m']['ema200'][map_15m], 5.0, 0.0)
285
+
286
+ # Momentum (30%)
287
+ m15_rsi = numpy_htf['15m']['RSI'][map_15m]
288
+ m15_macd = numpy_htf['15m']['MACD'][map_15m]
289
+ m15_macd_s = numpy_htf['15m']['MACD_s'][map_15m]
290
+
291
+ gov_points += np.where((m15_rsi > 45) & (m15_rsi < 70), 15.0, 0.0)
292
+ gov_points += np.where(m15_macd > m15_macd_s, 15.0, 0.0)
293
+
294
+ # Volatility & Volume (20%)
295
+ m15_bbw = numpy_htf['15m']['bb_width'][map_15m]
296
+ gov_points += np.where((m15_bbw > 0.02) & (m15_bbw < 0.15), 10.0, 0.0) # Not too tight, not too wide
297
+ gov_points += np.where(numpy_htf['15m']['rel_vol'][map_15m] > 1.0, 10.0, 0.0)
298
+
299
+ # Structure (20%)
300
+ # Price above VWAP
301
+ gov_points += np.where(m15_close > numpy_htf['15m']['vwap'][map_15m], 20.0, 0.0)
302
+
303
+ # Final Governance Score (0-100)
304
+ gov_scores_final = np.clip(gov_points, 0, 100)
305
+
306
+ # ============================================================
307
+ # 🤖 AI MODELS PREDICTIONS
308
+ # ============================================================
309
  titan_model = getattr(self.proc.titan, 'model', None)
310
  oracle_dir = getattr(self.proc.oracle, 'model_direction', None)
311
  oracle_cols = getattr(self.proc.oracle, 'feature_cols', [])
312
  sniper_models = getattr(self.proc.sniper, 'models', [])
313
  sniper_cols = getattr(self.proc.sniper, 'feature_names', [])
314
+
 
 
 
315
  global_titan_scores = np.full(len(arr_ts_1m), 0.5, dtype=np.float32)
316
  if titan_model:
317
+ # (Titan prediction logic kept same as original for stability)
318
  titan_cols = [
319
  '5m_open', '5m_high', '5m_low', '5m_close', '5m_volume', '5m_RSI', '5m_MACD', '5m_MACD_h',
320
  '5m_CCI', '5m_ADX', '5m_EMA_9_dist', '5m_EMA_21_dist', '5m_EMA_50_dist', '5m_EMA_200_dist',
 
326
  '1d_RSI', '1d_EMA_200_dist', '1d_Trend_Strong'
327
  ]
328
  try:
329
+ # Need map_5m and map_4h here strictly for Titan
330
+ def get_map_local(tf):
331
+ if tf not in numpy_htf: return np.zeros(len(arr_ts_1m), dtype=int)
332
+ return np.clip(np.searchsorted(numpy_htf[tf]['timestamp'], arr_ts_1m), 0, len(numpy_htf[tf]['timestamp']) - 1)
333
+ map_5m = get_map_local('5m'); map_4h = get_map_local('4h')
334
+
335
  t_vecs = []
336
  for col in titan_cols:
337
  parts = col.split('_', 1); tf = parts[0]; feat = parts[1]
 
348
  global_oracle_scores = np.full(len(arr_ts_1m), 0.5, dtype=np.float32)
349
  if oracle_dir:
350
  try:
351
+ # Need map_4h for Oracle too
352
+ map_4h = locals().get('map_4h', get_map('4h'))
353
  o_vecs = []
354
  for col in oracle_cols:
355
  if col.startswith('1h_'): o_vecs.append(numpy_htf['1h'].get(col[3:], np.zeros(len(arr_ts_1m)))[map_1h])
 
378
  global_sniper_scores = _revive_score_distribution(np.mean(preds, axis=0))
379
  except: pass
380
 
381
+ # 3. DISABLE GUARDIANS IN BACKTEST (Requested Update)
382
+ # We skip Hydra and Legacy V2 predictions to save time and because user requested.
383
+
384
+ # Filter (Refined with new L1)
385
+ # Keep candles where L1 is Valid OR Scores are very high
386
+ is_candidate_mask = valid_l1_mask | ((global_titan_scores > 0.6) & (global_oracle_scores > 0.6))
387
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
388
  candidate_indices = np.where(is_candidate_mask)[0]
389
  end_limit = len(arr_ts_1m) - 60
390
  candidate_indices = candidate_indices[candidate_indices < end_limit]
 
399
  'real_titan': global_titan_scores[candidate_indices],
400
  'oracle_conf': global_oracle_scores[candidate_indices],
401
  'sniper_score': global_sniper_scores[candidate_indices],
402
+ 'gov_score': gov_scores_final[candidate_indices], # ✅ New Gov Score
403
+ 'l1_valid': valid_l1_mask[candidate_indices].astype(int) # ✅ New L1 Flag
 
 
 
 
404
  })
405
 
406
  dt = time.time() - t0
 
410
  gc.collect()
411
 
412
  async def generate_truth_data(self):
413
+ # ✅ DATE LOGIC: Dynamic or Fixed
414
+ if self.USE_FIXED_DATES:
415
+ start_date_str = self.force_start_date
416
+ end_date_str = self.force_end_date
417
+ print(f"\n🚜 [Phase 1] Using FIXED dates: {start_date_str} -> {end_date_str}")
418
+ else:
419
+ now = datetime.now(timezone.utc)
420
+ end_date_str = now.strftime("%Y-%m-%d")
421
+ start_date_str = (now - timedelta(days=self.LOOKBACK_DAYS)).strftime("%Y-%m-%d")
422
+ print(f"\n🚜 [Phase 1] Using DYNAMIC dates (Last {self.LOOKBACK_DAYS} days): {start_date_str} -> {end_date_str}")
423
+
424
+ dt_s = datetime.strptime(start_date_str, "%Y-%m-%d").replace(tzinfo=timezone.utc)
425
+ dt_e = datetime.strptime(end_date_str, "%Y-%m-%d").replace(tzinfo=timezone.utc)
426
+ ms_s = int(dt_s.timestamp()*1000); ms_e = int(dt_e.timestamp()*1000)
427
+
428
+ for sym in self.TARGET_COINS:
429
+ c = await self._fetch_all_data_fast(sym, ms_s, ms_e)
430
+ if c: await self._process_data_in_memory(sym, c, ms_s, ms_e)
431
 
432
  @staticmethod
433
  def _worker_optimize(combinations_batch, scores_files, initial_capital, fees_pct, max_slots):
434
+ """🚀 HYPER-SPEED JUMP LOGIC (NO GUARDIANS, ADDED GOV)"""
435
  print(f" ⏳ [System] Loading {len(scores_files)} datasets...", flush=True)
436
  data = []
437
  for f in scores_files:
 
440
  if not data: return []
441
  df = pd.concat(data).sort_values('timestamp').reset_index(drop=True)
442
 
443
+ # Pre-load arrays
444
  ts = df['timestamp'].values
445
  close = df['close'].values.astype(float)
446
  sym = df['symbol'].values
 
449
  oracle = df['oracle_conf'].values
450
  sniper = df['sniper_score'].values
451
  titan = df['real_titan'].values
452
+ gov_s = df['gov_score'].values # ✅ Loaded Gov Score
453
+ l1_v = df['l1_valid'].values # ✅ Loaded L1 Valid Flag
 
 
 
454
 
455
  N = len(ts)
456
  print(f" 🚀 [System] Testing {len(combinations_batch)} configs on {N} candidates...", flush=True)
457
 
458
  res = []
459
  for cfg in combinations_batch:
460
+ # Updated Entry Mask: Requires Valid L1 & Gov Score
461
+ entry_mask = (l1_v == 1) & \
462
+ (gov_s >= cfg['GOV_SCORE']) & \
463
  (oracle >= cfg['ORACLE']) & \
464
  (sniper >= cfg['SNIPER']) & \
465
+ (titan >= cfg['TITAN'])
 
466
 
 
467
  valid_entry_indices = np.where(entry_mask)[0]
468
 
 
 
 
 
 
469
  # Simulation State
470
  pos = {} # sym_id -> (entry_price, size)
471
  bal = float(initial_capital)
472
  alloc = 0.0
473
  log = []
474
 
 
 
 
 
 
 
 
 
 
 
475
  for i in range(N):
476
  s = sym_id[i]; p = float(close[i])
477
 
478
+ # A. Check Exits (Standard TP/SL Logic only)
479
  if s in pos:
480
  entry_p, size_val = pos[s]
481
  pnl = (p - entry_p) / entry_p
482
 
483
+ # Exit Rules (Standard Backtest)
484
+ if (pnl > 0.04) or (pnl < -0.02):
 
 
 
 
 
 
 
485
  realized = pnl - (fees_pct * 2)
486
  bal += size_val * (1.0 + realized)
487
  alloc -= size_val
488
  del pos[s]
489
  log.append({'pnl': realized})
490
+ continue
491
 
492
+ # B. Check Entries
493
  if entry_mask[i] and len(pos) < max_slots:
494
  if s not in pos and bal >= 5.0:
495
  size = min(10.0, bal * 0.98)
 
510
  gross_l = abs(sum([x['pnl'] for x in losing]))
511
  profit_factor = (gross_p / gross_l) if gross_l > 0 else 99.9
512
 
 
513
  max_win_s = 0; max_loss_s = 0; curr_w = 0; curr_l = 0
514
  for t in log:
515
  if t['pnl'] > 0: curr_w +=1; curr_l = 0; max_win_s = max(max_win_s, curr_w)
 
520
  'total_trades': tot, 'win_rate': win_rate, 'profit_factor': profit_factor,
521
  'win_count': len(winning), 'loss_count': len(losing),
522
  'avg_win': avg_win, 'avg_loss': avg_loss,
523
+ 'max_win_streak': max_win_s, 'max_loss_streak': max_loss_s
 
524
  })
525
  return res
526
 
 
540
 
541
  mapped_config = {
542
  'w_titan': best['config']['TITAN'],
543
+ 'w_struct': 0.3, # Fixed as removed from grid
544
+ 'thresh': 50.0, # Fixed L1 Logic
545
  'oracle_thresh': best['config']['ORACLE'],
546
  'sniper_thresh': best['config']['SNIPER'],
547
+ 'gov_thresh': best['config']['GOV_SCORE'],
548
+ 'hydra_thresh': 0.85, # Default Safe
549
+ 'legacy_thresh': 0.95 # Default Safe
550
  }
551
 
552
  # Diagnosis
 
554
  if best['total_trades'] > 2000 and best['net_profit'] < 10: diag.append("⚠️ Overtrading")
555
  if best['win_rate'] > 55 and best['net_profit'] < 0: diag.append("⚠️ Fee Burn")
556
  if abs(best['avg_loss']) > best['avg_win'] and best['win_count'] > 0: diag.append("⚠️ Risk/Reward Inversion")
 
557
  if not diag: diag.append("✅ System Healthy")
558
 
559
  print("\n" + "="*60)
 
563
  print("-" * 60)
564
  print(f" 📊 Total Trades: {best['total_trades']}")
565
  print(f" 📈 Win Rate: {best['win_rate']:.1f}%")
 
 
 
566
  print(f" ⚖️ Profit Factor: {best['profit_factor']:.2f}")
567
  print("-" * 60)
 
 
 
 
568
  print(f" 🩺 DIAGNOSIS: {' '.join(diag)}")
569
 
570
  p_str = ""
 
584
  if proc.guardian_hydra: proc.guardian_hydra.set_silent_mode(True)
585
  hub = AdaptiveHub(r2); await hub.initialize()
586
  opt = HeavyDutyBacktester(dm, proc)
587
+
588
+ # Scenarios now just set the regime target for saving, dates handled internally
589
  scenarios = [
590
+ {"regime": "RANGE"},
591
+ # Add more regimes if needed to run consecutively
592
+ ]
593
+
 
594
  for s in scenarios:
595
+ # Dates are now handled by LOOKBACK_DAYS in constructor by default
596
  best_cfg, best_stats = await opt.run_optimization(s["regime"])
597
  if best_cfg: hub.submit_challenger(s["regime"], best_cfg, best_stats)
598
+
599
  await hub._save_state_to_r2()
600
  print("✅ [System] DNA Updated.")
601
  finally: