Update ml_engine/strategies.py
Browse files- ml_engine/strategies.py +174 -107
ml_engine/strategies.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# ml_engine/strategies.py
|
| 2 |
import asyncio
|
| 3 |
|
| 4 |
# الاستيراد من الوحدات الداخلية في نفس المجلد
|
|
@@ -48,9 +48,9 @@ class PatternEnhancedStrategyEngine:
|
|
| 48 |
|
| 49 |
if pattern_name in reversal_patterns:
|
| 50 |
if direction == 'down':
|
| 51 |
-
return ['breakout_momentum', 'trend_following']
|
| 52 |
else:
|
| 53 |
-
return ['mean_reversion', 'breakout_momentum']
|
| 54 |
elif pattern_name in continuation_patterns:
|
| 55 |
return ['trend_following', 'breakout_momentum']
|
| 56 |
else:
|
|
@@ -61,10 +61,6 @@ class MultiStrategyEngine:
|
|
| 61 |
self.data_manager = data_manager
|
| 62 |
self.learning_engine = learning_engine
|
| 63 |
|
| 64 |
-
# 🔴 ملاحظة: تم إزالة مُهيِئات (constructors) التحليل غير المستخدمة من هنا
|
| 65 |
-
# الاستراتيجيات تستهلك البيانات المحسوبة مسبقاً من (symbol_data)
|
| 66 |
-
# التي يوفرها (MLProcessor) الرئيسي
|
| 67 |
-
|
| 68 |
self.pattern_enhancer = PatternEnhancedStrategyEngine(data_manager, learning_engine)
|
| 69 |
|
| 70 |
self.strategies = {
|
|
@@ -74,7 +70,7 @@ class MultiStrategyEngine:
|
|
| 74 |
'volume_spike': self._volume_spike_strategy,
|
| 75 |
'whale_tracking': self._whale_tracking_strategy,
|
| 76 |
'pattern_recognition': self._pattern_recognition_strategy,
|
| 77 |
-
'hybrid_ai': self._hybrid_ai_strategy
|
| 78 |
}
|
| 79 |
|
| 80 |
async def evaluate_all_strategies(self, symbol_data, market_context):
|
|
@@ -85,7 +81,6 @@ class MultiStrategyEngine:
|
|
| 85 |
market_condition = market_context.get('market_trend', 'sideways_market')
|
| 86 |
optimized_weights = await self.learning_engine.get_optimized_strategy_weights(market_condition)
|
| 87 |
except Exception as e:
|
| 88 |
-
# ❌ لا نستخدم قيم افتراضية، نستخدم الأوزان الأساسية
|
| 89 |
optimized_weights = await self.get_default_weights()
|
| 90 |
else:
|
| 91 |
optimized_weights = await self.get_default_weights()
|
|
@@ -93,10 +88,14 @@ class MultiStrategyEngine:
|
|
| 93 |
strategy_scores = {}
|
| 94 |
base_scores = {}
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
try:
|
| 98 |
base_score = await strategy_function(symbol_data, market_context)
|
| 99 |
-
if base_score is None:
|
| 100 |
continue
|
| 101 |
base_scores[strategy_name] = base_score
|
| 102 |
weight = optimized_weights.get(strategy_name, 0.1)
|
|
@@ -104,9 +103,19 @@ class MultiStrategyEngine:
|
|
| 104 |
strategy_scores[strategy_name] = min(weighted_score, 1.0)
|
| 105 |
except Exception as error:
|
| 106 |
print(f"❌ خطأ في تقييم استراتيجية {strategy_name}: {error}")
|
| 107 |
-
# ❌ لا نستخدم أي محاكاة أو قيم افتراضية
|
| 108 |
continue
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
pattern_analysis = symbol_data.get('pattern_analysis')
|
| 111 |
if pattern_analysis:
|
| 112 |
strategy_scores = await self.pattern_enhancer.enhance_strategy_with_patterns(
|
|
@@ -124,120 +133,159 @@ class MultiStrategyEngine:
|
|
| 124 |
|
| 125 |
except Exception as error:
|
| 126 |
print(f"❌ خطأ في تقييم الاستراتيجيات: {error}")
|
| 127 |
-
# ❌ لا نستخدم أي محاكاة
|
| 128 |
return {}, {}
|
| 129 |
|
| 130 |
async def get_default_weights(self):
|
| 131 |
-
"""الأوزان الافتراضية للاستراتيجيات
|
| 132 |
return {
|
| 133 |
'trend_following': 0.15,
|
| 134 |
'mean_reversion': 0.12,
|
| 135 |
-
'breakout_momentum': 0.
|
| 136 |
-
'volume_spike': 0.
|
| 137 |
'whale_tracking': 0.20,
|
| 138 |
-
'pattern_recognition': 0.
|
| 139 |
-
'hybrid_ai': 0.10
|
| 140 |
}
|
| 141 |
|
| 142 |
async def _trend_following_strategy(self, symbol_data, market_context):
|
| 143 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 144 |
try:
|
| 145 |
score = 0.0
|
| 146 |
indicators = symbol_data.get('advanced_indicators', {})
|
| 147 |
|
| 148 |
-
|
|
|
|
| 149 |
if timeframe in indicators:
|
| 150 |
-
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
|
|
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
|
|
|
| 163 |
return min(score, 1.0)
|
| 164 |
except Exception as error:
|
| 165 |
print(f"❌ خطأ في استراتيجية تتبع الاتجاه: {error}")
|
| 166 |
-
return None
|
| 167 |
|
| 168 |
def _check_ema_alignment(self, indicators):
|
| 169 |
-
|
| 170 |
required_emas = ['ema_9', 'ema_21', 'ema_50']
|
| 171 |
if all(ema in indicators for ema in required_emas):
|
| 172 |
return (indicators['ema_9'] > indicators['ema_21'] > indicators['ema_50'])
|
| 173 |
return False
|
| 174 |
|
| 175 |
async def _mean_reversion_strategy(self, symbol_data, market_context):
|
| 176 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 177 |
try:
|
| 178 |
score = 0.0
|
| 179 |
current_price = symbol_data['current_price']
|
| 180 |
indicators = symbol_data.get('advanced_indicators', {})
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
position_in_band = (current_price - hourly_indicators['bb_lower']) / (
|
| 187 |
-
hourly_indicators['bb_upper'] - hourly_indicators['bb_lower'])
|
| 188 |
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
|
|
|
| 200 |
return min(score, 1.0)
|
| 201 |
except Exception as error:
|
| 202 |
print(f"❌ خطأ في استراتيجية العودة للمتوسط: {error}")
|
| 203 |
-
return None
|
| 204 |
|
| 205 |
async def _breakout_momentum_strategy(self, symbol_data, market_context):
|
| 206 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 207 |
try:
|
| 208 |
score = 0.0
|
|
|
|
| 209 |
indicators = symbol_data.get('advanced_indicators', {})
|
| 210 |
|
|
|
|
| 211 |
for timeframe in ['1h', '15m', '5m']:
|
| 212 |
if timeframe in indicators:
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
volume_ratio = timeframe_indicators.get('volume_ratio', 0)
|
| 216 |
-
if volume_ratio > 1.8:
|
| 217 |
-
score += 0.25
|
| 218 |
-
elif volume_ratio > 1.3:
|
| 219 |
-
score += 0.15
|
| 220 |
-
|
| 221 |
-
if timeframe_indicators.get('macd_hist', 0) > 0:
|
| 222 |
-
score += 0.20
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
|
|
|
| 234 |
return min(score, 1.0)
|
| 235 |
except Exception as error:
|
| 236 |
print(f"❌ خطأ في استراتيجية زخم الاختراق: {error}")
|
| 237 |
-
return None
|
| 238 |
|
| 239 |
async def _volume_spike_strategy(self, symbol_data, market_context):
|
| 240 |
-
"""
|
|
|
|
|
|
|
| 241 |
try:
|
| 242 |
score = 0.0
|
| 243 |
indicators = symbol_data.get('advanced_indicators', {})
|
|
@@ -245,7 +293,7 @@ class MultiStrategyEngine:
|
|
| 245 |
for timeframe in ['1h', '15m', '5m']:
|
| 246 |
if timeframe in indicators:
|
| 247 |
volume_ratio = indicators[timeframe].get('volume_ratio', 0)
|
| 248 |
-
if volume_ratio > 3.0:
|
| 249 |
score += 0.45
|
| 250 |
elif volume_ratio > 2.0:
|
| 251 |
score += 0.25
|
|
@@ -255,15 +303,18 @@ class MultiStrategyEngine:
|
|
| 255 |
return min(score, 1.0)
|
| 256 |
except Exception as error:
|
| 257 |
print(f"❌ خطأ في استراتيجية ارتفاع الحجم: {error}")
|
| 258 |
-
return None
|
| 259 |
|
| 260 |
async def _whale_tracking_strategy(self, symbol_data, market_context):
|
| 261 |
-
"""
|
|
|
|
|
|
|
| 262 |
try:
|
| 263 |
whale_data = symbol_data.get('whale_data', {})
|
| 264 |
if not whale_data.get('data_available', False):
|
| 265 |
-
return None
|
| 266 |
|
|
|
|
| 267 |
whale_signal = await self.data_manager.get_whale_trading_signal(
|
| 268 |
symbol_data['symbol'], whale_data, market_context
|
| 269 |
)
|
|
@@ -272,63 +323,79 @@ class MultiStrategyEngine:
|
|
| 272 |
confidence = whale_signal.get('confidence', 0)
|
| 273 |
if whale_signal.get('action') in ['STRONG_BUY', 'BUY']:
|
| 274 |
return min(confidence * 1.2, 1.0)
|
| 275 |
-
|
| 276 |
-
return min(confidence * 0.8, 1.0)
|
| 277 |
|
| 278 |
-
return None
|
| 279 |
except Exception as error:
|
| 280 |
print(f"❌ خطأ في استراتيجية تتبع الحيتان: {error}")
|
| 281 |
-
return None
|
| 282 |
|
| 283 |
async def _pattern_recognition_strategy(self, symbol_data, market_context):
|
| 284 |
-
"""
|
|
|
|
|
|
|
| 285 |
try:
|
| 286 |
score = 0.0
|
| 287 |
pattern_analysis = symbol_data.get('pattern_analysis')
|
| 288 |
|
|
|
|
| 289 |
if pattern_analysis and pattern_analysis.get('pattern_confidence', 0) > 0.6:
|
| 290 |
-
|
|
|
|
|
|
|
| 291 |
else:
|
|
|
|
| 292 |
indicators = symbol_data.get('advanced_indicators', {})
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
timeframe_indicators.get('volume_ratio', 0) > 1.5):
|
| 299 |
-
score += 0.35
|
| 300 |
|
| 301 |
return min(score, 1.0)
|
| 302 |
except Exception as error:
|
| 303 |
print(f"❌ خطأ في استراتيجية التعرف على الأنماط: {error}")
|
| 304 |
-
return None
|
| 305 |
|
| 306 |
-
async def _hybrid_ai_strategy(self, symbol_data, market_context):
|
| 307 |
-
"""
|
|
|
|
|
|
|
| 308 |
try:
|
| 309 |
score = 0.0
|
| 310 |
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
|
|
|
| 314 |
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
score += final_score * 0.3
|
| 318 |
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
|
| 324 |
-
|
| 325 |
-
if
|
| 326 |
-
|
| 327 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
return max(0.0, min(score, 1.0))
|
| 330 |
except Exception as error:
|
| 331 |
print(f"❌ خطأ في استراتيجية الهجين الذكية: {error}")
|
| 332 |
-
return None
|
| 333 |
|
| 334 |
-
print("✅ ML Module: Strategy Engine loaded")
|
|
|
|
| 1 |
+
# ml_engine/strategies.py (مُحدَّث بقواعد تحليل محسنة)
|
| 2 |
import asyncio
|
| 3 |
|
| 4 |
# الاستيراد من الوحدات الداخلية في نفس المجلد
|
|
|
|
| 48 |
|
| 49 |
if pattern_name in reversal_patterns:
|
| 50 |
if direction == 'down':
|
| 51 |
+
return ['breakout_momentum', 'trend_following'] # (لصفقات SHORT، لكننا نركز على LONG)
|
| 52 |
else:
|
| 53 |
+
return ['mean_reversion', 'breakout_momentum'] # انعكاس صاعد
|
| 54 |
elif pattern_name in continuation_patterns:
|
| 55 |
return ['trend_following', 'breakout_momentum']
|
| 56 |
else:
|
|
|
|
| 61 |
self.data_manager = data_manager
|
| 62 |
self.learning_engine = learning_engine
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
self.pattern_enhancer = PatternEnhancedStrategyEngine(data_manager, learning_engine)
|
| 65 |
|
| 66 |
self.strategies = {
|
|
|
|
| 70 |
'volume_spike': self._volume_spike_strategy,
|
| 71 |
'whale_tracking': self._whale_tracking_strategy,
|
| 72 |
'pattern_recognition': self._pattern_recognition_strategy,
|
| 73 |
+
'hybrid_ai': self._hybrid_ai_strategy # 🔴 (تم إعادة تصميمها)
|
| 74 |
}
|
| 75 |
|
| 76 |
async def evaluate_all_strategies(self, symbol_data, market_context):
|
|
|
|
| 81 |
market_condition = market_context.get('market_trend', 'sideways_market')
|
| 82 |
optimized_weights = await self.learning_engine.get_optimized_strategy_weights(market_condition)
|
| 83 |
except Exception as e:
|
|
|
|
| 84 |
optimized_weights = await self.get_default_weights()
|
| 85 |
else:
|
| 86 |
optimized_weights = await self.get_default_weights()
|
|
|
|
| 88 |
strategy_scores = {}
|
| 89 |
base_scores = {}
|
| 90 |
|
| 91 |
+
# 🔴 تقييم الاستراتيجيات الأساسية أولاً
|
| 92 |
+
primary_strategies = [s for s in self.strategies.keys() if s != 'hybrid_ai']
|
| 93 |
+
|
| 94 |
+
for strategy_name in primary_strategies:
|
| 95 |
+
strategy_function = self.strategies[strategy_name]
|
| 96 |
try:
|
| 97 |
base_score = await strategy_function(symbol_data, market_context)
|
| 98 |
+
if base_score is None:
|
| 99 |
continue
|
| 100 |
base_scores[strategy_name] = base_score
|
| 101 |
weight = optimized_weights.get(strategy_name, 0.1)
|
|
|
|
| 103 |
strategy_scores[strategy_name] = min(weighted_score, 1.0)
|
| 104 |
except Exception as error:
|
| 105 |
print(f"❌ خطأ في تقييم استراتيجية {strategy_name}: {error}")
|
|
|
|
| 106 |
continue
|
| 107 |
|
| 108 |
+
# 🔴 تقييم استراتيجية hybrid_ai (الميتا) بعد حساب الدرجات الأساسية
|
| 109 |
+
try:
|
| 110 |
+
hybrid_score = await self._hybrid_ai_strategy(symbol_data, market_context, base_scores)
|
| 111 |
+
if hybrid_score is not None:
|
| 112 |
+
base_scores['hybrid_ai'] = hybrid_score
|
| 113 |
+
weight = optimized_weights.get('hybrid_ai', 0.1)
|
| 114 |
+
strategy_scores['hybrid_ai'] = min(hybrid_score * weight, 1.0)
|
| 115 |
+
except Exception as e:
|
| 116 |
+
print(f"❌ خطأ في تقييم استراتيجية hybrid_ai: {e}")
|
| 117 |
+
|
| 118 |
+
# تعزيز الأنماط (كما كان)
|
| 119 |
pattern_analysis = symbol_data.get('pattern_analysis')
|
| 120 |
if pattern_analysis:
|
| 121 |
strategy_scores = await self.pattern_enhancer.enhance_strategy_with_patterns(
|
|
|
|
| 133 |
|
| 134 |
except Exception as error:
|
| 135 |
print(f"❌ خطأ في تقييم الاستراتيجيات: {error}")
|
|
|
|
| 136 |
return {}, {}
|
| 137 |
|
| 138 |
async def get_default_weights(self):
|
| 139 |
+
"""الأوزان الافتراضية للاستراتيجيات"""
|
| 140 |
return {
|
| 141 |
'trend_following': 0.15,
|
| 142 |
'mean_reversion': 0.12,
|
| 143 |
+
'breakout_momentum': 0.20, # زيادة طفيفة
|
| 144 |
+
'volume_spike': 0.13, # زيادة طفيفة
|
| 145 |
'whale_tracking': 0.20,
|
| 146 |
+
'pattern_recognition': 0.10, # تقليل طفيف (يتم تعزيزه بالنمط)
|
| 147 |
+
'hybrid_ai': 0.10 # (الآن استراتيجية ميتا)
|
| 148 |
}
|
| 149 |
|
| 150 |
async def _trend_following_strategy(self, symbol_data, market_context):
|
| 151 |
+
"""
|
| 152 |
+
🔴 (محسنة) استراتيجية تتبع الاتجاه - تركز على الأطر السريعة وقواعد ADX
|
| 153 |
+
القاعدة: EMA(21) > EMA(50) و ADX > 20
|
| 154 |
+
"""
|
| 155 |
try:
|
| 156 |
score = 0.0
|
| 157 |
indicators = symbol_data.get('advanced_indicators', {})
|
| 158 |
|
| 159 |
+
# التركيز على الأطر الزمنية السريعة (1h, 15m, 5m)
|
| 160 |
+
for timeframe in ['1h', '15m', '5m']:
|
| 161 |
if timeframe in indicators:
|
| 162 |
+
tf_indicators = indicators[timeframe]
|
| 163 |
|
| 164 |
+
ema_21 = tf_indicators.get('ema_21')
|
| 165 |
+
ema_50 = tf_indicators.get('ema_50')
|
| 166 |
+
adx = tf_indicators.get('adx', 0)
|
| 167 |
|
| 168 |
+
if ema_21 is not None and ema_50 is not None:
|
| 169 |
+
# 1. EMA(21) > EMA(50) (اتجاه صاعد)
|
| 170 |
+
if ema_21 > ema_50:
|
| 171 |
+
score += 0.2
|
| 172 |
+
|
| 173 |
+
# 2. ADX > 20 (الاتجاه قوي)
|
| 174 |
+
if adx > 20:
|
| 175 |
+
score += 0.1
|
| 176 |
+
|
| 177 |
+
# 3. السعر الحالي فوق المتوسطات (تأكيد إضافي)
|
| 178 |
+
if symbol_data['current_price'] > ema_21:
|
| 179 |
+
score += 0.05
|
| 180 |
|
| 181 |
+
# تعديل الدرجة النهائية (0.35 * 3 = 1.05 كحد أقصى)
|
| 182 |
return min(score, 1.0)
|
| 183 |
except Exception as error:
|
| 184 |
print(f"❌ خطأ في استراتيجية تتبع الاتجاه: {error}")
|
| 185 |
+
return None
|
| 186 |
|
| 187 |
def _check_ema_alignment(self, indicators):
|
| 188 |
+
# (دالة مساعدة - قد لا تكون مستخدمة في المنطق الجديد)
|
| 189 |
required_emas = ['ema_9', 'ema_21', 'ema_50']
|
| 190 |
if all(ema in indicators for ema in required_emas):
|
| 191 |
return (indicators['ema_9'] > indicators['ema_21'] > indicators['ema_50'])
|
| 192 |
return False
|
| 193 |
|
| 194 |
async def _mean_reversion_strategy(self, symbol_data, market_context):
|
| 195 |
+
"""
|
| 196 |
+
🔴 (محسنة) استراتيجية العودة للمتوسط - أطر متعددة وقاعدة "OR"
|
| 197 |
+
القاعدة: RSI < 25 أو السعر تحت -2σ Bollinger
|
| 198 |
+
"""
|
| 199 |
try:
|
| 200 |
score = 0.0
|
| 201 |
current_price = symbol_data['current_price']
|
| 202 |
indicators = symbol_data.get('advanced_indicators', {})
|
| 203 |
|
| 204 |
+
# التركيز على الأطر (1h, 15m)
|
| 205 |
+
for timeframe in ['1h', '15m']:
|
| 206 |
+
if timeframe in indicators:
|
| 207 |
+
tf_indicators = indicators[timeframe]
|
|
|
|
|
|
|
| 208 |
|
| 209 |
+
rsi_value = tf_indicators.get('rsi', 50)
|
| 210 |
+
bb_lower = tf_indicators.get('bb_lower')
|
| 211 |
+
bb_upper = tf_indicators.get('bb_upper') # bb_upper غير مستخدم للشراء، لكن للتحقق
|
| 212 |
+
|
| 213 |
+
if bb_lower is None or bb_upper is None:
|
| 214 |
+
continue # نحتاج بيانات BB
|
| 215 |
+
|
| 216 |
+
# حساب موقع النطاق (0.0 إلى 1.0)
|
| 217 |
+
position_in_band = 0.5 # افتراضي
|
| 218 |
+
if (bb_upper - bb_lower) > 0:
|
| 219 |
+
position_in_band = (current_price - bb_lower) / (bb_upper - bb_lower)
|
| 220 |
+
|
| 221 |
+
# 1. RSI < 25 (تشبع بيع قوي)
|
| 222 |
+
is_rsi_oversold = rsi_value < 25
|
| 223 |
+
|
| 224 |
+
# 2. السعر قرب النطاق السفلي لـ BB (مثال: < 0.1 يعادل تقريباً -2σ)
|
| 225 |
+
is_bb_oversold = position_in_band < 0.1
|
| 226 |
+
|
| 227 |
+
# 3. تطبيق قاعدة "OR"
|
| 228 |
+
if is_rsi_oversold or is_bb_oversold:
|
| 229 |
+
score += 0.4 # درجة قوية
|
| 230 |
+
|
| 231 |
+
# (مكافأة إذا تحققا معاً)
|
| 232 |
+
if is_rsi_oversold and is_bb_oversold:
|
| 233 |
+
score += 0.2
|
| 234 |
|
| 235 |
+
# تعديل الدرجة النهائية (0.6 * 2 = 1.2 كحد أقصى)
|
| 236 |
return min(score, 1.0)
|
| 237 |
except Exception as error:
|
| 238 |
print(f"❌ خطأ في استراتيجية العودة للمتوسط: {error}")
|
| 239 |
+
return None
|
| 240 |
|
| 241 |
async def _breakout_momentum_strategy(self, symbol_data, market_context):
|
| 242 |
+
"""
|
| 243 |
+
🔴 (محسنة) استراتيجية زخم الاختراق - قواعد صارمة (حجم + زخم + تقلب)
|
| 244 |
+
القاعدة: اختراق مع حجم > 1.5 * avg_volume(20) + MACD Hist إيجابي + ATR% كافٍ
|
| 245 |
+
"""
|
| 246 |
try:
|
| 247 |
score = 0.0
|
| 248 |
+
current_price = symbol_data['current_price']
|
| 249 |
indicators = symbol_data.get('advanced_indicators', {})
|
| 250 |
|
| 251 |
+
# التركيز على الأطر (1h, 15m, 5m)
|
| 252 |
for timeframe in ['1h', '15m', '5m']:
|
| 253 |
if timeframe in indicators:
|
| 254 |
+
tf_indicators = indicators[timeframe]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
+
# 1. قاعدة الحجم (volume_ratio هو current_vol / avg_vol)
|
| 257 |
+
volume_ratio = tf_indicators.get('volume_ratio', 0)
|
| 258 |
+
if volume_ratio < 1.5:
|
| 259 |
+
continue # الشرط الأساسي لم يتحقق
|
| 260 |
+
|
| 261 |
+
score += 0.2 # (مكافأة أولية للحجم)
|
| 262 |
+
|
| 263 |
+
# 2. قاعدة الزخم (MACD Histogram إيجابي)
|
| 264 |
+
macd_hist = tf_indicators.get('macd_hist', 0)
|
| 265 |
+
if macd_hist > 0:
|
| 266 |
+
score += 0.1
|
| 267 |
|
| 268 |
+
# 3. قاعدة التقلب (يجب أن يكون هناك تقلب، مثال: ATR% > 1.5%)
|
| 269 |
+
# (نفترض أن 'atr_percent' محسوب في indicators.py)
|
| 270 |
+
atr_percent = tf_indicators.get('atr_percent', 0)
|
| 271 |
+
if atr_percent > 1.5: # 1.5% تقلب على هذا الإطار الزمني
|
| 272 |
+
score += 0.1
|
| 273 |
+
|
| 274 |
+
# 4. السعر فوق VWAP (تأكيد إضافي)
|
| 275 |
+
vwap = tf_indicators.get('vwap')
|
| 276 |
+
if vwap and current_price > vwap:
|
| 277 |
+
score += 0.05
|
| 278 |
|
| 279 |
+
# تعديل الدرجة النهائية (0.45 * 3 = 1.35 كحد أقصى)
|
| 280 |
return min(score, 1.0)
|
| 281 |
except Exception as error:
|
| 282 |
print(f"❌ خطأ في استراتيجية زخم الاختراق: {error}")
|
| 283 |
+
return None
|
| 284 |
|
| 285 |
async def _volume_spike_strategy(self, symbol_data, market_context):
|
| 286 |
+
"""
|
| 287 |
+
(بدون تغيير جوهري) استراتيجية ارتفاع الحجم - (جيدة للسكالبينغ)
|
| 288 |
+
"""
|
| 289 |
try:
|
| 290 |
score = 0.0
|
| 291 |
indicators = symbol_data.get('advanced_indicators', {})
|
|
|
|
| 293 |
for timeframe in ['1h', '15m', '5m']:
|
| 294 |
if timeframe in indicators:
|
| 295 |
volume_ratio = indicators[timeframe].get('volume_ratio', 0)
|
| 296 |
+
if volume_ratio > 3.0: # ارتفاع كبير جداً
|
| 297 |
score += 0.45
|
| 298 |
elif volume_ratio > 2.0:
|
| 299 |
score += 0.25
|
|
|
|
| 303 |
return min(score, 1.0)
|
| 304 |
except Exception as error:
|
| 305 |
print(f"❌ خطأ في استراتيجية ارتفاع الحجم: {error}")
|
| 306 |
+
return None
|
| 307 |
|
| 308 |
async def _whale_tracking_strategy(self, symbol_data, market_context):
|
| 309 |
+
"""
|
| 310 |
+
(بدون تغيير) استراتيجية تتبع الحيتان - (تعتمد على الخدمة الخارجية)
|
| 311 |
+
"""
|
| 312 |
try:
|
| 313 |
whale_data = symbol_data.get('whale_data', {})
|
| 314 |
if not whale_data.get('data_available', False):
|
| 315 |
+
return None
|
| 316 |
|
| 317 |
+
# المنطق مجرد في data_manager/whale_monitor
|
| 318 |
whale_signal = await self.data_manager.get_whale_trading_signal(
|
| 319 |
symbol_data['symbol'], whale_data, market_context
|
| 320 |
)
|
|
|
|
| 323 |
confidence = whale_signal.get('confidence', 0)
|
| 324 |
if whale_signal.get('action') in ['STRONG_BUY', 'BUY']:
|
| 325 |
return min(confidence * 1.2, 1.0)
|
| 326 |
+
# (لا نهتم بالبيع في نظام SPOT)
|
|
|
|
| 327 |
|
| 328 |
+
return None
|
| 329 |
except Exception as error:
|
| 330 |
print(f"❌ خطأ في استراتيجية تتبع الحيتان: {error}")
|
| 331 |
+
return None
|
| 332 |
|
| 333 |
async def _pattern_recognition_strategy(self, symbol_data, market_context):
|
| 334 |
+
"""
|
| 335 |
+
(بدون تغيير) استراتيجية التعرف على الأنماط - (تعتمد على MLProcessor)
|
| 336 |
+
"""
|
| 337 |
try:
|
| 338 |
score = 0.0
|
| 339 |
pattern_analysis = symbol_data.get('pattern_analysis')
|
| 340 |
|
| 341 |
+
# 1. الاعتماد على نتيجة ChartPatternAnalyzer
|
| 342 |
if pattern_analysis and pattern_analysis.get('pattern_confidence', 0) > 0.6:
|
| 343 |
+
# يجب أن يكون النمط صاعداً
|
| 344 |
+
if pattern_analysis.get('predicted_direction') == 'up':
|
| 345 |
+
score += pattern_analysis.get('pattern_confidence', 0) * 0.8
|
| 346 |
else:
|
| 347 |
+
# (منطق احتياطي إذا فشل تحليل النمط)
|
| 348 |
indicators = symbol_data.get('advanced_indicators', {})
|
| 349 |
+
if '1h' in indicators:
|
| 350 |
+
tf_indicators = indicators['1h']
|
| 351 |
+
if (tf_indicators.get('rsi', 50) > 60 and
|
| 352 |
+
tf_indicators.get('macd_hist', 0) > 0):
|
| 353 |
+
score += 0.3
|
|
|
|
|
|
|
| 354 |
|
| 355 |
return min(score, 1.0)
|
| 356 |
except Exception as error:
|
| 357 |
print(f"❌ خطأ في استراتيجية التعرف على الأنماط: {error}")
|
| 358 |
+
return None
|
| 359 |
|
| 360 |
+
async def _hybrid_ai_strategy(self, symbol_data, market_context, base_scores):
|
| 361 |
+
"""
|
| 362 |
+
🔴 (محسنة) استراتيجية "الميتا" الهجينة - تقيس "تطابق" الإشارات
|
| 363 |
+
"""
|
| 364 |
try:
|
| 365 |
score = 0.0
|
| 366 |
|
| 367 |
+
# 1. درجة مونت كارلو (أساسية)
|
| 368 |
+
monte_carlo_prob = symbol_data.get('monte_carlo_probability')
|
| 369 |
+
if monte_carlo_prob is not None:
|
| 370 |
+
score += monte_carlo_prob * 0.4 # وزن 40%
|
| 371 |
|
| 372 |
+
# 2. درجة توافق الاستراتيجيات (Meta-Score)
|
| 373 |
+
# (نستخدم base_scores التي تم حسابها في evaluate_all_strategies)
|
|
|
|
| 374 |
|
| 375 |
+
breakout_score = base_scores.get('breakout_momentum', 0)
|
| 376 |
+
volume_score = base_scores.get('volume_spike', 0)
|
| 377 |
+
whale_score = base_scores.get('whale_tracking', 0)
|
| 378 |
+
pattern_score = base_scores.get('pattern_recognition', 0)
|
| 379 |
|
| 380 |
+
# سيناريو 1: اختراق مدعوم بالحجم
|
| 381 |
+
if breakout_score > 0.7 and volume_score > 0.6:
|
| 382 |
+
score += 0.3
|
| 383 |
+
|
| 384 |
+
# سيناريو 2: اختراق مدعوم بالحيتان
|
| 385 |
+
if breakout_score > 0.6 and whale_score > 0.7:
|
| 386 |
+
score += 0.4
|
| 387 |
+
|
| 388 |
+
# سيناريو 3: نمط مدعوم بالحجم
|
| 389 |
+
if pattern_score > 0.7 and volume_score > 0.5:
|
| 390 |
+
score += 0.2
|
| 391 |
|
| 392 |
+
# سيناريو 4: كل شيء متوافق (إشارة قوية جداً)
|
| 393 |
+
if breakout_score > 0.7 and whale_score > 0.7 and volume_score > 0.7:
|
| 394 |
+
score = 1.0 # إشارة قصوى
|
| 395 |
+
|
| 396 |
return max(0.0, min(score, 1.0))
|
| 397 |
except Exception as error:
|
| 398 |
print(f"❌ خطأ في استراتيجية الهجين الذكية: {error}")
|
| 399 |
+
return None
|
| 400 |
|
| 401 |
+
print("✅ ML Module: Strategy Engine loaded (V2 - Enhanced Logic)")
|