Files
ftdt-quant-lab/dashboard/server.py
T
ramseshk 7dd9e78e0b Verbose dashboard with backtesting tab and per-strategy 100 USDC allocation
Dashboard overhaul:
- Tabbed interface: Live Trading | Backtesting
- Live tab shows: global stats (equity, reserve, trades, win rate, active
  strategies), equity curve, per-strategy cards with allocation and PnL,
  real-time trade log
- Backtest tab: lists saved backtests with Sharpe, PnL, max DD, win rate;
  click to view full equity curve and detailed metrics
- Reads real data from /tmp/ftdt-metrics.json written by live node

Live node update:
- 5 strategies each with 100 USDC allocation (398 USDC reserve)
- Writes real-time metrics to shared JSON file
- Runs signal generators for each strategy type
- Logs tick-by-tick status

Backtest runner:
- Simulates 30 days of hourly data per strategy
- Different return profiles for each strategy type
- Saves results to backtests/results/ as JSON
- Accessible via dashboard API and frontend

Backtest results (30-day sim):
  Avellaneda-Stoikov:    +3.72%  Sharpe 2.53  DD 5.12%
  Order Book Imbalance:  +3.83%  Sharpe 1.60  DD 9.86%
  Pairs Trading:         +0.54%  Sharpe 0.41  DD 7.83%
  Funding Rate Arb:      +0.17%  Sharpe 0.35  DD 2.94%
  Iceberg Detection:     -9.15%  Sharpe -4.39 DD 11.94%
2026-08-04 03:12:21 +00:00

203 lines
7.9 KiB
Python

"""
Dashboard backend — WebSocket metrics server.
Reads live metrics from a shared JSON file (written by the live node)
and serves backtest results from disk. Streams everything to
connected dashboard clients via WebSocket.
Architecture:
- /ws — WebSocket for real-time streaming
- /backtests — list available backtest results
- /backtest/{name} — serve specific backtest result
- / — static HTML dashboard
Usage:
python dashboard/server.py --port 9175
"""
import asyncio
import json
import os
import time
import threading
from pathlib import Path
from typing import Optional
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse, JSONResponse
import uvicorn
# ═══════════════════════════════════════════════════════════
# Constants
# ═══════════════════════════════════════════════════════════
METRICS_FILE = "/tmp/ftdt-metrics.json"
BACKTEST_DIR = "/home/debian/ftdt-quant-lab/backtests/results"
STATIC_DIR = Path(__file__).parent / "static"
# Ensure backtest dir exists
os.makedirs(BACKTEST_DIR, exist_ok=True)
# ═══════════════════════════════════════════════════════════
# App
# ═══════════════════════════════════════════════════════════
app = FastAPI(title="FTDT Quant Lab Dashboard")
connected_clients: set[WebSocket] = set()
loop: Optional[asyncio.AbstractEventLoop] = None
# ═══════════════════════════════════════════════════════════
# Metrics reader
# ═══════════════════════════════════════════════════════════
def read_metrics() -> dict:
"""Read the shared metrics file written by the live node."""
try:
if os.path.exists(METRICS_FILE):
with open(METRICS_FILE) as f:
return json.load(f)
except (json.JSONDecodeError, IOError):
pass
return _empty_metrics()
def _empty_metrics() -> dict:
return {
"timestamp": time.time(),
"wallet": "0x...",
"total_equity": 898.0,
"total_pnl": 0.0,
"total_pnl_pct": 0.0,
"equity_history": [],
"strategies": {},
"trades": [],
"status": "starting",
}
# ═══════════════════════════════════════════════════════════
# Background broadcaster
# ═══════════════════════════════════════════════════════════
async def broadcast_to_client(ws: WebSocket, payload: str):
try:
await ws.send_text(payload)
except Exception:
connected_clients.discard(ws)
def broadcast_loop():
"""Continuously read metrics and broadcast to all clients."""
while True:
time.sleep(1)
data = read_metrics()
payload = json.dumps(data, default=str)
for ws in list(connected_clients):
if loop:
asyncio.run_coroutine_threadsafe(
broadcast_to_client(ws, payload), loop
)
# ═══════════════════════════════════════════════════════════
# WebSocket
# ═══════════════════════════════════════════════════════════
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
connected_clients.add(websocket)
try:
# Send initial state immediately
data = read_metrics()
await websocket.send_text(json.dumps(data, default=str))
while True:
await asyncio.sleep(30)
except WebSocketDisconnect:
connected_clients.discard(websocket)
# ═══════════════════════════════════════════════════════════
# Backtest endpoints
# ═══════════════════════════════════════════════════════════
@app.get("/api/backtests")
async def list_backtests():
"""List all saved backtest results."""
results = []
if os.path.isdir(BACKTEST_DIR):
for fname in sorted(os.listdir(BACKTEST_DIR), reverse=True):
if fname.endswith(".json"):
fpath = os.path.join(BACKTEST_DIR, fname)
try:
with open(fpath) as f:
data = json.load(f)
results.append({
"name": fname.replace(".json", ""),
"strategy": data.get("strategy", "unknown"),
"start": data.get("start_time"),
"end": data.get("end_time"),
"sharpe": data.get("sharpe", 0),
"pnl_pct": data.get("pnl_pct", 0),
"max_dd": data.get("max_dd", 0),
"win_rate": data.get("win_rate", 0),
})
except (json.JSONDecodeError, IOError):
pass
return JSONResponse(results)
@app.get("/api/backtest/{name}")
async def get_backtest(name: str):
"""Get full backtest result data."""
fpath = os.path.join(BACKTEST_DIR, f"{name}.json")
if os.path.exists(fpath):
with open(fpath) as f:
return JSONResponse(json.load(f))
return JSONResponse({"error": "not found"}, status_code=404)
# ═══════════════════════════════════════════════════════════
# Static
# ═══════════════════════════════════════════════════════════
@app.get("/")
async def root():
return FileResponse(STATIC_DIR / "index.html")
app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
# ═══════════════════════════════════════════════════════════
# Main
# ═══════════════════════════════════════════════════════════
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--port", type=int, default=9175)
parser.add_argument("--host", default="0.0.0.0")
args = parser.parse_args()
global loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# Start metrics broadcaster
broadcaster = threading.Thread(target=broadcast_loop, daemon=True)
broadcaster.start()
print(f"FTDT Quant Lab Dashboard")
print(f" http://{args.host}:{args.port}")
print(f" WebSocket: ws://{args.host}:{args.port}/ws")
print(f" Backtests: /api/backtests")
uvicorn.run(app, host=args.host, port=args.port, log_level="warning")
if __name__ == "__main__":
main()