Spaces:
Sleeping
Sleeping
Update learning_hub/adaptive_hub.py
Browse files- learning_hub/adaptive_hub.py +26 -27
learning_hub/adaptive_hub.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# ==============================================================================
|
| 2 |
# 🧠 learning_hub/adaptive_hub.py
|
| 3 |
-
# (V55.
|
| 4 |
# ==============================================================================
|
| 5 |
|
| 6 |
import json
|
|
@@ -18,10 +18,8 @@ class StrategyDNA:
|
|
| 18 |
self.filters = filters
|
| 19 |
self.guard_settings = guard_settings if guard_settings else {}
|
| 20 |
|
| 21 |
-
# ✅ الإضافة الجديدة: سجل أداء الباكتست الذي أنتج هذه الإعدادات
|
| 22 |
-
# هذا هو "السجل الرياضي" للبطل الحالي
|
| 23 |
self.backtest_performance = backtest_performance if backtest_performance else {
|
| 24 |
-
"net_profit": -9999.0,
|
| 25 |
"win_rate": 0.0,
|
| 26 |
"total_trades": 0,
|
| 27 |
"date_recorded": "N/A"
|
|
@@ -36,7 +34,7 @@ class StrategyDNA:
|
|
| 36 |
"ob_settings": self.ob_settings,
|
| 37 |
"filters": self.filters,
|
| 38 |
"guard_settings": self.guard_settings,
|
| 39 |
-
"backtest_performance": self.backtest_performance,
|
| 40 |
"stats": self.stats
|
| 41 |
}
|
| 42 |
|
|
@@ -46,7 +44,7 @@ class AdaptiveHub:
|
|
| 46 |
self.dna_file_key = "learning/strategic_dna_v5_struct.json"
|
| 47 |
self.current_market_regime = "RANGE"
|
| 48 |
self.strategies: Dict[str, StrategyDNA] = {}
|
| 49 |
-
print("🧠 [AdaptiveHub V55.
|
| 50 |
|
| 51 |
async def initialize(self):
|
| 52 |
try:
|
|
@@ -65,7 +63,7 @@ class AdaptiveHub:
|
|
| 65 |
|
| 66 |
def _create_default_dna(self):
|
| 67 |
default_guards = {"hydra_crash": 0.85, "hydra_giveback": 0.70, "legacy_v2": 0.95, "legacy_v3": 0.95}
|
| 68 |
-
|
| 69 |
self.strategies["BULL"] = StrategyDNA("BULL", {"titan": 0.50, "structure": 0.30, "sniper": 0.20}, {"wall_ratio_limit": 0.60, "imbalance_thresh": 0.5}, {"l1_min_score": 0.55, "l3_conf_thresh": 0.60}, default_guards)
|
| 70 |
self.strategies["BEAR"] = StrategyDNA("BEAR", {"titan": 0.30, "structure": 0.40, "sniper": 0.30}, {"wall_ratio_limit": 0.30, "imbalance_thresh": 0.7}, {"l1_min_score": 0.75, "l3_conf_thresh": 0.75}, default_guards)
|
| 71 |
self.strategies["RANGE"] = StrategyDNA("RANGE", {"titan": 0.40, "structure": 0.40, "sniper": 0.20}, {"wall_ratio_limit": 0.40, "imbalance_thresh": 0.6}, {"l1_min_score": 0.65, "l3_conf_thresh": 0.65}, default_guards)
|
|
@@ -79,25 +77,16 @@ class AdaptiveHub:
|
|
| 79 |
val["ob_settings"],
|
| 80 |
val["filters"],
|
| 81 |
val.get("guard_settings", {}),
|
| 82 |
-
val.get("backtest_performance", None)
|
| 83 |
)
|
| 84 |
self.current_market_regime = data.get("current_regime", "RANGE")
|
| 85 |
|
| 86 |
-
# 🔥🔥🔥 الدالة الجديدة: الحكم (The Judge) 🔥🔥🔥
|
| 87 |
def submit_challenger(self, regime: str, new_config: dict, new_stats: dict) -> bool:
|
| 88 |
-
"""
|
| 89 |
-
تقارن بين المتحدي الجديد والبطل الحالي.
|
| 90 |
-
تعيد True إذا فاز الجديد وتم التحديث، و False إذا تم رفضه.
|
| 91 |
-
"""
|
| 92 |
if regime not in self.strategies: return False
|
| 93 |
|
| 94 |
champion = self.strategies[regime]
|
| 95 |
old_stats = champion.backtest_performance
|
| 96 |
|
| 97 |
-
# معايير التحكيم:
|
| 98 |
-
# 1. الربح الصافي هو الملك.
|
| 99 |
-
# 2. إذا تساوى الربح، نختار نسبة الفوز الأعلى.
|
| 100 |
-
|
| 101 |
new_profit = new_stats.get('net_profit', -100)
|
| 102 |
old_profit = old_stats.get('net_profit', -9999)
|
| 103 |
|
|
@@ -106,25 +95,18 @@ class AdaptiveHub:
|
|
| 106 |
print(f" 🥊 Challenger: Profit ${new_profit:.2f} | WinRate {new_stats.get('win_rate', 0):.1f}%")
|
| 107 |
|
| 108 |
is_winner = False
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
if new_profit > old_profit:
|
| 112 |
-
is_winner = True
|
| 113 |
-
# القاعدة 2: إذا الربح متقارب جداً، نفضل نسبة الفوز الأعلى
|
| 114 |
-
elif abs(new_profit - old_profit) < 0.5 and new_stats.get('win_rate', 0) > old_stats.get('win_rate', 0):
|
| 115 |
-
is_winner = True
|
| 116 |
|
| 117 |
if is_winner:
|
| 118 |
print(f" ✅ [JUDGE] Challenger WINS! Updating DNA.")
|
| 119 |
-
# تحديث الجينات
|
| 120 |
champion.model_weights['titan'] = new_config['w_titan']
|
| 121 |
champion.model_weights['structure'] = new_config['w_struct']
|
| 122 |
champion.filters['l1_min_score'] = new_config['thresh']
|
| 123 |
-
# تحديث سجل البطل
|
| 124 |
champion.backtest_performance = new_stats
|
| 125 |
return True
|
| 126 |
else:
|
| 127 |
-
print(f" 🛡️ [JUDGE] Champion retains title.
|
| 128 |
return False
|
| 129 |
|
| 130 |
async def register_trade_outcome(self, trade_data: Dict[str, Any]):
|
|
@@ -159,6 +141,23 @@ class AdaptiveHub:
|
|
| 159 |
SystemLimits.L3_CONFIDENCE_THRESHOLD = active_dna.filters.get("l3_conf_thresh", 0.65)
|
| 160 |
SystemLimits.L4_OB_WALL_RATIO = active_dna.ob_settings.get("wall_ratio_limit", 0.4)
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
async def _save_state_to_r2(self):
|
| 163 |
if not self.r2: return
|
| 164 |
try:
|
|
|
|
| 1 |
# ==============================================================================
|
| 2 |
# 🧠 learning_hub/adaptive_hub.py
|
| 3 |
+
# (V55.1 - GEM-Architect: Fixed & Polished)
|
| 4 |
# ==============================================================================
|
| 5 |
|
| 6 |
import json
|
|
|
|
| 18 |
self.filters = filters
|
| 19 |
self.guard_settings = guard_settings if guard_settings else {}
|
| 20 |
|
|
|
|
|
|
|
| 21 |
self.backtest_performance = backtest_performance if backtest_performance else {
|
| 22 |
+
"net_profit": -9999.0,
|
| 23 |
"win_rate": 0.0,
|
| 24 |
"total_trades": 0,
|
| 25 |
"date_recorded": "N/A"
|
|
|
|
| 34 |
"ob_settings": self.ob_settings,
|
| 35 |
"filters": self.filters,
|
| 36 |
"guard_settings": self.guard_settings,
|
| 37 |
+
"backtest_performance": self.backtest_performance,
|
| 38 |
"stats": self.stats
|
| 39 |
}
|
| 40 |
|
|
|
|
| 44 |
self.dna_file_key = "learning/strategic_dna_v5_struct.json"
|
| 45 |
self.current_market_regime = "RANGE"
|
| 46 |
self.strategies: Dict[str, StrategyDNA] = {}
|
| 47 |
+
print("🧠 [AdaptiveHub V55.1] Core Initialized.")
|
| 48 |
|
| 49 |
async def initialize(self):
|
| 50 |
try:
|
|
|
|
| 63 |
|
| 64 |
def _create_default_dna(self):
|
| 65 |
default_guards = {"hydra_crash": 0.85, "hydra_giveback": 0.70, "legacy_v2": 0.95, "legacy_v3": 0.95}
|
| 66 |
+
|
| 67 |
self.strategies["BULL"] = StrategyDNA("BULL", {"titan": 0.50, "structure": 0.30, "sniper": 0.20}, {"wall_ratio_limit": 0.60, "imbalance_thresh": 0.5}, {"l1_min_score": 0.55, "l3_conf_thresh": 0.60}, default_guards)
|
| 68 |
self.strategies["BEAR"] = StrategyDNA("BEAR", {"titan": 0.30, "structure": 0.40, "sniper": 0.30}, {"wall_ratio_limit": 0.30, "imbalance_thresh": 0.7}, {"l1_min_score": 0.75, "l3_conf_thresh": 0.75}, default_guards)
|
| 69 |
self.strategies["RANGE"] = StrategyDNA("RANGE", {"titan": 0.40, "structure": 0.40, "sniper": 0.20}, {"wall_ratio_limit": 0.40, "imbalance_thresh": 0.6}, {"l1_min_score": 0.65, "l3_conf_thresh": 0.65}, default_guards)
|
|
|
|
| 77 |
val["ob_settings"],
|
| 78 |
val["filters"],
|
| 79 |
val.get("guard_settings", {}),
|
| 80 |
+
val.get("backtest_performance", None)
|
| 81 |
)
|
| 82 |
self.current_market_regime = data.get("current_regime", "RANGE")
|
| 83 |
|
|
|
|
| 84 |
def submit_challenger(self, regime: str, new_config: dict, new_stats: dict) -> bool:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
if regime not in self.strategies: return False
|
| 86 |
|
| 87 |
champion = self.strategies[regime]
|
| 88 |
old_stats = champion.backtest_performance
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
new_profit = new_stats.get('net_profit', -100)
|
| 91 |
old_profit = old_stats.get('net_profit', -9999)
|
| 92 |
|
|
|
|
| 95 |
print(f" 🥊 Challenger: Profit ${new_profit:.2f} | WinRate {new_stats.get('win_rate', 0):.1f}%")
|
| 96 |
|
| 97 |
is_winner = False
|
| 98 |
+
if new_profit > old_profit: is_winner = True
|
| 99 |
+
elif abs(new_profit - old_profit) < 0.5 and new_stats.get('win_rate', 0) > old_stats.get('win_rate', 0): is_winner = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
if is_winner:
|
| 102 |
print(f" ✅ [JUDGE] Challenger WINS! Updating DNA.")
|
|
|
|
| 103 |
champion.model_weights['titan'] = new_config['w_titan']
|
| 104 |
champion.model_weights['structure'] = new_config['w_struct']
|
| 105 |
champion.filters['l1_min_score'] = new_config['thresh']
|
|
|
|
| 106 |
champion.backtest_performance = new_stats
|
| 107 |
return True
|
| 108 |
else:
|
| 109 |
+
print(f" 🛡️ [JUDGE] Champion retains title.")
|
| 110 |
return False
|
| 111 |
|
| 112 |
async def register_trade_outcome(self, trade_data: Dict[str, Any]):
|
|
|
|
| 141 |
SystemLimits.L3_CONFIDENCE_THRESHOLD = active_dna.filters.get("l3_conf_thresh", 0.65)
|
| 142 |
SystemLimits.L4_OB_WALL_RATIO = active_dna.ob_settings.get("wall_ratio_limit", 0.4)
|
| 143 |
|
| 144 |
+
# ✅ الدالة المضافة لحل الخطأ
|
| 145 |
+
def get_status(self) -> str:
|
| 146 |
+
"""
|
| 147 |
+
تعيد ملخصاً نصياً لحالة النظام الحالية (للعرض في السجلات أو الواجهة).
|
| 148 |
+
"""
|
| 149 |
+
dna = self.strategies.get(self.current_market_regime)
|
| 150 |
+
if not dna: return "System Initializing..."
|
| 151 |
+
|
| 152 |
+
thresh_ratio = dna.filters.get('l1_min_score', 0)
|
| 153 |
+
titan_w = dna.model_weights.get('titan', 0)
|
| 154 |
+
struct_w = dna.model_weights.get('structure', 0)
|
| 155 |
+
|
| 156 |
+
return (f"Regime: {self.current_market_regime} | "
|
| 157 |
+
f"L1 Thresh: {thresh_ratio:.0%} | "
|
| 158 |
+
f"Titan: {titan_w:.2f} | "
|
| 159 |
+
f"Struct: {struct_w:.2f}")
|
| 160 |
+
|
| 161 |
async def _save_state_to_r2(self):
|
| 162 |
if not self.r2: return
|
| 163 |
try:
|