Space Quant Lab: Causal Meta-Ensembles for Leveraged Bitcoin Trading
Most retail crypto trading bots hide fragile curve-fitted rules behind opaque dashboards and unrealistic frictionless backtests. We built an open, zero-dependency 24/7 paper-trading and backtesting laboratory that treats Bitcoin perpetuals as an online non-stationary learning problem, with strict zero-lookahead proofs, realistic liquidation & funding math, and glass-box telemetry.
We present Space Research / Quant Lab, a self-hosted leveraged Bitcoin algorithmic trading platform combining six strictly causal strategies, a perpetual-futures margin engine (1×-20× leverage, Isolated/Cross liquidation, taker fees, directional slippage, and live 8-hour Binance Futures funding), and a 24/7 systemd paper-trading daemon backed by permanent SQLite trade and session archiving. Our flagship algorithm, Space Neural Meta-Ensemble (space_neural_manifold), runs a causal Multiplicative-Weights (Hedge) online meta-learner over four orthogonal alpha experts (2-state log-price Kalman filtering, Donchian compression breakouts, aggressive taker order-flow imbalance, and statistical Z-score reversion) gated by a fee-aware volatility floor and post-stop anti-whipsaw cooldowns. Across live Binance BTC/USDT historical windows (capped at 5× dynamic leverage), Space Meta-Ensemble delivers +5.99% over 7d 15m (Sharpe +5.97, Max DD 4.50% vs. −6.24% Buy & Hold), +8.33% over 30d 1h (Sharpe +2.48 vs. −25.05%), and +34.93% over 90d 4h (Sharpe +3.26, Max DD 7.08% vs. −20.58%), with zero liquidations across every horizon.
1. Treating markets like a glass-box scientific instrument
Applying the same engineering principles we use in Space Chat and Basalt to financial time series means enforcing three non-negotiable invariants before writing a single strategy rule:
- Zero-lookahead prefix invariance: Signals computed at bar
tmust be bit-for-bit identical whether barst+1..Nexist or not. Every bart−1close signal executes strictly at bartopen, enforced automatically by prefix-invariance unit tests across all six strategies. - Exact perpetual-futures friction: Both backtests and live paper trades charge round-trip taker fees (
4 bps/side), directional slippage (2 bps), and prorated 8-hour perpetual funding rates (pulled live from Binance FuturespremiumIndex), and check intra-bar highs/lows conservatively in order: Liquidation → Stop-Loss / Trailing Stop → Take-Profit. - Permanent, tamper-proof paper history: Every paper trade, funding accrual, and engine event persists in SQLite. Even resetting a paper account archives the completed session into
paper_sessionsrather than deleting past trades, eliminating survivorship bias in our own live experiments.
2. Space Neural Meta-Ensemble: Online Hedge learning across regimes
Bitcoin alternates violently between persistent directional expansions, liquidity-sweep mean reversion, and dead low-volatility chop. Any single static indicator (momentum or reversion) gets bled dry during its opposite regime. Our flagship space_neural_manifold strategy treats regime adaptation as an online prediction-with-expert-advice problem (the classic Hedge / Multiplicative Weights algorithm):
- Four orthogonal causal experts: Every bar produces bounded scores
e_k ∈ [−1, +1]from (E0) a 2-state constant-velocity Kalman Filter onlog(close)combined with SuperTrend, (E1) a causal 20-bar Donchian breakout + directional+DI/−DImomentum expert, (E2) Binance aggressive taker buy/sell Order-Flow Imbalance (OFI) plus rolling VWAP drift, and (E3) an ADX-damped Bollinger/RSI Z-score mean-reversion expert. - Causal exponential feedback: At bar
i, once the 4-bar horizon fromi−4toiresolves, each expert's log-weight updates strictly from past features:log_w[k] ← 0.96 · log_w[k] + 0.28 · e_k(i−4) · clip(ΔP / ATR, −2.5, 2.5), then maps through softmax with a diversity floor so no expert permanently dies before its regime returns. - Fee-aware volatility floor & macro alignment: Entries are suppressed whenever
ATR / close < 20 bps(preventing fee churn in dead chop) and demand stronger conviction (0.48vs.0.34) to trade against the 144-bar macro EMA.
3. Volatility-targeted leverage & perpetual risk mechanics
Static high leverage is catastrophic in crypto because volatility clusters. Instead of applying fixed leverage into liquidation cascades, our margin engine scales effective leverage inversely with annualized realized volatility and proportionally to signal conviction:
L_eff = clamp( L_max × clamp(σ_target / σ_realized, 0.35, 1.25) × (0.45 + 0.55 × confidence), 1.0, L_max )
Every leveraged position computes exact perpetual liquidation thresholds (P_entry × (1 − Collateral / Notional + MMR) for Longs and P_entry × (1 + Collateral / Notional − MMR) for Shorts under Isolated or Cross margin), pairs initial ATR stop-loss and take-profit levels with a ratcheting ATR trailing stop activated at +1.0 ATR, and enforces a 40% max-drawdown circuit breaker.
4. Complete six-algorithm benchmark on live Binance data
We benchmarked all six zero-lookahead strategies side-by-side on identical paginated Binance Spot BTC/USDT candles (up to 5× volatility-targeted dynamic leverage, 35% margin/trade, 4 bps taker fee, 2 bps slippage, 0.01% 8h funding):
| Algorithm | 7d (15m) | 30d (1h) | 90d (4h) | Liqs |
|---|---|---|---|---|
| Space Meta-Ensemble (Hedge) | +5.99% (Sh 5.97) | +8.33% (Sh 2.48) | +34.93% (Sh 3.26) | 0 |
| Adaptive Regime Ensemble | +0.48% (Sh 0.74) | +12.87% (Sh 3.07) | +15.76% (Sh 1.52) | 0 |
| Vol Squeeze & Donchian | +1.60% (Sh 1.82) | +18.08% (Sh 3.43) | −22.99% (Sh −2.02) | 0 |
| Online ML (AdaGrad Logistic) | −6.58% (Sh −10.9) | +15.38% (Sh 4.64) | −14.21% (Sh −1.65) | 0 |
| Statistical Z-Score Reversion | −9.99% (Sh −14.3) | +0.53% (Sh 0.35) | +18.89% (Sh 2.58) | 0 |
| Kalman + Order-Flow Momentum | −7.14% (Sh −4.85) | +8.83% (Sh 1.84) | −14.88% (Sh −1.04) | 0 |
| Benchmark: BTC Buy & Hold | −6.24% | −25.05% | −20.58% | , |
Notice the structural takeaway: specialized strategies shine in specific horizons (Online ML led 30d 1h at Sharpe 4.64; Z-Score Reversion captured +18.89% on 90d 4h), while Space Meta-Ensemble is the only strategy consistently profitable with Sharpe > 2.4 and max drawdown ≤ 8.1% across every tested horizon simultaneously.
5. Real live paper telemetry & the whipsaw bug we fixed
Because our paper engine records every live execution permanently in SQLite, its very first overnight run caught a subtle real-world failure mode immediately:
PAPER-0001(Win): Opened a3.74×leveragedSHORTat$60,363.98and locked in +$680.76 via our ATR trailing stop at$60,002.80.PAPER-0002(Whipsaw): Sixty seconds later, on the very next bar, the previous regime strategy immediately re-enteredSHORTat$59,993.75right at the local bottom and got squeezed out on signal flip for −$1,153.57.
That single live observation prompted our 3-bar causal post-stop cooldown (blocking same-direction re-entry after any STOP_LOSS, TRAILING_STOP, or LIQUIDATION exit) and the rollout of Space Meta-Ensemble as our live default, immediately flipping 7d 15m paper-regime performance from negative chop to +5.99% (Sharpe 5.97).
6. 24/7 autonomous daemon & mobile-first glass-box UI
The entire stack runs in pure Python 3.14 standard library + high-DPI HTML5 Canvas with zero external package bloat. It runs 24/7 as a persistent Linux systemd --user daemon (Linger=yes) behind a Cloudflare HTTPS tunnel, streams live ticks and expert softmax weights over Server-Sent Events (SSE), archives every trade and reset session permanently in SQLite with CSV export, and includes a mobile-native bottom navigation bar with touch-scrubbable candlestick & equity charts.
Watch the live 24/7 paper engine, inspect real-time Hedge expert allocation weights, browse permanent SQLite trade history, or run multi-algorithm leveraged backtests directly from your browser or phone (simulated paper money only).