File size: 7,617 Bytes
c7cd3b6 9c8e68c 3031827 c7cd3b6 ab034d5 c7cd3b6 3031827 c7cd3b6 3031827 c7cd3b6 ab034d5 c7cd3b6 ab034d5 c7cd3b6 ab034d5 c7cd3b6 ab034d5 c7cd3b6 ab034d5 c7cd3b6 ab034d5 c7cd3b6 3031827 c7cd3b6 3031827 c7cd3b6 ab034d5 c7cd3b6 ab034d5 c7cd3b6 3031827 c7cd3b6 9c8e68c 3031827 4a5c308 3031827 c7cd3b6 3031827 c7cd3b6 ab034d5 c7cd3b6 ab034d5 c7cd3b6 ab034d5 c7cd3b6 9c8e68c c7cd3b6 3031827 c7cd3b6 46ac63f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# learning_hub/hub_manager.py
# (محدث بالكامل - V2 - VADER Learning)
import asyncio
from typing import Any, Dict
# (استيراد جميع المكونات الداخلية للمركز)
from .schemas import *
from .policy_engine import PolicyEngine
from .memory_store import MemoryStore
from .statistical_analyzer import StatisticalAnalyzer
from .reflector import Reflector
from .curator import Curator
class LearningHubManager:
def __init__(self, r2_service: Any, llm_service: Any, data_manager: Any):
print("🚀 Initializing Learning Hub Manager...")
# 1. الخدمات الأساسية (يتم تمريرها من app.py)
self.r2_service = r2_service
self.llm_service = llm_service
self.data_manager = data_manager
# 2. تهيئة المكونات (بناء النظام)
self.policy_engine = PolicyEngine()
self.memory_store = MemoryStore(
r2_service=self.r2_service,
policy_engine=self.policy_engine,
llm_service=self.llm_service
)
self.reflector = Reflector(
llm_service=self.llm_service,
memory_store=self.memory_store
)
self.curator = Curator(
llm_service=self.llm_service,
memory_store=self.memory_store
)
self.statistical_analyzer = StatisticalAnalyzer(
r2_service=self.r2_service,
data_manager=self.data_manager
)
self.initialized = False
print("✅ Learning Hub Manager constructed. Ready for initialization.")
async def initialize(self):
"""
تهيئة جميع الأنظمة الفرعية، وخاصة تحميل الإحصائيات والأوزان.
"""
if self.initialized:
return
print("🔄 [HubManager] Initializing all sub-modules...")
await self.statistical_analyzer.initialize()
self.initialized = True
print("✅ [HubManager] All sub-modules initialized. Learning Hub is LIVE.")
async def analyze_trade_and_learn(self, trade_object: Dict[str, Any], close_reason: str):
"""
هذه هي الدالة الرئيسية التي يستدعيها TradeManager.
إنها تشغل كلاً من نظام التعلم السريع (Reflector) والبطيء (StatsAnalyzer).
"""
if not self.initialized:
print("⚠️ [HubManager] Learning Hub not initialized. Skipping learning.")
return
print(f"🧠 [HubManager] Learning from trade {trade_object.get('symbol')}...")
try:
# 1. التعلم السريع (Reflector):
await self.reflector.analyze_trade_outcome(trade_object, close_reason)
except Exception as e:
print(f"❌ [HubManager] Reflector (Fast-Learner) failed: {e}")
try:
# 2. التعلم البطيء (StatisticalAnalyzer):
await self.statistical_analyzer.update_statistics(trade_object, close_reason)
except Exception as e:
print(f"❌ [HubManager] StatisticalAnalyzer (Slow-Learner) failed: {e}")
print(f"✅ [HubManager] Learning complete for {trade_object.get('symbol')}.")
async def get_active_context_for_llm(self, domain: str, query: str) -> str:
"""
يُستخدم بواسطة LLMService لجلب "الدفتر" (Playbook) / القواعد (Deltas).
"""
if not self.initialized:
return "Learning Hub not initialized."
return await self.memory_store.get_active_context(domain, query)
async def get_statistical_feedback_for_llm(self, entry_strategy: str) -> str:
"""
يُستخدم بواسطة LLMService لجلب أفضل ملف خروج (إحصائياً).
"""
if not self.initialized:
return "Learning Hub not initialized."
best_profile = await self.statistical_analyzer.get_best_exit_profile(entry_strategy)
if best_profile != "unknown":
# (Prompt in English as requested)
feedback = f"Statistical Feedback: For the '{entry_strategy}' strategy, the '{best_profile}' exit profile has historically performed best."
return feedback
else:
return "No statistical feedback available for this strategy yet."
# 🔴 --- START OF CHANGE (V2 - VADER Learning) --- 🔴
async def get_statistical_news_score(self, raw_vader_score: float) -> float:
"""
يحول درجة VADER الخام إلى متوسط الربح/الخسارة التاريخي المتوقع.
(يُستخدم بواسطة app.py / MLProcessor للترتيب الداخلي)
"""
if not self.initialized:
return 0.0 # محايد
# (جلب متوسط الربح/الخسارة الفعلي من المحلل الإحصائي)
historical_pnl = await self.statistical_analyzer.get_statistical_vader_pnl(raw_vader_score)
# (إرجاع النسبة المئوية للربح/الخسارة مباشرة، مثلاً: 1.1 أو -0.5)
return historical_pnl
# 🔴 --- END OF CHANGE --- 🔴
# 🔴 --- START OF CHANGE --- 🔴
async def get_optimized_weights(self, market_condition: str) -> Dict[str, float]:
"""
يُستخدم بواسطة MLProcessor/StrategyEngine/Sentry لجلب الأوزان المعدلة إحصائياً.
"""
if not self.initialized:
# (الحصول على كل الأوزان الافتراضية)
return await self.statistical_analyzer.get_default_strategy_weights()
# (الحصول على كل الأوزان المحسنة)
return await self.statistical_analyzer.get_optimized_weights(market_condition)
# 🔴 --- END OF CHANGE --- 🔴
async def run_distillation_check(self):
"""
(يتم استدعاؤها دورياً من app.py)
للتحقق من جميع المجالات وتشغيل التقطير إذا لزم الأمر.
"""
if not self.initialized:
return
print("ℹ️ [HubManager] Running periodic distillation check...")
for domain in self.memory_store.domain_files.keys():
await self.curator.check_and_distill_domain(domain)
print("✅ [HubManager] Distillation check complete.")
# (No change to shutdown function)
async def shutdown(self):
"""
Saves all persistent data from the statistical analyzer.
"""
if not self.initialized:
return
print("🔄 [HubManager] Shutting down... Saving all learning data.")
try:
await self.statistical_analyzer.save_weights_to_r2()
await self.statistical_analyzer.save_performance_history()
await self.statistical_analyzer.save_exit_profile_effectiveness()
# 🔴 --- START OF CHANGE (V2 - VADER Learning) --- 🔴
await self.statistical_analyzer.save_vader_effectiveness()
# 🔴 --- END OF CHANGE --- 🔴
print("✅ [HubManager] All statistical (slow-learner) data saved.")
except Exception as e:
print(f"❌ [HubManager] Failed to save learning data on shutdown: {e}")
# 🔴 --- START OF CHANGE --- 🔴
# (تم حذف القوس } الزائد من هنا)
# 🔴 --- END OF CHANGE --- 🔴 |