Carry Trade Algo — Tech Deep Dive
Algorithmic Forex Strategy — 94-Day Paper Trading Validation
Tech Stack
Architecture
Data Flow
- 1
APScheduler triggers hourly tick during forex market hours (Sun 22:00 - Fri 22:00 UTC)
- 2
Reconciler syncs internal state with OANDA broker positions (broker = source of truth)
- 3
Data fetcher pulls 300 H1 candles per pair from OANDA v20 REST API
- 4
V3 Strategy evaluates golden cross, swap rates, RSI, and regime for entry/exit signals
- 5
ML Bandit gate (Thompson Sampling) decides TAKE or SKIP based on 14 contextual features
- 6
Dynamic sizer calculates position units: Kelly × ATR × regime × correlation × drawdown decay
- 7
Exit manager checks 7 conditions per tick: stop-loss, trailing, time-based, profit-scaled, regime change, trend reversal
- 8
Orders placed as limits at bid price (saves 1-2 pips vs market orders)
- 9
Circuit breakers evaluate daily/weekly/max-DD thresholds — halt trading if breached
- 10
SQLite WAL-mode persistence saves all state (survives container restarts)
- 11
Telegram bot dispatches alerts and responds to /status, /trends, /positions, /health commands
Code Patterns
Multi-Layer Risk Pipeline
Position sizing flows through 5 independent risk factors multiplied together: Kelly criterion (optimal bet size from win rate), ATR volatility scaling (less size when volatile), regime multiplier (0-100% based on trend + volatility state), correlation factor (reduces size when JPY pairs are >0.7 correlated), and quadratic drawdown decay (15% DD = 50% size). Three circuit breakers provide hard stops.
Regime-Adaptive Strategy
A composite regime detector classifies markets into 4 states using ADX (trend strength) and ATR (volatility): TREND_LOW_VOL (full size), TREND_HIGH_VOL (75%), RANGE_LOW_VOL (25%), RANGE_HIGH_VOL (0%). Weekly Bayesian optimization via Optuna retrains parameters per regime on recent data.
Operational Resilience
SQLite WAL-mode state store survives Docker container restarts. Hourly reconciliation with OANDA detects position mismatches. Economic calendar integration blackouts entries ±1h around high-impact events (FOMC, BOJ). Watchdog monitors heartbeats and escalates via Telegram on missed ticks.
System Metrics
What's Next
53 days remain in the 94-day validation protocol. The system needs 50+ completed trades for statistical significance and must maintain <15% drawdown to pass. Current trajectory: 30 clean trades with 70% win rate and 0.53% max drawdown.
After protocol completion, the Real Money Readiness checklist requires: regime detection as a hard entry gate (not just sizing), ML bandit activation (currently in shadow mode learning from outcomes), VIX > 25 hard block, BOJ meeting 48-hour blackout, and weekend gap protection.
Capital deployment follows a phased approach: $5-10K initial allocation with strict position limits, scaling to $20-25K after 3 months of live performance, with full deployment only after 6+ months of consistent, auditable results.
Want similar results for your project?