Riy777 commited on
Commit
d0d5e70
·
verified ·
1 Parent(s): c4d8943

Update trade_manager.py

Browse files
Files changed (1) hide show
  1. trade_manager.py +18 -14
trade_manager.py CHANGED
@@ -1,4 +1,4 @@
1
- # trade_manager.py (V23.9 - GEM-Architect: KeyError Fix)
2
 
3
  import asyncio
4
  import uuid
@@ -26,7 +26,7 @@ class TradeManager:
26
  }
27
 
28
  self.execution_lock = asyncio.Lock()
29
- print(f"🛡️ [TradeManager V23.9] Initialized (KeyError Fixed).")
30
 
31
  async def initialize_sentry_exchanges(self):
32
  print("🛡️ [TradeManager] Syncing state with R2...")
@@ -60,8 +60,8 @@ class TradeManager:
60
  if symbol in self.open_positions or symbol in self.watchlist:
61
  continue
62
 
63
- # جلب البيانات (Parallel Fetch)
64
- ohlcv_task = self.data_manager.get_latest_ohlcv(symbol, '1m', 600)
65
  ob_task = self.data_manager.get_order_book_snapshot(symbol)
66
  ohlcv_1m, order_book = await asyncio.gather(ohlcv_task, ob_task)
67
 
@@ -69,7 +69,6 @@ class TradeManager:
69
  print(f" -> [L4 Skip] {symbol} (Not enough data).")
70
  continue
71
 
72
- # استشارة القناص
73
  sniper_result = await self.processor.check_sniper_entry(ohlcv_1m, order_book)
74
 
75
  reason_str = sniper_result.get('reason', 'N/A')
@@ -87,7 +86,6 @@ class TradeManager:
87
  print(" -> [L4 Result] No candidates passed the Sniper check.")
88
  return
89
 
90
- # ✅ [FIXED HERE] استخدام الاسم الصحيح 'enhanced_final_score' واستخدام .get للحماية
91
  sniper_candidates.sort(
92
  key=lambda x: (x.get('sniper_confidence', 0), x.get('enhanced_final_score', 0)),
93
  reverse=True
@@ -129,10 +127,7 @@ class TradeManager:
129
  'sl_price': sl_price,
130
  'last_update': datetime.now().isoformat(),
131
  'strategy': 'Hybrid_Titan_V1',
132
-
133
- # ✅ [FIXED HERE] استخدام الاسم الصحيح للحفظ في السجل
134
  'l1_score': float(signal_data.get('enhanced_final_score', 0.0)),
135
-
136
  'l2_sniper_confidence': float(l2_result.get('confidence_prob', 0.0)),
137
  'decision_data': {
138
  'components': signal_data.get('components', {}),
@@ -161,7 +156,7 @@ class TradeManager:
161
  traceback.print_exc()
162
 
163
  # ==============================================================================
164
- # 🛡️ Hybrid Sentry
165
  # ==============================================================================
166
  async def _guardian_loop(self, symbol: str):
167
  print(f"🛡️ [Sentry] Guarding {symbol}...")
@@ -176,6 +171,7 @@ class TradeManager:
176
  current_price = await self.data_manager.get_latest_price_async(symbol)
177
  if current_price == 0.0: continue
178
 
 
179
  if current_price >= trade['tp_price']:
180
  async with self.execution_lock:
181
  await self._execute_exit(symbol, current_price, "TP_HIT_HARD")
@@ -185,17 +181,25 @@ class TradeManager:
185
  await self._execute_exit(symbol, current_price, "SL_HIT_HARD")
186
  break
187
 
 
188
  if time.time() - last_ai_check_time > 60:
189
- t1 = self.data_manager.get_latest_ohlcv(symbol, '1m', 300)
190
- t5 = self.data_manager.get_latest_ohlcv(symbol, '5m', 200)
191
- t15 = self.data_manager.get_latest_ohlcv(symbol, '15m', 100)
 
192
  d1, d5, d15 = await asyncio.gather(t1, t5, t15)
193
 
194
- if d1 and d5 and d15 and len(d1) >= 200:
 
195
  decision = self.processor.consult_guardian(d1, d5, d15, trade['entry_price'])
196
  action = decision.get('action', 'HOLD')
197
  scores = decision.get('scores', {})
198
 
 
 
 
 
 
199
  if action in ['EXIT_HARD', 'EXIT_SOFT']:
200
  print(f"🤖 [Guardian] {action}: {decision.get('reason')}")
201
  async with self.execution_lock:
 
1
+ # trade_manager.py (V23.10 - GEM-Architect: Data Depth Fix)
2
 
3
  import asyncio
4
  import uuid
 
26
  }
27
 
28
  self.execution_lock = asyncio.Lock()
29
+ print(f"🛡️ [TradeManager V23.10] Initialized (Data Depth Boosted).")
30
 
31
  async def initialize_sentry_exchanges(self):
32
  print("🛡️ [TradeManager] Syncing state with R2...")
 
60
  if symbol in self.open_positions or symbol in self.watchlist:
61
  continue
62
 
63
+ # [FIX] زيادة البيانات هنا أيضاً للقناص
64
+ ohlcv_task = self.data_manager.get_latest_ohlcv(symbol, '1m', 1000)
65
  ob_task = self.data_manager.get_order_book_snapshot(symbol)
66
  ohlcv_1m, order_book = await asyncio.gather(ohlcv_task, ob_task)
67
 
 
69
  print(f" -> [L4 Skip] {symbol} (Not enough data).")
70
  continue
71
 
 
72
  sniper_result = await self.processor.check_sniper_entry(ohlcv_1m, order_book)
73
 
74
  reason_str = sniper_result.get('reason', 'N/A')
 
86
  print(" -> [L4 Result] No candidates passed the Sniper check.")
87
  return
88
 
 
89
  sniper_candidates.sort(
90
  key=lambda x: (x.get('sniper_confidence', 0), x.get('enhanced_final_score', 0)),
91
  reverse=True
 
127
  'sl_price': sl_price,
128
  'last_update': datetime.now().isoformat(),
129
  'strategy': 'Hybrid_Titan_V1',
 
 
130
  'l1_score': float(signal_data.get('enhanced_final_score', 0.0)),
 
131
  'l2_sniper_confidence': float(l2_result.get('confidence_prob', 0.0)),
132
  'decision_data': {
133
  'components': signal_data.get('components', {}),
 
156
  traceback.print_exc()
157
 
158
  # ==============================================================================
159
+ # 🛡️ Hybrid Sentry (FIXED SECTION)
160
  # ==============================================================================
161
  async def _guardian_loop(self, symbol: str):
162
  print(f"🛡️ [Sentry] Guarding {symbol}...")
 
171
  current_price = await self.data_manager.get_latest_price_async(symbol)
172
  if current_price == 0.0: continue
173
 
174
+ # Hard TP/SL Check
175
  if current_price >= trade['tp_price']:
176
  async with self.execution_lock:
177
  await self._execute_exit(symbol, current_price, "TP_HIT_HARD")
 
181
  await self._execute_exit(symbol, current_price, "SL_HIT_HARD")
182
  break
183
 
184
+ # AI Guardian Check
185
  if time.time() - last_ai_check_time > 60:
186
+ # [CRITICAL FIX] زيادة عدد الشموع لتجاوز EMA200 Warm-up
187
+ t1 = self.data_manager.get_latest_ohlcv(symbol, '1m', 1000) # كان 300
188
+ t5 = self.data_manager.get_latest_ohlcv(symbol, '5m', 500) # كان 200
189
+ t15 = self.data_manager.get_latest_ohlcv(symbol, '15m', 500) # كان 100
190
  d1, d5, d15 = await asyncio.gather(t1, t5, t15)
191
 
192
+ # التحقق من أن البيانات كافية بعد الحسابات
193
+ if d1 and d5 and d15 and len(d1) >= 500:
194
  decision = self.processor.consult_guardian(d1, d5, d15, trade['entry_price'])
195
  action = decision.get('action', 'HOLD')
196
  scores = decision.get('scores', {})
197
 
198
+ # طباعة التشخيص (اختياري لملف التداول)
199
+ # v2_val = int(scores.get('v2', 0.0) * 100)
200
+ # v3_val = int(scores.get('v3', 0.0) * 100)
201
+ # print(f" 🕵️ [Sentry check] {symbol} | V2:{v2_val} V3:{v3_val} | {action}")
202
+
203
  if action in ['EXIT_HARD', 'EXIT_SOFT']:
204
  print(f"🤖 [Guardian] {action}: {decision.get('reason')}")
205
  async with self.execution_lock: