Spaces:
Paused
Paused
Update ml_engine/indicators.py
Browse files- ml_engine/indicators.py +8 -15
ml_engine/indicators.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# ml_engine/indicators.py (V5.
|
| 2 |
import pandas as pd
|
| 3 |
import pandas_ta as ta
|
| 4 |
import numpy as np
|
|
@@ -71,7 +71,7 @@ class AdvancedTechnicalAnalyzer:
|
|
| 71 |
if base_line is not None and not base_line.empty and not pd.isna(base_line.iloc[-1]):
|
| 72 |
trend['ichimoku_base'] = float(base_line.iloc[-1])
|
| 73 |
except Exception as ichimoku_error:
|
| 74 |
-
pass
|
| 75 |
|
| 76 |
if len(dataframe) >= 14:
|
| 77 |
try:
|
|
@@ -81,7 +81,7 @@ class AdvancedTechnicalAnalyzer:
|
|
| 81 |
if adx_value is not None and not adx_value.empty and not pd.isna(adx_value.iloc[-1]):
|
| 82 |
trend['adx'] = float(adx_value.iloc[-1])
|
| 83 |
except Exception as adx_error:
|
| 84 |
-
pass
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
print(f"⚠️ خطأ في حساب مؤشرات الاتجاه: {e}")
|
|
@@ -163,9 +163,11 @@ class AdvancedTechnicalAnalyzer:
|
|
| 163 |
except Exception as e:
|
| 164 |
print(f"⚠️ خطأ في حساب مؤشرات التقلب: {e}")
|
| 165 |
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
-
# 🔴 --- START OF CHANGE (V5.1 - VWAP FIX) --- 🔴
|
| 169 |
def _calculate_volume_indicators(self, dataframe, timeframe):
|
| 170 |
"""حساب مؤشرات الحجم"""
|
| 171 |
volume = {}
|
|
@@ -178,19 +180,15 @@ class AdvancedTechnicalAnalyzer:
|
|
| 178 |
try:
|
| 179 |
df_vwap = dataframe.copy()
|
| 180 |
|
| 181 |
-
# (التحقق من الفهرس وترتيبه - كما كان)
|
| 182 |
if not isinstance(df_vwap.index, pd.DatetimeIndex):
|
| 183 |
if 'timestamp' in df_vwap.columns:
|
| 184 |
df_vwap['timestamp'] = pd.to_datetime(df_vwap['timestamp'], unit='ms')
|
| 185 |
df_vwap.set_index('timestamp', inplace=True)
|
| 186 |
else:
|
| 187 |
-
# (إذا لم يكن هناك فهرس زمني ولا عمود، لا يمكن المتابعة)
|
| 188 |
raise ValueError("DataFrame needs 'timestamp' column or DatetimeIndex")
|
| 189 |
|
| 190 |
df_vwap.sort_index(inplace=True)
|
| 191 |
|
| 192 |
-
# (الإصلاح: إعادة تعيين الفهرس لتمرير أعمدة Series بسيطة إلى ta.vwap)
|
| 193 |
-
# (هذا يتجنب مشاكل مكتبة pandas_ta مع الفهرس الزمني)
|
| 194 |
df_vwap_reset = df_vwap.reset_index()
|
| 195 |
|
| 196 |
volume_weighted_average_price = ta.vwap(
|
|
@@ -200,16 +198,13 @@ class AdvancedTechnicalAnalyzer:
|
|
| 200 |
volume=df_vwap_reset['volume']
|
| 201 |
)
|
| 202 |
|
| 203 |
-
# (استخدام القيمة الأخيرة من السلسلة الناتجة)
|
| 204 |
if volume_weighted_average_price is not None and not volume_weighted_average_price.empty and not pd.isna(volume_weighted_average_price.iloc[-1]):
|
| 205 |
volume['vwap'] = float(volume_weighted_average_price.iloc[-1])
|
| 206 |
|
| 207 |
except Exception as vwap_error:
|
| 208 |
-
# (تقليل التشويش: طباعة فقط إذا كان الخطأ جوهرياً)
|
| 209 |
if "Index" not in str(vwap_error):
|
| 210 |
print(f"⚠️ خطأ في حساب VWAP لـ {timeframe}: {vwap_error}")
|
| 211 |
|
| 212 |
-
# (الكود الاحتياطي يبقى كما هو)
|
| 213 |
if len(dataframe) >= 20:
|
| 214 |
try:
|
| 215 |
typical_price = (dataframe['high'] + dataframe['low'] + dataframe['close']) / 3
|
|
@@ -219,7 +214,6 @@ class AdvancedTechnicalAnalyzer:
|
|
| 219 |
except Exception as simple_vwap_error:
|
| 220 |
pass
|
| 221 |
|
| 222 |
-
# (باقي مؤشرات الحجم - لا تغيير)
|
| 223 |
try:
|
| 224 |
on_balance_volume = ta.obv(dataframe['close'], dataframe['volume'])
|
| 225 |
if on_balance_volume is not None and not on_balance_volume.empty and not pd.isna(on_balance_volume.iloc[-1]):
|
|
@@ -250,7 +244,6 @@ class AdvancedTechnicalAnalyzer:
|
|
| 250 |
print(f"⚠️ خطأ في حساب مؤشرات الحجم: {e}")
|
| 251 |
|
| 252 |
return {key: value for key, value in volume.items() if value is not None and not np.isnan(value)}
|
| 253 |
-
# 🔴 --- END OF CHANGE (V5.1 - VWAP FIX) --- ��
|
| 254 |
|
| 255 |
def _calculate_cycle_indicators(self, dataframe):
|
| 256 |
"""حساب مؤشرات الدورة"""
|
|
@@ -277,4 +270,4 @@ class AdvancedTechnicalAnalyzer:
|
|
| 277 |
|
| 278 |
return {key: value for key, value in cycle.items() if value is not None and not np.isnan(value)}
|
| 279 |
|
| 280 |
-
print("✅ ML Module: Technical Indicators loaded (V5.
|
|
|
|
| 1 |
+
# ml_engine/indicators.py (V5.2 - Fixed 'volume' not defined bug)
|
| 2 |
import pandas as pd
|
| 3 |
import pandas_ta as ta
|
| 4 |
import numpy as np
|
|
|
|
| 71 |
if base_line is not None and not base_line.empty and not pd.isna(base_line.iloc[-1]):
|
| 72 |
trend['ichimoku_base'] = float(base_line.iloc[-1])
|
| 73 |
except Exception as ichimoku_error:
|
| 74 |
+
pass
|
| 75 |
|
| 76 |
if len(dataframe) >= 14:
|
| 77 |
try:
|
|
|
|
| 81 |
if adx_value is not None and not adx_value.empty and not pd.isna(adx_value.iloc[-1]):
|
| 82 |
trend['adx'] = float(adx_value.iloc[-1])
|
| 83 |
except Exception as adx_error:
|
| 84 |
+
pass
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
print(f"⚠️ خطأ في حساب مؤشرات الاتجاه: {e}")
|
|
|
|
| 163 |
except Exception as e:
|
| 164 |
print(f"⚠️ خطأ في حساب مؤشرات التقلب: {e}")
|
| 165 |
|
| 166 |
+
# 🔴 --- START OF CHANGE (V5.2 - FIX) --- 🔴
|
| 167 |
+
# (إصلاح: إعادة 'volatility' بدلاً من 'volume')
|
| 168 |
+
return {key: value for key, value in volatility.items() if value is not None and not np.isnan(value)}
|
| 169 |
+
# 🔴 --- END OF CHANGE --- 🔴
|
| 170 |
|
|
|
|
| 171 |
def _calculate_volume_indicators(self, dataframe, timeframe):
|
| 172 |
"""حساب مؤشرات الحجم"""
|
| 173 |
volume = {}
|
|
|
|
| 180 |
try:
|
| 181 |
df_vwap = dataframe.copy()
|
| 182 |
|
|
|
|
| 183 |
if not isinstance(df_vwap.index, pd.DatetimeIndex):
|
| 184 |
if 'timestamp' in df_vwap.columns:
|
| 185 |
df_vwap['timestamp'] = pd.to_datetime(df_vwap['timestamp'], unit='ms')
|
| 186 |
df_vwap.set_index('timestamp', inplace=True)
|
| 187 |
else:
|
|
|
|
| 188 |
raise ValueError("DataFrame needs 'timestamp' column or DatetimeIndex")
|
| 189 |
|
| 190 |
df_vwap.sort_index(inplace=True)
|
| 191 |
|
|
|
|
|
|
|
| 192 |
df_vwap_reset = df_vwap.reset_index()
|
| 193 |
|
| 194 |
volume_weighted_average_price = ta.vwap(
|
|
|
|
| 198 |
volume=df_vwap_reset['volume']
|
| 199 |
)
|
| 200 |
|
|
|
|
| 201 |
if volume_weighted_average_price is not None and not volume_weighted_average_price.empty and not pd.isna(volume_weighted_average_price.iloc[-1]):
|
| 202 |
volume['vwap'] = float(volume_weighted_average_price.iloc[-1])
|
| 203 |
|
| 204 |
except Exception as vwap_error:
|
|
|
|
| 205 |
if "Index" not in str(vwap_error):
|
| 206 |
print(f"⚠️ خطأ في حساب VWAP لـ {timeframe}: {vwap_error}")
|
| 207 |
|
|
|
|
| 208 |
if len(dataframe) >= 20:
|
| 209 |
try:
|
| 210 |
typical_price = (dataframe['high'] + dataframe['low'] + dataframe['close']) / 3
|
|
|
|
| 214 |
except Exception as simple_vwap_error:
|
| 215 |
pass
|
| 216 |
|
|
|
|
| 217 |
try:
|
| 218 |
on_balance_volume = ta.obv(dataframe['close'], dataframe['volume'])
|
| 219 |
if on_balance_volume is not None and not on_balance_volume.empty and not pd.isna(on_balance_volume.iloc[-1]):
|
|
|
|
| 244 |
print(f"⚠️ خطأ في حساب مؤشرات الحجم: {e}")
|
| 245 |
|
| 246 |
return {key: value for key, value in volume.items() if value is not None and not np.isnan(value)}
|
|
|
|
| 247 |
|
| 248 |
def _calculate_cycle_indicators(self, dataframe):
|
| 249 |
"""حساب مؤشرات الدورة"""
|
|
|
|
| 270 |
|
| 271 |
return {key: value for key, value in cycle.items() if value is not None and not np.isnan(value)}
|
| 272 |
|
| 273 |
+
print("✅ ML Module: Technical Indicators loaded (V5.2 - 'volume' not defined Fix)")
|