Spaces:
Paused
Paused
Update ml_engine/processor.py
Browse files- ml_engine/processor.py +37 -9
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
|
|
@@ -112,14 +112,15 @@ class MLProcessor:
|
|
| 112 |
print(f"❌ Error creating DataFrame: {e}")
|
| 113 |
return pd.DataFrame()
|
| 114 |
|
| 115 |
-
# 🔴 --- START OF CHANGE (V6.
|
| 116 |
def _calculate_enhanced_final_score(self, analysis):
|
| 117 |
-
"""(محدث V6.
|
| 118 |
try:
|
| 119 |
base_score = analysis.get('final_score', 0)
|
| 120 |
pattern_confidence = analysis.get('pattern_analysis', {}).get('pattern_confidence', 0)
|
| 121 |
strategy_confidence = analysis.get('strategy_confidence', 0)
|
| 122 |
|
|
|
|
| 123 |
mc_distribution = analysis.get('monte_carlo_distribution')
|
| 124 |
monte_carlo_score = 0
|
| 125 |
|
|
@@ -134,13 +135,12 @@ class MLProcessor:
|
|
| 134 |
if normalized_var > 0.05: risk_penalty = 0.5
|
| 135 |
elif normalized_var > 0.03: risk_penalty = 0.8
|
| 136 |
|
| 137 |
-
# (تعديل: تطبيع الاحتمالية)
|
| 138 |
-
# (0.5 -> 0), (0.75 -> 0.5), (1.0 -> 1.0)
|
| 139 |
normalized_prob_score = max(0.0, (prob_gain - 0.5) * 2)
|
| 140 |
monte_carlo_score = normalized_prob_score * risk_penalty
|
| 141 |
else:
|
| 142 |
monte_carlo_score = 0
|
| 143 |
|
|
|
|
| 144 |
whale_confidence = 0
|
| 145 |
whale_data = analysis.get('whale_data')
|
| 146 |
if whale_data and whale_data.get('data_available'):
|
|
@@ -148,6 +148,7 @@ class MLProcessor:
|
|
| 148 |
if signal.get('action') != 'HOLD' and signal.get('confidence', 0) >= 0.5:
|
| 149 |
whale_confidence = signal.get('confidence', 0)
|
| 150 |
|
|
|
|
| 151 |
components = []
|
| 152 |
weights = []
|
| 153 |
|
|
@@ -161,7 +162,28 @@ class MLProcessor:
|
|
| 161 |
total_weight = sum(weights)
|
| 162 |
if total_weight == 0: return 0
|
| 163 |
enhanced_score = sum(comp * weight for comp, weight in zip(components, weights)) / total_weight
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
except Exception as e:
|
| 167 |
print(f"❌ Error calculating enhanced score: {e}")
|
|
@@ -169,7 +191,7 @@ class MLProcessor:
|
|
| 169 |
# 🔴 --- END OF CHANGE --- 🔴
|
| 170 |
|
| 171 |
async def process_and_score_symbol(self, raw_data):
|
| 172 |
-
|
| 173 |
try:
|
| 174 |
symbol = raw_data['symbol']
|
| 175 |
ohlcv_data = raw_data.get('ohlcv')
|
|
@@ -179,11 +201,17 @@ class MLProcessor:
|
|
| 179 |
reasons = raw_data.get('reasons_for_candidacy', [])
|
| 180 |
final_score = layer1_score
|
| 181 |
successful_timeframes = raw_data.get('successful_timeframes', 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
return {
|
| 183 |
'symbol': symbol, 'current_price': current_price, 'final_score': final_score,
|
| 184 |
'enhanced_final_score': final_score, 'reasons_for_candidacy': reasons,
|
| 185 |
'layer1_score': layer1_score, 'ohlcv': ohlcv_data,
|
| 186 |
-
'successful_timeframes': successful_timeframes
|
|
|
|
| 187 |
}
|
| 188 |
except Exception as error:
|
| 189 |
print(f"❌ Error in basic symbol processing {raw_data.get('symbol', 'unknown')}: {error}")
|
|
@@ -241,4 +269,4 @@ def safe_json_parse(json_string):
|
|
| 241 |
return json.loads(s)
|
| 242 |
except json.JSONDecodeError: return None
|
| 243 |
|
| 244 |
-
print("✅ ML Processor loaded - (
|
|
|
|
| 1 |
+
# ml_engine/processor.py (Updated to V6.3 - Exhaustion Penalty Score)
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
from datetime import datetime
|
|
|
|
| 112 |
print(f"❌ Error creating DataFrame: {e}")
|
| 113 |
return pd.DataFrame()
|
| 114 |
|
| 115 |
+
# 🔴 --- START OF CHANGE (V6.3 - EXHAUSTION PENALTY) --- 🔴
|
| 116 |
def _calculate_enhanced_final_score(self, analysis):
|
| 117 |
+
"""(محدث V6.3) تطبيق 'عامل جزاء الإرهاق' على النتيجة النهائية."""
|
| 118 |
try:
|
| 119 |
base_score = analysis.get('final_score', 0)
|
| 120 |
pattern_confidence = analysis.get('pattern_analysis', {}).get('pattern_confidence', 0)
|
| 121 |
strategy_confidence = analysis.get('strategy_confidence', 0)
|
| 122 |
|
| 123 |
+
# --- 1. حساب درجة مونت كارلو (كما في V6.2) ---
|
| 124 |
mc_distribution = analysis.get('monte_carlo_distribution')
|
| 125 |
monte_carlo_score = 0
|
| 126 |
|
|
|
|
| 135 |
if normalized_var > 0.05: risk_penalty = 0.5
|
| 136 |
elif normalized_var > 0.03: risk_penalty = 0.8
|
| 137 |
|
|
|
|
|
|
|
| 138 |
normalized_prob_score = max(0.0, (prob_gain - 0.5) * 2)
|
| 139 |
monte_carlo_score = normalized_prob_score * risk_penalty
|
| 140 |
else:
|
| 141 |
monte_carlo_score = 0
|
| 142 |
|
| 143 |
+
# --- 2. حساب درجة الحيتان (كما في V6.2) ---
|
| 144 |
whale_confidence = 0
|
| 145 |
whale_data = analysis.get('whale_data')
|
| 146 |
if whale_data and whale_data.get('data_available'):
|
|
|
|
| 148 |
if signal.get('action') != 'HOLD' and signal.get('confidence', 0) >= 0.5:
|
| 149 |
whale_confidence = signal.get('confidence', 0)
|
| 150 |
|
| 151 |
+
# --- 3. حساب النتيجة الموزونة الأولية (كما في V6.2) ---
|
| 152 |
components = []
|
| 153 |
weights = []
|
| 154 |
|
|
|
|
| 162 |
total_weight = sum(weights)
|
| 163 |
if total_weight == 0: return 0
|
| 164 |
enhanced_score = sum(comp * weight for comp, weight in zip(components, weights)) / total_weight
|
| 165 |
+
|
| 166 |
+
# --- 4. (جديد V6.3) تطبيق 'عامل جزاء الإرهاق' ---
|
| 167 |
+
exhaustion_penalty_factor = 1.0
|
| 168 |
+
|
| 169 |
+
# (جلب البيانات المطلوبة من القاموس)
|
| 170 |
+
price_change_24h = analysis.get('price_change_24h', 0)
|
| 171 |
+
rsi_1d = analysis.get('advanced_indicators', {}).get('1d', {}).get('rsi', 50)
|
| 172 |
+
rsi_4h = analysis.get('advanced_indicators', {}).get('4h', {}).get('rsi', 50)
|
| 173 |
+
|
| 174 |
+
if price_change_24h > 60 and (rsi_1d > 80 or rsi_4h > 80):
|
| 175 |
+
exhaustion_penalty_factor = 0.4 # (عقوبة قاسية - 60%)
|
| 176 |
+
elif price_change_24h > 40 and (rsi_1d > 75 or rsi_4h > 75):
|
| 177 |
+
exhaustion_penalty_factor = 0.6 # (عقوبة متوسطة - 40%)
|
| 178 |
+
|
| 179 |
+
if exhaustion_penalty_factor < 1.0:
|
| 180 |
+
# (اختياري: للطباعة التشخيصية)
|
| 181 |
+
# print(f" ⚠️ [Processor] {analysis.get('symbol')} Exhaustion Penalty Applied! Score {enhanced_score:.2f} * {exhaustion_penalty_factor} (24h: {price_change_24h:+.1f}%, RSI 1D: {rsi_1d:.1f})")
|
| 182 |
+
pass
|
| 183 |
+
|
| 184 |
+
final_penalized_score = enhanced_score * exhaustion_penalty_factor
|
| 185 |
+
|
| 186 |
+
return min(max(final_penalized_score, 0.0), 1.0)
|
| 187 |
|
| 188 |
except Exception as e:
|
| 189 |
print(f"❌ Error calculating enhanced score: {e}")
|
|
|
|
| 191 |
# 🔴 --- END OF CHANGE --- 🔴
|
| 192 |
|
| 193 |
async def process_and_score_symbol(self, raw_data):
|
| 194 |
+
"""(محدث V6.3) إضافة price_change_24h إلى القاموس"""
|
| 195 |
try:
|
| 196 |
symbol = raw_data['symbol']
|
| 197 |
ohlcv_data = raw_data.get('ohlcv')
|
|
|
|
| 201 |
reasons = raw_data.get('reasons_for_candidacy', [])
|
| 202 |
final_score = layer1_score
|
| 203 |
successful_timeframes = raw_data.get('successful_timeframes', 0)
|
| 204 |
+
|
| 205 |
+
# 🔴 --- START OF CHANGE (V6.3) --- 🔴
|
| 206 |
+
price_change_24h = raw_data.get('price_change_24h', 0)
|
| 207 |
+
# 🔴 --- END OF CHANGE --- 🔴
|
| 208 |
+
|
| 209 |
return {
|
| 210 |
'symbol': symbol, 'current_price': current_price, 'final_score': final_score,
|
| 211 |
'enhanced_final_score': final_score, 'reasons_for_candidacy': reasons,
|
| 212 |
'layer1_score': layer1_score, 'ohlcv': ohlcv_data,
|
| 213 |
+
'successful_timeframes': successful_timeframes,
|
| 214 |
+
'price_change_24h': price_change_24h # (إضافة الحقل هنا)
|
| 215 |
}
|
| 216 |
except Exception as error:
|
| 217 |
print(f"❌ Error in basic symbol processing {raw_data.get('symbol', 'unknown')}: {error}")
|
|
|
|
| 269 |
return json.loads(s)
|
| 270 |
except json.JSONDecodeError: return None
|
| 271 |
|
| 272 |
+
print("✅ ML Processor loaded - V6.3 (Exhaustion Penalty Score)")
|