Spaces:
Paused
Paused
Update backtest_engine.py
Browse files- backtest_engine.py +146 -142
backtest_engine.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# ============================================================
|
| 2 |
-
# 🧪 backtest_engine.py (
|
| 3 |
# ============================================================
|
| 4 |
|
| 5 |
import asyncio
|
|
@@ -9,7 +9,7 @@ import time
|
|
| 9 |
import logging
|
| 10 |
import itertools
|
| 11 |
import os
|
| 12 |
-
import gc
|
| 13 |
import concurrent.futures
|
| 14 |
from typing import Dict, Any, List
|
| 15 |
|
|
@@ -31,7 +31,6 @@ class HeavyDutyBacktester:
|
|
| 31 |
self.GRID_DENSITY = 10
|
| 32 |
self.BACKTEST_DAYS = 7
|
| 33 |
|
| 34 |
-
# 💰 إعدادات التوأم الرقمي
|
| 35 |
self.INITIAL_CAPITAL = 10.0
|
| 36 |
self.TRADING_FEES = 0.001
|
| 37 |
self.MAX_SLOTS = 4
|
|
@@ -44,7 +43,7 @@ class HeavyDutyBacktester:
|
|
| 44 |
]
|
| 45 |
|
| 46 |
if not os.path.exists(CACHE_DIR): os.makedirs(CACHE_DIR)
|
| 47 |
-
print(f"🧪 [Backtest
|
| 48 |
|
| 49 |
# ==============================================================
|
| 50 |
# 🛠️ Helpers
|
|
@@ -54,153 +53,158 @@ class HeavyDutyBacktester:
|
|
| 54 |
return df[['timestamp', 'open', 'high', 'low', 'close', 'volume']].values.tolist()
|
| 55 |
|
| 56 |
# ==============================================================
|
| 57 |
-
#
|
| 58 |
# ==============================================================
|
| 59 |
-
async def
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# تنظيف الذاكرة قبل البدء بأي عملة جديدة
|
| 69 |
-
gc.collect()
|
| 70 |
-
|
| 71 |
-
if os.path.exists(scores_file):
|
| 72 |
-
print(f" 📂 {sym} scores ready. Skipping.")
|
| 73 |
-
continue
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
try:
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
all_candles_1m = [c for c in all_candles_1m if c[0] <= end_time_ms]
|
| 99 |
-
if not all_candles_1m:
|
| 100 |
-
print(" No Data.")
|
| 101 |
-
continue
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
real_titan = proc_res.get('titan_score', 0.5)
|
| 176 |
-
except: pass
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
del all_candles_1m
|
| 200 |
-
del df_1m
|
| 201 |
-
del frames
|
| 202 |
-
# استدعاء جامع القمامة يدوياً
|
| 203 |
-
gc.collect()
|
| 204 |
|
| 205 |
# ==============================================================
|
| 206 |
# PHASE 2: Portfolio Digital Twin Engine
|
|
@@ -208,8 +212,9 @@ class HeavyDutyBacktester:
|
|
| 208 |
@staticmethod
|
| 209 |
def _worker_optimize(combinations_batch, scores_files, initial_capital, fees_pct, max_slots):
|
| 210 |
results = []
|
| 211 |
-
|
| 212 |
all_data = []
|
|
|
|
|
|
|
| 213 |
for fp in scores_files:
|
| 214 |
try:
|
| 215 |
df = pd.read_pickle(fp)
|
|
@@ -288,11 +293,10 @@ class HeavyDutyBacktester:
|
|
| 288 |
|
| 289 |
if wallet["balance"] < 1.0 and len(wallet["positions"]) == 0: break
|
| 290 |
|
| 291 |
-
#
|
| 292 |
trades = wallet["trades_history"]
|
| 293 |
if trades:
|
| 294 |
net_profit = wallet["balance"] - initial_capital
|
| 295 |
-
|
| 296 |
pnls = [t['pnl'] for t in trades]
|
| 297 |
wins = [p for p in pnls if p > 0]
|
| 298 |
losses = [p for p in pnls if p <= 0]
|
|
@@ -300,7 +304,7 @@ class HeavyDutyBacktester:
|
|
| 300 |
win_count = len(wins)
|
| 301 |
loss_count = len(losses)
|
| 302 |
total_trades = len(trades)
|
| 303 |
-
win_rate = (win_count / total_trades) * 100
|
| 304 |
|
| 305 |
max_single_win = max(pnls) if pnls else 0.0
|
| 306 |
max_single_loss = min(pnls) if pnls else 0.0
|
|
|
|
| 1 |
# ============================================================
|
| 2 |
+
# 🧪 backtest_engine.py (V72.0 - GEM-Architect: Isolation Mode)
|
| 3 |
# ============================================================
|
| 4 |
|
| 5 |
import asyncio
|
|
|
|
| 9 |
import logging
|
| 10 |
import itertools
|
| 11 |
import os
|
| 12 |
+
import gc
|
| 13 |
import concurrent.futures
|
| 14 |
from typing import Dict, Any, List
|
| 15 |
|
|
|
|
| 31 |
self.GRID_DENSITY = 10
|
| 32 |
self.BACKTEST_DAYS = 7
|
| 33 |
|
|
|
|
| 34 |
self.INITIAL_CAPITAL = 10.0
|
| 35 |
self.TRADING_FEES = 0.001
|
| 36 |
self.MAX_SLOTS = 4
|
|
|
|
| 43 |
]
|
| 44 |
|
| 45 |
if not os.path.exists(CACHE_DIR): os.makedirs(CACHE_DIR)
|
| 46 |
+
print(f"🧪 [Backtest V72.0] Isolation Mode (Zero-Retention).")
|
| 47 |
|
| 48 |
# ==============================================================
|
| 49 |
# 🛠️ Helpers
|
|
|
|
| 53 |
return df[['timestamp', 'open', 'high', 'low', 'close', 'volume']].values.tolist()
|
| 54 |
|
| 55 |
# ==============================================================
|
| 56 |
+
# 🧱 Core Logic: Single Coin Processor (Isolated Scope)
|
| 57 |
# ==============================================================
|
| 58 |
+
async def _process_single_coin_task(self, sym, start_time_ms, end_time_ms):
|
| 59 |
+
"""
|
| 60 |
+
دالة معزولة لمعالجة عملة واحدة.
|
| 61 |
+
عند انتهاء هذه الدالة، يتم تدمير كل المتغيرات داخلها تلقائياً.
|
| 62 |
+
"""
|
| 63 |
+
safe_sym = sym.replace('/', '_')
|
| 64 |
+
scores_file = f"{CACHE_DIR}/{safe_sym}_fullstack_scores.pkl"
|
| 65 |
|
| 66 |
+
if os.path.exists(scores_file):
|
| 67 |
+
print(f" 📂 {sym} scores ready. Skipping.")
|
| 68 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
print(f" ⚙️ Simulating {sym}...", end="", flush=True)
|
| 71 |
+
|
| 72 |
+
# 1. جلب البيانات
|
| 73 |
+
all_candles_1m = []
|
| 74 |
+
current_since = start_time_ms
|
| 75 |
+
|
| 76 |
+
while current_since < end_time_ms:
|
| 77 |
try:
|
| 78 |
+
batch = await self.dm.exchange.fetch_ohlcv(sym, '1m', since=current_since, limit=1000)
|
| 79 |
+
if not batch: break
|
| 80 |
+
last_ts = batch[-1][0]
|
| 81 |
+
if last_ts <= current_since: break
|
| 82 |
+
all_candles_1m.extend(batch)
|
| 83 |
+
current_since = last_ts + 1
|
| 84 |
+
await asyncio.sleep(0.01)
|
| 85 |
+
if current_since >= end_time_ms: break
|
| 86 |
+
except: await asyncio.sleep(0.5)
|
| 87 |
+
|
| 88 |
+
all_candles_1m = [c for c in all_candles_1m if c[0] <= end_time_ms]
|
| 89 |
+
if not all_candles_1m:
|
| 90 |
+
print(" No Data.")
|
| 91 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
# 2. بناء الـ DataFrame
|
| 94 |
+
df_1m = pd.DataFrame(all_candles_1m, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
|
| 95 |
+
|
| 96 |
+
# 🔥 تقليل استهلاك الذاكرة: تحويل float64 إلى float32
|
| 97 |
+
cols = ['open', 'high', 'low', 'close', 'volume']
|
| 98 |
+
df_1m[cols] = df_1m[cols].astype('float32')
|
| 99 |
+
|
| 100 |
+
df_1m['datetime'] = pd.to_datetime(df_1m['timestamp'], unit='ms')
|
| 101 |
+
df_1m.set_index('datetime', inplace=True)
|
| 102 |
+
df_1m = df_1m.sort_index()
|
| 103 |
+
|
| 104 |
+
# 3. Vectorization (Resampling)
|
| 105 |
+
agg_dict = {'open': 'first', 'high': 'max', 'low': 'min', 'close': 'last', 'volume': 'sum'}
|
| 106 |
+
frames = {}
|
| 107 |
+
|
| 108 |
+
# 1m Direct
|
| 109 |
+
df_1m_ready = df_1m.copy()
|
| 110 |
+
df_1m_ready['timestamp'] = df_1m_ready.index.astype(np.int64) // 10**6
|
| 111 |
+
frames['1m'] = df_1m_ready
|
| 112 |
|
| 113 |
+
# Resampling Loop
|
| 114 |
+
for tf_str, tf_code in [('5m', '5T'), ('15m', '15T'), ('1h', '1h'), ('4h', '4h'), ('1d', '1D')]:
|
| 115 |
+
resampled = df_1m.resample(tf_code).agg(agg_dict).dropna()
|
| 116 |
+
# Ensure float32 persists
|
| 117 |
+
resampled[cols] = resampled[cols].astype('float32')
|
| 118 |
+
resampled['timestamp'] = resampled.index.astype(np.int64) // 10**6
|
| 119 |
+
frames[tf_str] = resampled
|
| 120 |
|
| 121 |
+
ai_results = []
|
| 122 |
+
valid_indices = frames['5m'].index[500:]
|
| 123 |
+
|
| 124 |
+
# 4. Scanning Loop
|
| 125 |
+
for t_idx in valid_indices:
|
| 126 |
+
current_timestamp = int(t_idx.timestamp() * 1000)
|
| 127 |
+
|
| 128 |
+
# Slicing
|
| 129 |
+
ohlcv_data = {}
|
| 130 |
+
try:
|
| 131 |
+
ohlcv_data['1m'] = self.df_to_list(frames['1m'].loc[:t_idx].tail(500))
|
| 132 |
+
ohlcv_data['5m'] = self.df_to_list(frames['5m'].loc[:t_idx].tail(200))
|
| 133 |
+
ohlcv_data['15m'] = self.df_to_list(frames['15m'].loc[:t_idx].tail(200))
|
| 134 |
+
ohlcv_data['1h'] = self.df_to_list(frames['1h'].loc[:t_idx].tail(200))
|
| 135 |
+
ohlcv_data['4h'] = self.df_to_list(frames['4h'].loc[:t_idx].tail(100))
|
| 136 |
+
ohlcv_data['1d'] = self.df_to_list(frames['1d'].loc[:t_idx].tail(50))
|
| 137 |
+
except: continue
|
| 138 |
|
| 139 |
+
if len(ohlcv_data['1h']) < 60: continue
|
| 140 |
+
|
| 141 |
+
current_price = frames['5m'].loc[t_idx]['close']
|
| 142 |
|
| 143 |
+
# Logic Check
|
| 144 |
+
logic_packet = {
|
| 145 |
+
'symbol': sym,
|
| 146 |
+
'ohlcv_1h': ohlcv_data['1h'][-60:],
|
| 147 |
+
'ohlcv_15m': ohlcv_data['15m'][-60:],
|
| 148 |
+
'change_24h': 0.0
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
try:
|
| 152 |
+
if len(ohlcv_data['1h']) >= 24:
|
| 153 |
+
p_now = ohlcv_data['1h'][-1][4]
|
| 154 |
+
p_old = ohlcv_data['1h'][-24][4]
|
| 155 |
+
logic_packet['change_24h'] = ((p_now - p_old) / p_old) * 100
|
| 156 |
+
except: pass
|
| 157 |
|
| 158 |
+
logic_result = self.dm._apply_logic_tree(logic_packet)
|
| 159 |
+
signal_type = logic_result.get('type', 'NONE')
|
| 160 |
+
l1_score = logic_result.get('score', 0.0)
|
| 161 |
+
|
| 162 |
+
real_titan = 0.5
|
| 163 |
+
if signal_type in ['BREAKOUT', 'REVERSAL']:
|
| 164 |
+
raw_data_for_proc = {
|
| 165 |
+
'symbol': sym,
|
| 166 |
+
'ohlcv': ohlcv_data,
|
| 167 |
+
'current_price': current_price
|
| 168 |
+
}
|
| 169 |
+
try:
|
| 170 |
+
proc_res = await self.proc.process_compound_signal(raw_data_for_proc)
|
| 171 |
+
if proc_res:
|
| 172 |
+
real_titan = proc_res.get('titan_score', 0.5)
|
| 173 |
+
except: pass
|
|
|
|
|
|
|
| 174 |
|
| 175 |
+
ai_results.append({
|
| 176 |
+
'timestamp': current_timestamp,
|
| 177 |
+
'symbol': sym,
|
| 178 |
+
'close': current_price,
|
| 179 |
+
'real_titan': real_titan,
|
| 180 |
+
'signal_type': signal_type,
|
| 181 |
+
'l1_score': l1_score
|
| 182 |
+
})
|
| 183 |
+
|
| 184 |
+
# Save & Clear
|
| 185 |
+
if ai_results:
|
| 186 |
+
pd.DataFrame(ai_results).to_pickle(scores_file)
|
| 187 |
+
print(f" ✅ Saved ({len(ai_results)}).")
|
| 188 |
+
else:
|
| 189 |
+
print(" ⚠️ No signals.")
|
| 190 |
+
|
| 191 |
+
return True
|
| 192 |
|
| 193 |
+
# ==============================================================
|
| 194 |
+
# PHASE 1: Main Loop
|
| 195 |
+
# ==============================================================
|
| 196 |
+
async def generate_truth_data(self):
|
| 197 |
+
print(f"\n🚜 [Phase 1] Processing Logic Tree + Titan ({self.BACKTEST_DAYS} Days)...")
|
| 198 |
+
end_time_ms = int(time.time() * 1000)
|
| 199 |
+
start_time_ms = end_time_ms - (self.BACKTEST_DAYS * 24 * 60 * 60 * 1000)
|
| 200 |
+
|
| 201 |
+
for sym in self.TARGET_COINS:
|
| 202 |
+
# 🔥 استدعاء الدالة المعزولة
|
| 203 |
+
await self._process_single_coin_task(sym, start_time_ms, end_time_ms)
|
| 204 |
|
| 205 |
+
# 🔥 تنظيف الذاكرة الإجباري بعد كل عملة
|
| 206 |
+
gc.collect()
|
| 207 |
+
await asyncio.sleep(0.1) # استراحة قصيرة للمعالج
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
# ==============================================================
|
| 210 |
# PHASE 2: Portfolio Digital Twin Engine
|
|
|
|
| 212 |
@staticmethod
|
| 213 |
def _worker_optimize(combinations_batch, scores_files, initial_capital, fees_pct, max_slots):
|
| 214 |
results = []
|
|
|
|
| 215 |
all_data = []
|
| 216 |
+
|
| 217 |
+
# Load data safely
|
| 218 |
for fp in scores_files:
|
| 219 |
try:
|
| 220 |
df = pd.read_pickle(fp)
|
|
|
|
| 293 |
|
| 294 |
if wallet["balance"] < 1.0 and len(wallet["positions"]) == 0: break
|
| 295 |
|
| 296 |
+
# Analytics
|
| 297 |
trades = wallet["trades_history"]
|
| 298 |
if trades:
|
| 299 |
net_profit = wallet["balance"] - initial_capital
|
|
|
|
| 300 |
pnls = [t['pnl'] for t in trades]
|
| 301 |
wins = [p for p in pnls if p > 0]
|
| 302 |
losses = [p for p in pnls if p <= 0]
|
|
|
|
| 304 |
win_count = len(wins)
|
| 305 |
loss_count = len(losses)
|
| 306 |
total_trades = len(trades)
|
| 307 |
+
win_rate = (win_count / total_trades) * 100 if total_trades > 0 else 0
|
| 308 |
|
| 309 |
max_single_win = max(pnls) if pnls else 0.0
|
| 310 |
max_single_loss = min(pnls) if pnls else 0.0
|