Spaces:
Sleeping
Sleeping
Update ml_engine/processor.py
Browse files- ml_engine/processor.py +70 -93
ml_engine/processor.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# ============================================================
|
| 2 |
-
# 🧠 ml_engine/processor.py (V34.
|
| 3 |
# ============================================================
|
| 4 |
|
| 5 |
import asyncio
|
|
@@ -31,15 +31,15 @@ try:
|
|
| 31 |
from .sniper_engine import SniperEngine
|
| 32 |
except ImportError: SniperEngine = None
|
| 33 |
|
| 34 |
-
# ✅ استيراد الحارسين
|
| 35 |
try:
|
| 36 |
-
from .hybrid_guardian import HybridDeepSteward
|
| 37 |
except ImportError:
|
| 38 |
print("⚠️ [Processor] HybridDeepSteward not found.")
|
| 39 |
HybridDeepSteward = None
|
| 40 |
|
| 41 |
try:
|
| 42 |
-
from .guardian_hydra import GuardianHydra
|
| 43 |
except ImportError:
|
| 44 |
print("⚠️ [Processor] GuardianHydra not found.")
|
| 45 |
GuardianHydra = None
|
|
@@ -54,12 +54,10 @@ MODELS_PATTERN_DIR = os.path.join(BASE_DIR, "ml_models", "xgboost_pattern2")
|
|
| 54 |
MODELS_UNIFIED_DIR = os.path.join(BASE_DIR, "ml_models", "Unified_Models_V1")
|
| 55 |
MODELS_SNIPER_DIR = os.path.join(BASE_DIR, "ml_models", "guard_v2")
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
MODEL_V3_PATH = os.path.join(BASE_DIR, "ml_models", "DeepSteward_V3_Production.json")
|
| 62 |
-
MODEL_V3_FEAT = os.path.join(BASE_DIR, "ml_models", "DeepSteward_V3_Features.json")
|
| 63 |
|
| 64 |
# ============================================================
|
| 65 |
# 🎛️ SYSTEM LIMITS & THRESHOLDS
|
|
@@ -100,10 +98,6 @@ class SystemLimits:
|
|
| 100 |
HYDRA_GIVEBACK_THRESH = 0.70
|
| 101 |
HYDRA_STAGNATION_THRESH = 0.50
|
| 102 |
|
| 103 |
-
# --- Layer 0: Hybrid Thresholds (Legacy) - يمكن إضافتها هنا للتحكم المركزي ---
|
| 104 |
-
LEGACY_PANIC_THRESH = 0.90
|
| 105 |
-
LEGACY_SAFE_LIMIT = 0.50
|
| 106 |
-
|
| 107 |
@classmethod
|
| 108 |
def to_dict(cls) -> Dict[str, Any]:
|
| 109 |
return {k: v for k, v in cls.__dict__.items() if not k.startswith('__') and not callable(v)}
|
|
@@ -123,30 +117,25 @@ class MLProcessor:
|
|
| 123 |
self.data_manager = data_manager
|
| 124 |
self.initialized = False
|
| 125 |
|
| 126 |
-
# Engines Init (Standard)
|
| 127 |
self.titan = TitanEngine(model_dir=MODELS_L2_DIR) if TitanEngine else None
|
| 128 |
self.pattern_engine = ChartPatternAnalyzer(models_dir=MODELS_PATTERN_DIR) if ChartPatternAnalyzer else None
|
| 129 |
self.mc_analyzer = MonteCarloEngine() if MonteCarloEngine else None
|
| 130 |
self.oracle = OracleEngine(model_dir=MODELS_UNIFIED_DIR) if OracleEngine else None
|
| 131 |
self.sniper = SniperEngine(models_dir=MODELS_SNIPER_DIR) if SniperEngine else None
|
| 132 |
|
| 133 |
-
# ✅ Dual Guardian Initialization
|
| 134 |
-
# 1. The Modern Hydra (X-Ray)
|
| 135 |
self.guardian_hydra = None
|
| 136 |
if GuardianHydra:
|
| 137 |
self.guardian_hydra = GuardianHydra(model_dir=MODELS_HYDRA_DIR)
|
| 138 |
|
| 139 |
-
# 2. The Legacy Steward (Hybrid)
|
| 140 |
self.guardian_legacy = None
|
| 141 |
if HybridDeepSteward:
|
| 142 |
-
# نفترض وجود المجلد legacy داخل ml_models
|
| 143 |
self.guardian_legacy = HybridDeepSteward(
|
| 144 |
v2_model_path=MODEL_V2_PATH,
|
| 145 |
v3_model_path=MODEL_V3_PATH,
|
| 146 |
v3_features_map_path=MODEL_V3_FEAT
|
| 147 |
)
|
| 148 |
|
| 149 |
-
print(f"🧠 [MLProcessor V34.
|
| 150 |
|
| 151 |
async def initialize(self):
|
| 152 |
if self.initialized: return
|
|
@@ -166,13 +155,11 @@ class MLProcessor:
|
|
| 166 |
|
| 167 |
if tasks: await asyncio.gather(*tasks)
|
| 168 |
|
| 169 |
-
# ✅ Initialize Dual Guardians
|
| 170 |
if self.guardian_hydra:
|
| 171 |
self.guardian_hydra.initialize()
|
| 172 |
print(" 🛡️ [Guard 1] Hydra X-Ray: Active")
|
| 173 |
|
| 174 |
if self.guardian_legacy:
|
| 175 |
-
# Legacy guardian init is usually synchronous in provided code, but checking just in case
|
| 176 |
if asyncio.iscoroutinefunction(self.guardian_legacy.initialize):
|
| 177 |
await self.guardian_legacy.initialize()
|
| 178 |
else:
|
|
@@ -186,11 +173,7 @@ class MLProcessor:
|
|
| 186 |
print(f"❌ [Processor FATAL] Init failed: {e}")
|
| 187 |
traceback.print_exc()
|
| 188 |
|
| 189 |
-
# ... (process_compound_signal, consult_oracle, check_sniper_entry remain UNCHANGED) ...
|
| 190 |
-
# قم بنسخ الدوال process_compound_signal و consult_oracle و check_sniper_entry كما هي من الكود السابق
|
| 191 |
-
|
| 192 |
async def process_compound_signal(self, raw_data: Dict[str, Any]) -> Optional[Dict[str, Any]]:
|
| 193 |
-
# (نفس الكود السابق تماماً للاختصار، لا تغيير هنا)
|
| 194 |
if not self.initialized: await self.initialize()
|
| 195 |
symbol = raw_data.get('symbol')
|
| 196 |
ohlcv_data = raw_data.get('ohlcv')
|
|
@@ -232,98 +215,92 @@ class MLProcessor:
|
|
| 232 |
return {'signal': 'WAIT', 'reason': 'Sniper Engine Missing'}
|
| 233 |
|
| 234 |
# ==========================================================================
|
| 235 |
-
# 🛡️ 4. الحارس المزدوج
|
| 236 |
# ==========================================================================
|
| 237 |
def consult_dual_guardians(self, symbol, ohlcv_1m, ohlcv_5m, ohlcv_15m, trade_context):
|
| 238 |
"""
|
| 239 |
-
استشارة
|
| 240 |
"""
|
| 241 |
response = {
|
| 242 |
'action': 'HOLD',
|
| 243 |
-
'
|
| 244 |
-
'
|
| 245 |
}
|
| 246 |
|
| 247 |
-
# -
|
| 248 |
-
|
| 249 |
-
# ------------------------------------------
|
| 250 |
-
hydra_result = {'action': 'HOLD', 'reason': 'Disabled'}
|
| 251 |
if self.guardian_hydra and self.guardian_hydra.initialized:
|
| 252 |
hydra_result = self.guardian_hydra.analyze_position(symbol, ohlcv_1m, ohlcv_5m, ohlcv_15m, trade_context)
|
| 253 |
|
| 254 |
-
# تطبيق سياسات SystemLimits على الهيدرا
|
| 255 |
-
probs = hydra_result.get('probs', {})
|
| 256 |
-
p_crash = probs.get('crash', 0.0)
|
| 257 |
-
p_giveback = probs.get('giveback', 0.0)
|
| 258 |
-
|
| 259 |
# Policy Overlay
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
if hydra_result['action'] == 'HOLD':
|
| 261 |
if p_crash >= SystemLimits.HYDRA_CRASH_THRESH:
|
| 262 |
hydra_result['action'] = 'EXIT_HARD'
|
| 263 |
-
hydra_result['reason'] = f"Hydra
|
| 264 |
elif p_giveback >= SystemLimits.HYDRA_GIVEBACK_THRESH:
|
| 265 |
hydra_result['action'] = 'EXIT_SOFT'
|
| 266 |
-
hydra_result['reason'] = f"Hydra
|
| 267 |
|
| 268 |
-
#
|
| 269 |
-
|
| 270 |
-
# ------------------------------------------
|
| 271 |
-
legacy_result = {'action': 'HOLD', 'reason': 'Disabled'}
|
| 272 |
if self.guardian_legacy and self.guardian_legacy.initialized:
|
| 273 |
-
# الحارس القديم يحتاج entry_price كرقم منفصل
|
| 274 |
entry_price = float(trade_context.get('entry_price', 0.0))
|
| 275 |
legacy_result = self.guardian_legacy.analyze_position(ohlcv_1m, ohlcv_5m, ohlcv_15m, entry_price)
|
| 276 |
|
| 277 |
-
#
|
| 278 |
-
#
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
# الأولوية للسلامة (Safety First - OR Logic)
|
| 284 |
|
| 285 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
if hydra_result['action'] == 'EXIT_HARD':
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
}
|
| 292 |
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
}
|
| 307 |
-
|
| 308 |
-
if legacy_result['action'] == 'EXIT_SOFT':
|
| 309 |
-
return {
|
| 310 |
-
'action': 'EXIT_SOFT',
|
| 311 |
-
'reason': f"[Legacy Warning] {legacy_result['reason']}",
|
| 312 |
-
'scores': legacy_result.get('scores', {})
|
| 313 |
-
}
|
| 314 |
-
|
| 315 |
-
# ج) إدارة وقف الخسارة المتحرك (من الهيدرا فقط حالياً)
|
| 316 |
-
if hydra_result['action'] in ['TIGHTEN_SL', 'TRAIL_SL']:
|
| 317 |
-
return {
|
| 318 |
-
'action': hydra_result['action'],
|
| 319 |
-
'reason': hydra_result['reason']
|
| 320 |
-
}
|
| 321 |
-
|
| 322 |
-
# د) البقاء
|
| 323 |
return {
|
| 324 |
-
'action':
|
| 325 |
-
'reason':
|
| 326 |
-
'detailed_log': f"
|
|
|
|
|
|
|
| 327 |
}
|
| 328 |
|
| 329 |
async def run_advanced_monte_carlo(self, symbol, timeframe='1h'):
|
|
@@ -337,4 +314,4 @@ class MLProcessor:
|
|
| 337 |
except Exception: pass
|
| 338 |
return 0.0
|
| 339 |
|
| 340 |
-
print("✅ [MLProcessor V34.
|
|
|
|
| 1 |
# ============================================================
|
| 2 |
+
# 🧠 ml_engine/processor.py (V34.1 - GEM-Architect: Transparent Stamps)
|
| 3 |
# ============================================================
|
| 4 |
|
| 5 |
import asyncio
|
|
|
|
| 31 |
from .sniper_engine import SniperEngine
|
| 32 |
except ImportError: SniperEngine = None
|
| 33 |
|
| 34 |
+
# ✅ استيراد الحارسين
|
| 35 |
try:
|
| 36 |
+
from .hybrid_guardian import HybridDeepSteward
|
| 37 |
except ImportError:
|
| 38 |
print("⚠️ [Processor] HybridDeepSteward not found.")
|
| 39 |
HybridDeepSteward = None
|
| 40 |
|
| 41 |
try:
|
| 42 |
+
from .guardian_hydra import GuardianHydra
|
| 43 |
except ImportError:
|
| 44 |
print("⚠️ [Processor] GuardianHydra not found.")
|
| 45 |
GuardianHydra = None
|
|
|
|
| 54 |
MODELS_UNIFIED_DIR = os.path.join(BASE_DIR, "ml_models", "Unified_Models_V1")
|
| 55 |
MODELS_SNIPER_DIR = os.path.join(BASE_DIR, "ml_models", "guard_v2")
|
| 56 |
|
| 57 |
+
MODELS_HYDRA_DIR = os.path.join(BASE_DIR, "ml_models", "guard_v1")
|
| 58 |
+
MODEL_V2_PATH = os.path.join(BASE_DIR, "ml_models", "legacy", "v2_regime.json")
|
| 59 |
+
MODEL_V3_PATH = os.path.join(BASE_DIR, "ml_models", "legacy", "v3_sniper.json")
|
| 60 |
+
MODEL_V3_FEAT = os.path.join(BASE_DIR, "ml_models", "legacy", "v3_features.json")
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# ============================================================
|
| 63 |
# 🎛️ SYSTEM LIMITS & THRESHOLDS
|
|
|
|
| 98 |
HYDRA_GIVEBACK_THRESH = 0.70
|
| 99 |
HYDRA_STAGNATION_THRESH = 0.50
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
@classmethod
|
| 102 |
def to_dict(cls) -> Dict[str, Any]:
|
| 103 |
return {k: v for k, v in cls.__dict__.items() if not k.startswith('__') and not callable(v)}
|
|
|
|
| 117 |
self.data_manager = data_manager
|
| 118 |
self.initialized = False
|
| 119 |
|
|
|
|
| 120 |
self.titan = TitanEngine(model_dir=MODELS_L2_DIR) if TitanEngine else None
|
| 121 |
self.pattern_engine = ChartPatternAnalyzer(models_dir=MODELS_PATTERN_DIR) if ChartPatternAnalyzer else None
|
| 122 |
self.mc_analyzer = MonteCarloEngine() if MonteCarloEngine else None
|
| 123 |
self.oracle = OracleEngine(model_dir=MODELS_UNIFIED_DIR) if OracleEngine else None
|
| 124 |
self.sniper = SniperEngine(models_dir=MODELS_SNIPER_DIR) if SniperEngine else None
|
| 125 |
|
|
|
|
|
|
|
| 126 |
self.guardian_hydra = None
|
| 127 |
if GuardianHydra:
|
| 128 |
self.guardian_hydra = GuardianHydra(model_dir=MODELS_HYDRA_DIR)
|
| 129 |
|
|
|
|
| 130 |
self.guardian_legacy = None
|
| 131 |
if HybridDeepSteward:
|
|
|
|
| 132 |
self.guardian_legacy = HybridDeepSteward(
|
| 133 |
v2_model_path=MODEL_V2_PATH,
|
| 134 |
v3_model_path=MODEL_V3_PATH,
|
| 135 |
v3_features_map_path=MODEL_V3_FEAT
|
| 136 |
)
|
| 137 |
|
| 138 |
+
print(f"🧠 [MLProcessor V34.1] Dual-Core Guardians + Detail Stamps Online.")
|
| 139 |
|
| 140 |
async def initialize(self):
|
| 141 |
if self.initialized: return
|
|
|
|
| 155 |
|
| 156 |
if tasks: await asyncio.gather(*tasks)
|
| 157 |
|
|
|
|
| 158 |
if self.guardian_hydra:
|
| 159 |
self.guardian_hydra.initialize()
|
| 160 |
print(" 🛡️ [Guard 1] Hydra X-Ray: Active")
|
| 161 |
|
| 162 |
if self.guardian_legacy:
|
|
|
|
| 163 |
if asyncio.iscoroutinefunction(self.guardian_legacy.initialize):
|
| 164 |
await self.guardian_legacy.initialize()
|
| 165 |
else:
|
|
|
|
| 173 |
print(f"❌ [Processor FATAL] Init failed: {e}")
|
| 174 |
traceback.print_exc()
|
| 175 |
|
|
|
|
|
|
|
|
|
|
| 176 |
async def process_compound_signal(self, raw_data: Dict[str, Any]) -> Optional[Dict[str, Any]]:
|
|
|
|
| 177 |
if not self.initialized: await self.initialize()
|
| 178 |
symbol = raw_data.get('symbol')
|
| 179 |
ohlcv_data = raw_data.get('ohlcv')
|
|
|
|
| 215 |
return {'signal': 'WAIT', 'reason': 'Sniper Engine Missing'}
|
| 216 |
|
| 217 |
# ==========================================================================
|
| 218 |
+
# 🛡️ 4. الحارس المزدوج مع الطوابع الدقيقة (Detailed Stamps)
|
| 219 |
# ==========================================================================
|
| 220 |
def consult_dual_guardians(self, symbol, ohlcv_1m, ohlcv_5m, ohlcv_15m, trade_context):
|
| 221 |
"""
|
| 222 |
+
استشارة الحارسين وإنشاء تقرير مفصل بالأرقام.
|
| 223 |
"""
|
| 224 |
response = {
|
| 225 |
'action': 'HOLD',
|
| 226 |
+
'detailed_log': '',
|
| 227 |
+
'probs': {}
|
| 228 |
}
|
| 229 |
|
| 230 |
+
# 1. Hydra X-Ray Scores
|
| 231 |
+
hydra_result = {'action': 'HOLD', 'reason': 'Disabled', 'probs': {}}
|
|
|
|
|
|
|
| 232 |
if self.guardian_hydra and self.guardian_hydra.initialized:
|
| 233 |
hydra_result = self.guardian_hydra.analyze_position(symbol, ohlcv_1m, ohlcv_5m, ohlcv_15m, trade_context)
|
| 234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
# Policy Overlay
|
| 236 |
+
h_probs = hydra_result.get('probs', {})
|
| 237 |
+
p_crash = h_probs.get('crash', 0.0)
|
| 238 |
+
p_giveback = h_probs.get('giveback', 0.0)
|
| 239 |
+
|
| 240 |
if hydra_result['action'] == 'HOLD':
|
| 241 |
if p_crash >= SystemLimits.HYDRA_CRASH_THRESH:
|
| 242 |
hydra_result['action'] = 'EXIT_HARD'
|
| 243 |
+
hydra_result['reason'] = f"Hydra Crash Risk {p_crash:.2f}"
|
| 244 |
elif p_giveback >= SystemLimits.HYDRA_GIVEBACK_THRESH:
|
| 245 |
hydra_result['action'] = 'EXIT_SOFT'
|
| 246 |
+
hydra_result['reason'] = f"Hydra Giveback Risk {p_giveback:.2f}"
|
| 247 |
|
| 248 |
+
# 2. Legacy Hybrid Scores
|
| 249 |
+
legacy_result = {'action': 'HOLD', 'reason': 'Disabled', 'scores': {}}
|
|
|
|
|
|
|
| 250 |
if self.guardian_legacy and self.guardian_legacy.initialized:
|
|
|
|
| 251 |
entry_price = float(trade_context.get('entry_price', 0.0))
|
| 252 |
legacy_result = self.guardian_legacy.analyze_position(ohlcv_1m, ohlcv_5m, ohlcv_15m, entry_price)
|
| 253 |
|
| 254 |
+
# 3. تجميع الطوابع (Stamps Collection)
|
| 255 |
+
# Hydra Stamps
|
| 256 |
+
h_probs = hydra_result.get('probs', {})
|
| 257 |
+
h_c = h_probs.get('crash', 0.0)
|
| 258 |
+
h_g = h_probs.get('giveback', 0.0)
|
| 259 |
+
h_s = h_probs.get('stagnation', 0.0)
|
|
|
|
| 260 |
|
| 261 |
+
# Legacy Stamps
|
| 262 |
+
l_scores = legacy_result.get('scores', {})
|
| 263 |
+
l_v2 = l_scores.get('v2', 0.0) # Regime
|
| 264 |
+
l_v3 = l_scores.get('v3', 0.0) # Sniper
|
| 265 |
+
|
| 266 |
+
# ✅ صياغة التقرير المفصل (The Detailed Stamp)
|
| 267 |
+
# مثال: 🐲[C:12%|G:05%|S:00%] 🕸️[V2:45%|V3:10%]
|
| 268 |
+
stamp_str = (
|
| 269 |
+
f"🐲[C:{h_c:.0%}|G:{h_g:.0%}|S:{h_s:.0%}] "
|
| 270 |
+
f"🕸️[V2:{l_v2:.0%}|V3:{l_v3:.0%}]"
|
| 271 |
+
)
|
| 272 |
+
|
| 273 |
+
# 4. Decision Logic (OR Gate)
|
| 274 |
+
final_action = 'HOLD'
|
| 275 |
+
final_reason = f"Safe. {stamp_str}"
|
| 276 |
+
|
| 277 |
+
# أ) فحص الخروج القوي
|
| 278 |
if hydra_result['action'] == 'EXIT_HARD':
|
| 279 |
+
final_action = 'EXIT_HARD'
|
| 280 |
+
final_reason = f"🚨 HYDRA KILL: {hydra_result['reason']} | {stamp_str}"
|
| 281 |
+
elif legacy_result['action'] == 'EXIT_HARD':
|
| 282 |
+
final_action = 'EXIT_HARD'
|
| 283 |
+
final_reason = f"🚨 LEGACY KILL: {legacy_result['reason']} | {stamp_str}"
|
| 284 |
|
| 285 |
+
# ب) فحص الخروج الناعم
|
| 286 |
+
elif hydra_result['action'] == 'EXIT_SOFT':
|
| 287 |
+
final_action = 'EXIT_SOFT'
|
| 288 |
+
final_reason = f"⚠️ HYDRA WARN: {hydra_result['reason']} | {stamp_str}"
|
| 289 |
+
elif legacy_result['action'] == 'EXIT_SOFT':
|
| 290 |
+
final_action = 'EXIT_SOFT'
|
| 291 |
+
final_reason = f"⚠️ LEGACY WARN: {legacy_result['reason']} | {stamp_str}"
|
| 292 |
+
|
| 293 |
+
# ج) SL Management
|
| 294 |
+
elif hydra_result['action'] in ['TIGHTEN_SL', 'TRAIL_SL']:
|
| 295 |
+
final_action = hydra_result['action']
|
| 296 |
+
final_reason = f"🛡️ {hydra_result['action']} | {stamp_str}"
|
| 297 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
return {
|
| 299 |
+
'action': final_action,
|
| 300 |
+
'reason': final_reason,
|
| 301 |
+
'detailed_log': f"{final_action} | {stamp_str}", # هذا ما سيظهر في الواجهة
|
| 302 |
+
'probs': h_probs,
|
| 303 |
+
'scores': l_scores
|
| 304 |
}
|
| 305 |
|
| 306 |
async def run_advanced_monte_carlo(self, symbol, timeframe='1h'):
|
|
|
|
| 314 |
except Exception: pass
|
| 315 |
return 0.0
|
| 316 |
|
| 317 |
+
print("✅ [MLProcessor V34.1] Detailed Stamps Logic Integrated.")
|