Riy777 commited on
Commit
a9383fb
·
verified ·
1 Parent(s): 54a1e58

Update ml_engine/processor.py

Browse files
Files changed (1) hide show
  1. ml_engine/processor.py +70 -47
ml_engine/processor.py CHANGED
@@ -1,10 +1,9 @@
1
  # ml_engine/processor.py
2
- # (V13.10 - GEM-Architect: Full Enterprise Logic + Init Flag Fix)
3
- # - Fully implemented Feature Engineering.
4
- # - Integrated Light MC for Tier 1.
5
- # - Integrated Advanced MC Helper for Tier 2.
6
- # - Detailed Oracle Logging.
7
- # - Added 'initialized' flag to prevent TradeManager crashes.
8
 
9
  import asyncio
10
  import numpy as np
@@ -29,14 +28,14 @@ except ImportError:
29
  class MLProcessor:
30
  """
31
  The Central Neural Engine of Titan.
32
- Coordinates Analysis, Scoring, and Decision Making.
33
  """
34
 
35
  def __init__(self, data_manager):
36
  self.data_manager = data_manager
37
  self.hub_manager = None # Dependency Injection later
38
 
39
- # [CRITICAL FIX] حالة التهيئة لمنع الأخطاء في TradeManager
40
  self.initialized = False
41
 
42
  # تهيئة محرك المحاكاة
@@ -57,12 +56,12 @@ class MLProcessor:
57
 
58
  # إعدادات العتبات (Decision Thresholds)
59
  self.thresholds = {
60
- 'buy_moderate': 0.62, # تم ضبطها لتكون متوازنة
61
  'stop_loss_hard': -0.05,
62
  'take_profit_base': 0.025
63
  }
64
 
65
- print("✅ [MLProcessor V13.10] Enterprise Engine Loaded.")
66
 
67
  async def initialize(self):
68
  """
@@ -76,7 +75,6 @@ class MLProcessor:
76
  print(" -> Analytics Engines: Online")
77
  print(" -> Monte Carlo: Hybrid Mode Active")
78
 
79
- # [CRITICAL FIX] تعيين العلامة لتأكيد الجاهزية
80
  self.initialized = True
81
  print("✅ [MLProcessor] Initialization Complete.")
82
 
@@ -146,34 +144,76 @@ class MLProcessor:
146
  return result_package
147
 
148
  except Exception as e:
149
- # logger.error(f"Error processing {symbol}: {e}")
150
  return None
151
 
152
  # ==============================================================================
153
- # 🔬 Layer 2.5: Advanced Helper (Called by App.py)
154
  # ==============================================================================
155
 
156
  async def run_advanced_monte_carlo(self, symbol: str, timeframe: str = '1h') -> float:
157
- """
158
- تشغيل المحاكاة المتقدمة لأفضل المرشحين فقط.
159
- """
160
  try:
161
- # جلب بيانات تاريخية أطول (500 شمعة)
162
  ohlcv = await self.data_manager.get_latest_ohlcv(symbol, timeframe, limit=500)
163
  if not ohlcv or len(ohlcv) < 100:
164
  return 0.0
165
-
166
  prices = [c[4] for c in ohlcv]
167
-
168
- # استدعاء المحرك المتقدم
169
  adv_score = self.mc_engine.run_advanced_simulation(prices, num_simulations=3000, time_horizon=24)
170
-
171
  return adv_score
172
-
173
  except Exception as e:
174
  print(f"⚠️ [Processor] Advanced MC Error ({symbol}): {e}")
175
  return 0.0
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  # ==============================================================================
178
  # ⚙️ Internal Logic: Indicators & Features
179
  # ==============================================================================
@@ -217,10 +257,10 @@ class MLProcessor:
217
 
218
  score = 0.5
219
 
220
- # Logic: Buy dips in uptrends, avoid extremes
221
  if 30 < rsi < 70: score = 0.6
222
- elif rsi <= 30: score = 0.8 # Oversold Opportunity
223
- elif rsi >= 75: score = 0.3 # Overbought Risk
224
 
225
  # Trend Confirmation
226
  sma_20 = np.mean(closes[-20:]) if len(closes) >= 20 else np.mean(closes)
@@ -230,21 +270,17 @@ class MLProcessor:
230
  except: return 0.5
231
 
232
  def _calculate_pattern_score(self, features: Dict) -> float:
233
- """Pattern Logic: Volume & Breakouts"""
234
  try:
235
  volumes = features.get('volumes_15m')
236
- closes = features.get('closes_15m')
237
  if volumes is None or len(volumes) < 20: return 0.5
238
 
239
  avg_vol = np.mean(volumes[:-5])
240
  curr_vol = np.mean(volumes[-3:])
241
 
242
  score = 0.5
243
-
244
- # Volume Spikes
245
  if curr_vol > avg_vol * 2.5: score = 0.9
246
  elif curr_vol > avg_vol * 1.5: score = 0.7
247
- elif curr_vol < avg_vol * 0.5: score = 0.3
248
 
249
  return max(0.0, min(1.0, score))
250
  except: return 0.5
@@ -253,10 +289,8 @@ class MLProcessor:
253
  try:
254
  closes = features.get('closes_15m')
255
  if closes is None: return "Unknown"
256
-
257
  log_returns = np.diff(np.log(closes))
258
  volatility = np.std(log_returns)
259
-
260
  if volatility > 0.02: return "High"
261
  if volatility > 0.01: return "Medium"
262
  return "Low"
@@ -267,9 +301,7 @@ class MLProcessor:
267
  # ==============================================================================
268
 
269
  def consult_guardian(self, d1, d5, d15, entry_price):
270
- """
271
- Guardian Logic for Open Trades.
272
- """
273
  try:
274
  if not d1 or len(d1) == 0:
275
  return {'action': 'HOLD', 'reason': 'No Data'}
@@ -283,7 +315,7 @@ class MLProcessor:
283
  if pnl_pct < self.thresholds['stop_loss_hard']:
284
  return {'action': 'EXIT_HARD', 'reason': f'Stop Loss ({pnl_pct*100:.2f}%)'}
285
 
286
- # Take Profit / Trailing
287
  if pnl_pct > 0.05:
288
  return {'action': 'EXIT_PARTIAL', 'reason': 'Secure Profit (>5%)'}
289
 
@@ -294,20 +326,15 @@ class MLProcessor:
294
  return {'action': 'HOLD', 'reason': 'Guardian Error'}
295
 
296
  async def consult_oracle(self, signal):
297
- """
298
- Oracle Logic for New Signals (Verbose).
299
- """
300
  try:
301
  symbol = signal.get('symbol', 'UNKNOWN')
302
  conf = signal.get('enhanced_final_score', 0.0)
303
  price = signal.get('current_price', 0)
304
 
305
- # العتبة المحددة
306
  threshold = self.thresholds['buy_moderate']
307
 
308
- # [LOGIC] القرار
309
  if conf >= threshold:
310
- # أهداف ديناميكية بسيطة
311
  tp = price * 1.03
312
  sl = price * 0.975
313
 
@@ -320,12 +347,8 @@ class MLProcessor:
320
  'reason': f'Approved (Score {conf:.2f})'
321
  }
322
 
323
- # في حال الرفض، سجل السبب
324
  print(f" 🔮 [Oracle] REJECTED {symbol}: Score {conf:.2f} < {threshold}")
325
- return {
326
- 'action': 'IGNORE',
327
- 'reason': f'Score {conf:.2f} < {threshold}'
328
- }
329
 
330
  except Exception as e:
331
  print(f"⚠️ [Oracle] Critical Error: {e}")
 
1
  # ml_engine/processor.py
2
+ # (V13.11 - GEM-Architect: Full Enterprise Logic + Sniper Entry Fix)
3
+ # - Features: Feature Engineering, Titan Score, Pattern Score.
4
+ # - Stats: Hybrid Monte Carlo (Light + Advanced).
5
+ # - Decisions: Guardian (Open Trades), Oracle (Filtering).
6
+ # - Execution: Sniper Entry Check (Micro-structure analysis) [ADDED].
 
7
 
8
  import asyncio
9
  import numpy as np
 
28
  class MLProcessor:
29
  """
30
  The Central Neural Engine of Titan.
31
+ Coordinates Analysis, Scoring, Decision Making, and Execution Timing.
32
  """
33
 
34
  def __init__(self, data_manager):
35
  self.data_manager = data_manager
36
  self.hub_manager = None # Dependency Injection later
37
 
38
+ # حالة التهيئة
39
  self.initialized = False
40
 
41
  # تهيئة محرك المحاكاة
 
56
 
57
  # إعدادات العتبات (Decision Thresholds)
58
  self.thresholds = {
59
+ 'buy_moderate': 0.62,
60
  'stop_loss_hard': -0.05,
61
  'take_profit_base': 0.025
62
  }
63
 
64
+ print("✅ [MLProcessor V13.11] Enterprise Engine Loaded (Sniper Ready).")
65
 
66
  async def initialize(self):
67
  """
 
75
  print(" -> Analytics Engines: Online")
76
  print(" -> Monte Carlo: Hybrid Mode Active")
77
 
 
78
  self.initialized = True
79
  print("✅ [MLProcessor] Initialization Complete.")
80
 
 
144
  return result_package
145
 
146
  except Exception as e:
 
147
  return None
148
 
149
  # ==============================================================================
150
+ # 🔬 Layer 2.5 & L4: Advanced Helpers & Sniper Check
151
  # ==============================================================================
152
 
153
  async def run_advanced_monte_carlo(self, symbol: str, timeframe: str = '1h') -> float:
154
+ """تشغيل المحاكاة المتقدمة لأفضل المرشحين فقط (L2.5)."""
 
 
155
  try:
 
156
  ohlcv = await self.data_manager.get_latest_ohlcv(symbol, timeframe, limit=500)
157
  if not ohlcv or len(ohlcv) < 100:
158
  return 0.0
 
159
  prices = [c[4] for c in ohlcv]
 
 
160
  adv_score = self.mc_engine.run_advanced_simulation(prices, num_simulations=3000, time_horizon=24)
 
161
  return adv_score
 
162
  except Exception as e:
163
  print(f"⚠️ [Processor] Advanced MC Error ({symbol}): {e}")
164
  return 0.0
165
 
166
+ async def check_sniper_entry(self, ohlcv_1m: List[list], order_book: Dict) -> bool:
167
+ """
168
+ [L4 SNIPER LOGIC] - الدالة المضافة
169
+ فحص دقيق قبل التنفيذ مباشرة. تتأكد من:
170
+ 1. السبريد (Spread) مقبول.
171
+ 2. لا توجد جدران بيع ضخمة (Sell Walls).
172
+ 3. الزخم اللحظي (1m Momentum) ليس سلبياً بحدة.
173
+ """
174
+ try:
175
+ # 1. فحص دفتر الطلبات (Order Book)
176
+ bids = order_book.get('bids', [])
177
+ asks = order_book.get('asks', [])
178
+
179
+ if not bids or not asks:
180
+ # إذا لم تتوفر البيانات، نسمح بالدخول (Fail Open) لتجنب تفويت الفرص بسبب نقص البيانات اللحظي
181
+ return True
182
+
183
+ best_bid = float(bids[0][0])
184
+ best_ask = float(asks[0][0])
185
+
186
+ # حساب السبريد
187
+ spread_pct = (best_ask - best_bid) / best_bid
188
+ if spread_pct > 0.01: # 1% Spread is too high for scalping
189
+ print(f" ⚠️ [Sniper] High Spread: {spread_pct*100:.2f}%")
190
+ return False
191
+
192
+ # فحص جدار البيع (Sell Wall)
193
+ # نجمع حجم أول 5 طلبات
194
+ bid_vol = sum([b[1] for b in bids[:5]])
195
+ ask_vol = sum([a[1] for a in asks[:5]])
196
+
197
+ if ask_vol > bid_vol * 4: # ضغط بيع هائل في الواجهة
198
+ print(f" ⚠️ [Sniper] Sell Wall Detected (Ratio 1:{ask_vol/bid_vol:.1f})")
199
+ return False
200
+
201
+ # 2. فحص الزخم اللحظي (1m Candles)
202
+ if ohlcv_1m and len(ohlcv_1m) >= 3:
203
+ closes = [c[4] for c in ohlcv_1m]
204
+ # إذا كانت آخر شمعتين هبوط حاد، ننتظر قليلاً
205
+ # (بسيط جداً حالياً، يمكن تعقيده)
206
+ change_last_2m = (closes[-1] - closes[-3]) / closes[-3]
207
+ if change_last_2m < -0.015: # هبوط 1.5% في دقيقتين
208
+ print(" ⚠️ [Sniper] Falling Knife detected (-1.5% in 2m)")
209
+ return False
210
+
211
+ return True # Entry Approved
212
+
213
+ except Exception as e:
214
+ print(f"⚠️ [Sniper] Check Error: {e}. Proceeding cautiously.")
215
+ return True # السماح بالدخول في حالة الخطأ (Fail Open)
216
+
217
  # ==============================================================================
218
  # ⚙️ Internal Logic: Indicators & Features
219
  # ==============================================================================
 
257
 
258
  score = 0.5
259
 
260
+ # Logic: Buy dips in uptrends
261
  if 30 < rsi < 70: score = 0.6
262
+ elif rsi <= 30: score = 0.8 # Oversold
263
+ elif rsi >= 75: score = 0.3 # Overbought
264
 
265
  # Trend Confirmation
266
  sma_20 = np.mean(closes[-20:]) if len(closes) >= 20 else np.mean(closes)
 
270
  except: return 0.5
271
 
272
  def _calculate_pattern_score(self, features: Dict) -> float:
273
+ """Pattern Logic: Volume"""
274
  try:
275
  volumes = features.get('volumes_15m')
 
276
  if volumes is None or len(volumes) < 20: return 0.5
277
 
278
  avg_vol = np.mean(volumes[:-5])
279
  curr_vol = np.mean(volumes[-3:])
280
 
281
  score = 0.5
 
 
282
  if curr_vol > avg_vol * 2.5: score = 0.9
283
  elif curr_vol > avg_vol * 1.5: score = 0.7
 
284
 
285
  return max(0.0, min(1.0, score))
286
  except: return 0.5
 
289
  try:
290
  closes = features.get('closes_15m')
291
  if closes is None: return "Unknown"
 
292
  log_returns = np.diff(np.log(closes))
293
  volatility = np.std(log_returns)
 
294
  if volatility > 0.02: return "High"
295
  if volatility > 0.01: return "Medium"
296
  return "Low"
 
301
  # ==============================================================================
302
 
303
  def consult_guardian(self, d1, d5, d15, entry_price):
304
+ """Guardian Logic for Open Trades."""
 
 
305
  try:
306
  if not d1 or len(d1) == 0:
307
  return {'action': 'HOLD', 'reason': 'No Data'}
 
315
  if pnl_pct < self.thresholds['stop_loss_hard']:
316
  return {'action': 'EXIT_HARD', 'reason': f'Stop Loss ({pnl_pct*100:.2f}%)'}
317
 
318
+ # Take Profit
319
  if pnl_pct > 0.05:
320
  return {'action': 'EXIT_PARTIAL', 'reason': 'Secure Profit (>5%)'}
321
 
 
326
  return {'action': 'HOLD', 'reason': 'Guardian Error'}
327
 
328
  async def consult_oracle(self, signal):
329
+ """Oracle Logic for New Signals."""
 
 
330
  try:
331
  symbol = signal.get('symbol', 'UNKNOWN')
332
  conf = signal.get('enhanced_final_score', 0.0)
333
  price = signal.get('current_price', 0)
334
 
 
335
  threshold = self.thresholds['buy_moderate']
336
 
 
337
  if conf >= threshold:
 
338
  tp = price * 1.03
339
  sl = price * 0.975
340
 
 
347
  'reason': f'Approved (Score {conf:.2f})'
348
  }
349
 
 
350
  print(f" 🔮 [Oracle] REJECTED {symbol}: Score {conf:.2f} < {threshold}")
351
+ return {'action': 'IGNORE', 'reason': f'Score {conf:.2f} < {threshold}'}
 
 
 
352
 
353
  except Exception as e:
354
  print(f"⚠️ [Oracle] Critical Error: {e}")