Spaces:
Running
Running
Update data_manager.py
Browse files- data_manager.py +32 -39
data_manager.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# data_manager.py (Updated to V9.
|
| 2 |
import os
|
| 3 |
import asyncio
|
| 4 |
import httpx
|
|
@@ -66,24 +66,22 @@ class DataManager:
|
|
| 66 |
print(f"❌ [DataManager] فشل تهيئة محرك الأنماط V8: {e}")
|
| 67 |
self.pattern_analyzer = ChartPatternAnalyzer(r2_service=None)
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
print(" > [DataManager] تهيئة الكاشف المصغر (Layer1 Ranker V9.1)...")
|
| 72 |
try:
|
| 73 |
-
# (
|
| 74 |
model_file_path = "ml_models/layer1_ranker.lgbm"
|
| 75 |
self.layer1_ranker = Layer1Ranker(model_path=model_file_path)
|
| 76 |
await self.layer1_ranker.initialize()
|
| 77 |
if self.layer1_ranker.model is None:
|
| 78 |
-
print(" ⚠️ [DataManager V9.
|
| 79 |
else:
|
| 80 |
-
print(f" ✅ [DataManager V9.
|
| 81 |
except Exception as e:
|
| 82 |
-
print(f"❌ [DataManager V9.
|
| 83 |
self.layer1_ranker = None
|
| 84 |
-
# 🔴 --- END OF CHANGE (V9.1) --- 🔴
|
| 85 |
|
| 86 |
-
print("✅ DataManager initialized - V9.
|
| 87 |
|
| 88 |
async def _load_markets(self):
|
| 89 |
try:
|
|
@@ -191,25 +189,25 @@ class DataManager:
|
|
| 191 |
|
| 192 |
async def layer1_rapid_screening(self) -> List[Dict[str, Any]]:
|
| 193 |
"""
|
| 194 |
-
الطبقة 1: فحص سريع - (محدث V9.
|
| 195 |
-
-
|
| 196 |
"""
|
| 197 |
-
print("📊 الطبقة 1 (V9.
|
| 198 |
|
| 199 |
if not self.layer1_ranker:
|
| 200 |
-
print("❌ [V9.
|
| 201 |
return []
|
| 202 |
|
| 203 |
# الخطوة 1: جلب أفضل 100 عملة حسب الحجم
|
| 204 |
volume_data = await self._get_volume_data_optimal()
|
| 205 |
if not volume_data: volume_data = await self._get_volume_data_direct_api()
|
| 206 |
if not volume_data:
|
| 207 |
-
print("❌ [V9.
|
| 208 |
return []
|
| 209 |
|
| 210 |
volume_data.sort(key=lambda x: x['dollar_volume'], reverse=True)
|
| 211 |
top_100_by_volume = volume_data[:100]
|
| 212 |
-
print(f"✅ [V9.
|
| 213 |
|
| 214 |
final_candidates_with_scores = []
|
| 215 |
batch_symbols_data = top_100_by_volume
|
|
@@ -227,18 +225,19 @@ class DataManager:
|
|
| 227 |
valid_symbol_data_for_ranking.append(symbol_data)
|
| 228 |
|
| 229 |
if not valid_symbol_data_for_ranking:
|
| 230 |
-
print("❌ [V9.
|
| 231 |
return []
|
| 232 |
|
| 233 |
-
print(f" 🔄 [V9.
|
| 234 |
|
| 235 |
-
# الخطوة 3: حساب "الميزات الذكية V9.
|
| 236 |
all_features_list = []
|
| 237 |
symbols_in_order = []
|
| 238 |
for symbol_data in valid_symbol_data_for_ranking:
|
| 239 |
try:
|
| 240 |
df = self._create_dataframe(symbol_data['ohlcv_1h_raw'])
|
| 241 |
if df.empty: continue
|
|
|
|
| 242 |
smart_features = self.technical_analyzer.calculate_v9_smart_features(df)
|
| 243 |
if smart_features:
|
| 244 |
all_features_list.append(smart_features)
|
|
@@ -246,45 +245,39 @@ class DataManager:
|
|
| 246 |
except Exception: pass
|
| 247 |
|
| 248 |
if not all_features_list:
|
| 249 |
-
print("❌ [V9.
|
| 250 |
return []
|
| 251 |
|
| 252 |
# الخطوة 4: التنبؤ (التصنيف)
|
| 253 |
-
print(f" 🧠 [V9.
|
| 254 |
features_dataframe = pd.DataFrame(all_features_list)
|
| 255 |
probabilities = self.layer1_ranker.predict_proba(features_dataframe)
|
| 256 |
|
| 257 |
-
#
|
| 258 |
-
print(f" 🔍 [V9.
|
| 259 |
-
# (إنشاء قائمة (الرمز، الدرجة) فقط للطباعة التشخيصية)
|
| 260 |
debug_scores = []
|
| 261 |
for i, (symbol_data) in enumerate(symbols_in_order):
|
| 262 |
debug_scores.append((symbol_data['symbol'], probabilities[i]))
|
| 263 |
-
|
| 264 |
-
# (فرز النتائج من الأعلى إلى الأقل)
|
| 265 |
debug_scores.sort(key=lambda x: x[1], reverse=True)
|
| 266 |
-
|
| 267 |
-
# (طباعة أفضل 10 نتائج "خام" للتشخيص)
|
| 268 |
-
print(" --- 📋 [V9.5 DEBUG] أعلى 10 درجات خام (قبل الفلترة) ---")
|
| 269 |
for i, (symbol, score) in enumerate(debug_scores[:10]):
|
| 270 |
-
print(f" {i+1}. {symbol}: {score:.4f}")
|
| 271 |
print(" -------------------------------------------------")
|
| 272 |
-
# 🔴 --- END OF NEW DEBUG LOGGING (V9.5) --- 🔴
|
| 273 |
|
| 274 |
-
# الخطوة 5: تجميع النتائج (مع عتبة
|
| 275 |
for i, (symbol_data) in enumerate(symbols_in_order):
|
| 276 |
score = probabilities[i]
|
| 277 |
|
| 278 |
-
# (عتبة
|
| 279 |
-
if score >= 0.
|
| 280 |
symbol = symbol_data['symbol']
|
| 281 |
-
print(f" ✅ {symbol}: نجح
|
| 282 |
symbol_data['layer1_score'] = float(score)
|
| 283 |
-
symbol_data['reasons_for_candidacy'] = ["
|
| 284 |
if 'ohlcv_1h_raw' in symbol_data: del symbol_data['ohlcv_1h_raw']
|
| 285 |
final_candidates_with_scores.append(symbol_data)
|
| 286 |
|
| 287 |
-
print(f"🎯 اكتملت الغربلة (V9.
|
| 288 |
|
| 289 |
if final_candidates_with_scores:
|
| 290 |
final_candidates_with_scores.sort(key=lambda x: x['layer1_score'], reverse=True)
|
|
@@ -292,7 +285,7 @@ class DataManager:
|
|
| 292 |
for k, candidate in enumerate(final_candidates_with_scores[:5]):
|
| 293 |
print(f" {k+1}. {candidate['symbol']}: (Score: {candidate.get('layer1_score'):.3f})")
|
| 294 |
else:
|
| 295 |
-
print("⚠️ [V9.
|
| 296 |
|
| 297 |
return final_candidates_with_scores[:20]
|
| 298 |
|
|
@@ -508,4 +501,4 @@ class DataManager:
|
|
| 508 |
except Exception as e:
|
| 509 |
return {'action': 'HOLD', 'confidence': 0.3, 'reason': f'Error: {str(e)}', 'source': 'whale_analysis'}
|
| 510 |
|
| 511 |
-
print("✅ DataManager loaded - V9.
|
|
|
|
| 1 |
+
# data_manager.py (Updated to V9.8 - Sniper Threshold 60%)
|
| 2 |
import os
|
| 3 |
import asyncio
|
| 4 |
import httpx
|
|
|
|
| 66 |
print(f"❌ [DataManager] فشل تهيئة محرك الأنماط V8: {e}")
|
| 67 |
self.pattern_analyzer = ChartPatternAnalyzer(r2_service=None)
|
| 68 |
|
| 69 |
+
# (تهيئة "العقل الذكي" - نموذج الرانكر V9.8)
|
| 70 |
+
print(" > [DataManager] تهيئة الكاشف المصغر (Layer1 Ranker V9.8)...")
|
|
|
|
| 71 |
try:
|
| 72 |
+
# (تأكد من أن النموذج V9.8 موجود ومسمى بهذا الاسم)
|
| 73 |
model_file_path = "ml_models/layer1_ranker.lgbm"
|
| 74 |
self.layer1_ranker = Layer1Ranker(model_path=model_file_path)
|
| 75 |
await self.layer1_ranker.initialize()
|
| 76 |
if self.layer1_ranker.model is None:
|
| 77 |
+
print(" ⚠️ [DataManager V9.8] الرانكر في وضع 'وهمي' (Placeholder).")
|
| 78 |
else:
|
| 79 |
+
print(f" ✅ [DataManager V9.8] الرانكر {self.layer1_ranker.model_name} جاهز للعمل.")
|
| 80 |
except Exception as e:
|
| 81 |
+
print(f"❌ [DataManager V9.8] فشل تهيئة الرانكر V9.8: {e}")
|
| 82 |
self.layer1_ranker = None
|
|
|
|
| 83 |
|
| 84 |
+
print("✅ DataManager initialized - V9.8 (Sniper Threshold 60%)")
|
| 85 |
|
| 86 |
async def _load_markets(self):
|
| 87 |
try:
|
|
|
|
| 189 |
|
| 190 |
async def layer1_rapid_screening(self) -> List[Dict[str, Any]]:
|
| 191 |
"""
|
| 192 |
+
الطبقة 1: فحص سريع - (محدث V9.8)
|
| 193 |
+
- استخدام عتبة القناص 60% (0.60).
|
| 194 |
"""
|
| 195 |
+
print("📊 الطبقة 1 (V9.8 - Sniper Threshold 60%): بدء الغربلة...")
|
| 196 |
|
| 197 |
if not self.layer1_ranker:
|
| 198 |
+
print("❌ [V9.8] الرانكر غير مهيأ. إيقاف الغربلة.")
|
| 199 |
return []
|
| 200 |
|
| 201 |
# الخطوة 1: جلب أفضل 100 عملة حسب الحجم
|
| 202 |
volume_data = await self._get_volume_data_optimal()
|
| 203 |
if not volume_data: volume_data = await self._get_volume_data_direct_api()
|
| 204 |
if not volume_data:
|
| 205 |
+
print("❌ [V9.8] فشل جلب بيانات الأحجام.")
|
| 206 |
return []
|
| 207 |
|
| 208 |
volume_data.sort(key=lambda x: x['dollar_volume'], reverse=True)
|
| 209 |
top_100_by_volume = volume_data[:100]
|
| 210 |
+
print(f"✅ [V9.8] تم تحديد أفضل {len(top_100_by_volume)} عملة. بدء حساب الميزات الذكية...")
|
| 211 |
|
| 212 |
final_candidates_with_scores = []
|
| 213 |
batch_symbols_data = top_100_by_volume
|
|
|
|
| 225 |
valid_symbol_data_for_ranking.append(symbol_data)
|
| 226 |
|
| 227 |
if not valid_symbol_data_for_ranking:
|
| 228 |
+
print("❌ [V9.8] لا توجد عملات صالحة (تحتاج 200 شمعة 1H).")
|
| 229 |
return []
|
| 230 |
|
| 231 |
+
print(f" 🔄 [V9.8] حساب الميزات الذكية لـ {len(valid_symbol_data_for_ranking)} عملة...")
|
| 232 |
|
| 233 |
+
# الخطوة 3: حساب "الميزات الذكية V9.8"
|
| 234 |
all_features_list = []
|
| 235 |
symbols_in_order = []
|
| 236 |
for symbol_data in valid_symbol_data_for_ranking:
|
| 237 |
try:
|
| 238 |
df = self._create_dataframe(symbol_data['ohlcv_1h_raw'])
|
| 239 |
if df.empty: continue
|
| 240 |
+
# (استدعاء العقل الحسابي V9.8)
|
| 241 |
smart_features = self.technical_analyzer.calculate_v9_smart_features(df)
|
| 242 |
if smart_features:
|
| 243 |
all_features_list.append(smart_features)
|
|
|
|
| 245 |
except Exception: pass
|
| 246 |
|
| 247 |
if not all_features_list:
|
| 248 |
+
print("❌ [V9.8] فشل حساب الميزات الذكية.")
|
| 249 |
return []
|
| 250 |
|
| 251 |
# الخطوة 4: التنبؤ (التصنيف)
|
| 252 |
+
print(f" 🧠 [V9.8] إرسال {len(all_features_list)} عملة إلى نموذج الرانكر...")
|
| 253 |
features_dataframe = pd.DataFrame(all_features_list)
|
| 254 |
probabilities = self.layer1_ranker.predict_proba(features_dataframe)
|
| 255 |
|
| 256 |
+
# (الطباعة التشخيصية V9.5 - لا تزال مفيدة)
|
| 257 |
+
print(f" 🔍 [V9.8 DEBUG] تم استلام {len(probabilities)} نتيجة من الرانكر.")
|
|
|
|
| 258 |
debug_scores = []
|
| 259 |
for i, (symbol_data) in enumerate(symbols_in_order):
|
| 260 |
debug_scores.append((symbol_data['symbol'], probabilities[i]))
|
|
|
|
|
|
|
| 261 |
debug_scores.sort(key=lambda x: x[1], reverse=True)
|
| 262 |
+
print(" --- 📋 [V9.8 DEBUG] أعلى 10 درجات خام (قبل الفلترة) ---")
|
|
|
|
|
|
|
| 263 |
for i, (symbol, score) in enumerate(debug_scores[:10]):
|
| 264 |
+
print(f" {i+1}. {symbol}: {score:.4f}")
|
| 265 |
print(" -------------------------------------------------")
|
|
|
|
| 266 |
|
| 267 |
+
# الخطوة 5: تجميع النتائج (مع عتبة القناص 60%)
|
| 268 |
for i, (symbol_data) in enumerate(symbols_in_order):
|
| 269 |
score = probabilities[i]
|
| 270 |
|
| 271 |
+
# 🔴 (V9.8: عتبة القناص 60% - بناءً على نتائجنا) 🔴
|
| 272 |
+
if score >= 0.60:
|
| 273 |
symbol = symbol_data['symbol']
|
| 274 |
+
print(f" ✅ {symbol}: نجح (الاحتمالية: {score:.3f})")
|
| 275 |
symbol_data['layer1_score'] = float(score)
|
| 276 |
+
symbol_data['reasons_for_candidacy'] = ["V9_SMART_RANKER_SNIPER_60"]
|
| 277 |
if 'ohlcv_1h_raw' in symbol_data: del symbol_data['ohlcv_1h_raw']
|
| 278 |
final_candidates_with_scores.append(symbol_data)
|
| 279 |
|
| 280 |
+
print(f"🎯 اكتملت الغربلة (V9.8). تم تأهيل {len(final_candidates_with_scores)} عملة (ثقة >= 60%).")
|
| 281 |
|
| 282 |
if final_candidates_with_scores:
|
| 283 |
final_candidates_with_scores.sort(key=lambda x: x['layer1_score'], reverse=True)
|
|
|
|
| 285 |
for k, candidate in enumerate(final_candidates_with_scores[:5]):
|
| 286 |
print(f" {k+1}. {candidate['symbol']}: (Score: {candidate.get('layer1_score'):.3f})")
|
| 287 |
else:
|
| 288 |
+
print("⚠️ [V9.8] لم تنجح أي عملة في تجاوز عتبة الثقة 60%. (هذا جيد، النظام حذر)")
|
| 289 |
|
| 290 |
return final_candidates_with_scores[:20]
|
| 291 |
|
|
|
|
| 501 |
except Exception as e:
|
| 502 |
return {'action': 'HOLD', 'confidence': 0.3, 'reason': f'Error: {str(e)}', 'source': 'whale_analysis'}
|
| 503 |
|
| 504 |
+
print("✅ DataManager loaded - V9.8 (Sniper Threshold 60%)")
|