Back to Case Study

Carry Trade Algo — Tech Deep Dive

Algorithmic Forex Strategy — 94-Day Paper Trading Validation

Tech Stack

PythonOANDA APIpandasSQLiteDockerAWS
Architecture: Event-Driven Pipeline

Architecture

OANDA v20 REST API300 H1 Candles · Positions · Swap RatesHourly TickV3 Carry StrategyGolden Cross (50/200 MA) + Positive Swap6 JPY Pairs · RSI < 75 · Regime FilterRegime DetectorADX + ATR → 4 Market StatesTrend/Range × Low/High VolNews BlackoutFOMC · BOJ · ECB±1h High-Impact EventsSignalsML Signal GateThompson Sampling · 14 Features · TAKE / SKIPDynamic Sizer + Exit ManagerKelly × ATR × Regime × Correlation × DD Decay7 Exit Conditions · Trailing Stops · Profit Scaling5-Layer Risk1. Kelly Criterion2. ATR Volatility Scaling3. Regime Multiplier4. Correlation Monitor5. Drawdown DecayCircuit BreakersDaily: -3% · Weekly: -7%Max DD: -20% → HALTMax 6 Positions · 15%/PairLimit Orders @ BidSQLite (WAL)Trades · Equity · ProtocolSurvives Container RestartsTelegram Bot/status · /trends · /positionsReal-time Alerts · WatchdogDocker on AWS EC2LIVE — Day 41/94Hourly event-driven pipeline: OANDA data → Strategy → ML Gate → Risk Sizing → Execution → PersistenceCore PipelineRisk / FiltersDecision NodeGuard / Gate

Data Flow

  1. 1

    APScheduler triggers hourly tick during forex market hours (Sun 22:00 - Fri 22:00 UTC)

  2. 2

    Reconciler syncs internal state with OANDA broker positions (broker = source of truth)

  3. 3

    Data fetcher pulls 300 H1 candles per pair from OANDA v20 REST API

  4. 4

    V3 Strategy evaluates golden cross, swap rates, RSI, and regime for entry/exit signals

  5. 5

    ML Bandit gate (Thompson Sampling) decides TAKE or SKIP based on 14 contextual features

  6. 6

    Dynamic sizer calculates position units: Kelly × ATR × regime × correlation × drawdown decay

  7. 7

    Exit manager checks 7 conditions per tick: stop-loss, trailing, time-based, profit-scaled, regime change, trend reversal

  8. 8

    Orders placed as limits at bid price (saves 1-2 pips vs market orders)

  9. 9

    Circuit breakers evaluate daily/weekly/max-DD thresholds — halt trading if breached

  10. 10

    SQLite WAL-mode persistence saves all state (survives container restarts)

  11. 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

473 tests
Test Suite
4 (V3 active)
Strategy Versions
5 + 3 breakers
Risk Layers
23
Source Modules

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.

Active Development — Follow the Journey

Want similar results for your project?