Spaces:
Paused
Paused
Update ml_engine/processor.py
Browse files- ml_engine/processor.py +43 -22
ml_engine/processor.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# ml_engine/processor.py
|
| 2 |
-
# (V13.
|
| 3 |
-
# -
|
| 4 |
-
# -
|
| 5 |
|
| 6 |
import asyncio
|
| 7 |
import numpy as np
|
|
@@ -52,7 +52,7 @@ class MLProcessor:
|
|
| 52 |
'take_profit_base': 0.025
|
| 53 |
}
|
| 54 |
|
| 55 |
-
print("✅ [MLProcessor V13.
|
| 56 |
|
| 57 |
async def initialize(self):
|
| 58 |
print("🔄 [Processor] Initializing Neural Core...")
|
|
@@ -122,8 +122,7 @@ class MLProcessor:
|
|
| 122 |
|
| 123 |
async def check_sniper_entry(self, ohlcv_1m: List[list], order_book: Dict) -> Dict[str, Any]:
|
| 124 |
"""
|
| 125 |
-
[L4 SNIPER LOGIC] - [FIXED
|
| 126 |
-
Must return a Dictionary {'passed': bool, 'reason': str}
|
| 127 |
"""
|
| 128 |
try:
|
| 129 |
# 1. Order Book Analysis
|
|
@@ -131,37 +130,61 @@ class MLProcessor:
|
|
| 131 |
asks = order_book.get('asks', [])
|
| 132 |
|
| 133 |
if not bids or not asks:
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
best_bid = float(bids[0][0])
|
| 137 |
best_ask = float(asks[0][0])
|
| 138 |
|
| 139 |
# Spread Check
|
| 140 |
spread_pct = (best_ask - best_bid) / best_bid
|
| 141 |
-
if spread_pct > 0.015:
|
| 142 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
# Sell Wall Check
|
| 145 |
bid_vol = sum([b[1] for b in bids[:5]])
|
| 146 |
ask_vol = sum([a[1] for a in asks[:5]])
|
| 147 |
|
| 148 |
if ask_vol > bid_vol * 5:
|
| 149 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
# 2. Momentum Check (Falling Knife)
|
| 152 |
if ohlcv_1m and len(ohlcv_1m) >= 3:
|
| 153 |
closes = [c[4] for c in ohlcv_1m]
|
| 154 |
change_last_2m = (closes[-1] - closes[-3]) / closes[-3]
|
| 155 |
-
if change_last_2m < -0.02:
|
| 156 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
except Exception as e:
|
| 161 |
print(f"⚠️ [Sniper] Check Error: {e}")
|
| 162 |
-
#
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
# ==============================================================================
|
| 167 |
# ⚙️ Internal Logic
|
|
@@ -217,14 +240,15 @@ class MLProcessor:
|
|
| 217 |
try:
|
| 218 |
closes = features.get('closes_15m')
|
| 219 |
if closes is None: return "Unknown"
|
| 220 |
-
|
|
|
|
| 221 |
if volatility > 0.02: return "High"
|
| 222 |
if volatility > 0.01: return "Medium"
|
| 223 |
return "Low"
|
| 224 |
except: return "Unknown"
|
| 225 |
|
| 226 |
# ==============================================================================
|
| 227 |
-
# 🛡️
|
| 228 |
# ==============================================================================
|
| 229 |
|
| 230 |
def consult_guardian(self, d1, d5, d15, entry_price):
|
|
@@ -247,7 +271,6 @@ class MLProcessor:
|
|
| 247 |
symbol = signal.get('symbol', 'UNKNOWN')
|
| 248 |
conf = signal.get('enhanced_final_score', 0.0)
|
| 249 |
price = signal.get('current_price', 0)
|
| 250 |
-
|
| 251 |
threshold = self.thresholds['buy_moderate']
|
| 252 |
|
| 253 |
if conf >= threshold:
|
|
@@ -261,12 +284,10 @@ class MLProcessor:
|
|
| 261 |
'sl_price': sl,
|
| 262 |
'reason': f'Approved (Score {conf:.2f})'
|
| 263 |
}
|
| 264 |
-
|
| 265 |
print(f" 🔮 [Oracle] REJECTED {symbol}: Score {conf:.2f} < {threshold}")
|
| 266 |
return {'action': 'IGNORE', 'reason': f'Score {conf:.2f} < {threshold}'}
|
| 267 |
-
|
| 268 |
except Exception as e:
|
| 269 |
-
print(f"⚠️ [Oracle] Error: {e}")
|
| 270 |
return {'action': 'IGNORE', 'reason': 'Oracle Logic Error'}
|
| 271 |
|
| 272 |
async def cleanup(self):
|
|
|
|
| 1 |
# ml_engine/processor.py
|
| 2 |
+
# (V13.13 - GEM-Architect: Sniper Signal Key Fix)
|
| 3 |
+
# - Added 'signal' key to check_sniper_entry return dict.
|
| 4 |
+
# - Fixes KeyError in TradeManager.
|
| 5 |
|
| 6 |
import asyncio
|
| 7 |
import numpy as np
|
|
|
|
| 52 |
'take_profit_base': 0.025
|
| 53 |
}
|
| 54 |
|
| 55 |
+
print("✅ [MLProcessor V13.13] Enterprise Engine Loaded (Sniper Signal Fix).")
|
| 56 |
|
| 57 |
async def initialize(self):
|
| 58 |
print("🔄 [Processor] Initializing Neural Core...")
|
|
|
|
| 122 |
|
| 123 |
async def check_sniper_entry(self, ohlcv_1m: List[list], order_book: Dict) -> Dict[str, Any]:
|
| 124 |
"""
|
| 125 |
+
[L4 SNIPER LOGIC] - [FIXED] Returns 'signal' key.
|
|
|
|
| 126 |
"""
|
| 127 |
try:
|
| 128 |
# 1. Order Book Analysis
|
|
|
|
| 130 |
asks = order_book.get('asks', [])
|
| 131 |
|
| 132 |
if not bids or not asks:
|
| 133 |
+
# Fail Open if data missing to avoid blocking
|
| 134 |
+
return {
|
| 135 |
+
'passed': True,
|
| 136 |
+
'signal': 'BUY',
|
| 137 |
+
'reason': 'OB Data Unavailable (Fail Open)'
|
| 138 |
+
}
|
| 139 |
|
| 140 |
best_bid = float(bids[0][0])
|
| 141 |
best_ask = float(asks[0][0])
|
| 142 |
|
| 143 |
# Spread Check
|
| 144 |
spread_pct = (best_ask - best_bid) / best_bid
|
| 145 |
+
if spread_pct > 0.015:
|
| 146 |
+
return {
|
| 147 |
+
'passed': False,
|
| 148 |
+
'signal': 'HOLD',
|
| 149 |
+
'reason': f'High Spread ({spread_pct*100:.2f}%)'
|
| 150 |
+
}
|
| 151 |
|
| 152 |
# Sell Wall Check
|
| 153 |
bid_vol = sum([b[1] for b in bids[:5]])
|
| 154 |
ask_vol = sum([a[1] for a in asks[:5]])
|
| 155 |
|
| 156 |
if ask_vol > bid_vol * 5:
|
| 157 |
+
return {
|
| 158 |
+
'passed': False,
|
| 159 |
+
'signal': 'HOLD',
|
| 160 |
+
'reason': f'Sell Wall (Ratio 1:{ask_vol/bid_vol:.1f})'
|
| 161 |
+
}
|
| 162 |
|
| 163 |
# 2. Momentum Check (Falling Knife)
|
| 164 |
if ohlcv_1m and len(ohlcv_1m) >= 3:
|
| 165 |
closes = [c[4] for c in ohlcv_1m]
|
| 166 |
change_last_2m = (closes[-1] - closes[-3]) / closes[-3]
|
| 167 |
+
if change_last_2m < -0.02:
|
| 168 |
+
return {
|
| 169 |
+
'passed': False,
|
| 170 |
+
'signal': 'HOLD',
|
| 171 |
+
'reason': 'Falling Knife Detected'
|
| 172 |
+
}
|
| 173 |
|
| 174 |
+
return {
|
| 175 |
+
'passed': True,
|
| 176 |
+
'signal': 'BUY',
|
| 177 |
+
'reason': 'Sniper Conditions Met'
|
| 178 |
+
}
|
| 179 |
|
| 180 |
except Exception as e:
|
| 181 |
print(f"⚠️ [Sniper] Check Error: {e}")
|
| 182 |
+
# Default to BUY on error (Fail Open) to ensure execution continuity
|
| 183 |
+
return {
|
| 184 |
+
'passed': True,
|
| 185 |
+
'signal': 'BUY',
|
| 186 |
+
'reason': f'Error ({e}) - Fail Open'
|
| 187 |
+
}
|
| 188 |
|
| 189 |
# ==============================================================================
|
| 190 |
# ⚙️ Internal Logic
|
|
|
|
| 240 |
try:
|
| 241 |
closes = features.get('closes_15m')
|
| 242 |
if closes is None: return "Unknown"
|
| 243 |
+
log_returns = np.diff(np.log(closes))
|
| 244 |
+
volatility = np.std(log_returns)
|
| 245 |
if volatility > 0.02: return "High"
|
| 246 |
if volatility > 0.01: return "Medium"
|
| 247 |
return "Low"
|
| 248 |
except: return "Unknown"
|
| 249 |
|
| 250 |
# ==============================================================================
|
| 251 |
+
# 🛡️ Decision Layers
|
| 252 |
# ==============================================================================
|
| 253 |
|
| 254 |
def consult_guardian(self, d1, d5, d15, entry_price):
|
|
|
|
| 271 |
symbol = signal.get('symbol', 'UNKNOWN')
|
| 272 |
conf = signal.get('enhanced_final_score', 0.0)
|
| 273 |
price = signal.get('current_price', 0)
|
|
|
|
| 274 |
threshold = self.thresholds['buy_moderate']
|
| 275 |
|
| 276 |
if conf >= threshold:
|
|
|
|
| 284 |
'sl_price': sl,
|
| 285 |
'reason': f'Approved (Score {conf:.2f})'
|
| 286 |
}
|
|
|
|
| 287 |
print(f" 🔮 [Oracle] REJECTED {symbol}: Score {conf:.2f} < {threshold}")
|
| 288 |
return {'action': 'IGNORE', 'reason': f'Score {conf:.2f} < {threshold}'}
|
|
|
|
| 289 |
except Exception as e:
|
| 290 |
+
print(f"⚠️ [Oracle] Critical Error: {e}")
|
| 291 |
return {'action': 'IGNORE', 'reason': 'Oracle Logic Error'}
|
| 292 |
|
| 293 |
async def cleanup(self):
|