Riy777 commited on
Commit
cbd4bfa
·
verified ·
1 Parent(s): e5b484a

Update learning_hub/adaptive_hub.py

Browse files
Files changed (1) hide show
  1. learning_hub/adaptive_hub.py +60 -20
learning_hub/adaptive_hub.py CHANGED
@@ -1,6 +1,6 @@
1
  # ==============================================================================
2
  # 🧠 learning_hub/adaptive_hub.py
3
- # (V55.2 - GEM-Architect: Fixed Guardian Threshold Injection)
4
  # ==============================================================================
5
 
6
  import json
@@ -15,7 +15,8 @@ class StrategyDNA:
15
  self.name = name
16
  self.model_weights = model_weights
17
  self.ob_settings = ob_settings
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 {
@@ -41,10 +42,10 @@ class StrategyDNA:
41
  class AdaptiveHub:
42
  def __init__(self, r2_service=None):
43
  self.r2 = r2_service
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.2] Core Initialized.")
48
 
49
  async def initialize(self):
50
  try:
@@ -62,12 +63,41 @@ class AdaptiveHub:
62
  self._create_default_dna()
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)
70
- self.strategies["DEAD"] = StrategyDNA("DEAD", {"titan": 0.25, "structure": 0.25, "sniper": 0.25}, {"wall_ratio_limit": 0.20, "imbalance_thresh": 0.8}, {"l1_min_score": 0.85, "l3_conf_thresh": 0.80}, default_guards)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  def _load_from_dict(self, data):
73
  for key, val in data.get("strategies", {}).items():
@@ -100,11 +130,17 @@ class AdaptiveHub:
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
 
107
- # Fix: Actually Save the New Guard Thresholds
108
  champion.guard_settings['hydra_crash'] = new_config['hydra_thresh']
109
  champion.guard_settings['legacy_v2'] = new_config['legacy_thresh']
110
 
@@ -131,43 +167,47 @@ class AdaptiveHub:
131
  except Exception: traceback.print_exc()
132
 
133
  def _inject_current_parameters(self):
134
- """ ✅ Injects ALL Parameters (Including Guardian Thresholds) into SystemLimits """
135
  if self.current_market_regime not in self.strategies: return
136
  active_dna = self.strategies[self.current_market_regime]
137
 
138
  mw = active_dna.model_weights
139
  total_w = sum(mw.values()) if sum(mw.values()) > 0 else 1.0
140
 
 
141
  SystemLimits.L2_WEIGHT_TITAN = mw.get("titan", 0.4) / total_w
142
  SystemLimits.L2_WEIGHT_PATTERNS = mw.get("structure", 0.3) / total_w
143
 
 
144
  raw_score_limit = (active_dna.filters.get("l1_min_score", 0.65) * 100.0) - 20.0
145
  SystemLimits.L1_MIN_AFFINITY_SCORE = raw_score_limit
 
 
 
 
146
  SystemLimits.CURRENT_REGIME = self.current_market_regime
147
- SystemLimits.L3_CONFIDENCE_THRESHOLD = active_dna.filters.get("l3_conf_thresh", 0.65)
148
  SystemLimits.L4_OB_WALL_RATIO = active_dna.ob_settings.get("wall_ratio_limit", 0.4)
149
 
150
- # Fix: Inject Guardian Thresholds
151
  SystemLimits.HYDRA_CRASH_THRESH = active_dna.guard_settings.get('hydra_crash', 0.85)
152
  SystemLimits.LEGACY_V2_PANIC_THRESH = active_dna.guard_settings.get('legacy_v2', 0.95)
153
 
154
- # print(f" 💉 DNA Injected: Hydra={SystemLimits.HYDRA_CRASH_THRESH}, Legacy={SystemLimits.LEGACY_V2_PANIC_THRESH}")
155
 
156
  def get_status(self) -> str:
157
- """
158
- Returns status summary for UI.
159
- """
160
  dna = self.strategies.get(self.current_market_regime)
161
  if not dna: return "System Initializing..."
162
 
163
- thresh_ratio = dna.filters.get('l1_min_score', 0)
164
  titan_w = dna.model_weights.get('titan', 0)
 
 
165
  hydra_t = dna.guard_settings.get('hydra_crash', 0.85)
166
 
167
  return (f"Regime: {self.current_market_regime} | "
168
- f"L1 Thresh: {thresh_ratio:.0%} | "
169
  f"Titan: {titan_w:.2f} | "
170
- f"HydraGuard: {hydra_t:.2f}")
 
 
171
 
172
  async def _save_state_to_r2(self):
173
  if not self.r2: return
 
1
  # ==============================================================================
2
  # 🧠 learning_hub/adaptive_hub.py
3
+ # (V55.3 - GEM-Architect: Full Learning Loop - Oracle & Sniper Included)
4
  # ==============================================================================
5
 
6
  import json
 
15
  self.name = name
16
  self.model_weights = model_weights
17
  self.ob_settings = ob_settings
18
+ # filters now includes: l1_min_score, l3_oracle_thresh, l4_sniper_thresh
19
+ self.filters = filters
20
  self.guard_settings = guard_settings if guard_settings else {}
21
 
22
  self.backtest_performance = backtest_performance if backtest_performance else {
 
42
  class AdaptiveHub:
43
  def __init__(self, r2_service=None):
44
  self.r2 = r2_service
45
+ self.dna_file_key = "learning/strategic_dna_v6_full.json"
46
  self.current_market_regime = "RANGE"
47
  self.strategies: Dict[str, StrategyDNA] = {}
48
+ print("🧠 [AdaptiveHub V55.3] Full Learning Loop (Titan+Oracle+Sniper+Guards).")
49
 
50
  async def initialize(self):
51
  try:
 
63
  self._create_default_dna()
64
 
65
  def _create_default_dna(self):
66
+ # Default Guardians
67
+ d_guards = {"hydra_crash": 0.85, "hydra_giveback": 0.70, "legacy_v2": 0.95}
68
 
69
+ # Default Filters (Now includes Oracle & Sniper defaults)
70
+ # l3_oracle_thresh: 0.65 (Default)
71
+ # l4_sniper_thresh: 0.40 (Default)
72
+
73
+ self.strategies["BULL"] = StrategyDNA(
74
+ "BULL",
75
+ {"titan": 0.50, "structure": 0.30},
76
+ {"wall_ratio_limit": 0.60},
77
+ {"l1_min_score": 0.55, "l3_oracle_thresh": 0.60, "l4_sniper_thresh": 0.40},
78
+ d_guards
79
+ )
80
+ self.strategies["BEAR"] = StrategyDNA(
81
+ "BEAR",
82
+ {"titan": 0.30, "structure": 0.40},
83
+ {"wall_ratio_limit": 0.30},
84
+ {"l1_min_score": 0.75, "l3_oracle_thresh": 0.75, "l4_sniper_thresh": 0.50},
85
+ d_guards
86
+ )
87
+ self.strategies["RANGE"] = StrategyDNA(
88
+ "RANGE",
89
+ {"titan": 0.40, "structure": 0.40},
90
+ {"wall_ratio_limit": 0.40},
91
+ {"l1_min_score": 0.65, "l3_oracle_thresh": 0.65, "l4_sniper_thresh": 0.40},
92
+ d_guards
93
+ )
94
+ self.strategies["DEAD"] = StrategyDNA(
95
+ "DEAD",
96
+ {"titan": 0.25, "structure": 0.25},
97
+ {"wall_ratio_limit": 0.20},
98
+ {"l1_min_score": 0.85, "l3_oracle_thresh": 0.80, "l4_sniper_thresh": 0.60},
99
+ d_guards
100
+ )
101
 
102
  def _load_from_dict(self, data):
103
  for key, val in data.get("strategies", {}).items():
 
130
 
131
  if is_winner:
132
  print(f" ✅ [JUDGE] Challenger WINS! Updating DNA.")
133
+
134
+ # 1. Update Core Weights
135
  champion.model_weights['titan'] = new_config['w_titan']
136
  champion.model_weights['structure'] = new_config['w_struct']
137
+
138
+ # 2. Update Filters (L1 + Oracle + Sniper)
139
  champion.filters['l1_min_score'] = new_config['thresh']
140
+ champion.filters['l3_oracle_thresh'] = new_config.get('oracle_thresh', 0.65)
141
+ champion.filters['l4_sniper_thresh'] = new_config.get('sniper_thresh', 0.40)
142
 
143
+ # 3. Update Guardians
144
  champion.guard_settings['hydra_crash'] = new_config['hydra_thresh']
145
  champion.guard_settings['legacy_v2'] = new_config['legacy_thresh']
146
 
 
167
  except Exception: traceback.print_exc()
168
 
169
  def _inject_current_parameters(self):
170
+ """ ✅ Injects ALL Parameters into SystemLimits (Live System) """
171
  if self.current_market_regime not in self.strategies: return
172
  active_dna = self.strategies[self.current_market_regime]
173
 
174
  mw = active_dna.model_weights
175
  total_w = sum(mw.values()) if sum(mw.values()) > 0 else 1.0
176
 
177
+ # 1. Weights
178
  SystemLimits.L2_WEIGHT_TITAN = mw.get("titan", 0.4) / total_w
179
  SystemLimits.L2_WEIGHT_PATTERNS = mw.get("structure", 0.3) / total_w
180
 
181
+ # 2. Thresholds (L1, Oracle, Sniper)
182
  raw_score_limit = (active_dna.filters.get("l1_min_score", 0.65) * 100.0) - 20.0
183
  SystemLimits.L1_MIN_AFFINITY_SCORE = raw_score_limit
184
+ SystemLimits.L3_CONFIDENCE_THRESHOLD = active_dna.filters.get("l3_oracle_thresh", 0.65)
185
+ SystemLimits.L4_ENTRY_THRESHOLD = active_dna.filters.get("l4_sniper_thresh", 0.40)
186
+
187
+ # 3. Settings
188
  SystemLimits.CURRENT_REGIME = self.current_market_regime
 
189
  SystemLimits.L4_OB_WALL_RATIO = active_dna.ob_settings.get("wall_ratio_limit", 0.4)
190
 
191
+ # 4. Guardians
192
  SystemLimits.HYDRA_CRASH_THRESH = active_dna.guard_settings.get('hydra_crash', 0.85)
193
  SystemLimits.LEGACY_V2_PANIC_THRESH = active_dna.guard_settings.get('legacy_v2', 0.95)
194
 
195
+ # print(f" 💉 DNA Injected: Regime={self.current_market_regime}, Oracle={SystemLimits.L3_CONFIDENCE_THRESHOLD}, Sniper={SystemLimits.L4_ENTRY_THRESHOLD}")
196
 
197
  def get_status(self) -> str:
 
 
 
198
  dna = self.strategies.get(self.current_market_regime)
199
  if not dna: return "System Initializing..."
200
 
 
201
  titan_w = dna.model_weights.get('titan', 0)
202
+ oracle_t = dna.filters.get('l3_oracle_thresh', 0.65)
203
+ sniper_t = dna.filters.get('l4_sniper_thresh', 0.40)
204
  hydra_t = dna.guard_settings.get('hydra_crash', 0.85)
205
 
206
  return (f"Regime: {self.current_market_regime} | "
 
207
  f"Titan: {titan_w:.2f} | "
208
+ f"Oracle: {oracle_t:.2f} | "
209
+ f"Sniper: {sniper_t:.2f} | "
210
+ f"Hydra: {hydra_t:.2f}")
211
 
212
  async def _save_state_to_r2(self):
213
  if not self.r2: return