Commit Graph

9 Commits

Author SHA1 Message Date
ramseshk 1bf54b4c00 Add real historical backtesting with Hyperliquid mainnet candle data
backtests/historical_runner.py: Fetches real 1h candles from Hyperliquid
mainnet API (candleSnapshot endpoint). Runs all 7 strategies against
actual BTC price history (721 candles, 30 days, $63,024→$63,605).
Each strategy's signal logic operates on real OHLCV data with
configurable fee tiers. Saves to backtests/results/historical/.

Results on 30d BTC data at VIP0:
  Mean Reversion: +93.87% net (Sharpe 0.94)
  Order Book Imbalance: +54.31% net (Sharpe 1.03)
  Avellaneda-Stoikov: -1.02% net (Sharpe -0.13)
  Iceberg Detection: -33.20% net
  Momentum Breakout: -54.72% net

Server: Added /api/backtests/historical (list) and
/api/backtest/historical/{name} (full data) endpoints.

Dashboard: Added "Historical" tab with "Real Data" badge. Cards show
coin + mainnet source. Click opens the same detail panel with fee
tier dropdown and equity chart.
2026-08-04 07:29:51 +00:00
ramseshk 0c0d2124ad Add Hyperliquid fee tier selector — 7 VIP levels × 7 staking tiers
config/fee_tiers.py: complete Hyperliquid fee schedule with perps and spot
base rates plus staking discount multipliers. effective_rate() computes
the actual fee after staking discount. get_perp_fees() returns the
effective rate for a given VIP tier, staking tier, and fee model.

Backtest runner: added --fee-tier (0-6) and --staking-tier flags.
Regenerated all 12 backtests at VIP 0 baseline. Runner now shows fee tier
info at startup.

Server: /api/backtest/{name}/recalc endpoint accepts ?fee_tier=X&staking_tier=Y
and returns recalculated PnL with the new fee structure. On-the-fly
recalculation — no need to re-run the backtest.

Dashboard: VIP tier dropdown (VIP 0-6) and staking tier dropdown
(None/Wood/Bronze/Silver/Gold/Platinum/Diamond) in backtest detail panel.
Changing either instantly recalculates PnL via the API.

Key finding: Cartea-Jaimungal goes from -5.58% net at VIP0 to +2.39% net
at VIP6+Diamond (maker rebate: exchange pays YOU -0.0024% to provide
liquidity). Fee structure completely changes strategy viability assessment.
2026-08-04 07:26:40 +00:00
ramseshk ecfdd56d8f Add fee-toggle for backtests, CSV trade download, fee simulation in runner
Backtest runner: added per-trade fee simulation (maker 2bps, taker 5bps).
Each trade now records pnl_gross, pnl_net, and fee. New --no-fees flag
excludes fees from PnL. Output includes pnl_gross/pnl_gross_pct and
fees_total alongside existing pnl (net). Regenerated all 12 backtests.

Server: added /api/backtest/{name}/csv endpoint — returns trades as CSV
with columns time,side,size,price,pnl_gross,pnl_net,fee.
Content-Disposition: attachment triggers browser download.

Dashboard: added "Inc. fees" checkbox toggle in backtest detail panel.
Unchecking shows gross PnL (before fees). "↓ CSV" button downloads
the trade history. Both hidden when detail is closed.
2026-08-04 07:16:41 +00:00
ramseshk e4de21192a Fix dashboard backtest detail, deterministic backtest seeds, paper trader fees, live node crash guard
Backtest detail: openDetail() now fetches full backtest JSON from the API
instead of showing "Full trade data not in summary". Renders equity curve
chart + full trade history table with 100 rows.

Backtest reproducibility: replaced hash(key) with fixed per-strategy seeds.
Python's hash() is randomized per process (PYTHONHASHSEED), causing wildly
different results for same strategy across runs. Now deterministic.

Server: added total_trades and sortino to /api/backtests summary response.

Paper trader: fixed Avellaneda-Stoikov simulate using TAKER_FEE instead of
MAKER_FEE. Lowered OBI signal threshold from 5bps to 1.5bps for flat markets.

Live node: added None-guard in get_mark_prices — Hyperliquid testnet API
sometimes returns null, crashing the node. Wrapped in try/except.
2026-08-04 07:07:15 +00:00
ramseshk 1c83fd378e Fix backtest tab: match API field names pnl_pct, max_dd 2026-08-04 06:42:47 +00:00
ramseshk 9768bf80cc Cartea-Jaimungal, Queue Imbalance, Guéant MM: 3 new quant finance strategies + backtests 2026-08-04 06:19:19 +00:00
ramseshk 2c0750e355 Fee optimization: per-strategy maker/taker model + signal strength filter + 9 backtests 2026-08-04 06:12:35 +00:00
ramseshk 4d5ddc5f18 Tight quoting at best bid/ask + post-only fallback + 7-strategy backtests
Execution model upgrade:
- Orders now placed AT best bid/ask (not mid ± arbitrary spread)
- Avellaneda-Stoikov: dual-sided simultaneous quoting at bid AND ask
- Post-only fallback: when spread is too tight, falls back to IOC limit
  to capture the fill instead of rejecting

Backtest runner updated for all 7 strategies:
  Iceberg: +16.92%, Sharpe 7.85
  Mean Reversion: +16.97%, Sharpe 10.43
  Avellaneda-Stoikov: +15.54%, Sharpe 11.37
  Momentum Breakout: +8.86%, Sharpe 3.42
  Funding Arb: +6.01%, Sharpe 11.12
  Pairs Trading: +0.33%
  OFI: -13.57% (high variance, seed-dependent)

HFT efficiency note: POST-ONLY orders at best bid/ask minimize fees
(0.02% maker) and capture spread. Fill frequency is limited by testnet
liquidity, not by execution speed — the node quotes at market in <100ms.
On mainnet with real volume, fill rates would be 100-1000x higher.
2026-08-04 04:13:04 +00:00
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