Spaces:
Paused
Paused
Update ml_engine/hybrid_guardian.py
Browse files- ml_engine/hybrid_guardian.py +13 -30
ml_engine/hybrid_guardian.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# ml_engine/hybrid_guardian.py
|
| 2 |
-
# (V35.
|
| 3 |
|
| 4 |
import os
|
| 5 |
import json
|
|
@@ -12,9 +12,8 @@ import traceback
|
|
| 12 |
class HybridDeepSteward:
|
| 13 |
def __init__(self, v2_model_path, v3_model_path, v3_features_map_path):
|
| 14 |
"""
|
| 15 |
-
The Hybrid Guardian (Time-Aware
|
| 16 |
-
-
|
| 17 |
-
- Veto: Checks if Order Book has enough depth to absorb 30m of Volume.
|
| 18 |
"""
|
| 19 |
self.v2_path = v2_model_path
|
| 20 |
self.v3_path = v3_model_path
|
|
@@ -29,8 +28,7 @@ class HybridDeepSteward:
|
|
| 29 |
self.FEATS_5M = ['log_ret_5m', 'rsi_5m', 'fib_pos_5m', 'trend_slope_5m']
|
| 30 |
self.FEATS_15M = ['log_ret_15m', 'rsi_15m', 'dist_fib618_15m', 'trend_slope_15m']
|
| 31 |
|
| 32 |
-
# Thresholds
|
| 33 |
-
self.V2_SAFE_LIMIT = 0.40
|
| 34 |
self.V2_PANIC_TRIGGER = 0.95
|
| 35 |
self.V3_SOFT_EXIT = 0.85
|
| 36 |
self.V3_HARD_EXIT = 0.95
|
|
@@ -48,6 +46,14 @@ class HybridDeepSteward:
|
|
| 48 |
return True
|
| 49 |
except: return False
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
def _prepare_df(self, ohlcv_data):
|
| 52 |
if not ohlcv_data: return pd.DataFrame()
|
| 53 |
try:
|
|
@@ -121,39 +127,18 @@ class HybridDeepSteward:
|
|
| 121 |
# 🔍 GEM-Architect: The Time-Weighted Logic (30-Minute Check)
|
| 122 |
# ==========================================================================
|
| 123 |
def _validate_with_time_weighted_ob(self, order_book, signal_type, volume_30m_usd):
|
| 124 |
-
"""
|
| 125 |
-
Validates if the Order Book 'Wall' is significant relative to the last 30 minutes of volume.
|
| 126 |
-
|
| 127 |
-
Logic:
|
| 128 |
-
- If 'EXIT_HARD' (Panic Sell) is triggered:
|
| 129 |
-
We check if (Buy Wall Size) > (Signficant % of 30m Volume).
|
| 130 |
-
If True, it means there is massive absorption capability -> BLOCK the Exit.
|
| 131 |
-
"""
|
| 132 |
if not order_book: return True
|
| 133 |
-
if volume_30m_usd <= 0: return True
|
| 134 |
|
| 135 |
try:
|
| 136 |
bids = order_book.get('bids', [])
|
| 137 |
-
asks = order_book.get('asks', [])
|
| 138 |
-
|
| 139 |
if not bids: return True
|
| 140 |
|
| 141 |
-
# 1. Calculate 'Buy Wall' (Liquidity close to price)
|
| 142 |
-
# We sum the top 15 levels of bids (approx immediate support)
|
| 143 |
bid_wall_usd = sum([float(x[0]) * float(x[1]) for x in bids[:15]])
|
| 144 |
-
|
| 145 |
-
# 2. Compare Wall vs 30m Flow
|
| 146 |
-
# If the immediate Buy Wall is larger than 10% of the ENTIRE 30m volume,
|
| 147 |
-
# that is a HUGE static wall. It implies stability for the near future.
|
| 148 |
absorption_ratio = bid_wall_usd / volume_30m_usd
|
| 149 |
|
| 150 |
-
# 3. Decision Logic
|
| 151 |
if signal_type == 'EXIT_HARD':
|
| 152 |
-
# If Buy Wall is strong (> 5% of 30m volume sitting in top 15 ticks),
|
| 153 |
-
# we consider this a "Safe Floor".
|
| 154 |
if absorption_ratio > 0.05:
|
| 155 |
-
# يوجد جدار شراء يمثل 5% من حجم تداول نصف ساعة كاملة في أول مستويات فقط!
|
| 156 |
-
# هذا دعم قوي جداً زمنياً.
|
| 157 |
return False # VETO THE EXIT
|
| 158 |
|
| 159 |
return True
|
|
@@ -185,8 +170,6 @@ class HybridDeepSteward:
|
|
| 185 |
v2 = scores['v2']
|
| 186 |
v3 = scores['v3']
|
| 187 |
|
| 188 |
-
# 🛑 VETO CHECK
|
| 189 |
-
# If veto returns FALSE, we BLOCK the exit.
|
| 190 |
allow_exit = self._validate_with_time_weighted_ob(order_book, 'EXIT_HARD', volume_30m_usd)
|
| 191 |
|
| 192 |
# --- V2 CHECK ---
|
|
|
|
| 1 |
# ml_engine/hybrid_guardian.py
|
| 2 |
+
# (V35.4 - GEM-Architect: Configurable Thresholds)
|
| 3 |
|
| 4 |
import os
|
| 5 |
import json
|
|
|
|
| 12 |
class HybridDeepSteward:
|
| 13 |
def __init__(self, v2_model_path, v3_model_path, v3_features_map_path):
|
| 14 |
"""
|
| 15 |
+
The Hybrid Guardian (Configurable & Time-Aware)
|
| 16 |
+
- Thresholds are now controlled by Processor/SystemLimits.
|
|
|
|
| 17 |
"""
|
| 18 |
self.v2_path = v2_model_path
|
| 19 |
self.v3_path = v3_model_path
|
|
|
|
| 28 |
self.FEATS_5M = ['log_ret_5m', 'rsi_5m', 'fib_pos_5m', 'trend_slope_5m']
|
| 29 |
self.FEATS_15M = ['log_ret_15m', 'rsi_15m', 'dist_fib618_15m', 'trend_slope_15m']
|
| 30 |
|
| 31 |
+
# Default Thresholds (Will be overwritten by configure_thresholds)
|
|
|
|
| 32 |
self.V2_PANIC_TRIGGER = 0.95
|
| 33 |
self.V3_SOFT_EXIT = 0.85
|
| 34 |
self.V3_HARD_EXIT = 0.95
|
|
|
|
| 46 |
return True
|
| 47 |
except: return False
|
| 48 |
|
| 49 |
+
# ✅ GEM-Architect: External Control Method
|
| 50 |
+
def configure_thresholds(self, v2_panic, v3_hard, v3_soft, v3_ultra):
|
| 51 |
+
self.V2_PANIC_TRIGGER = float(v2_panic)
|
| 52 |
+
self.V3_HARD_EXIT = float(v3_hard)
|
| 53 |
+
self.V3_SOFT_EXIT = float(v3_soft)
|
| 54 |
+
self.V3_ULTRA_CONF = float(v3_ultra)
|
| 55 |
+
print(f" 🕸️ [Legacy Guard] Thresholds Updated: V2_PANIC={v2_panic}, V3_HARD={v3_hard}")
|
| 56 |
+
|
| 57 |
def _prepare_df(self, ohlcv_data):
|
| 58 |
if not ohlcv_data: return pd.DataFrame()
|
| 59 |
try:
|
|
|
|
| 127 |
# 🔍 GEM-Architect: The Time-Weighted Logic (30-Minute Check)
|
| 128 |
# ==========================================================================
|
| 129 |
def _validate_with_time_weighted_ob(self, order_book, signal_type, volume_30m_usd):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
if not order_book: return True
|
| 131 |
+
if volume_30m_usd <= 0: return True
|
| 132 |
|
| 133 |
try:
|
| 134 |
bids = order_book.get('bids', [])
|
|
|
|
|
|
|
| 135 |
if not bids: return True
|
| 136 |
|
|
|
|
|
|
|
| 137 |
bid_wall_usd = sum([float(x[0]) * float(x[1]) for x in bids[:15]])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
absorption_ratio = bid_wall_usd / volume_30m_usd
|
| 139 |
|
|
|
|
| 140 |
if signal_type == 'EXIT_HARD':
|
|
|
|
|
|
|
| 141 |
if absorption_ratio > 0.05:
|
|
|
|
|
|
|
| 142 |
return False # VETO THE EXIT
|
| 143 |
|
| 144 |
return True
|
|
|
|
| 170 |
v2 = scores['v2']
|
| 171 |
v3 = scores['v3']
|
| 172 |
|
|
|
|
|
|
|
| 173 |
allow_exit = self._validate_with_time_weighted_ob(order_book, 'EXIT_HARD', volume_30m_usd)
|
| 174 |
|
| 175 |
# --- V2 CHECK ---
|