Spaces:
Paused
Paused
Update backtest_engine.py
Browse files- backtest_engine.py +8 -6
backtest_engine.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# ============================================================
|
| 2 |
-
# 🧪 backtest_engine.py (
|
| 3 |
# ============================================================
|
| 4 |
|
| 5 |
import asyncio
|
|
@@ -21,7 +21,7 @@ try:
|
|
| 21 |
from learning_hub.adaptive_hub import StrategyDNA, AdaptiveHub
|
| 22 |
from r2 import R2Service
|
| 23 |
import ccxt.async_support as ccxt
|
| 24 |
-
import xgboost as xgb
|
| 25 |
except ImportError:
|
| 26 |
pass
|
| 27 |
|
|
@@ -53,7 +53,7 @@ class HeavyDutyBacktester:
|
|
| 53 |
self.force_end_date = None
|
| 54 |
|
| 55 |
if not os.path.exists(CACHE_DIR): os.makedirs(CACHE_DIR)
|
| 56 |
-
print(f"🧪 [Backtest
|
| 57 |
|
| 58 |
def set_date_range(self, start_str, end_str):
|
| 59 |
self.force_start_date = start_str
|
|
@@ -121,7 +121,7 @@ class HeavyDutyBacktester:
|
|
| 121 |
df['rsi'] = 100 - (100 / (1 + rs))
|
| 122 |
df['ema20'] = df['close'].ewm(span=20, adjust=False).mean()
|
| 123 |
df['ema50'] = df['close'].ewm(span=50, adjust=False).mean()
|
| 124 |
-
df['ema200'] = df['close'].ewm(span=200, adjust=False).mean()
|
| 125 |
|
| 126 |
# 3. ATR
|
| 127 |
high_low = df['high'] - df['low']
|
|
@@ -218,8 +218,10 @@ class HeavyDutyBacktester:
|
|
| 218 |
numpy_htf = {}
|
| 219 |
for tf_str, tf_code in [('5m', '5T'), ('15m', '15T'), ('1h', '1h'), ('4h', '4h'), ('1d', '1D')]:
|
| 220 |
resampled = df_1m.resample(tf_code).agg(agg_dict).dropna()
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
| 223 |
resampled['timestamp'] = resampled.index.astype(np.int64) // 10**6
|
| 224 |
frames[tf_str] = resampled
|
| 225 |
|
|
|
|
| 1 |
# ============================================================
|
| 2 |
+
# 🧪 backtest_engine.py (V106.0 - GEM-Architect: Crash Fix & Optimization)
|
| 3 |
# ============================================================
|
| 4 |
|
| 5 |
import asyncio
|
|
|
|
| 21 |
from learning_hub.adaptive_hub import StrategyDNA, AdaptiveHub
|
| 22 |
from r2 import R2Service
|
| 23 |
import ccxt.async_support as ccxt
|
| 24 |
+
import xgboost as xgb
|
| 25 |
except ImportError:
|
| 26 |
pass
|
| 27 |
|
|
|
|
| 53 |
self.force_end_date = None
|
| 54 |
|
| 55 |
if not os.path.exists(CACHE_DIR): os.makedirs(CACHE_DIR)
|
| 56 |
+
print(f"🧪 [Backtest V106.0] Ultimate Speed (Hydra + Legacy V2/V3 Injection) - Crash Fixed.")
|
| 57 |
|
| 58 |
def set_date_range(self, start_str, end_str):
|
| 59 |
self.force_start_date = start_str
|
|
|
|
| 121 |
df['rsi'] = 100 - (100 / (1 + rs))
|
| 122 |
df['ema20'] = df['close'].ewm(span=20, adjust=False).mean()
|
| 123 |
df['ema50'] = df['close'].ewm(span=50, adjust=False).mean()
|
| 124 |
+
df['ema200'] = df['close'].ewm(span=200, adjust=False).mean()
|
| 125 |
|
| 126 |
# 3. ATR
|
| 127 |
high_low = df['high'] - df['low']
|
|
|
|
| 218 |
numpy_htf = {}
|
| 219 |
for tf_str, tf_code in [('5m', '5T'), ('15m', '15T'), ('1h', '1h'), ('4h', '4h'), ('1d', '1D')]:
|
| 220 |
resampled = df_1m.resample(tf_code).agg(agg_dict).dropna()
|
| 221 |
+
|
| 222 |
+
# ✅ FIX: ALWAYS calculate indicators, even for 4h/1d, to prevent KeyError 'rsi'
|
| 223 |
+
resampled = self._calculate_indicators_vectorized(resampled)
|
| 224 |
+
|
| 225 |
resampled['timestamp'] = resampled.index.astype(np.int64) // 10**6
|
| 226 |
frames[tf_str] = resampled
|
| 227 |
|