FX Backtest & Execution Stack
End-to-end foreign-exchange research and trading toolkit that links feature engineering, backtesting, ML-driven signal generation, and OANDA execution into one repo.
Highlights
- Unified
StrategyEngine(seeQuantResearch/core/backtest/strategy_engine.py) powers historical backtests, walk-forward studies, paper trading, and the live runner so signals behave identically across environments. - Strategy registry ships with SMA/ATR trend, Bollinger & band mean-revert, breakout momentum, and an XGBoost probability model (
QuantResearch/strategies/*), allowing multi-strategy voting through YAML configs such asQuantTrader/config/usdjpy_multi_strategy.yaml. - Research workflows enforce data-manifest validation, risk sims, KPI summaries (
results/<run_id>/summary.json), and promotion of vetted artifacts intoQuantTrader/artifacts/before they are allowed to reach trading. - Runtime layer contains async OANDA data/execution handlers, event-driven risk checks, and pluggable multi-strategy allocation for both paper (
scripts/paper_trade.py) and live trading (scripts/live_trade.py). - Monitoring stack (Pushgateway + Prometheus + Grafana) ships ready-to-import risk dashboards (
monitoring/grafana/*.json), custom drilldown plugins (logs/traces/profiles/metrics), and Slack/pushgateway hooks for diagnostics automation.
Repository Layout
QuantResearch/– Research code, datasets, strategy implementations, notebooks/scripts, docs, artifacts, and test suites.QuantTrader/– Trading runtime with execution/risk/data engines, configs, logging, and artifact promotion targets.monitoring/– Dockerized observability stack plus Grafana dashboards & plugins for metrics/logs/traces/profiles.shared/– Cross-cutting helpers (shared/utils/config.pyloads OANDA/Slack/Pushgateway secrets from.env).results/– Canonical run outputs uploaded with PRs (e.g., walk-forward summaries) for auditing.metrics/– Lightweight operational CSVs (e.g., execution latencies) that can be pushed to Prometheus.
Quick Start
-
Clone & create a virtual environment
git clone <your fork url> cd FX_Backtest python -m venv .venv source .venv/bin/activate pip install --upgrade pip pip install -r QuantResearch/requirements.txt pip install -r QuantTrader/requirements.txtPython 3.10+ is recommended for
pandas/xgboostcompatibility. -
Configure secrets
cp .env.demo .env # edit .env with your OANDA practice/live credentials + webhook URLs source .envAll scripts that touch OANDA import from
shared.utils.config, so missing env vars fail fast. -
Prepare data
-
Drop raw CSVs (e.g.,
USDJPY_H1.csv) underQuantResearch/data/raw/. -
Rebuild the manifest + integrity reports any time data changes:
cd QuantResearch python scripts/build_dataset_manifest.py --dirs data/raw data/derived --output data/_manifest.json python scripts/check_data_integrity.py
-
-
Run a backtest
python QuantResearch/scripts/backtest_strategy.py \ --csv QuantResearch/data/raw/USDJPY_H1.csv \ --symbol USDJPY \ --fast 20 --slow 80 \ --strategies QuantTrader/config/usdjpy_multi_strategy.yamlThe script validates the dataset, runs the engine, and writes KPIs plus
equity/,trades/, andstats/artifacts underQuantResearch/data/outputs/. -
Train or refresh the XGBoost signal
python QuantResearch/scripts/train_xgb_usdjpy.py \ --csv QuantResearch/data/raw/USDJPY_H1.csv \ --symbol USDJPY \ --out QuantResearch/artifacts/models/usdjpy_h1_xgbThis exports
model.json, feature lists, thresholds, and updatesusdjpy_h1_xgb_latest.jsonso trading configs can point to the latest model. -
Run walk-forward analysis (optional gating)
python QuantResearch/scripts/run_walkforward.py \ --config QuantTrader/config/usdjpy_multi_strategy.yaml \ --csv QuantResearch/data/raw/USDJPY_H1.csv \ --train-bars 4000 --test-bars 1000 \ --output-root QuantResearch/results \ --label usdjpy_xgbEach window produces metrics and a
summary.jsonunderQuantResearch/results/<run_id>/. Reference these run IDs in PRs. -
Promote artifacts to the trader
After validating a run, sync configs/params into
QuantTrader/artifacts/(seeQuantTrader/artifacts/README.md):cp QuantTrader/config/usdjpy_multi_strategy.yaml QuantTrader/artifacts/config/ cp QuantResearch/artifacts/models/usdjpy_h1_xgb_latest.json QuantTrader/artifacts/params/ -
Paper trading or live execution
-
Paper (uses live pricing -> StrategyEngine -> simulated fills):
python QuantTrader/scripts/paper_trade.py \ --config QuantTrader/config/usdjpy_multi_strategy.yaml \ --symbol USDJPY \ --timeframe 60s -
Live example (direct OANDA handler + RSI strategy template, see
QuantTrader/scripts/live_trade.py):python QuantTrader/scripts/live_trade.py
Customize the risk manager, strategy, and execution handler before pointing to a funded account.
-
-
Spin up monitoring (optional but recommended)
docker compose up -dThis launches Pushgateway (
:9091), Prometheus (:9090), and Grafana (:3000). Importmonitoring/grafana/risk_metrics_dashboard.jsonand enable the bundled drilldown plugins for logs/traces/profiles/metrics exploration.
Common Workflows
- Data quality gating:
python QuantResearch/scripts/watch_quality.pyor the CI-friendlyscripts/watch_risk_metrics.pypush metrics to Slack/Pushgateway before PRs merge. - Batch experiments:
python QuantResearch/scripts/run_batch_backtests.py --config config/eurusd_grid.yamlsweeps parameter grids and streams metrics underresults/<batch>/. - Stress testing:
python QuantResearch/scripts/validate_stress_scenarios.py --config ...replays adverse cost scenarios to validate drawdown budgets. - Risk sims:
RUN=<run_id> ./QuantResearch/scripts/run_risk_sim.sh && ./QuantResearch/bin/backfill_risk.shkeepresults/risk/metrics.csvaligned with latest runs.
Monitoring & Diagnostics
QuantResearch/scripts/export_metrics_prom.pystreams aggregated KPIs to Pushgateway (PUSHGATEWAY_URL).QuantResearch/scripts/notify_risk_metrics.shwrapswatch_risk_metrics.pyto send Slack alerts usingSLACK_RISK_WEBHOOK.- Grafana plugins under
monitoring/grafana/plugins/grafana-*-app/document the queryless drilldown experiences for logs (Loki), metrics (Prometheus), traces (Tempo), and profiles (Pyroscope). monitoring/grafana/risk_metrics_dashboard.jsonvisualizes walk-forward pass rates, tail risk, exposure, and per-strategy attribution. Load it after Grafana boots (admin/adminby default).
Testing & Validation
- Unit tests:
pytest QuantResearch/tests QuantTrader/tests. - Strategy registry coverage:
QuantResearch/tests/test_strategy_registry.pyensures new strategies register correctly; add fixtures before contributing. - Result validation:
python QuantResearch/scripts/validate_results.py QuantResearch/results/<run_id>checks KPI completeness + data references. - Data feed/execution smoke tests:
python QuantTrader/tests/test_execution_adapters.pymocks OANDA flows.
Extending the Stack
- Implement a new research strategy under
QuantResearch/strategies/and decorate it with@register("my_strategy"). - Reference it inside a config YAML (e.g.,
usdjpy_multi_strategy.yaml) with weights/params. - Add risk rules in
QuantTrader/core/risk/if the position sizing model needs to change. - Document any new process in
QuantResearch/docs/or module-level READMEs so CI reviewers have breadcrumbs.
Related Docs
QuantResearch/README.md– data submission rules, risk/diagnostics workflow.QuantTrader/artifacts/README.md– promotion checklist for configs/params.monitoring/grafana/plugins/*/README.md– upstream plugin instructions.
License
No open-source license is declared yet. Keep the repository private or add a LICENSE file before publishing.