Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,7 +26,7 @@ llm_service_global = None
|
|
| 26 |
learning_engine_global = None
|
| 27 |
trade_manager_global = None
|
| 28 |
sentiment_analyzer_global = None
|
| 29 |
-
symbol_whale_monitor_global = None
|
| 30 |
|
| 31 |
class StateManager:
|
| 32 |
def __init__(self):
|
|
@@ -40,7 +40,7 @@ class StateManager:
|
|
| 40 |
'learning_engine': False,
|
| 41 |
'trade_manager': False,
|
| 42 |
'sentiment_analyzer': False,
|
| 43 |
-
'symbol_whale_monitor': False
|
| 44 |
}
|
| 45 |
|
| 46 |
async def wait_for_initialization(self, timeout=30):
|
|
@@ -78,7 +78,6 @@ async def monitor_market_async():
|
|
| 78 |
|
| 79 |
should_halt_trading, halt_reason = False, ""
|
| 80 |
|
| 81 |
-
# استخدام مؤشرات السوق الأخرى فقط (بدون الحيتان العامة)
|
| 82 |
if bitcoin_sentiment == 'BEARISH' and (fear_greed_index is not None and fear_greed_index < 30):
|
| 83 |
should_halt_trading, halt_reason = True, "ظروف سوق هابطة"
|
| 84 |
|
|
@@ -99,7 +98,6 @@ async def monitor_market_async():
|
|
| 99 |
async def analyze_market_strategy(market_context):
|
| 100 |
"""تحليل استراتيجية السوق بدون اعتماد على الحيتان العامة"""
|
| 101 |
try:
|
| 102 |
-
# استخدام مؤشرات السوق الأساسية فقط
|
| 103 |
prompt = f"Analyze current market conditions and determine trading strategy.\n\nMarket Data:\n- BTC Sentiment: {market_context.get('btc_sentiment')}\n- Fear & Greed Index: {market_context.get('fear_and_greed_index')}\n\nOutput JSON:\n{{\"primary_strategy\": \"STRATEGY_NAME\",\"reasoning\": \"Brief reasoning\",\"risk_tolerance\": 5,\"optimal_scan_count\": 100}}"
|
| 104 |
response = await llm_service_global._call_llm(prompt)
|
| 105 |
try:
|
|
@@ -107,7 +105,6 @@ async def analyze_market_strategy(market_context):
|
|
| 107 |
json_str = parse_json_from_response(response)
|
| 108 |
strategy_data = json.loads(json_str)
|
| 109 |
except:
|
| 110 |
-
# استراتيجية افتراضية في حال فشل التحليل
|
| 111 |
strategy_data = {
|
| 112 |
"primary_strategy": "GENERIC",
|
| 113 |
"reasoning": "Fallback strategy due to analysis error",
|
|
@@ -172,7 +169,7 @@ async def find_strategy_specific_candidates(strategy, scan_count):
|
|
| 172 |
return []
|
| 173 |
|
| 174 |
async def enhanced_llm_analysis_with_whale_data(candidate):
|
| 175 |
-
"""تحليل محسن يشمل بيانات حيتان
|
| 176 |
global symbol_whale_monitor_global
|
| 177 |
|
| 178 |
try:
|
|
@@ -194,17 +191,17 @@ async def enhanced_llm_analysis_with_whale_data(candidate):
|
|
| 194 |
# 3. دمج النتائج
|
| 195 |
enhanced_analysis = {
|
| 196 |
**llm_analysis,
|
| 197 |
-
'whale_analysis': whale_analysis
|
| 198 |
'combined_confidence': await calculate_combined_confidence(
|
| 199 |
llm_analysis.get('confidence_level', 0.5),
|
| 200 |
-
whale_analysis
|
| 201 |
),
|
| 202 |
'analysis_timestamp': datetime.now().isoformat(),
|
| 203 |
'analysis_source': 'enhanced_with_whale_data'
|
| 204 |
}
|
| 205 |
|
| 206 |
# 4. تطبيق قواعد السلامة بناء على نشاط الحيتان
|
| 207 |
-
if whale_analysis
|
| 208 |
enhanced_analysis = apply_whale_safety_filters(enhanced_analysis, whale_analysis)
|
| 209 |
|
| 210 |
print(f"✅ اكتمل التحليل المتقدم لـ {candidate['symbol']}")
|
|
@@ -217,29 +214,26 @@ async def enhanced_llm_analysis_with_whale_data(candidate):
|
|
| 217 |
|
| 218 |
async def calculate_combined_confidence(llm_confidence, whale_confidence):
|
| 219 |
"""حساب الثقة المجمعة مع إعطاء وزن أكبر لبيانات الحيتان"""
|
| 220 |
-
# وزن بيانات الحيتان 60% والتحليل الأساسي 40%
|
| 221 |
combined = (llm_confidence * 0.4) + (whale_confidence * 0.6)
|
| 222 |
-
return min(combined, 0.95)
|
| 223 |
|
| 224 |
def apply_whale_safety_filters(analysis, whale_analysis):
|
| 225 |
"""تطبيق فلاتر السلامة بناء على نشاط الحيتان الحرج"""
|
| 226 |
|
| 227 |
-
whale_signal = whale_analysis
|
| 228 |
|
| 229 |
-
if whale_signal
|
| 230 |
-
|
| 231 |
-
if analysis['action'] == 'BUY':
|
| 232 |
analysis.update({
|
| 233 |
'action': 'HOLD',
|
| 234 |
-
'reasoning': f"{analysis.get('reasoning', '')} | تصحيح بسبب نشاط الحيتان: {whale_signal
|
| 235 |
'confidence_level': analysis.get('confidence_level', 0.5) * 0.7
|
| 236 |
})
|
| 237 |
-
elif analysis
|
| 238 |
analysis['confidence_level'] = analysis.get('confidence_level', 0.5) * 0.9
|
| 239 |
|
| 240 |
-
elif whale_signal
|
| 241 |
-
|
| 242 |
-
if analysis['action'] == 'BUY':
|
| 243 |
analysis['confidence_level'] = min(analysis.get('confidence_level', 0.5) * 1.2, 0.95)
|
| 244 |
analysis['reasoning'] = f"{analysis.get('reasoning', '')} | تعزيز بسبب نشاط الحيتان الإيجابي"
|
| 245 |
|
|
@@ -569,20 +563,13 @@ async def lifespan(application: FastAPI):
|
|
| 569 |
contracts_database = await r2_service_global.load_contracts_db_async()
|
| 570 |
print("✅ Contracts database loaded")
|
| 571 |
|
| 572 |
-
# ❌ إزالة النظام العام للحيتان
|
| 573 |
-
# ✅ استخدام النظام الخاص فقط للمرشحين النهائيين
|
| 574 |
from whale_news_data import EnhancedWhaleMonitor
|
| 575 |
symbol_whale_monitor_global = EnhancedWhaleMonitor(contracts_database, r2_service_global)
|
| 576 |
state_manager.set_service_initialized('symbol_whale_monitor')
|
| 577 |
print("✅ Symbol Specific Whale Monitor initialized")
|
| 578 |
|
| 579 |
-
|
| 580 |
-
whale_monitor_global = EnhancedWhaleMonitor(contracts_database)
|
| 581 |
-
print("✅ Whale Monitor initialized")
|
| 582 |
-
|
| 583 |
-
data_manager_global = DataManager(contracts_database, whale_monitor_global)
|
| 584 |
await data_manager_global.initialize()
|
| 585 |
-
whale_monitor_global.data_manager = data_manager_global
|
| 586 |
state_manager.set_service_initialized('data_manager')
|
| 587 |
print("✅ Data Manager initialized")
|
| 588 |
|
|
@@ -689,7 +676,7 @@ async def get_performance_stats():
|
|
| 689 |
"learning_engine": learning_stats,
|
| 690 |
"whale_monitoring": {
|
| 691 |
"symbol_specific_active": symbol_whale_monitor_global is not None,
|
| 692 |
-
"monitoring_type": "
|
| 693 |
}
|
| 694 |
}
|
| 695 |
return stats
|
|
|
|
| 26 |
learning_engine_global = None
|
| 27 |
trade_manager_global = None
|
| 28 |
sentiment_analyzer_global = None
|
| 29 |
+
symbol_whale_monitor_global = None
|
| 30 |
|
| 31 |
class StateManager:
|
| 32 |
def __init__(self):
|
|
|
|
| 40 |
'learning_engine': False,
|
| 41 |
'trade_manager': False,
|
| 42 |
'sentiment_analyzer': False,
|
| 43 |
+
'symbol_whale_monitor': False
|
| 44 |
}
|
| 45 |
|
| 46 |
async def wait_for_initialization(self, timeout=30):
|
|
|
|
| 78 |
|
| 79 |
should_halt_trading, halt_reason = False, ""
|
| 80 |
|
|
|
|
| 81 |
if bitcoin_sentiment == 'BEARISH' and (fear_greed_index is not None and fear_greed_index < 30):
|
| 82 |
should_halt_trading, halt_reason = True, "ظروف سوق هابطة"
|
| 83 |
|
|
|
|
| 98 |
async def analyze_market_strategy(market_context):
|
| 99 |
"""تحليل استراتيجية السوق بدون اعتماد على الحيتان العامة"""
|
| 100 |
try:
|
|
|
|
| 101 |
prompt = f"Analyze current market conditions and determine trading strategy.\n\nMarket Data:\n- BTC Sentiment: {market_context.get('btc_sentiment')}\n- Fear & Greed Index: {market_context.get('fear_and_greed_index')}\n\nOutput JSON:\n{{\"primary_strategy\": \"STRATEGY_NAME\",\"reasoning\": \"Brief reasoning\",\"risk_tolerance\": 5,\"optimal_scan_count\": 100}}"
|
| 102 |
response = await llm_service_global._call_llm(prompt)
|
| 103 |
try:
|
|
|
|
| 105 |
json_str = parse_json_from_response(response)
|
| 106 |
strategy_data = json.loads(json_str)
|
| 107 |
except:
|
|
|
|
| 108 |
strategy_data = {
|
| 109 |
"primary_strategy": "GENERIC",
|
| 110 |
"reasoning": "Fallback strategy due to analysis error",
|
|
|
|
| 169 |
return []
|
| 170 |
|
| 171 |
async def enhanced_llm_analysis_with_whale_data(candidate):
|
| 172 |
+
"""تحليل محسن يشمل بيانات حيتان للمرشحين النهائيين"""
|
| 173 |
global symbol_whale_monitor_global
|
| 174 |
|
| 175 |
try:
|
|
|
|
| 191 |
# 3. دمج النتائج
|
| 192 |
enhanced_analysis = {
|
| 193 |
**llm_analysis,
|
| 194 |
+
'whale_analysis': whale_analysis.get('llm_friendly_summary', {}),
|
| 195 |
'combined_confidence': await calculate_combined_confidence(
|
| 196 |
llm_analysis.get('confidence_level', 0.5),
|
| 197 |
+
whale_analysis.get('trading_signal', {}).get('confidence', 0.5)
|
| 198 |
),
|
| 199 |
'analysis_timestamp': datetime.now().isoformat(),
|
| 200 |
'analysis_source': 'enhanced_with_whale_data'
|
| 201 |
}
|
| 202 |
|
| 203 |
# 4. تطبيق قواعد السلامة بناء على نشاط الحيتان
|
| 204 |
+
if whale_analysis.get('trading_signal', {}).get('critical_alert'):
|
| 205 |
enhanced_analysis = apply_whale_safety_filters(enhanced_analysis, whale_analysis)
|
| 206 |
|
| 207 |
print(f"✅ اكتمل التحليل المتقدم لـ {candidate['symbol']}")
|
|
|
|
| 214 |
|
| 215 |
async def calculate_combined_confidence(llm_confidence, whale_confidence):
|
| 216 |
"""حساب الثقة المجمعة مع إعطاء وزن أكبر لبيانات الحيتان"""
|
|
|
|
| 217 |
combined = (llm_confidence * 0.4) + (whale_confidence * 0.6)
|
| 218 |
+
return min(combined, 0.95)
|
| 219 |
|
| 220 |
def apply_whale_safety_filters(analysis, whale_analysis):
|
| 221 |
"""تطبيق فلاتر السلامة بناء على نشاط الحيتان الحرج"""
|
| 222 |
|
| 223 |
+
whale_signal = whale_analysis.get('trading_signal', {})
|
| 224 |
|
| 225 |
+
if whale_signal.get('action') in ['STRONG_SELL', 'SELL']:
|
| 226 |
+
if analysis.get('action') == 'BUY':
|
|
|
|
| 227 |
analysis.update({
|
| 228 |
'action': 'HOLD',
|
| 229 |
+
'reasoning': f"{analysis.get('reasoning', '')} | تصحيح بسبب نشاط الحيتان: {whale_signal.get('reason', '')}",
|
| 230 |
'confidence_level': analysis.get('confidence_level', 0.5) * 0.7
|
| 231 |
})
|
| 232 |
+
elif analysis.get('action') == 'HOLD':
|
| 233 |
analysis['confidence_level'] = analysis.get('confidence_level', 0.5) * 0.9
|
| 234 |
|
| 235 |
+
elif whale_signal.get('action') in ['STRONG_BUY', 'BUY']:
|
| 236 |
+
if analysis.get('action') == 'BUY':
|
|
|
|
| 237 |
analysis['confidence_level'] = min(analysis.get('confidence_level', 0.5) * 1.2, 0.95)
|
| 238 |
analysis['reasoning'] = f"{analysis.get('reasoning', '')} | تعزيز بسبب نشاط الحيتان الإيجابي"
|
| 239 |
|
|
|
|
| 563 |
contracts_database = await r2_service_global.load_contracts_db_async()
|
| 564 |
print("✅ Contracts database loaded")
|
| 565 |
|
|
|
|
|
|
|
| 566 |
from whale_news_data import EnhancedWhaleMonitor
|
| 567 |
symbol_whale_monitor_global = EnhancedWhaleMonitor(contracts_database, r2_service_global)
|
| 568 |
state_manager.set_service_initialized('symbol_whale_monitor')
|
| 569 |
print("✅ Symbol Specific Whale Monitor initialized")
|
| 570 |
|
| 571 |
+
data_manager_global = DataManager(contracts_database, symbol_whale_monitor_global)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 572 |
await data_manager_global.initialize()
|
|
|
|
| 573 |
state_manager.set_service_initialized('data_manager')
|
| 574 |
print("✅ Data Manager initialized")
|
| 575 |
|
|
|
|
| 676 |
"learning_engine": learning_stats,
|
| 677 |
"whale_monitoring": {
|
| 678 |
"symbol_specific_active": symbol_whale_monitor_global is not None,
|
| 679 |
+
"monitoring_type": "TARGETED_NETWORK_ONLY"
|
| 680 |
}
|
| 681 |
}
|
| 682 |
return stats
|