Trad / ml_engine /patterns.py
Riy777's picture
Update ml_engine/patterns.py
79c68cb
raw
history blame
13 kB
# ml_engine/patterns.py
# (V8.8 - الحل النهائي: إعادة فحوصات السلامة (Column Checks) لجميع المؤشرات)
import pandas as pd
import numpy as np
import joblib
import asyncio
import io
# (يجب التأكد من أن pandas-ta مثبت في بيئة Hugging Face)
try:
import pandas_ta as ta
except ImportError:
print("❌❌ [PatternEngineV8] مكتبة pandas_ta غير موجودة! هذا المحرك سيفشل.")
ta = None
class ChartPatternAnalyzer:
def __init__(self, r2_service=None,
model_key="lgbm_pattern_model_combined.pkl",
scaler_key="scaler_combined.pkl",
window_size=60):
"""
تهيئة المحرك بتحميل النماذج من R2.
"""
self.window_size = window_size
self.model = None
self.scaler = None
self.class_names = ["Bearish Pattern", "Neutral / No Pattern", "Bullish Pattern"]
self.r2_service = r2_service
self.model_key = model_key
self.scaler_key = scaler_key
# (الوصفة V8.7 - 35 عموداً رقمياً)
self.feature_names = [
'open', 'high', 'low', 'close', 'volume',
'RSI_14', 'MACD_12_26_9', 'MACDh_12_26_9', 'MACDs_12_26_9', 'SMA_20',
'EMA_20', 'BBL_5_2.0_2.0', 'BBM_5_2.0_2.0', 'BBU_5_2.0_2.0', 'BBB_5_2.0_2.0',
'BBP_5_2.0_2.0', 'STOCHk_14_3_3', 'STOCHd_14_3_3', 'STOCHh_14_3_3',
'ADX_14', 'ADXR_14_2', 'DMP_14', 'DMN_14', 'VWAP_D', 'MIDPOINT_14',
'TEMA_20', 'OBV', 'AD', 'ATRr_14', 'DPO_20', 'KVO_34_55_13',
'KVOs_34_55_13', 'CMO_14', 'ROC_10', 'WILLR_14'
]
if not self.r2_service:
print("⚠️ [PatternEngineV8] R2Service غير متوفر. يجب التحميل يدوياً.")
async def initialize(self):
"""
يجب استدعاؤها من app.py أو data_manager لتحميل النماذج.
"""
if self.model and self.scaler:
return True
if not self.r2_service:
print("❌ [PatternEngineV8] لا يمكن التهيئة بدون R2 Service.")
return False
try:
print(f" > [PatternEngineV8] تحميل {self.model_key} من R2...")
model_obj = self.r2_service.s3_client.get_object(Bucket=self.r2_service.BUCKET_NAME, Key=self.model_key)
model_bytes = io.BytesIO(model_obj['Body'].read())
self.model = joblib.load(model_bytes)
print(f" > [PatternEngineV8] تحميل {self.scaler_key} من R2...")
scaler_obj = self.r2_service.s3_client.get_object(Bucket=self.r2_service.BUCKET_NAME, Key=self.scaler_key)
scaler_bytes = io.BytesIO(scaler_obj['Body'].read())
self.scaler = joblib.load(scaler_bytes)
print("✅ [PatternEngineV8] تم تحميل النموذج (58%) والمقياس بنجاح.")
if hasattr(self.scaler, 'feature_names_in_'):
print(f" > يتوقع المقياس {len(self.scaler.feature_names_in_)} خاصية.")
if len(self.scaler.feature_names_in_) == len(self.feature_names):
print(" > ✅ (V8.8) عدد الخصائص (35) متطابق مع المقياس.")
else:
print(f" > ⚠️ (V8.8) تحذير: عدم تطابق الخصائص! الكود يتوقع {len(self.feature_names)}, المقياس يتوقع {len(self.scaler.feature_names_in_)}")
return True
except Exception as e:
print(f"❌❌ [PatternEngineV8] فشل فادح في تحميل النماذج من R2: {e}")
self.model = None
self.scaler = None
return False
# 🔴 --- START OF CHANGE (V8.8) --- 🔴
# (إعادة فحوصات السلامة (Column Checks) لجميع المؤشرات)
def _extract_features(self, df_ranged: pd.DataFrame, df_indexed: pd.DataFrame) -> pd.DataFrame:
"""
(الوصفة V8.8 - إرجاع 35 عموداً + فحوصات سلامة كاملة)
"""
if not ta:
raise ImportError("مكتبة pandas-ta غير مثبتة.")
# (1. البدء بآخر صف من البيانات الأساسية)
df_features = df_ranged.iloc[-1:].copy()
# (2. بيانات مفهرسة لـ VWAP)
h_idx = df_indexed['high']
l_idx = df_indexed['low']
c_idx = df_indexed['close']
v_idx = df_indexed['volume']
# (3. بيانات غير مفهرسة (السريعة) لباقي المؤشرات)
c = df_ranged['close']
h = df_ranged['high']
l = df_ranged['low']
v = df_ranged['volume']
try:
# --- حساب الـ 30 مؤشر (مع فحوصات السلامة) ---
# (المؤشرات التي تُرجع سلسلة Series - آمنة نسبياً)
df_features['RSI_14'] = ta.rsi(c, length=14).iloc[-1]
df_features['SMA_20'] = ta.sma(c, length=20).iloc[-1]
df_features['EMA_20'] = ta.ema(c, length=20).iloc[-1]
df_features['MIDPOINT_14'] = ta.midpoint(c, length=14).iloc[-1]
df_features['TEMA_20'] = ta.tema(c, length=20).iloc[-1]
df_features['OBV'] = ta.obv(c, v).iloc[-1]
df_features['AD'] = ta.ad(h, l, c, v).iloc[-1]
df_features['ATRr_14'] = ta.atr(h, l, c, percent=True, length=14).iloc[-1]
df_features['DPO_20'] = ta.dpo(c, length=20).iloc[-1]
df_features['CMO_14'] = ta.cmo(c, length=14).iloc[-1]
df_features['ROC_10'] = ta.roc(c, length=10).iloc[-1]
df_features['WILLR_14'] = ta.willr(h, l, c, length=14).iloc[-1]
# (الاستثناء: VWAP يستخدم بيانات مفهرسة)
vwap_series = ta.vwap(h_idx, l_idx, c_idx, v_idx)
if vwap_series is not None:
df_features['VWAP_D'] = vwap_series.iloc[-1]
# --- (المؤشرات التي تُرجع DataFrame - تحتاج فحص سلامة) ---
macd_data = ta.macd(c, fast=12, slow=26, signal=9)
if macd_data is not None and not macd_data.empty and 'MACD_12_26_9' in macd_data.columns:
df_features['MACD_12_26_9'] = macd_data['MACD_12_26_9'].iloc[-1]
df_features['MACDh_12_26_9'] = macd_data['MACDh_12_26_9'].iloc[-1]
df_features['MACDs_12_26_9'] = macd_data['MACDs_12_26_9'].iloc[-1]
bb_data = ta.bbands(c, length=5, std=2.0)
if bb_data is not None and not bb_data.empty and 'BBL_5_2.0' in bb_data.columns:
df_features['BBL_5_2.0_2.0'] = bb_data['BBL_5_2.0'].iloc[-1]
df_features['BBM_5_2.0_2.0'] = bb_data['BBM_5_2.0'].iloc[-1]
df_features['BBU_5_2.0_2.0'] = bb_data['BBU_5_2.0'].iloc[-1]
df_features['BBB_5_2.0_2.0'] = bb_data['BBB_5_2.0'].iloc[-1]
df_features['BBP_5_2.0_2.0'] = bb_data['BBP_5_2.0'].iloc[-1]
stoch_data = ta.stoch(h, l, c, k=14, d=3, smooth_k=3)
if stoch_data is not None and not stoch_data.empty and 'STOCHk_14_3_3' in stoch_data.columns:
df_features['STOCHk_14_3_3'] = stoch_data['STOCHk_14_3_3'].iloc[-1]
df_features['STOCHd_14_3_3'] = stoch_data['STOCHd_14_3_3'].iloc[-1]
df_features['STOCHh_14_3_3'] = stoch_data['STOCHh_14_3_3'].iloc[-1]
adx_data = ta.adx(h, l, c, length=14, adxr=2)
if adx_data is not None and not adx_data.empty and 'ADX_14' in adx_data.columns:
df_features['ADX_14'] = adx_data['ADX_14'].iloc[-1]
df_features['ADXR_14_2'] = adx_data['ADXR_14_2'].iloc[-1]
df_features['DMP_14'] = adx_data['DMP_14'].iloc[-1]
df_features['DMN_14'] = adx_data['DMN_14'].iloc[-1]
kvo_data = ta.kvo(h, l, c, v, fast=34, slow=55, signal=13)
if kvo_data is not None and not kvo_data.empty and 'KVO_34_55_13' in kvo_data.columns:
df_features['KVO_34_55_13'] = kvo_data['KVO_34_55_13'].iloc[-1]
df_features['KVOs_34_55_13'] = kvo_data['KVOs_34_55_13'].iloc[-1]
except Exception as e:
# (هذا الخطأ يجب ألا يظهر الآن إلا في حالات نادرة جداً)
print(f"❌ [PatternEngineV8.8] خطأ أثناء حساب المؤشرات وظيفياً: {e}")
pass
# --- (نهاية حساب المؤشرات) ---
# (ملء أي قيم مفقودة (NaN) بـ 0 قبل إرسالها للمقياس)
df_features.fillna(0, inplace=True)
# (التأكد من أننا نرسل فقط الـ 35 عموداً التي يتوقعها المقياس)
final_features_df = pd.DataFrame(columns=self.feature_names)
for col in self.feature_names:
if col in df_features:
final_features_df[col] = df_features[col].values
else:
final_features_df[col] = 0
return final_features_df
# 🔴 --- END OF CHANGE (V8.8) --- 🔴
async def detect_chart_patterns(self, ohlcv_data: dict) -> dict:
"""
(الدالة الرئيسية - لا تغيير هنا عن V8.7)
"""
best_match = {
'pattern_detected': 'no_clear_pattern',
'pattern_confidence': 0,
'predicted_direction': 'neutral',
'timeframe': None,
'details': {}
}
if not self.model or not self.scaler:
if not hasattr(self, '_init_warned'):
print("⚠️ [PatternEngineV8] النموذج/المقياس غير محمل. يجب استدعاء .initialize() أولاً.")
self._init_warned = True
return best_match
all_results = []
for timeframe, candles in ohlcv_data.items():
if len(candles) >= max(self.window_size, 200):
try:
window_candles = candles[-200:]
# (1. نسخة غير مفهرسة (RangeIndex 0,1,2...))
df_ranged = pd.DataFrame(window_candles, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
# (2. نسخة مفهرسة (DatetimeIndex))
df_indexed = df_ranged.copy()
df_indexed['timestamp'] = pd.to_datetime(df_indexed['timestamp'], unit='ms')
df_indexed.set_index('timestamp', inplace=True)
# (3. استخراج الخصائص (V8.8))
features_df = self._extract_features(df_ranged, df_indexed)
if features_df is None or features_df.empty:
continue
# (4. تطبيع الخصائص (Scaler))
features_scaled = self.scaler.transform(features_df)
# (5. التنبؤ بالاحتماليات (Probabilities))
probabilities = self.model.predict_proba(features_scaled)[0]
best_class_index = np.argmax(probabilities)
confidence = probabilities[best_class_index]
pattern_name = self.class_names[best_class_index]
if pattern_name != "Neutral / No Pattern" and confidence > 0.5:
all_results.append({
'pattern': pattern_name,
'confidence': float(confidence),
'timeframe': timeframe
})
except Exception as e:
print(f"❌ [PatternEngineV8.8] فشل التنبؤ لـ {timeframe}: {e}")
# (6. اختيار أفضل نمط)
if all_results:
best_result = max(all_results, key=lambda x: x['confidence'])
direction = 'neutral'
if "Bullish" in best_result['pattern']: direction = 'up'
elif "Bearish" in best_result['pattern']: direction = 'down'
best_match['pattern_detected'] = best_result['pattern']
best_match['pattern_confidence'] = best_result['confidence']
best_match['timeframe'] = best_result['timeframe']
best_match['predicted_direction'] = direction
best_match['details'] = {'ml_confidence': best_result['confidence']}
return best_match
print("✅ ML Module: Pattern Engine V8.8 (Robust DataFrame Checks) loaded")