Riy777 commited on
Commit
edb7934
·
verified ·
1 Parent(s): fa7f154

Update trade_manager.py

Browse files
Files changed (1) hide show
  1. trade_manager.py +13 -14
trade_manager.py CHANGED
@@ -1,4 +1,4 @@
1
- # trade_manager.py (V22.3 - GEM-Architect: Single Wallet / Multi-Stats View)
2
  import asyncio
3
  import json
4
  import uuid
@@ -41,7 +41,7 @@ class TradeManager:
41
 
42
  self.execution_lock = asyncio.Lock()
43
 
44
- print(f"🛡️ [TradeManager V22.3] Initialized. Single Wallet. Multi-View Stats.")
45
 
46
  async def initialize_sentry_exchanges(self):
47
  print("🛡️ [TradeManager] Initializing and syncing state with R2...")
@@ -296,17 +296,16 @@ class TradeManager:
296
  break
297
 
298
  # 🚀 3. [ 🧠 ] Hybrid DeepSteward (V2 + V3 Dual Brain)
299
-
300
  if self.deep_steward and self.deep_steward.initialized:
301
- # جلب البيانات بشكل متوازي
302
- task1 = self.data_manager.get_latest_ohlcv(symbol, '1m', 120)
303
- task5 = self.data_manager.get_latest_ohlcv(symbol, '5m', 100)
304
- task15 = self.data_manager.get_latest_ohlcv(symbol, '15m', 50)
305
 
306
  d1, d5, d15 = await asyncio.gather(task1, task5, task15)
307
 
308
- # التأكد من وجود بيانات كافية
309
- if d1 and d5 and d15 and len(d1) >= 64: # V2 needs 64
310
  # استشارة الحارس الهجين
311
  decision = self.deep_steward.analyze_position(d1, d5, d15, trade['entry_price'])
312
  action = decision.get('action', 'HOLD')
@@ -320,12 +319,13 @@ class TradeManager:
320
 
321
  elif action == 'EXIT_SOFT':
322
  print(f"🤖 [Hybrid Guardian] ⚠️ تحذير خروج! {decision['reason']}")
 
323
  async with self.execution_lock:
324
  await self._execute_exit(symbol, current_price, "AI_SOFT_EXIT", ai_scores=scores)
325
  break
326
 
327
  else:
328
- print(f"⚠️ [Sentry] بيانات الحارس الهجين غير مكتملة لـ {symbol}. تخطي الدورة.")
329
  else:
330
  pass # لا يوجد نموذج فعال
331
 
@@ -356,7 +356,7 @@ class TradeManager:
356
  self.ai_stats[key]["missed"] += abs(usd_impact)
357
 
358
  async def _analyze_after_exit_task(self, symbol, exit_price, exit_time, position_size_usd, ai_scores):
359
- # ⏳ ننتظر 15 دقيقة
360
  await asyncio.sleep(900)
361
 
362
  try:
@@ -398,10 +398,10 @@ class TradeManager:
398
  }
399
 
400
  await self.r2.append_deep_steward_audit(audit_record)
401
- print(f"👻 [Ghost Monitor] {symbol}: {audit_record['verdict']} | Hybrid: Updated, V2/V3 Updated based on contribution.")
402
 
403
  except Exception as e:
404
- print(f"⚠️ [Ghost Error] {symbol}: {e}")
405
 
406
  # ==============================================================================
407
  # 🔴 دالة الخروج المحاسبية (Accounting Exit Logic)
@@ -464,7 +464,6 @@ class TradeManager:
464
  print(f"✅ [EXIT EXECUTED] {symbol} | Reason: {reason} | PnL: {profit_pct_display:.2f}% (${pnl_usd:.2f}) | New Cap: ${new_capital:.2f}")
465
 
466
  # 6. [ 🧠 ] إطلاق المراقب الشبحي إذا كان الخروج بسبب الذكاء الاصطناعي
467
- # نمرر حجم الصفقة (current_capital) لحساب الأثر الحقيقي بالدولار
468
  if "AI_" in reason:
469
  self._launch_post_exit_analysis(symbol, exit_price, trade['exit_time'], current_capital, ai_scores)
470
 
 
1
+ # trade_manager.py (V22.4 - GEM-Architect: Full Logic + Multi-Stats + 300 Candle Fix)
2
  import asyncio
3
  import json
4
  import uuid
 
41
 
42
  self.execution_lock = asyncio.Lock()
43
 
44
+ print(f"🛡️ [TradeManager V22.4] Initialized. Hybrid Guardian Active (Limit: 300).")
45
 
46
  async def initialize_sentry_exchanges(self):
47
  print("🛡️ [TradeManager] Initializing and syncing state with R2...")
 
296
  break
297
 
298
  # 🚀 3. [ 🧠 ] Hybrid DeepSteward (V2 + V3 Dual Brain)
 
299
  if self.deep_steward and self.deep_steward.initialized:
300
+ # --- [تعديل جوهري] زيادة الحد إلى 300 شمعة لتجنب أخطاء النموذج ---
301
+ task1 = self.data_manager.get_latest_ohlcv(symbol, '1m', 300)
302
+ task5 = self.data_manager.get_latest_ohlcv(symbol, '5m', 200)
303
+ task15 = self.data_manager.get_latest_ohlcv(symbol, '15m', 100)
304
 
305
  d1, d5, d15 = await asyncio.gather(task1, task5, task15)
306
 
307
+ # التأكد من وجود بيانات كافية (V2 يحتاج 64 شمعة، المؤشرات تحتاج 200)
308
+ if d1 and d5 and d15 and len(d1) >= 200:
309
  # استشارة الحارس الهجين
310
  decision = self.deep_steward.analyze_position(d1, d5, d15, trade['entry_price'])
311
  action = decision.get('action', 'HOLD')
 
319
 
320
  elif action == 'EXIT_SOFT':
321
  print(f"🤖 [Hybrid Guardian] ⚠️ تحذير خروج! {decision['reason']}")
322
+ # هنا يمكن إضافة منطق لتشديد الستوب بدلاً من الخروج المباشر
323
  async with self.execution_lock:
324
  await self._execute_exit(symbol, current_price, "AI_SOFT_EXIT", ai_scores=scores)
325
  break
326
 
327
  else:
328
+ print(f"⚠️ [Sentry] بيانات الحارس الهجين غير مكتملة لـ {symbol} (Rows: {len(d1) if d1 else 0}). تخطي الدورة.")
329
  else:
330
  pass # لا يوجد نموذج فعال
331
 
 
356
  self.ai_stats[key]["missed"] += abs(usd_impact)
357
 
358
  async def _analyze_after_exit_task(self, symbol, exit_price, exit_time, position_size_usd, ai_scores):
359
+ # ⏳ ننتظر 15 دقيقة (900 ثانية)
360
  await asyncio.sleep(900)
361
 
362
  try:
 
398
  }
399
 
400
  await self.r2.append_deep_steward_audit(audit_record)
401
+ print(f"👻 [Ghost Monitor] {symbol}: {audit_record['verdict']} | Impact: ${usd_impact:.2f}")
402
 
403
  except Exception as e:
404
+ print(f"⚠️ [Ghost Error] فشل تحليل ما بعد الخروج لـ {symbol}: {e}")
405
 
406
  # ==============================================================================
407
  # 🔴 دالة الخروج المحاسبية (Accounting Exit Logic)
 
464
  print(f"✅ [EXIT EXECUTED] {symbol} | Reason: {reason} | PnL: {profit_pct_display:.2f}% (${pnl_usd:.2f}) | New Cap: ${new_capital:.2f}")
465
 
466
  # 6. [ 🧠 ] إطلاق المراقب الشبحي إذا كان الخروج بسبب الذكاء الاصطناعي
 
467
  if "AI_" in reason:
468
  self._launch_post_exit_analysis(symbol, exit_price, trade['exit_time'], current_capital, ai_scores)
469