Spaces:
Running
Running
Update ML.py
Browse files
ML.py
CHANGED
|
@@ -491,49 +491,77 @@ class MLProcessor:
|
|
| 491 |
|
| 492 |
async def process_and_score_symbol_enhanced(self, raw_data):
|
| 493 |
try:
|
| 494 |
-
if not raw_data or not raw_data.get('ohlcv'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
raw_data['raw_ohlcv'] = raw_data.get('ohlcv', {})
|
| 496 |
base_analysis = await self.process_and_score_symbol(raw_data)
|
| 497 |
-
if not base_analysis:
|
|
|
|
|
|
|
|
|
|
| 498 |
try:
|
| 499 |
current_price = base_analysis.get('current_price', 0)
|
| 500 |
quality_issues = self._validate_indicators_quality_enhanced(base_analysis.get('advanced_indicators', {}), current_price)
|
|
|
|
| 501 |
if quality_issues:
|
| 502 |
-
|
|
|
|
|
|
|
|
|
|
| 503 |
if hasattr(self, 'strategy_engine') and self.strategy_engine:
|
| 504 |
strategy_scores, base_scores = await self.strategy_engine.evaluate_all_strategies(base_analysis, self.market_context)
|
| 505 |
base_analysis['strategy_scores'] = strategy_scores
|
| 506 |
base_analysis['base_strategy_scores'] = base_scores
|
|
|
|
| 507 |
if base_scores:
|
| 508 |
best_strategy = max(base_scores.items(), key=lambda x: x[1])
|
| 509 |
best_strategy_name = best_strategy[0]
|
| 510 |
best_strategy_score = best_strategy[1]
|
| 511 |
base_analysis['recommended_strategy'] = best_strategy_name
|
| 512 |
base_analysis['strategy_confidence'] = best_strategy_score
|
| 513 |
-
|
| 514 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 515 |
else:
|
| 516 |
base_analysis['recommended_strategy'] = 'GENERIC'
|
| 517 |
base_analysis['strategy_confidence'] = 0.3
|
| 518 |
base_analysis['target_strategy'] = 'GENERIC'
|
|
|
|
|
|
|
| 519 |
enhanced_score = self._calculate_enhanced_score_with_safety(base_analysis, strategy_scores, quality_issues)
|
| 520 |
base_analysis['enhanced_final_score'] = enhanced_score
|
|
|
|
| 521 |
else:
|
| 522 |
base_analysis['strategy_scores'] = {}
|
| 523 |
base_analysis['enhanced_final_score'] = base_analysis.get('final_score', 0.5)
|
| 524 |
base_analysis['recommended_strategy'] = 'GENERIC'
|
| 525 |
base_analysis['strategy_confidence'] = 0.3
|
| 526 |
base_analysis['target_strategy'] = 'GENERIC'
|
|
|
|
|
|
|
| 527 |
base_analysis['quality_warnings'] = quality_issues
|
|
|
|
| 528 |
except Exception as strategy_error:
|
|
|
|
| 529 |
base_analysis['strategy_scores'] = {}
|
| 530 |
base_analysis['enhanced_final_score'] = base_analysis.get('final_score', 0.5)
|
| 531 |
base_analysis['recommended_strategy'] = 'GENERIC'
|
| 532 |
base_analysis['strategy_confidence'] = 0.3
|
| 533 |
base_analysis['target_strategy'] = 'GENERIC'
|
| 534 |
base_analysis['quality_warnings'] = ['Strategy evaluation failed']
|
|
|
|
| 535 |
return base_analysis
|
| 536 |
except Exception as error:
|
|
|
|
| 537 |
return await self.process_and_score_symbol(raw_data)
|
| 538 |
|
| 539 |
def _improve_fibonacci_levels(self, daily_dataframe, current_price):
|
|
@@ -555,17 +583,27 @@ class MLProcessor:
|
|
| 555 |
symbol = raw_data['symbol']
|
| 556 |
ohlcv_data = raw_data['ohlcv']
|
| 557 |
reasons_for_candidacy = raw_data.get('reasons', [])
|
| 558 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 559 |
try:
|
|
|
|
| 560 |
all_indicators = {}
|
| 561 |
for timeframe, candles in ohlcv_data.items():
|
| 562 |
if candles:
|
| 563 |
dataframe = pd.DataFrame(candles, columns=['time', 'open', 'high', 'low', 'close', 'volume'])
|
| 564 |
dataframe[['open', 'high', 'low', 'close', 'volume']] = dataframe[['open', 'high', 'low', 'close', 'volume']].astype(float)
|
| 565 |
all_indicators[timeframe] = self._calculate_indicators(dataframe, timeframe)
|
|
|
|
| 566 |
hourly_dataframe = pd.DataFrame(ohlcv_data.get('1h', []), columns=['time', 'open', 'high', 'low', 'close', 'volume'])
|
| 567 |
-
if hourly_dataframe.empty:
|
|
|
|
|
|
|
|
|
|
| 568 |
hourly_dataframe[['open', 'high', 'low', 'close', 'volume']] = hourly_dataframe[['open', 'high', 'low', 'close', 'volume']].astype(float)
|
|
|
|
| 569 |
try:
|
| 570 |
current_price = float(hourly_dataframe['close'].iloc[-1])
|
| 571 |
if ohlcv_data.get('5m'):
|
|
@@ -573,21 +611,37 @@ class MLProcessor:
|
|
| 573 |
if not five_minute_dataframe.empty:
|
| 574 |
five_minute_dataframe[['open', 'high', 'low', 'close', 'volume']] = five_minute_dataframe[['open', 'high', 'low', 'close', 'volume']].astype(float)
|
| 575 |
current_price = float(five_minute_dataframe['close'].iloc[-1])
|
|
|
|
| 576 |
liquidity_score = self._calculate_liquidity_score(hourly_dataframe)
|
| 577 |
daily_dataframe = pd.DataFrame(ohlcv_data.get('1d', []), columns=['time', 'open', 'high', 'low', 'close', 'volume'])
|
| 578 |
-
if not daily_dataframe.empty:
|
|
|
|
|
|
|
| 579 |
average_daily_volume = float(daily_dataframe['volume'].mean()) if not daily_dataframe.empty else 0.0
|
| 580 |
fibonacci_levels = self._improve_fibonacci_levels(daily_dataframe, current_price)
|
| 581 |
-
|
|
|
|
|
|
|
|
|
|
| 582 |
except Exception as whale_error:
|
| 583 |
whale_data = {"transfer_count": 0, "total_volume": 0, "source": "no_data", "data_available": False}
|
|
|
|
|
|
|
| 584 |
whale_score = await self.data_manager.whale_monitor._calculate_whale_activity_score(whale_data)
|
| 585 |
opportunity_classification = self.classify_opportunity_type(all_indicators, current_price)
|
| 586 |
initial_score = self._calculate_initial_score(all_indicators, current_price, self.market_context)
|
|
|
|
|
|
|
| 587 |
monte_carlo_probability = self._run_monte_carlo_simulation(hourly_dataframe)
|
|
|
|
|
|
|
| 588 |
final_score = (0.35 * initial_score) + (0.50 * monte_carlo_probability) + (0.15 * whale_score)
|
| 589 |
final_score *= opportunity_classification['confidence']
|
|
|
|
| 590 |
normalized_indicators = {timeframe: self._normalize_features_corrected(indicators) for timeframe, indicators in all_indicators.items()}
|
|
|
|
|
|
|
|
|
|
| 591 |
return {
|
| 592 |
'symbol': symbol, 'reasons_for_candidacy': reasons_for_candidacy, 'current_price': float(current_price),
|
| 593 |
'liquidity_score': float(liquidity_score) if not np.isnan(liquidity_score) else 0.0, 'avg_daily_volume': float(average_daily_volume),
|
|
@@ -598,8 +652,11 @@ class MLProcessor:
|
|
| 598 |
'recommended_strategy': 'GENERIC', 'enhanced_final_score': float(final_score), 'target_strategy': 'GENERIC',
|
| 599 |
'raw_ohlcv': ohlcv_data
|
| 600 |
}
|
| 601 |
-
except (KeyError, IndexError) as error:
|
|
|
|
|
|
|
| 602 |
except Exception as error:
|
|
|
|
| 603 |
return None
|
| 604 |
|
| 605 |
def _calculate_indicators(self, dataframe, timeframe):
|
|
@@ -659,19 +716,27 @@ class MLProcessor:
|
|
| 659 |
return normalized_features
|
| 660 |
|
| 661 |
def _run_monte_carlo_simulation(self, dataframe, number_of_simulations=1000, number_of_steps=20):
|
| 662 |
-
if dataframe.empty or len(dataframe) < 2:
|
|
|
|
|
|
|
| 663 |
log_returns = np.log(dataframe['close'] / dataframe['close'].shift(1)).dropna()
|
| 664 |
-
if log_returns.empty:
|
|
|
|
|
|
|
| 665 |
mean_return = log_returns.mean()
|
| 666 |
volatility = log_returns.std()
|
| 667 |
initial_price = dataframe['close'].iloc[-1]
|
| 668 |
success_count = 0
|
|
|
|
| 669 |
for _ in range(number_of_simulations):
|
| 670 |
random_values = np.random.normal(0, 1, number_of_steps)
|
| 671 |
daily_returns = np.exp(mean_return - 0.5 * volatility**2 + volatility * random_values)
|
| 672 |
simulated_prices = initial_price * daily_returns.cumprod()
|
| 673 |
-
if (simulated_prices[-1] / initial_price) > 1.02:
|
| 674 |
-
|
|
|
|
|
|
|
|
|
|
| 675 |
|
| 676 |
def _calculate_initial_score(self, indicators, current_price, market_context):
|
| 677 |
score = 0.5
|
|
@@ -758,4 +823,18 @@ class MLProcessor:
|
|
| 758 |
|
| 759 |
def filter_top_candidates(self, candidates, number_of_candidates=10):
|
| 760 |
valid_candidates = [candidate for candidate in candidates if candidate is not None]
|
| 761 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 491 |
|
| 492 |
async def process_and_score_symbol_enhanced(self, raw_data):
|
| 493 |
try:
|
| 494 |
+
if not raw_data or not raw_data.get('ohlcv'):
|
| 495 |
+
print(f"❌ بيانات غير صالحة للرمز {raw_data.get('symbol', 'unknown')}")
|
| 496 |
+
return None
|
| 497 |
+
|
| 498 |
+
symbol = raw_data['symbol']
|
| 499 |
+
print(f"🔍 معالجة الرمز {symbol}...")
|
| 500 |
+
|
| 501 |
raw_data['raw_ohlcv'] = raw_data.get('ohlcv', {})
|
| 502 |
base_analysis = await self.process_and_score_symbol(raw_data)
|
| 503 |
+
if not base_analysis:
|
| 504 |
+
print(f"❌ فشل التحليل الأساسي للرمز {symbol}")
|
| 505 |
+
return None
|
| 506 |
+
|
| 507 |
try:
|
| 508 |
current_price = base_analysis.get('current_price', 0)
|
| 509 |
quality_issues = self._validate_indicators_quality_enhanced(base_analysis.get('advanced_indicators', {}), current_price)
|
| 510 |
+
|
| 511 |
if quality_issues:
|
| 512 |
+
print(f"⚠️ مشاكل جودة في {symbol}:")
|
| 513 |
+
for issue in quality_issues:
|
| 514 |
+
print(f" {issue}")
|
| 515 |
+
|
| 516 |
if hasattr(self, 'strategy_engine') and self.strategy_engine:
|
| 517 |
strategy_scores, base_scores = await self.strategy_engine.evaluate_all_strategies(base_analysis, self.market_context)
|
| 518 |
base_analysis['strategy_scores'] = strategy_scores
|
| 519 |
base_analysis['base_strategy_scores'] = base_scores
|
| 520 |
+
|
| 521 |
if base_scores:
|
| 522 |
best_strategy = max(base_scores.items(), key=lambda x: x[1])
|
| 523 |
best_strategy_name = best_strategy[0]
|
| 524 |
best_strategy_score = best_strategy[1]
|
| 525 |
base_analysis['recommended_strategy'] = best_strategy_name
|
| 526 |
base_analysis['strategy_confidence'] = best_strategy_score
|
| 527 |
+
|
| 528 |
+
if best_strategy_score > 0.3:
|
| 529 |
+
base_analysis['target_strategy'] = best_strategy_name
|
| 530 |
+
else:
|
| 531 |
+
base_analysis['target_strategy'] = 'GENERIC'
|
| 532 |
+
|
| 533 |
+
print(f"🎯 أفضل استراتيجية لـ {symbol}: {best_strategy_name} (ثقة: {best_strategy_score:.2f})")
|
| 534 |
else:
|
| 535 |
base_analysis['recommended_strategy'] = 'GENERIC'
|
| 536 |
base_analysis['strategy_confidence'] = 0.3
|
| 537 |
base_analysis['target_strategy'] = 'GENERIC'
|
| 538 |
+
print(f"⚠️ لا توجد استراتيجيات مناسبة لـ {symbol}")
|
| 539 |
+
|
| 540 |
enhanced_score = self._calculate_enhanced_score_with_safety(base_analysis, strategy_scores, quality_issues)
|
| 541 |
base_analysis['enhanced_final_score'] = enhanced_score
|
| 542 |
+
print(f"📊 النتيجة النهائية لـ {symbol}: {enhanced_score:.3f}")
|
| 543 |
else:
|
| 544 |
base_analysis['strategy_scores'] = {}
|
| 545 |
base_analysis['enhanced_final_score'] = base_analysis.get('final_score', 0.5)
|
| 546 |
base_analysis['recommended_strategy'] = 'GENERIC'
|
| 547 |
base_analysis['strategy_confidence'] = 0.3
|
| 548 |
base_analysis['target_strategy'] = 'GENERIC'
|
| 549 |
+
print(f"⚠️ محرك الاستراتيجيات غير متوفر لـ {symbol}")
|
| 550 |
+
|
| 551 |
base_analysis['quality_warnings'] = quality_issues
|
| 552 |
+
|
| 553 |
except Exception as strategy_error:
|
| 554 |
+
print(f"❌ خطأ في تقييم الاستراتيجية لـ {symbol}: {strategy_error}")
|
| 555 |
base_analysis['strategy_scores'] = {}
|
| 556 |
base_analysis['enhanced_final_score'] = base_analysis.get('final_score', 0.5)
|
| 557 |
base_analysis['recommended_strategy'] = 'GENERIC'
|
| 558 |
base_analysis['strategy_confidence'] = 0.3
|
| 559 |
base_analysis['target_strategy'] = 'GENERIC'
|
| 560 |
base_analysis['quality_warnings'] = ['Strategy evaluation failed']
|
| 561 |
+
|
| 562 |
return base_analysis
|
| 563 |
except Exception as error:
|
| 564 |
+
print(f"❌ خطأ في المعالجة المحسنة للرمز {raw_data.get('symbol', 'unknown')}: {error}")
|
| 565 |
return await self.process_and_score_symbol(raw_data)
|
| 566 |
|
| 567 |
def _improve_fibonacci_levels(self, daily_dataframe, current_price):
|
|
|
|
| 583 |
symbol = raw_data['symbol']
|
| 584 |
ohlcv_data = raw_data['ohlcv']
|
| 585 |
reasons_for_candidacy = raw_data.get('reasons', [])
|
| 586 |
+
|
| 587 |
+
if not ohlcv_data:
|
| 588 |
+
print(f"❌ لا توجد بيانات OHLCV للرمز {symbol}")
|
| 589 |
+
return None
|
| 590 |
+
|
| 591 |
try:
|
| 592 |
+
print(f"📈 تحليل المؤشرات للرمز {symbol}...")
|
| 593 |
all_indicators = {}
|
| 594 |
for timeframe, candles in ohlcv_data.items():
|
| 595 |
if candles:
|
| 596 |
dataframe = pd.DataFrame(candles, columns=['time', 'open', 'high', 'low', 'close', 'volume'])
|
| 597 |
dataframe[['open', 'high', 'low', 'close', 'volume']] = dataframe[['open', 'high', 'low', 'close', 'volume']].astype(float)
|
| 598 |
all_indicators[timeframe] = self._calculate_indicators(dataframe, timeframe)
|
| 599 |
+
|
| 600 |
hourly_dataframe = pd.DataFrame(ohlcv_data.get('1h', []), columns=['time', 'open', 'high', 'low', 'close', 'volume'])
|
| 601 |
+
if hourly_dataframe.empty:
|
| 602 |
+
print(f"❌ لا توجد بيانات ساعة للرمز {symbol}")
|
| 603 |
+
return None
|
| 604 |
+
|
| 605 |
hourly_dataframe[['open', 'high', 'low', 'close', 'volume']] = hourly_dataframe[['open', 'high', 'low', 'close', 'volume']].astype(float)
|
| 606 |
+
|
| 607 |
try:
|
| 608 |
current_price = float(hourly_dataframe['close'].iloc[-1])
|
| 609 |
if ohlcv_data.get('5m'):
|
|
|
|
| 611 |
if not five_minute_dataframe.empty:
|
| 612 |
five_minute_dataframe[['open', 'high', 'low', 'close', 'volume']] = five_minute_dataframe[['open', 'high', 'low', 'close', 'volume']].astype(float)
|
| 613 |
current_price = float(five_minute_dataframe['close'].iloc[-1])
|
| 614 |
+
|
| 615 |
liquidity_score = self._calculate_liquidity_score(hourly_dataframe)
|
| 616 |
daily_dataframe = pd.DataFrame(ohlcv_data.get('1d', []), columns=['time', 'open', 'high', 'low', 'close', 'volume'])
|
| 617 |
+
if not daily_dataframe.empty:
|
| 618 |
+
daily_dataframe[['open', 'high', 'low', 'close', 'volume']] = daily_dataframe[['open', 'high', 'low', 'close', 'volume']].astype(float)
|
| 619 |
+
|
| 620 |
average_daily_volume = float(daily_dataframe['volume'].mean()) if not daily_dataframe.empty else 0.0
|
| 621 |
fibonacci_levels = self._improve_fibonacci_levels(daily_dataframe, current_price)
|
| 622 |
+
|
| 623 |
+
try:
|
| 624 |
+
whale_data = await self.data_manager.get_symbol_specific_whale_data(symbol)
|
| 625 |
+
print(f"🐋 بيانات الحيتان لـ {symbol}: {whale_data.get('transfer_count', 0)} تحويل")
|
| 626 |
except Exception as whale_error:
|
| 627 |
whale_data = {"transfer_count": 0, "total_volume": 0, "source": "no_data", "data_available": False}
|
| 628 |
+
print(f"⚠️ لا توجد بيانات حيتان لـ {symbol}")
|
| 629 |
+
|
| 630 |
whale_score = await self.data_manager.whale_monitor._calculate_whale_activity_score(whale_data)
|
| 631 |
opportunity_classification = self.classify_opportunity_type(all_indicators, current_price)
|
| 632 |
initial_score = self._calculate_initial_score(all_indicators, current_price, self.market_context)
|
| 633 |
+
|
| 634 |
+
print(f"🎲 تشغيل محاكاة مونت كارلو لـ {symbol}...")
|
| 635 |
monte_carlo_probability = self._run_monte_carlo_simulation(hourly_dataframe)
|
| 636 |
+
print(f"📊 نتيجة مونت كارلو لـ {symbol}: {monte_carlo_probability:.3f}")
|
| 637 |
+
|
| 638 |
final_score = (0.35 * initial_score) + (0.50 * monte_carlo_probability) + (0.15 * whale_score)
|
| 639 |
final_score *= opportunity_classification['confidence']
|
| 640 |
+
|
| 641 |
normalized_indicators = {timeframe: self._normalize_features_corrected(indicators) for timeframe, indicators in all_indicators.items()}
|
| 642 |
+
|
| 643 |
+
print(f"✅ اكتمل تحليل {symbol} - النتيجة: {final_score:.3f}")
|
| 644 |
+
|
| 645 |
return {
|
| 646 |
'symbol': symbol, 'reasons_for_candidacy': reasons_for_candidacy, 'current_price': float(current_price),
|
| 647 |
'liquidity_score': float(liquidity_score) if not np.isnan(liquidity_score) else 0.0, 'avg_daily_volume': float(average_daily_volume),
|
|
|
|
| 652 |
'recommended_strategy': 'GENERIC', 'enhanced_final_score': float(final_score), 'target_strategy': 'GENERIC',
|
| 653 |
'raw_ohlcv': ohlcv_data
|
| 654 |
}
|
| 655 |
+
except (KeyError, IndexError) as error:
|
| 656 |
+
print(f"❌ خطأ في فهارس البيانات لـ {symbol}: {error}")
|
| 657 |
+
return None
|
| 658 |
except Exception as error:
|
| 659 |
+
print(f"❌ خطأ عام في معالجة الرمز {symbol}: {error}")
|
| 660 |
return None
|
| 661 |
|
| 662 |
def _calculate_indicators(self, dataframe, timeframe):
|
|
|
|
| 716 |
return normalized_features
|
| 717 |
|
| 718 |
def _run_monte_carlo_simulation(self, dataframe, number_of_simulations=1000, number_of_steps=20):
|
| 719 |
+
if dataframe.empty or len(dataframe) < 2:
|
| 720 |
+
return 0.0
|
| 721 |
+
|
| 722 |
log_returns = np.log(dataframe['close'] / dataframe['close'].shift(1)).dropna()
|
| 723 |
+
if log_returns.empty:
|
| 724 |
+
return 0.0
|
| 725 |
+
|
| 726 |
mean_return = log_returns.mean()
|
| 727 |
volatility = log_returns.std()
|
| 728 |
initial_price = dataframe['close'].iloc[-1]
|
| 729 |
success_count = 0
|
| 730 |
+
|
| 731 |
for _ in range(number_of_simulations):
|
| 732 |
random_values = np.random.normal(0, 1, number_of_steps)
|
| 733 |
daily_returns = np.exp(mean_return - 0.5 * volatility**2 + volatility * random_values)
|
| 734 |
simulated_prices = initial_price * daily_returns.cumprod()
|
| 735 |
+
if (simulated_prices[-1] / initial_price) > 1.02:
|
| 736 |
+
success_count += 1
|
| 737 |
+
|
| 738 |
+
probability = success_count / number_of_simulations
|
| 739 |
+
return probability
|
| 740 |
|
| 741 |
def _calculate_initial_score(self, indicators, current_price, market_context):
|
| 742 |
score = 0.5
|
|
|
|
| 823 |
|
| 824 |
def filter_top_candidates(self, candidates, number_of_candidates=10):
|
| 825 |
valid_candidates = [candidate for candidate in candidates if candidate is not None]
|
| 826 |
+
|
| 827 |
+
if not valid_candidates:
|
| 828 |
+
print("❌ لا توجد مرشحات صالحة للتصفية")
|
| 829 |
+
return []
|
| 830 |
+
|
| 831 |
+
sorted_candidates = sorted(valid_candidates, key=lambda candidate: candidate.get('enhanced_final_score', candidate.get('final_score', 0)), reverse=True)
|
| 832 |
+
top_candidates = sorted_candidates[:number_of_candidates]
|
| 833 |
+
|
| 834 |
+
print(f"🎖️ أفضل {len(top_candidates)} مرشح:")
|
| 835 |
+
for i, candidate in enumerate(top_candidates):
|
| 836 |
+
score = candidate.get('enhanced_final_score', 0)
|
| 837 |
+
strategy = candidate.get('recommended_strategy', 'GENERIC')
|
| 838 |
+
print(f" {i+1}. {candidate['symbol']}: {score:.3f} - {strategy}")
|
| 839 |
+
|
| 840 |
+
return top_candidates
|