Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py (V15.16 - OracleEngine Integration)
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
import traceback
|
|
@@ -21,6 +21,7 @@ try:
|
|
| 21 |
from trade_manager import TradeManager
|
| 22 |
|
| 23 |
# --- [ 🔴 التعديل: استبدال LLM بـ OracleEngine ] ---
|
|
|
|
| 24 |
from ml_engine.oracle_engine import OracleEngine
|
| 25 |
# (تمت إزالة LLMService و LearningHubManager)
|
| 26 |
# --- [ نهاية التعديل ] ---
|
|
@@ -120,6 +121,7 @@ async def lifespan(app: FastAPI):
|
|
| 120 |
|
| 121 |
print(" [6/8] تهيئة MLProcessor (L1 Engine)...")
|
| 122 |
# --- [ 🔴 التعديل: إزالة learning_hub من MLProcessor ] ---
|
|
|
|
| 123 |
ml_processor = MLProcessor(market_context=None, data_manager=data_manager, learning_hub=None)
|
| 124 |
# --- [ نهاية التعديل ] ---
|
| 125 |
await ml_processor.initialize()
|
|
@@ -133,7 +135,7 @@ async def lifespan(app: FastAPI):
|
|
| 133 |
print(" [8/8] تهيئة SniperEngine (L2 Entry Sniper)...")
|
| 134 |
sniper_engine = SniperEngine(models_dir=SHARED_GUARD_MODELS_DIR)
|
| 135 |
await sniper_engine.initialize()
|
| 136 |
-
sniper_engine.set_entry_threshold(0.60)
|
| 137 |
|
| 138 |
print(" [FINAL] تهيئة TradeManager...")
|
| 139 |
trade_manager = TradeManager(
|
|
@@ -171,9 +173,7 @@ async def lifespan(app: FastAPI):
|
|
| 171 |
async def _analyze_symbol_task(symbol: str) -> Dict[str, Any]:
|
| 172 |
global data_manager, ml_processor
|
| 173 |
try:
|
| 174 |
-
# (
|
| 175 |
-
# (L1/L2 يحتاج 5m, 15m, 1h, 4h, 1d)
|
| 176 |
-
# لذا، هذه القائمة صحيحة وتغطي كل الاحتياجات
|
| 177 |
required_tfs = ["5m", "15m", "1h", "4h", "1d"]
|
| 178 |
data_tasks = [data_manager.get_latest_ohlcv(symbol, tf, limit=250) for tf in required_tfs]
|
| 179 |
all_data = await asyncio.gather(*data_tasks)
|
|
@@ -182,7 +182,8 @@ async def _analyze_symbol_task(symbol: str) -> Dict[str, Any]:
|
|
| 182 |
for tf, data in zip(required_tfs, all_data):
|
| 183 |
if data and len(data) > 0: ohlcv_data[tf] = data
|
| 184 |
|
| 185 |
-
if '5m' not in ohlcv_data or '1h' not in ohlcv_data
|
|
|
|
| 186 |
return None
|
| 187 |
|
| 188 |
current_price = await data_manager.get_latest_price_async(symbol)
|
|
@@ -348,7 +349,7 @@ async def run_unified_cycle():
|
|
| 348 |
|
| 349 |
print("="*135 + "\n")
|
| 350 |
|
| 351 |
-
# --- [ 🔴 🔴 🔴 التعديل: خطوة 4 - استبدال LLM بـ OracleEngine ] ---
|
| 352 |
# --- 4. اختيار أفضل 5 للنموذج الضخم ---
|
| 353 |
top_5_candidates = sorted_finalists[:5]
|
| 354 |
print(f" [Cycle 4/5] 🧠 إرسال أفضل {len(top_5_candidates)} عملة للعقل الاحتمالي (Oracle Engine)...")
|
|
@@ -359,6 +360,7 @@ async def run_unified_cycle():
|
|
| 359 |
for signal in top_5_candidates:
|
| 360 |
symbol = signal['symbol']
|
| 361 |
|
|
|
|
| 362 |
if signal['final_total_score'] < 0.60:
|
| 363 |
print(f" -> 🛑 {symbol} تم استبعاده (الدرجة النهائية {signal['final_total_score']:.2f} < 0.60)")
|
| 364 |
continue
|
|
@@ -371,7 +373,9 @@ async def run_unified_cycle():
|
|
| 371 |
|
| 372 |
action = oracle_decision.get('action')
|
| 373 |
confidence = oracle_decision.get('confidence', 0.0)
|
| 374 |
-
|
|
|
|
|
|
|
| 375 |
|
| 376 |
if action == 'WATCH': # (نحن نبحث عن 'WATCH' بناءً على كود Oracle)
|
| 377 |
print(f" 🔥 [Oracle APPROVED] {symbol} (Conf: {confidence:.2f})")
|
|
@@ -386,7 +390,7 @@ async def run_unified_cycle():
|
|
| 386 |
processed_count += 1
|
| 387 |
else:
|
| 388 |
print(f" 🛑 [Oracle REJECTED] {symbol} (Conf: {confidence:.2f})")
|
| 389 |
-
print(f" 📝 Reason: {reason[:200]}...")
|
| 390 |
|
| 391 |
except Exception as e:
|
| 392 |
print(f" ⚠️ [Oracle Error] فشل تحليل Oracle لـ {symbol}: {e}")
|
|
@@ -413,8 +417,8 @@ async def run_unified_cycle():
|
|
| 413 |
app = FastAPI(
|
| 414 |
lifespan=lifespan,
|
| 415 |
# --- [ 🔴 التعديل: تحديث العنوان ] ---
|
| 416 |
-
title="Titan V16.
|
| 417 |
-
description="نظام تداول آلي هجين (L1/L2/L3-ML) مع قرار احتمالي"
|
| 418 |
# --- [ نهاية التعديل ] ---
|
| 419 |
)
|
| 420 |
|
|
@@ -423,7 +427,9 @@ async def root_status():
|
|
| 423 |
return {
|
| 424 |
"system_status": "ONLINE",
|
| 425 |
"initialized": sys_state.ready,
|
| 426 |
-
|
|
|
|
|
|
|
| 427 |
"guard_v2_active": guard_engine.initialized if guard_engine else False,
|
| 428 |
"sniper_v3_active": sniper_engine.initialized if sniper_engine else False,
|
| 429 |
"current_mode": "RE-ANALYSIS" if trade_manager and trade_manager.open_positions else "EXPLORER"
|
|
@@ -434,7 +440,9 @@ async def trigger_cycle_endpoint(background_tasks: BackgroundTasks):
|
|
| 434 |
if not sys_state.ready:
|
| 435 |
raise HTTPException(status_code=503, detail="System is still initializing.")
|
| 436 |
background_tasks.add_task(run_unified_cycle)
|
| 437 |
-
|
|
|
|
|
|
|
| 438 |
|
| 439 |
@app.get("/status")
|
| 440 |
async def get_full_status():
|
|
|
|
| 1 |
+
# app.py (V15.16 - OracleEngine Integration - Patched)
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
import traceback
|
|
|
|
| 21 |
from trade_manager import TradeManager
|
| 22 |
|
| 23 |
# --- [ 🔴 التعديل: استبدال LLM بـ OracleEngine ] ---
|
| 24 |
+
# (تأكد من أن الملف أعلاه موجود في: ml_engine/oracle_engine.py)
|
| 25 |
from ml_engine.oracle_engine import OracleEngine
|
| 26 |
# (تمت إزالة LLMService و LearningHubManager)
|
| 27 |
# --- [ نهاية التعديل ] ---
|
|
|
|
| 121 |
|
| 122 |
print(" [6/8] تهيئة MLProcessor (L1 Engine)...")
|
| 123 |
# --- [ 🔴 التعديل: إزالة learning_hub من MLProcessor ] ---
|
| 124 |
+
# (استناداً إلى `processor (34).py` و `app (43).py` الأصلي)
|
| 125 |
ml_processor = MLProcessor(market_context=None, data_manager=data_manager, learning_hub=None)
|
| 126 |
# --- [ نهاية التعديل ] ---
|
| 127 |
await ml_processor.initialize()
|
|
|
|
| 135 |
print(" [8/8] تهيئة SniperEngine (L2 Entry Sniper)...")
|
| 136 |
sniper_engine = SniperEngine(models_dir=SHARED_GUARD_MODELS_DIR)
|
| 137 |
await sniper_engine.initialize()
|
| 138 |
+
sniper_engine.set_entry_threshold(0.60) #
|
| 139 |
|
| 140 |
print(" [FINAL] تهيئة TradeManager...")
|
| 141 |
trade_manager = TradeManager(
|
|
|
|
| 173 |
async def _analyze_symbol_task(symbol: str) -> Dict[str, Any]:
|
| 174 |
global data_manager, ml_processor
|
| 175 |
try:
|
| 176 |
+
# (هذه القائمة تغطي احتياجات L1/L2 واحتياجات Oracle L3)
|
|
|
|
|
|
|
| 177 |
required_tfs = ["5m", "15m", "1h", "4h", "1d"]
|
| 178 |
data_tasks = [data_manager.get_latest_ohlcv(symbol, tf, limit=250) for tf in required_tfs]
|
| 179 |
all_data = await asyncio.gather(*data_tasks)
|
|
|
|
| 182 |
for tf, data in zip(required_tfs, all_data):
|
| 183 |
if data and len(data) > 0: ohlcv_data[tf] = data
|
| 184 |
|
| 185 |
+
if '5m' not in ohlcv_data or '15m' not in ohlcv_data or '1h' not in ohlcv_data or '4h' not in ohlcv_data:
|
| 186 |
+
print(f" -> ⚠️ [L1 Task] بيانات ناقصة لـ {symbol}، تم الإلغاء.")
|
| 187 |
return None
|
| 188 |
|
| 189 |
current_price = await data_manager.get_latest_price_async(symbol)
|
|
|
|
| 349 |
|
| 350 |
print("="*135 + "\n")
|
| 351 |
|
| 352 |
+
# --- [ 🔴 🔴 🔴 التعديل: خطوة 4 - استبدال LLM بـ OracleEngine وتطبيق إصلاح اللوغاريتمات ] ---
|
| 353 |
# --- 4. اختيار أفضل 5 للنموذج الضخم ---
|
| 354 |
top_5_candidates = sorted_finalists[:5]
|
| 355 |
print(f" [Cycle 4/5] 🧠 إرسال أفضل {len(top_5_candidates)} عملة للعقل الاحتمالي (Oracle Engine)...")
|
|
|
|
| 360 |
for signal in top_5_candidates:
|
| 361 |
symbol = signal['symbol']
|
| 362 |
|
| 363 |
+
# (عتبة المرور إلى L3 لا تتغير)
|
| 364 |
if signal['final_total_score'] < 0.60:
|
| 365 |
print(f" -> 🛑 {symbol} تم استبعاده (الدرجة النهائية {signal['final_total_score']:.2f} < 0.60)")
|
| 366 |
continue
|
|
|
|
| 373 |
|
| 374 |
action = oracle_decision.get('action')
|
| 375 |
confidence = oracle_decision.get('confidence', 0.0)
|
| 376 |
+
|
| 377 |
+
# (الإصلاح: البحث عن "السبب" في مفتاحين)
|
| 378 |
+
reason = oracle_decision.get('analysis_summary') or oracle_decision.get('reason', 'No summary provided')
|
| 379 |
|
| 380 |
if action == 'WATCH': # (نحن نبحث عن 'WATCH' بناءً على كود Oracle)
|
| 381 |
print(f" 🔥 [Oracle APPROVED] {symbol} (Conf: {confidence:.2f})")
|
|
|
|
| 390 |
processed_count += 1
|
| 391 |
else:
|
| 392 |
print(f" 🛑 [Oracle REJECTED] {symbol} (Conf: {confidence:.2f})")
|
| 393 |
+
print(f" 📝 Reason: {reason[:200]}...") # (الآن سيطبع السبب الحقيقي)
|
| 394 |
|
| 395 |
except Exception as e:
|
| 396 |
print(f" ⚠️ [Oracle Error] فشل تحليل Oracle لـ {symbol}: {e}")
|
|
|
|
| 417 |
app = FastAPI(
|
| 418 |
lifespan=lifespan,
|
| 419 |
# --- [ 🔴 التعديل: تحديث العنوان ] ---
|
| 420 |
+
title="Titan V16.1 (Oracle Engine V2.1 - Patched)",
|
| 421 |
+
description="نظام تداول آلي هجين (L1/L2/L3-ML) مع قرار احتمالي متعدد الأطر"
|
| 422 |
# --- [ نهاية التعديل ] ---
|
| 423 |
)
|
| 424 |
|
|
|
|
| 427 |
return {
|
| 428 |
"system_status": "ONLINE",
|
| 429 |
"initialized": sys_state.ready,
|
| 430 |
+
# --- [ 🔴 التعديل: تحديث الحالة ] ---
|
| 431 |
+
"brain_type": "Oracle_Engine_V2.1 (Multi-TF)",
|
| 432 |
+
# --- [ نهاية التعديل ] ---
|
| 433 |
"guard_v2_active": guard_engine.initialized if guard_engine else False,
|
| 434 |
"sniper_v3_active": sniper_engine.initialized if sniper_engine else False,
|
| 435 |
"current_mode": "RE-ANALYSIS" if trade_manager and trade_manager.open_positions else "EXPLORER"
|
|
|
|
| 440 |
if not sys_state.ready:
|
| 441 |
raise HTTPException(status_code=503, detail="System is still initializing.")
|
| 442 |
background_tasks.add_task(run_unified_cycle)
|
| 443 |
+
# --- [ 🔴 التعديل: تحديث الرسالة ] ---
|
| 444 |
+
return {"status": "ACCEPTED", "message": "Unified smart cycle triggered (Oracle V2.1 Mode)."}
|
| 445 |
+
# --- [ نهاية التعديل ] ---
|
| 446 |
|
| 447 |
@app.get("/status")
|
| 448 |
async def get_full_status():
|