Spaces:
Paused
Paused
Update ml_engine/oracle_engine.py
Browse files- ml_engine/oracle_engine.py +22 -23
ml_engine/oracle_engine.py
CHANGED
|
@@ -41,7 +41,7 @@ class OracleEngine:
|
|
| 41 |
self.feature_names: List[str] = []
|
| 42 |
self.initialized = False
|
| 43 |
# (تحديث الإصدار)
|
| 44 |
-
print("🧠 [OracleEngine V2.
|
| 45 |
|
| 46 |
async def initialize(self):
|
| 47 |
"""
|
|
@@ -50,7 +50,7 @@ class OracleEngine:
|
|
| 50 |
if self.initialized:
|
| 51 |
return True
|
| 52 |
|
| 53 |
-
print(f"🧠 [OracleEngine V2.
|
| 54 |
try:
|
| 55 |
# 1. تحميل نماذج "لجنة القرار" (Strategy Ensemble)
|
| 56 |
for i in range(N_STRATEGY_MODELS):
|
|
@@ -79,12 +79,12 @@ class OracleEngine:
|
|
| 79 |
self.feature_names = self.strategy_boosters[0].feature_name()
|
| 80 |
self.initialized = True
|
| 81 |
|
| 82 |
-
print(f"✅ [OracleEngine V2.
|
| 83 |
print(f" -> سيعمل على الأطر: {TIMEBYTES_TO_PROCESS}")
|
| 84 |
return True
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
-
print(f"❌ [OracleEngine V2.
|
| 88 |
self.initialized = False
|
| 89 |
return False
|
| 90 |
|
|
@@ -170,12 +170,10 @@ class OracleEngine:
|
|
| 170 |
try:
|
| 171 |
feature_vector = latest_features[self.feature_names]
|
| 172 |
|
| 173 |
-
# --- [
|
| 174 |
-
# (إصلاح حالات مثل USDC التي ليس لها تقلب)
|
| 175 |
feature_vector = feature_vector.fillna(0)
|
| 176 |
# --- [ نهاية الإصلاح ] ---
|
| 177 |
|
| 178 |
-
# (الفحص الأخير، رغم أن fillna(0) يجب أن تحل المشكلة)
|
| 179 |
if feature_vector.isnull().values.any():
|
| 180 |
print("⚠️ [Oracle Warning] Feature vector still contains NaN after fill(0).")
|
| 181 |
return None
|
|
@@ -214,16 +212,12 @@ class OracleEngine:
|
|
| 214 |
]
|
| 215 |
ensemble_probs = np.mean(all_probs, axis=0)[0]
|
| 216 |
|
| 217 |
-
# --- [
|
| 218 |
-
# (بدلاً من أخذ np.argmax، سنتجاهل الفئة 0)
|
| 219 |
-
|
| 220 |
-
# نسخ الاحتمالات وتعيين احتمالية 'WAIT' (index 0) إلى 0
|
| 221 |
actionable_probs = ensemble_probs.copy()
|
| 222 |
actionable_probs[0] = 0.0 # (تجاهل WAIT)
|
| 223 |
|
| 224 |
-
# إيجاد أفضل إشارة "قابلة للتنفيذ"
|
| 225 |
predicted_strategy_idx = np.argmax(actionable_probs)
|
| 226 |
-
confidence = actionable_probs[predicted_strategy_idx]
|
| 227 |
strategy_name = STRATEGY_MAP.get(predicted_strategy_idx, 'WAIT')
|
| 228 |
# --- [ نهاية الإصلاح ] ---
|
| 229 |
|
|
@@ -244,7 +238,18 @@ class OracleEngine:
|
|
| 244 |
confidence = best_decision['confidence']
|
| 245 |
best_tf = best_decision['timeframe']
|
| 246 |
|
| 247 |
-
# --- [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
if confidence < DECISION_CONFIDENCE_THRESHOLD:
|
| 249 |
return {
|
| 250 |
'action': 'IGNORE',
|
|
@@ -253,8 +258,6 @@ class OracleEngine:
|
|
| 253 |
'strategy': strategy_name
|
| 254 |
}
|
| 255 |
|
| 256 |
-
# (الآن نحن متأكدون أن strategy_name ليست 'WAIT' وأن الثقة عالية)
|
| 257 |
-
|
| 258 |
# --- [ الخطوة 4: (نجحت الثقة) - تشغيل "لجنة الأهداف" ] ---
|
| 259 |
winning_feature_vector = best_decision['feature_vector']
|
| 260 |
preds_quantile = {}
|
|
@@ -272,13 +275,9 @@ class OracleEngine:
|
|
| 272 |
tp_price = current_price * (1 + tp_pct)
|
| 273 |
sl_price = current_price * (1 - sl_pct)
|
| 274 |
action_type = "BUY"
|
| 275 |
-
elif "SHORT" in strategy_name:
|
| 276 |
-
tp_price = current_price * (1 - tp_pct)
|
| 277 |
-
sl_price = current_price * (1 + sl_pct)
|
| 278 |
-
action_type = "SELL"
|
| 279 |
else:
|
| 280 |
-
# (
|
| 281 |
-
return {'action': 'IGNORE', 'reason': 'Strategy not actionable'}
|
| 282 |
|
| 283 |
# --- [ الخطوة 6: إرجاع القرار الكامل ] ---
|
| 284 |
return {
|
|
@@ -294,7 +293,7 @@ class OracleEngine:
|
|
| 294 |
}
|
| 295 |
|
| 296 |
except Exception as e:
|
| 297 |
-
print(f"❌ [OracleEngine V2.
|
| 298 |
import traceback
|
| 299 |
traceback.print_exc()
|
| 300 |
return {'action': 'WAIT', 'reason': f'Exception: {e}'}
|
|
|
|
| 41 |
self.feature_names: List[str] = []
|
| 42 |
self.initialized = False
|
| 43 |
# (تحديث الإصدار)
|
| 44 |
+
print("🧠 [OracleEngine V2.3] تم الإنشاء (Spot/Long-Only Mode). جاهز للتهيئة.")
|
| 45 |
|
| 46 |
async def initialize(self):
|
| 47 |
"""
|
|
|
|
| 50 |
if self.initialized:
|
| 51 |
return True
|
| 52 |
|
| 53 |
+
print(f"🧠 [OracleEngine V2.3] جاري تحميل 15 نموذجاً من {self.model_dir}...")
|
| 54 |
try:
|
| 55 |
# 1. تحميل نماذج "لجنة القرار" (Strategy Ensemble)
|
| 56 |
for i in range(N_STRATEGY_MODELS):
|
|
|
|
| 79 |
self.feature_names = self.strategy_boosters[0].feature_name()
|
| 80 |
self.initialized = True
|
| 81 |
|
| 82 |
+
print(f"✅ [OracleEngine V2.3] جاهز. (Threshold: {DECISION_CONFIDENCE_THRESHOLD*100}%)")
|
| 83 |
print(f" -> سيعمل على الأطر: {TIMEBYTES_TO_PROCESS}")
|
| 84 |
return True
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
+
print(f"❌ [OracleEngine V2.3] فشل فادح أثناء التهيئة: {e}")
|
| 88 |
self.initialized = False
|
| 89 |
return False
|
| 90 |
|
|
|
|
| 170 |
try:
|
| 171 |
feature_vector = latest_features[self.feature_names]
|
| 172 |
|
| 173 |
+
# --- [ إصلاح المشكلة: NaN Bug ] ---
|
|
|
|
| 174 |
feature_vector = feature_vector.fillna(0)
|
| 175 |
# --- [ نهاية الإصلاح ] ---
|
| 176 |
|
|
|
|
| 177 |
if feature_vector.isnull().values.any():
|
| 178 |
print("⚠️ [Oracle Warning] Feature vector still contains NaN after fill(0).")
|
| 179 |
return None
|
|
|
|
| 212 |
]
|
| 213 |
ensemble_probs = np.mean(all_probs, axis=0)[0]
|
| 214 |
|
| 215 |
+
# --- [ إصلاح المشكلة: منطق WAIT ] ---
|
|
|
|
|
|
|
|
|
|
| 216 |
actionable_probs = ensemble_probs.copy()
|
| 217 |
actionable_probs[0] = 0.0 # (تجاهل WAIT)
|
| 218 |
|
|
|
|
| 219 |
predicted_strategy_idx = np.argmax(actionable_probs)
|
| 220 |
+
confidence = actionable_probs[predicted_strategy_idx]
|
| 221 |
strategy_name = STRATEGY_MAP.get(predicted_strategy_idx, 'WAIT')
|
| 222 |
# --- [ نهاية الإصلاح ] ---
|
| 223 |
|
|
|
|
| 238 |
confidence = best_decision['confidence']
|
| 239 |
best_tf = best_decision['timeframe']
|
| 240 |
|
| 241 |
+
# --- [ 🛑 🛑 🛑 التعديل الجديد: فلتر Spot Only ] ---
|
| 242 |
+
# إذا كانت الاستراتيجية تحتوي على "SHORT"، يتم تجاهلها فوراً
|
| 243 |
+
if "SHORT" in strategy_name:
|
| 244 |
+
return {
|
| 245 |
+
'action': 'IGNORE',
|
| 246 |
+
'reason': f"Spot Mode: Ignored {strategy_name} signal (Shorts disabled)",
|
| 247 |
+
'confidence': confidence,
|
| 248 |
+
'strategy': strategy_name
|
| 249 |
+
}
|
| 250 |
+
# --- [ نهاية التعديل ] ---
|
| 251 |
+
|
| 252 |
+
# --- [ الخطوة 3: تطبيق فلتر الثقة ] ---
|
| 253 |
if confidence < DECISION_CONFIDENCE_THRESHOLD:
|
| 254 |
return {
|
| 255 |
'action': 'IGNORE',
|
|
|
|
| 258 |
'strategy': strategy_name
|
| 259 |
}
|
| 260 |
|
|
|
|
|
|
|
| 261 |
# --- [ الخطوة 4: (نجحت الثقة) - تشغيل "لجنة الأهداف" ] ---
|
| 262 |
winning_feature_vector = best_decision['feature_vector']
|
| 263 |
preds_quantile = {}
|
|
|
|
| 275 |
tp_price = current_price * (1 + tp_pct)
|
| 276 |
sl_price = current_price * (1 - sl_pct)
|
| 277 |
action_type = "BUY"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
else:
|
| 279 |
+
# (لن نصل إلى هنا لأننا قمنا بتصفية SHORT مسبقاً)
|
| 280 |
+
return {'action': 'IGNORE', 'reason': 'Strategy not actionable (Unknown type)'}
|
| 281 |
|
| 282 |
# --- [ الخطوة 6: إرجاع القرار الكامل ] ---
|
| 283 |
return {
|
|
|
|
| 293 |
}
|
| 294 |
|
| 295 |
except Exception as e:
|
| 296 |
+
print(f"❌ [OracleEngine V2.3] فشل فادح أثناء التنبؤ: {e}")
|
| 297 |
import traceback
|
| 298 |
traceback.print_exc()
|
| 299 |
return {'action': 'WAIT', 'reason': f'Exception: {e}'}
|