Badumetsibb commited on
Commit
e890ee2
·
verified ·
1 Parent(s): e63f647

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -186
app.py CHANGED
@@ -44,8 +44,15 @@ if not firebase_admin._apps:
44
  except Exception as e:
45
  print(f"❌ Firebase Error: {e}")
46
 
 
 
 
 
 
 
 
47
  # ==========================================
48
- # 1. DATA SOURCES (CALENDAR & NEWS)
49
  # ==========================================
50
  class MacroCalendar:
51
  def __init__(self):
@@ -55,72 +62,56 @@ class MacroCalendar:
55
  self.last_fetch = 0
56
 
57
  def fetch(self):
58
- # Cache for 10 mins to avoid spamming their server
59
- if time.time() - self.last_fetch < 600 and self.cache:
60
- return self.cache
61
  try:
62
  r = requests.get(self.url, headers=self.headers, timeout=5)
63
  self.cache = r.json()
64
  self.last_fetch = time.time()
65
  return self.cache
66
- except:
67
- return []
68
 
69
  def get_ui_table(self):
70
  data = self.fetch()
71
  ui_data = []
72
  impact_map = {'Low': 1, 'Medium': 2, 'High': 3}
73
  today = datetime.datetime.utcnow().date()
74
-
75
  for i in data:
76
  try:
77
  dt = pd.to_datetime(i.get('date'), utc=True)
78
  if dt.date() == today:
79
  imp = i.get('impact', 'Low')
80
- if impact_map.get(imp, 1) >= 2: # Medium/High only
81
  ui_data.append([dt.strftime('%H:%M'), i.get('country'), i.get('title'), imp])
82
  except: continue
83
  return pd.DataFrame(ui_data, columns=["Time", "Country", "Event", "Impact"])
84
 
85
  def get_events_for_pair(self, pair):
86
- """Returns string of events relevant to a specific pair (e.g. AUDUSD -> AUD & USD news)"""
87
  data = self.fetch()
88
  ccy1, ccy2 = pair[:3], pair[3:]
89
  relevant = []
90
  now = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
91
-
92
  for i in data:
93
  try:
94
- country = i.get('country')
95
- # Map country code (USD is usually 'USD', but check generic mapping)
96
- if country in [ccy1, ccy2]:
97
  dt = pd.to_datetime(i.get('date'), utc=True)
98
- # Get events starting 2 hours ago up to 4 hours in future
99
  if (dt - now).total_seconds() > -7200 and (dt - now).total_seconds() < 14400:
100
- relevant.append(f"[{i.get('impact')}] {country}: {i.get('title')} @ {dt.strftime('%H:%M')}")
101
  except: continue
102
  return "; ".join(relevant) if relevant else "No major events nearby."
103
 
104
  def get_shock_features(self):
105
- """Calculates model inputs based on calendar proximity"""
106
  data = self.fetch()
107
  now = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
108
  recent_shock = 0.0
109
  decay = 0.0
110
-
111
  for i in data:
112
  try:
113
  if i.get('impact') == 'High':
114
  dt = pd.to_datetime(i.get('date'), utc=True)
115
- delta_hours = (now - dt).total_seconds() / 3600
116
- # If high impact news happened in last hour
117
- if 0 <= delta_hours <= 1:
118
- recent_shock = 1.0
119
- # Simple decay function
120
- if 0 <= delta_hours <= 24:
121
- decay += 1.0 / (delta_hours + 1)
122
  except: continue
123
-
124
  return recent_shock, decay
125
 
126
  calendar = MacroCalendar()
@@ -137,7 +128,7 @@ class GlobalSentinel:
137
  sentinel = GlobalSentinel()
138
 
139
  # ==========================================
140
- # 2. P&L MANAGER (The Fix for 0 Stats)
141
  # ==========================================
142
  class TradeManager:
143
  def __init__(self):
@@ -145,10 +136,8 @@ class TradeManager:
145
  self.ref_stats = db.reference("stats")
146
 
147
  def update_pnl(self, current_prices):
148
- """Checks open trades and closes them if > 4 hours or TP/SL hit"""
149
  trades = self.ref_trades.get()
150
  if not trades: return
151
-
152
  updates_made = False
153
  stats = self.ref_stats.get() or {"wins": 0, "losses": 0, "pips": 0.0}
154
 
@@ -156,55 +145,26 @@ class TradeManager:
156
  if t.get("status") == "OPEN":
157
  pair_raw = t['pair'].replace("=X", "")
158
  if pair_raw not in current_prices: continue
159
-
160
- curr_price = current_prices[pair_raw]
161
- entry_time = datetime.datetime.fromisoformat(t['timestamp'])
162
- now = datetime.datetime.now()
163
- duration = (now - entry_time).total_seconds() / 3600
164
-
165
- # Logic: Close after 4 hours OR if stop/profit logic (simplified here)
166
- # Calculating Pips (Standard Lot approximation)
167
- diff = curr_price - t['entry_price']
168
- if "JPY" in pair_raw: diff *= 100
169
- else: diff *= 10000
170
-
171
  if t['action'] == "SELL": diff = -diff
172
 
173
- # Close trade
174
- if duration >= 4:
 
175
  result = "WIN" if diff > 0 else "LOSS"
176
- self.ref_trades.child(key).update({
177
- "status": "CLOSED",
178
- "exit_price": curr_price,
179
- "pnl_pips": diff,
180
- "result": result,
181
- "exit_time": now.isoformat()
182
- })
183
-
184
- # Update Stats
185
- stats['pips'] = stats.get('pips', 0) + diff
186
- if result == "WIN": stats['wins'] = stats.get('wins', 0) + 1
187
- else: stats['losses'] = stats.get('losses', 0) + 1
188
  updates_made = True
189
-
190
- if updates_made:
191
- self.ref_stats.set(stats)
192
- print("💰 P&L Updated")
193
 
194
  trade_mgr = TradeManager()
195
 
196
  # ==========================================
197
- # 3. MODEL ARCHITECTURE
198
  # ==========================================
199
- FEATURE_COLS = [
200
- 'log_returns', 'volatility_10', 'volatility_30', 'momentum_5', 'momentum_20', 'momentum_60',
201
- 'body_pct', 'upper_wick', 'lower_wick', 'candle_range', 'close_position',
202
- 'volume_ratio', 'volume_momentum', 'ATR_15m', 'RSI_15m', 'Price_to_SMA', 'RSI_5m',
203
- 'price_to_sma10', 'price_to_sma20', 'price_to_sma50', 'hour_sin', 'hour_cos', 'day_sin', 'day_cos',
204
- 'Session_London', 'Session_NY', 'Session_Tokyo', 'Session_Overlap',
205
- 'News_Shock_Decay', 'Effective_Surprise', 'Recent_News_60m', 'Gold_Returns', 'Fed_Rate_Change'
206
- ]
207
-
208
  class EnhancedGraphTransformer(nn.Module):
209
  def __init__(self, num_nodes=4, feats_per_node=33, d_model=128, nhead=4, num_layers=3, dropout=0.2):
210
  super(EnhancedGraphTransformer, self).__init__()
@@ -220,7 +180,6 @@ class EnhancedGraphTransformer(nn.Module):
220
  self.spatial_norm = nn.LayerNorm(d_model); self.spatial_dropout = nn.Dropout(dropout)
221
  self.pre_head = nn.Linear(d_model, d_model)
222
  self.head = nn.Sequential(nn.Linear(d_model, 128), nn.GELU(), nn.Dropout(dropout), nn.Linear(128, 64), nn.GELU(), nn.Dropout(dropout), nn.Linear(64, 3))
223
-
224
  def forward(self, x):
225
  b, s, n, f = x.size()
226
  x_norm = self.instance_norm(x.permute(0, 2, 3, 1).contiguous().view(b*n, f, s)).permute(0, 2, 1).contiguous()
@@ -236,54 +195,47 @@ class EnhancedGraphTransformer(nn.Module):
236
  h_final = self.spatial_norm(last_state + h_spatial)
237
  return self.head(self.pre_head(h_final) + h_final)
238
 
 
 
 
 
 
 
 
 
 
239
  def engineer_features(df, gold_returns):
240
  df = df.copy()
241
- # Basics
242
  df['log_returns'] = np.log(df['Close'] / df['Close'].shift(1)).fillna(0)
243
  df['volatility_10'] = df['log_returns'].rolling(10).std()
244
  df['volatility_30'] = df['log_returns'].rolling(30).std()
245
  for m in [5, 20, 60]: df[f'momentum_{m}'] = df['Close'].pct_change(m)
246
-
247
- # Candle
248
  r = (df['High'] - df['Low']).replace(0, 1e-6)
249
  df['body_pct'] = (df['Close'] - df['Open']).abs() / r
250
  df['upper_wick'] = (df['High'] - df[['Open', 'Close']].max(axis=1)) / r
251
  df['lower_wick'] = (df[['Open', 'Close']].min(axis=1) - df['Low']) / r
252
  df['candle_range'] = r
253
  df['close_position'] = (df['Close'] - df['Low']) / r
254
-
255
- # Volume & Indicators
256
- df['volume_ratio'] = 1.0 # Yahoo volume unreliable for FX
257
- df['volume_momentum'] = 0.0
258
  df['ATR_15m'] = r.rolling(15).mean()
259
  delta = df['Close'].diff()
260
  gain = (delta.where(delta > 0, 0)).rolling(14).mean()
261
  loss = (-delta.where(delta < 0, 0)).rolling(14).mean()
262
  df['RSI_15m'] = 100 - (100 / (1 + (gain / (loss + 1e-9))))
263
- df['RSI_5m'] = df['RSI_15m']
264
-
265
  for p in [10, 20, 50]: df[f'price_to_sma{p}'] = df['Close'] / df['Close'].rolling(p).mean()
266
  df['Price_to_SMA'] = df['price_to_sma20']
267
-
268
- # Time
269
- hour = df.index.hour
270
- df['hour_sin'] = np.sin(2 * np.pi * hour / 24)
271
- df['hour_cos'] = np.cos(2 * np.pi * hour / 24)
272
- df['day_sin'] = np.sin(2 * np.pi * df.index.dayofweek / 7)
273
- df['day_cos'] = np.cos(2 * np.pi * df.index.dayofweek / 7)
274
- df['Session_London'] = ((hour >= 7) & (hour <= 16)).astype(float)
275
- df['Session_NY'] = ((hour >= 12) & (hour <= 21)).astype(float)
276
- df['Session_Tokyo'] = ((hour >= 0) & (hour <= 9)).astype(float)
277
  df['Session_Overlap'] = df['Session_London'] * df['Session_NY']
278
-
279
- # External / Calendar Injection
280
- recent_shock, decay = calendar.get_shock_features()
281
- df['News_Shock_Decay'] = decay
282
- df['Recent_News_60m'] = recent_shock
283
- df['Effective_Surprise'] = 0.0 # Placeholder as we lack forecast data in JSON
284
- df['Fed_Rate_Change'] = 0.0
285
  df['Gold_Returns'] = gold_returns
286
-
287
  return df[FEATURE_COLS].fillna(0)
288
 
289
  # ==========================================
@@ -296,137 +248,111 @@ class GeminiAgent:
296
 
297
  def consult(self, pair, action, price, tech_conf, headlines, pair_events):
298
  prompt = f"""
299
- Role: Senior Forex Risk Manager.
300
- Task: Validate a technical trade signal.
301
-
302
- Signal: {action} {pair} at {price} (Technical Confidence: {tech_conf:.2f})
303
-
304
- Context:
305
- 1. Pair Specific Events: {pair_events}
306
- 2. Global Headlines: {headlines}
307
 
308
  Rules:
309
- - If high-impact news for {pair} is imminent (< 30 mins), REJECT.
310
- - If news aligns with direction, CONFIRM.
 
311
 
312
- Output format strictly:
313
- VERDICT: [ALIGN or CONFLICT]
314
- REASONING: [Detailed explanation of why, referencing the specific news]
315
  """
316
  try:
317
  resp = self.model.generate_content(prompt)
318
  txt = resp.text
319
  verdict = "ALIGN" if "ALIGN" in txt else "CONFLICT"
320
  return verdict, txt
321
- except Exception as e:
322
- return "ALIGN", f"Agent Offline: {e}"
323
 
324
  agent = GeminiAgent()
325
 
326
  def run_system():
327
- log_messages = []
328
  try:
329
- # 1. Load Resources
330
- log_messages.append("⏳ Fetching Market Data...")
331
- try:
332
- checkpoint = torch.load(MODEL_PATH, map_location=DEVICE, weights_only=False)
333
- model = EnhancedGraphTransformer(num_nodes=4, feats_per_node=33).to(DEVICE)
334
- model.load_state_dict(checkpoint['model_state_dict'])
335
- model.eval()
336
- scaler = checkpoint.get('scaler', RobustScaler())
337
- except: return "❌ Model Load Failed", pd.DataFrame(), pd.DataFrame(), ""
338
-
339
- # 2. Fetch Prices
340
- dfs = []
341
- prices_now = {}
342
  for t in TICKERS:
343
  df = yf.download(t, period="5d", interval="5m", progress=False)
344
  if isinstance(df.columns, pd.MultiIndex): df.columns = df.columns.droplevel(1)
345
  dfs.append(df)
346
- prices_now[t.replace("=X","")] = df.iloc[-1]['Close']
347
 
348
  gold = yf.download("GC=F", period="5d", interval="5m", progress=False)
349
  if isinstance(gold.columns, pd.MultiIndex): gold.columns = gold.columns.droplevel(1)
350
 
351
- # 3. Update P&L
352
- trade_mgr.update_pnl(prices_now)
353
 
354
- # 4. Alignment & Feature Engineering
355
- common_idx = dfs[0].index
356
- for d in dfs[1:]: common_idx = common_idx.intersection(d.index)
357
- gold_aligned = gold.reindex(common_idx).ffill()
358
- gold_ret = np.log(gold_aligned['Close'] / gold_aligned['Close'].shift(1)).fillna(0)
359
 
360
- processed = [engineer_features(df.reindex(common_idx).ffill(), gold_ret).values for df in dfs]
361
- X_raw = np.stack(processed, axis=1)
 
362
 
363
- if len(X_raw) < SEQ_LEN: return "⚠️ Not enough data", pd.DataFrame(), pd.DataFrame(), ""
364
-
365
- X_scaled = scaler.transform(X_raw[-SEQ_LEN:].reshape(-1, 33)).reshape(1, SEQ_LEN, 4, 33)
366
-
367
- # 5. Inference
368
  with torch.no_grad():
369
- logits = model(torch.FloatTensor(X_scaled).to(DEVICE))
370
  probs = F.softmax(logits, dim=-1)
371
  confs, preds = torch.max(probs, dim=-1)
372
- preds = preds.numpy()[0]; confs = confs.numpy()[0]
373
 
374
- # 6. Logic Loop
375
  headlines = sentinel.get_headlines()
376
- results = []
377
- full_reasoning = ""
378
-
379
- dyn_thresholds = db.reference("thresholds").get() or {p: 0.35 for p in PAIRS}
380
 
381
  for i, pair in enumerate(PAIRS):
382
- cf = float(confs[i])
383
- thresh = dyn_thresholds.get(pair, 0.35)
384
- p_idx = preds[i]
385
- price = prices_now[pair]
386
-
387
- # Get PAIR SPECIFIC Events
388
- pair_events = calendar.get_events_for_pair(pair)
389
 
390
- action = "HOLD"
391
- color = "⚪"
392
 
393
  if cf > thresh and p_idx != 1:
394
- raw_action = "BUY" if p_idx == 2 else "SELL"
 
 
395
 
396
- # Consult Agent with SPECIFIC Context
397
- verdict, reasoning = agent.consult(pair, raw_action, price, cf, headlines, pair_events)
398
- full_reasoning += f"**{pair} Analysis:**\n{reasoning}\n\n---\n"
399
 
400
  if verdict == "ALIGN":
401
- action = raw_action
402
  color = "🟢" if action == "BUY" else "🔴"
403
-
404
- # Log & Alert
405
- trade_data = {
406
  "pair": pair, "action": action, "entry_price": price,
407
- "conf": cf, "timestamp": datetime.datetime.now().isoformat(),
408
- "status": "OPEN", "reasoning": reasoning[:100]
409
- }
410
- db.reference("trade_history").push(trade_data)
411
  requests.post(f"https://ntfy.sh/{NTFY_TOPIC}",
412
- data=f"⚡ {pair} {action}\nReason: {reasoning}".encode("utf-8"),
413
  headers={"Title": f"Trade Executed ({cf:.2f})"})
414
  else:
415
- action = f"BLOCKED ({raw_action})"
416
  color = "🛡️"
417
- db.reference("conflicts").push({"pair": pair, "reason": reasoning, "time": datetime.datetime.now().isoformat()})
 
 
 
 
418
 
419
  results.append([pair, f"{color} {action}", f"{cf:.3f}", f"{thresh:.3f}"])
420
 
421
- # 7. Final Stats
422
  stats = db.reference("stats").get() or {}
423
  stat_str = f"Wins: {stats.get('wins',0)} | Losses: {stats.get('losses',0)} | Net Pips: {stats.get('pips',0):.1f}"
424
-
425
- return stat_str, results, calendar.get_ui_table(), full_reasoning
426
 
427
  except Exception as e:
428
- print(f"CRITICAL LOOP ERROR: {e}")
429
- return f"Error: {str(e)}", [], pd.DataFrame(), ""
430
 
431
  # ==========================================
432
  # 5. UI
@@ -435,26 +361,23 @@ with gr.Blocks(title="Sentinel Pro", theme=gr.themes.Soft()) as demo:
435
  gr.Markdown("# 🦅 Sentinel Pro: Autonomous Multi-Asset Agent")
436
 
437
  with gr.Row():
438
- status_box = gr.Textbox(label="Account Health (Auto-Updated)", value="Initializing...")
439
  scan_btn = gr.Button("FORCE SCAN", variant="primary")
440
 
441
  with gr.Tabs():
442
  with gr.Tab("Live Command"):
443
  res_table = gr.Dataframe(headers=["Pair", "Signal", "Conf", "Thresh"], label="Live Signals")
444
- reasoning_box = gr.Markdown(label="Agent Reasoning (Full Text)")
445
-
446
  with gr.Tab("Macro Intelligence"):
447
- cal_table = gr.Dataframe(label="Today's Calendar (High Impact)")
448
 
449
- # AUTO-UPDATE LOGIC
450
- # Running every 300s (5 mins). The function handles errors internally so timer won't die.
451
  timer = gr.Timer(300)
452
 
453
- inputs = []
454
- outputs = [status_box, res_table, cal_table, reasoning_box]
455
 
456
- scan_btn.click(run_system, inputs=inputs, outputs=outputs)
457
- timer.tick(run_system, inputs=inputs, outputs=outputs)
458
 
459
  if __name__ == "__main__":
460
  demo.launch()
 
44
  except Exception as e:
45
  print(f"❌ Firebase Error: {e}")
46
 
47
+ # SEND STARTUP NOTIFICATION
48
+ try:
49
+ requests.post(f"https://ntfy.sh/{NTFY_TOPIC}",
50
+ data="System Online. Waiting for signals...".encode("utf-8"),
51
+ headers={"Title": "🦅 Sentinel Started", "Tags": "zap"})
52
+ except: pass
53
+
54
  # ==========================================
55
+ # 1. DATA SOURCES
56
  # ==========================================
57
  class MacroCalendar:
58
  def __init__(self):
 
62
  self.last_fetch = 0
63
 
64
  def fetch(self):
65
+ if time.time() - self.last_fetch < 600 and self.cache: return self.cache
 
 
66
  try:
67
  r = requests.get(self.url, headers=self.headers, timeout=5)
68
  self.cache = r.json()
69
  self.last_fetch = time.time()
70
  return self.cache
71
+ except: return []
 
72
 
73
  def get_ui_table(self):
74
  data = self.fetch()
75
  ui_data = []
76
  impact_map = {'Low': 1, 'Medium': 2, 'High': 3}
77
  today = datetime.datetime.utcnow().date()
 
78
  for i in data:
79
  try:
80
  dt = pd.to_datetime(i.get('date'), utc=True)
81
  if dt.date() == today:
82
  imp = i.get('impact', 'Low')
83
+ if impact_map.get(imp, 1) >= 2:
84
  ui_data.append([dt.strftime('%H:%M'), i.get('country'), i.get('title'), imp])
85
  except: continue
86
  return pd.DataFrame(ui_data, columns=["Time", "Country", "Event", "Impact"])
87
 
88
  def get_events_for_pair(self, pair):
 
89
  data = self.fetch()
90
  ccy1, ccy2 = pair[:3], pair[3:]
91
  relevant = []
92
  now = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
 
93
  for i in data:
94
  try:
95
+ if i.get('country') in [ccy1, ccy2, 'USD']: # USD always relevant
 
 
96
  dt = pd.to_datetime(i.get('date'), utc=True)
 
97
  if (dt - now).total_seconds() > -7200 and (dt - now).total_seconds() < 14400:
98
+ relevant.append(f"[{i.get('impact')}] {i.get('country')}: {i.get('title')} @ {dt.strftime('%H:%M')}")
99
  except: continue
100
  return "; ".join(relevant) if relevant else "No major events nearby."
101
 
102
  def get_shock_features(self):
 
103
  data = self.fetch()
104
  now = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
105
  recent_shock = 0.0
106
  decay = 0.0
 
107
  for i in data:
108
  try:
109
  if i.get('impact') == 'High':
110
  dt = pd.to_datetime(i.get('date'), utc=True)
111
+ hours = (now - dt).total_seconds() / 3600
112
+ if 0 <= hours <= 1: recent_shock = 1.0
113
+ if 0 <= hours <= 24: decay += 1.0 / (hours + 1)
 
 
 
 
114
  except: continue
 
115
  return recent_shock, decay
116
 
117
  calendar = MacroCalendar()
 
128
  sentinel = GlobalSentinel()
129
 
130
  # ==========================================
131
+ # 2. P&L MANAGER
132
  # ==========================================
133
  class TradeManager:
134
  def __init__(self):
 
136
  self.ref_stats = db.reference("stats")
137
 
138
  def update_pnl(self, current_prices):
 
139
  trades = self.ref_trades.get()
140
  if not trades: return
 
141
  updates_made = False
142
  stats = self.ref_stats.get() or {"wins": 0, "losses": 0, "pips": 0.0}
143
 
 
145
  if t.get("status") == "OPEN":
146
  pair_raw = t['pair'].replace("=X", "")
147
  if pair_raw not in current_prices: continue
148
+ curr = current_prices[pair_raw]
149
+ diff = (curr - t['entry_price']) * (100 if "JPY" in pair_raw else 10000)
 
 
 
 
 
 
 
 
 
 
150
  if t['action'] == "SELL": diff = -diff
151
 
152
+ # Close logic: > 4 hours
153
+ entry_time = datetime.datetime.fromisoformat(t['timestamp'])
154
+ if (datetime.datetime.now() - entry_time).total_seconds() > 14400:
155
  result = "WIN" if diff > 0 else "LOSS"
156
+ self.ref_trades.child(key).update({"status": "CLOSED", "exit_price": curr, "pnl_pips": diff, "result": result})
157
+ stats['pips'] += diff
158
+ if result == "WIN": stats['wins'] += 1
159
+ else: stats['losses'] += 1
 
 
 
 
 
 
 
 
160
  updates_made = True
161
+ if updates_made: self.ref_stats.set(stats)
 
 
 
162
 
163
  trade_mgr = TradeManager()
164
 
165
  # ==========================================
166
+ # 3. MODEL
167
  # ==========================================
 
 
 
 
 
 
 
 
 
168
  class EnhancedGraphTransformer(nn.Module):
169
  def __init__(self, num_nodes=4, feats_per_node=33, d_model=128, nhead=4, num_layers=3, dropout=0.2):
170
  super(EnhancedGraphTransformer, self).__init__()
 
180
  self.spatial_norm = nn.LayerNorm(d_model); self.spatial_dropout = nn.Dropout(dropout)
181
  self.pre_head = nn.Linear(d_model, d_model)
182
  self.head = nn.Sequential(nn.Linear(d_model, 128), nn.GELU(), nn.Dropout(dropout), nn.Linear(128, 64), nn.GELU(), nn.Dropout(dropout), nn.Linear(64, 3))
 
183
  def forward(self, x):
184
  b, s, n, f = x.size()
185
  x_norm = self.instance_norm(x.permute(0, 2, 3, 1).contiguous().view(b*n, f, s)).permute(0, 2, 1).contiguous()
 
195
  h_final = self.spatial_norm(last_state + h_spatial)
196
  return self.head(self.pre_head(h_final) + h_final)
197
 
198
+ FEATURE_COLS = [
199
+ 'log_returns', 'volatility_10', 'volatility_30', 'momentum_5', 'momentum_20', 'momentum_60',
200
+ 'body_pct', 'upper_wick', 'lower_wick', 'candle_range', 'close_position',
201
+ 'volume_ratio', 'volume_momentum', 'ATR_15m', 'RSI_15m', 'Price_to_SMA', 'RSI_5m',
202
+ 'price_to_sma10', 'price_to_sma20', 'price_to_sma50', 'hour_sin', 'hour_cos', 'day_sin', 'day_cos',
203
+ 'Session_London', 'Session_NY', 'Session_Tokyo', 'Session_Overlap',
204
+ 'News_Shock_Decay', 'Effective_Surprise', 'Recent_News_60m', 'Gold_Returns', 'Fed_Rate_Change'
205
+ ]
206
+
207
  def engineer_features(df, gold_returns):
208
  df = df.copy()
 
209
  df['log_returns'] = np.log(df['Close'] / df['Close'].shift(1)).fillna(0)
210
  df['volatility_10'] = df['log_returns'].rolling(10).std()
211
  df['volatility_30'] = df['log_returns'].rolling(30).std()
212
  for m in [5, 20, 60]: df[f'momentum_{m}'] = df['Close'].pct_change(m)
 
 
213
  r = (df['High'] - df['Low']).replace(0, 1e-6)
214
  df['body_pct'] = (df['Close'] - df['Open']).abs() / r
215
  df['upper_wick'] = (df['High'] - df[['Open', 'Close']].max(axis=1)) / r
216
  df['lower_wick'] = (df[['Open', 'Close']].min(axis=1) - df['Low']) / r
217
  df['candle_range'] = r
218
  df['close_position'] = (df['Close'] - df['Low']) / r
219
+ df['volume_ratio'] = 1.0; df['volume_momentum'] = 0.0
 
 
 
220
  df['ATR_15m'] = r.rolling(15).mean()
221
  delta = df['Close'].diff()
222
  gain = (delta.where(delta > 0, 0)).rolling(14).mean()
223
  loss = (-delta.where(delta < 0, 0)).rolling(14).mean()
224
  df['RSI_15m'] = 100 - (100 / (1 + (gain / (loss + 1e-9))))
225
+ df['RSI_5m'] = df['RSI_15m']
 
226
  for p in [10, 20, 50]: df[f'price_to_sma{p}'] = df['Close'] / df['Close'].rolling(p).mean()
227
  df['Price_to_SMA'] = df['price_to_sma20']
228
+ h = df.index.hour
229
+ df['hour_sin'] = np.sin(2 * np.pi * h / 24); df['hour_cos'] = np.cos(2 * np.pi * h / 24)
230
+ df['day_sin'] = np.sin(2 * np.pi * df.index.dayofweek / 7); df['day_cos'] = np.cos(2 * np.pi * df.index.dayofweek / 7)
231
+ df['Session_London'] = ((h >= 7) & (h <= 16)).astype(float)
232
+ df['Session_NY'] = ((h >= 12) & (h <= 21)).astype(float)
233
+ df['Session_Tokyo'] = ((h >= 0) & (h <= 9)).astype(float)
 
 
 
 
234
  df['Session_Overlap'] = df['Session_London'] * df['Session_NY']
235
+ s, d = calendar.get_shock_features()
236
+ df['News_Shock_Decay'] = d; df['Recent_News_60m'] = s
237
+ df['Effective_Surprise'] = 0.0; df['Fed_Rate_Change'] = 0.0
 
 
 
 
238
  df['Gold_Returns'] = gold_returns
 
239
  return df[FEATURE_COLS].fillna(0)
240
 
241
  # ==========================================
 
248
 
249
  def consult(self, pair, action, price, tech_conf, headlines, pair_events):
250
  prompt = f"""
251
+ Role: Forex Risk Manager.
252
+ Signal: {action} {pair} @ {price} (Conf: {tech_conf:.2f})
253
+ Events: {pair_events}
254
+ News: {headlines}
 
 
 
 
255
 
256
  Rules:
257
+ 1. REJECT if high-impact news for {pair} is < 30 mins away.
258
+ 2. REJECT if global news clearly opposes trade direction.
259
+ 3. OTHERWISE ALIGN.
260
 
261
+ Format: VERDICT: [ALIGN/CONFLICT] | REASON: [Brief explanation]
 
 
262
  """
263
  try:
264
  resp = self.model.generate_content(prompt)
265
  txt = resp.text
266
  verdict = "ALIGN" if "ALIGN" in txt else "CONFLICT"
267
  return verdict, txt
268
+ except: return "ALIGN", "Agent Offline"
 
269
 
270
  agent = GeminiAgent()
271
 
272
  def run_system():
 
273
  try:
274
+ # Load Model
275
+ checkpoint = torch.load(MODEL_PATH, map_location=DEVICE, weights_only=False)
276
+ model = EnhancedGraphTransformer(num_nodes=4, feats_per_node=33).to(DEVICE)
277
+ model.load_state_dict(checkpoint['model_state_dict'])
278
+ model.eval()
279
+ scaler = checkpoint.get('scaler', RobustScaler())
280
+
281
+ # Fetch Data
282
+ dfs = []; prices = {}
 
 
 
 
283
  for t in TICKERS:
284
  df = yf.download(t, period="5d", interval="5m", progress=False)
285
  if isinstance(df.columns, pd.MultiIndex): df.columns = df.columns.droplevel(1)
286
  dfs.append(df)
287
+ prices[t.replace("=X","")] = df.iloc[-1]['Close']
288
 
289
  gold = yf.download("GC=F", period="5d", interval="5m", progress=False)
290
  if isinstance(gold.columns, pd.MultiIndex): gold.columns = gold.columns.droplevel(1)
291
 
292
+ trade_mgr.update_pnl(prices)
 
293
 
294
+ # Infer
295
+ common = dfs[0].index
296
+ for d in dfs[1:]: common = common.intersection(d.index)
297
+ gold = gold.reindex(common).ffill()
298
+ g_ret = np.log(gold['Close'] / gold['Close'].shift(1)).fillna(0)
299
 
300
+ processed = [engineer_features(df.reindex(common).ffill(), g_ret).values for df in dfs]
301
+ X = np.stack(processed, axis=1)
302
+ X_s = scaler.transform(X[-SEQ_LEN:].reshape(-1, 33)).reshape(1, SEQ_LEN, 4, 33)
303
 
 
 
 
 
 
304
  with torch.no_grad():
305
+ logits = model(torch.FloatTensor(X_s).to(DEVICE))
306
  probs = F.softmax(logits, dim=-1)
307
  confs, preds = torch.max(probs, dim=-1)
 
308
 
309
+ # Decisions
310
  headlines = sentinel.get_headlines()
311
+ results = []; full_log = ""
312
+ thresholds = db.reference("thresholds").get() or {p: 0.35 for p in PAIRS}
 
 
313
 
314
  for i, pair in enumerate(PAIRS):
315
+ cf = float(confs[0][i])
316
+ thresh = thresholds.get(pair, 0.35)
317
+ p_idx = preds[0][i].item()
318
+ price = prices[pair]
 
 
 
319
 
320
+ action = "HOLD"; color = "⚪"
 
321
 
322
  if cf > thresh and p_idx != 1:
323
+ raw_act = "BUY" if p_idx == 2 else "SELL"
324
+ events = calendar.get_events_for_pair(pair)
325
+ verdict, reason = agent.consult(pair, raw_act, price, cf, headlines, events)
326
 
327
+ full_log += f"**{pair}**: {reason}\n\n"
 
 
328
 
329
  if verdict == "ALIGN":
330
+ action = raw_act
331
  color = "🟢" if action == "BUY" else "🔴"
332
+ db.reference("trade_history").push({
 
 
333
  "pair": pair, "action": action, "entry_price": price,
334
+ "conf": cf, "timestamp": datetime.datetime.now().isoformat(), "status": "OPEN"
335
+ })
 
 
336
  requests.post(f"https://ntfy.sh/{NTFY_TOPIC}",
337
+ data=f"🚀 {pair} {action}\nReason: {reason}",
338
  headers={"Title": f"Trade Executed ({cf:.2f})"})
339
  else:
340
+ action = f"BLOCKED ({raw_act})"
341
  color = "🛡️"
342
+ db.reference("conflicts").push({"pair": pair, "reason": reason, "time": datetime.datetime.now().isoformat()})
343
+ # ALERT FOR BLOCKED TRADES NOW ACTIVE
344
+ requests.post(f"https://ntfy.sh/{NTFY_TOPIC}",
345
+ data=f"🛡️ {pair} Blocked\nReason: {reason}",
346
+ headers={"Title": f"Signal Rejected ({cf:.2f})", "Tags": "shield"})
347
 
348
  results.append([pair, f"{color} {action}", f"{cf:.3f}", f"{thresh:.3f}"])
349
 
 
350
  stats = db.reference("stats").get() or {}
351
  stat_str = f"Wins: {stats.get('wins',0)} | Losses: {stats.get('losses',0)} | Net Pips: {stats.get('pips',0):.1f}"
352
+ return stat_str, results, calendar.get_ui_table(), full_log
 
353
 
354
  except Exception as e:
355
+ return f"Error: {e}", [], pd.DataFrame(), ""
 
356
 
357
  # ==========================================
358
  # 5. UI
 
361
  gr.Markdown("# 🦅 Sentinel Pro: Autonomous Multi-Asset Agent")
362
 
363
  with gr.Row():
364
+ status_box = gr.Textbox(label="Account Health", value="Initializing...")
365
  scan_btn = gr.Button("FORCE SCAN", variant="primary")
366
 
367
  with gr.Tabs():
368
  with gr.Tab("Live Command"):
369
  res_table = gr.Dataframe(headers=["Pair", "Signal", "Conf", "Thresh"], label="Live Signals")
370
+ reasoning_box = gr.Markdown(label="Agent Reasoning")
 
371
  with gr.Tab("Macro Intelligence"):
372
+ cal_table = gr.Dataframe(label="Today's Calendar")
373
 
 
 
374
  timer = gr.Timer(300)
375
 
376
+ # CRITICAL FIX: Run ON LOAD so you don't stare at a blank screen
377
+ demo.load(run_system, inputs=[], outputs=[status_box, res_table, cal_table, reasoning_box])
378
 
379
+ scan_btn.click(run_system, inputs=[], outputs=[status_box, res_table, cal_table, reasoning_box])
380
+ timer.tick(run_system, inputs=[], outputs=[status_box, res_table, cal_table, reasoning_box])
381
 
382
  if __name__ == "__main__":
383
  demo.launch()