Update ml_engine/processor.py
Browse files- ml_engine/processor.py +27 -11
ml_engine/processor.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# ml_engine/processor.py (Updated to V6.
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
from datetime import datetime
|
|
@@ -83,7 +83,7 @@ class MLProcessor:
|
|
| 83 |
base_analysis['whale_data'] = preloaded_whale_data.get(symbol, {'data_available': False, 'reason': 'Not preloaded'})
|
| 84 |
else:
|
| 85 |
base_analysis['whale_data'] = {'data_available': False, 'reason': 'Preloading disabled'}
|
| 86 |
-
|
| 87 |
# 🔴 (This call now uses the Learning Hub via strategy_engine)
|
| 88 |
strategy_scores, base_scores = await self.strategy_engine.evaluate_all_strategies(base_analysis, self.market_context)
|
| 89 |
base_analysis['strategy_scores'] = strategy_scores
|
|
@@ -97,6 +97,7 @@ class MLProcessor:
|
|
| 97 |
base_analysis['strategy_confidence'] = best_strategy_score
|
| 98 |
base_analysis['target_strategy'] = best_strategy_name if best_strategy_score > 0.3 else 'GENERIC'
|
| 99 |
|
|
|
|
| 100 |
enhanced_score = self._calculate_enhanced_final_score(base_analysis)
|
| 101 |
base_analysis['enhanced_final_score'] = enhanced_score
|
| 102 |
|
|
@@ -124,14 +125,20 @@ class MLProcessor:
|
|
| 124 |
print(f"❌ Error creating DataFrame: {e}")
|
| 125 |
return pd.DataFrame()
|
| 126 |
|
| 127 |
-
# 🔴 --- START OF CHANGE (V6.
|
| 128 |
def _calculate_enhanced_final_score(self, analysis):
|
| 129 |
-
"""(محدث V6.
|
| 130 |
try:
|
| 131 |
base_score = analysis.get('final_score', 0)
|
| 132 |
pattern_confidence = analysis.get('pattern_analysis', {}).get('pattern_confidence', 0)
|
| 133 |
strategy_confidence = analysis.get('strategy_confidence', 0)
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
# --- 1. حساب درجة مونت كارلو (كما في V6.2) ---
|
| 136 |
mc_distribution = analysis.get('monte_carlo_distribution')
|
| 137 |
monte_carlo_score = 0
|
|
@@ -160,22 +167,28 @@ class MLProcessor:
|
|
| 160 |
if signal.get('action') != 'HOLD' and signal.get('confidence', 0) >= 0.5:
|
| 161 |
whale_confidence = signal.get('confidence', 0)
|
| 162 |
|
| 163 |
-
# --- 3. حساب النتيجة الموزونة الأولية (
|
| 164 |
components = []
|
| 165 |
weights = []
|
| 166 |
|
| 167 |
-
|
| 168 |
-
if
|
| 169 |
-
if
|
|
|
|
| 170 |
if strategy_confidence > 0: components.append(strategy_confidence); weights.append(0.15)
|
| 171 |
-
if whale_confidence > 0: components.append(whale_confidence); weights.append(0.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
if not components: return 0
|
| 174 |
total_weight = sum(weights)
|
| 175 |
if total_weight == 0: return 0
|
| 176 |
enhanced_score = sum(comp * weight for comp, weight in zip(components, weights)) / total_weight
|
| 177 |
|
| 178 |
-
# --- 4. (
|
| 179 |
exhaustion_penalty_factor = 1.0
|
| 180 |
|
| 181 |
# (جلب البيانات المطلوبة من القاموس)
|
|
@@ -247,6 +260,9 @@ class MLProcessor:
|
|
| 247 |
whale_data = c.get('whale_data')
|
| 248 |
if whale_data and whale_data.get('data_available'):
|
| 249 |
signal = whale_data.get('trading_signal', {}); print(f" 🐋 Whale: {signal.get('action', 'HOLD')} (Conf: {signal.get('confidence', 0):.2f})")
|
|
|
|
|
|
|
|
|
|
| 250 |
return top_candidates
|
| 251 |
|
| 252 |
async def process_multiple_symbols_parallel(self, symbols_data_list, preloaded_whale_data: dict, max_concurrent=5):
|
|
@@ -281,4 +297,4 @@ def safe_json_parse(json_string):
|
|
| 281 |
return json.loads(s)
|
| 282 |
except json.JSONDecodeError: return None
|
| 283 |
|
| 284 |
-
print("✅ ML Processor loaded - V6.
|
|
|
|
| 1 |
+
# ml_engine/processor.py (Updated to V6.5 - News Score Integration)
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
from datetime import datetime
|
|
|
|
| 83 |
base_analysis['whale_data'] = preloaded_whale_data.get(symbol, {'data_available': False, 'reason': 'Not preloaded'})
|
| 84 |
else:
|
| 85 |
base_analysis['whale_data'] = {'data_available': False, 'reason': 'Preloading disabled'}
|
| 86 |
+
|
| 87 |
# 🔴 (This call now uses the Learning Hub via strategy_engine)
|
| 88 |
strategy_scores, base_scores = await self.strategy_engine.evaluate_all_strategies(base_analysis, self.market_context)
|
| 89 |
base_analysis['strategy_scores'] = strategy_scores
|
|
|
|
| 97 |
base_analysis['strategy_confidence'] = best_strategy_score
|
| 98 |
base_analysis['target_strategy'] = best_strategy_name if best_strategy_score > 0.3 else 'GENERIC'
|
| 99 |
|
| 100 |
+
# 🔴 (V6.5) استدعاء الدالة المحدثة (بدون درجة الأخبار هنا، لأنها تضاف لاحقاً)
|
| 101 |
enhanced_score = self._calculate_enhanced_final_score(base_analysis)
|
| 102 |
base_analysis['enhanced_final_score'] = enhanced_score
|
| 103 |
|
|
|
|
| 125 |
print(f"❌ Error creating DataFrame: {e}")
|
| 126 |
return pd.DataFrame()
|
| 127 |
|
| 128 |
+
# 🔴 --- START OF CHANGE (V6.5 - NEWS SCORE WEIGHT) --- 🔴
|
| 129 |
def _calculate_enhanced_final_score(self, analysis):
|
| 130 |
+
"""(محدث V6.5) تطبيق 'عامل جزاء الإرهاق' وإضافة 'درجة الأخبار' (إن وجدت)."""
|
| 131 |
try:
|
| 132 |
base_score = analysis.get('final_score', 0)
|
| 133 |
pattern_confidence = analysis.get('pattern_analysis', {}).get('pattern_confidence', 0)
|
| 134 |
strategy_confidence = analysis.get('strategy_confidence', 0)
|
| 135 |
|
| 136 |
+
# (جديد V6.5) جلب درجة الأخبار (ستكون 0 لمعظم العمليات، إلا عند إعادة الحساب)
|
| 137 |
+
# (VADER-Compound: -1 to +1) -> (NewsScore: 0 to 1.0)
|
| 138 |
+
# (نحول الدرجة من -1..+1 إلى 0..+1)
|
| 139 |
+
raw_news_score = analysis.get('news_score', 0.0) # الافتراضي هو 0.0 (محايد)
|
| 140 |
+
normalized_news_score = (raw_news_score + 1) / 2
|
| 141 |
+
|
| 142 |
# --- 1. حساب درجة مونت كارلو (كما في V6.2) ---
|
| 143 |
mc_distribution = analysis.get('monte_carlo_distribution')
|
| 144 |
monte_carlo_score = 0
|
|
|
|
| 167 |
if signal.get('action') != 'HOLD' and signal.get('confidence', 0) >= 0.5:
|
| 168 |
whale_confidence = signal.get('confidence', 0)
|
| 169 |
|
| 170 |
+
# --- 3. حساب النتيجة الموزونة الأولية (محدث V6.5 بالأوزان الجديدة) ---
|
| 171 |
components = []
|
| 172 |
weights = []
|
| 173 |
|
| 174 |
+
# (الأوزان الجديدة التي تدمج الأخبار بـ 0.10)
|
| 175 |
+
if base_score > 0: components.append(base_score); weights.append(0.15)
|
| 176 |
+
if monte_carlo_score > 0: components.append(monte_carlo_score); weights.append(0.20)
|
| 177 |
+
if pattern_confidence > 0: components.append(pattern_confidence); weights.append(0.20)
|
| 178 |
if strategy_confidence > 0: components.append(strategy_confidence); weights.append(0.15)
|
| 179 |
+
if whale_confidence > 0: components.append(whale_confidence); weights.append(0.20)
|
| 180 |
+
|
| 181 |
+
# (إضافة درجة الأخبار بالوزن 0.10)
|
| 182 |
+
# (ستكون 0.5 إذا كانت الدرجة الافتراضية 0، وهو ما يعني "محايد" بوزن 0.10)
|
| 183 |
+
components.append(normalized_news_score); weights.append(0.10)
|
| 184 |
+
|
| 185 |
|
| 186 |
if not components: return 0
|
| 187 |
total_weight = sum(weights)
|
| 188 |
if total_weight == 0: return 0
|
| 189 |
enhanced_score = sum(comp * weight for comp, weight in zip(components, weights)) / total_weight
|
| 190 |
|
| 191 |
+
# --- 4. ( V6.3) تطبيق 'عامل جزاء الإرهاق' ---
|
| 192 |
exhaustion_penalty_factor = 1.0
|
| 193 |
|
| 194 |
# (جلب البيانات المطلوبة من القاموس)
|
|
|
|
| 260 |
whale_data = c.get('whale_data')
|
| 261 |
if whale_data and whale_data.get('data_available'):
|
| 262 |
signal = whale_data.get('trading_signal', {}); print(f" 🐋 Whale: {signal.get('action', 'HOLD')} (Conf: {signal.get('confidence', 0):.2f})")
|
| 263 |
+
# (V6.5) طباعة درجة الأخبار إن وجدت (للتحقق)
|
| 264 |
+
if 'news_score' in c:
|
| 265 |
+
print(f" 📰 News Score: {c['news_score']:.2f}")
|
| 266 |
return top_candidates
|
| 267 |
|
| 268 |
async def process_multiple_symbols_parallel(self, symbols_data_list, preloaded_whale_data: dict, max_concurrent=5):
|
|
|
|
| 297 |
return json.loads(s)
|
| 298 |
except json.JSONDecodeError: return None
|
| 299 |
|
| 300 |
+
print("✅ ML Processor loaded - V6.5 (News Score Integration)")
|