Public
Remixed
XAU/USD Risk Guardian — risk scoring, alerts, paper trading
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.

🛡️ Risk Guardian — XAU/USD Signal Monitor

Real-time risk monitoring system for XAU/USD (Gold) trading signals. Connects to your gold-confirm and main signal bots, tracks positions, scores risk, and sends Telegram alerts when risk escalates.

🔗 Live Dashboard: https://risk-guardian.val.run/

Features

  • Live Gold Price — fetches XAU/USD spot price + DXY from multiple free APIs
  • Signal Polling — auto-discovers signal APIs from gold-confirm + main signal bots
  • Webhook Receiver — external bots can POST signals directly to /api/signals/webhook
  • Risk Scoring Engine — 0–100 score based on SL distance, TP distance, momentum, DXY, P&L
  • Demo Paper Trading — auto-opens/closes trades, tracks balance, equity, win rate
  • Telegram Alerts — new positions, risk escalations, and position closures
  • Dashboard UI — live risk cards, stats bar, alert feed, demo account panel
  • Cron Monitor — runs every 15 min to poll signals, assess risk, and update demo

Architecture

Rendering mermaid diagram...

File Structure

index.ts                        ← Hono API server (all routes)
cron.ts                         ← Interval cron (auto-monitor every 15 min)
lib/
  types.ts                      ← Shared TypeScript interfaces
  db.ts                         ← SQLite database layer
  risk.ts                       ← Risk scoring engine (0–100)
  price.ts                      ← XAU/USD + DXY price feeds
  telegram.ts                   ← Telegram alert sender + formatters
  signal-poller.ts              ← Multi-source signal polling + webhook
  demo.ts                       ← Paper trading engine (auto open/close)
frontend/
  index.html                    ← HTML shell (Twind + React)
  index.tsx                     ← React entrypoint
  components/
    App.tsx                     ← Root dashboard (tabs + live data)
    RiskCard.tsx                ← Per-position risk card with score bar
    StatsBar.tsx                ← Summary stats row
    AlertFeed.tsx               ← Alert history list
    DemoPanel.tsx               ← Demo account + trade tables

API Endpoints

MethodPathDescription
GET/api/healthHealth check
GET/api/statsDashboard summary stats
GET/api/priceLive gold price + DXY
GET/api/riskLive risk assessment for all active positions
GET/api/risk/historyRecent risk history entries
GET/api/risk/:idRisk history for specific position
GET/api/positionsAll positions
GET/api/positions/activeActive (watching) positions only
GET/api/alertsRecent alerts
GET/api/demoDemo account stats
GET/api/demo/tradesAll demo trades
GET/api/demo/openOpen demo trades
POST/api/pollManually trigger signal polling
POST/api/signals/webhookReceive signals from external bots
POST/api/signals/injectManually inject a signal
POST/api/monitorFull monitor cycle (poll + risk + demo)

Webhook Format

POST to /api/signals/webhook:

{ "signal_id": "sig-2024-001", "direction": "LONG", "entry_price": 2380.50, "sl_price": 2370.00, "tp_price": 2400.00, "invalidation_level": 2365.00, "status": "confirmed", "timestamp": "2024-07-01T12:00:00Z" }

Environment Variables

KeyRequiredDescription
TELEGRAM_BOT_TOKENNo*Telegram bot token for alerts
TELEGRAM_CHAT_IDNo*Telegram chat ID for alerts

*Without Telegram credentials, alerts are logged but not sent.

👉 Add TELEGRAM_BOT_TOKEN here: https://www.val.town/x/Hunter5/risk-guardian/environment-variables?key=TELEGRAM_BOT_TOKEN 👉 Add TELEGRAM_CHAT_ID here: https://www.val.town/x/Hunter5/risk-guardian/environment-variables?key=TELEGRAM_CHAT_ID

Risk Scoring (0–100)

ScoreLevelMeaning
0–34🟢 SafePosition is healthy
35–59🟡 CautionMonitor closely
60–79⚠️ DangerConsider action
80–100🚨 CriticalImmediate attention needed

Factors: SL distance (40pts), momentum (25pts), DXY correlation (15pts), unrealized P&L (15pts), invalidation proximity (10pts), TP proximity (-15pts).