XAUUSD Quantitative Trading System β Multi-Timeframe Multi-Strategy Channel Analysis + Backtesting
A Python-based quantitative trading project for gold, covering the complete pipeline: data acquisition β technical indicators β multi-strategy channel analysis β multi-period backtesting β live trading interface.
Gold_Quant_Project/
βββ analysis/ # Channel analysis module
β βββ channel_analyzer.py # Multi-timeframe analyzer (aggregates strategies + consensus)
β βββ strategies.py # 4 channel analysis strategies
βββ backtest/ # Backtesting engine
β βββ engine.py # Backtrader-based engine wrapper
βββ data/ # Data layer
β βββ data_fetcher.py # Data fetchers (YFinance / MT5)
βββ execution/ # Live execution
β βββ mt5_trader.py # MetaTrader5 trading interface (Windows only)
βββ factors/ # Factor computation
β βββ technical_indicators.py # RSI, MACD, SMA, ATR, etc.
βββ strategies/ # Trading strategies
β βββ dual_ma_strategy.py # Dual moving average strategy
β βββ enhanced_ma_strategy.py # Enhanced MA strategy
β βββ swing_strategy.py # Swing trading strategy
β βββ optimized_swing.py # Optimized swing V1
β βββ optimized_swing_v2.py # Optimized swing V2
βββ output/ # Analysis output directory (.gitignore)
βββ main.py # Main entry point
βββ run_channel_analysis.py # Multi-timeframe channel analysis script
βββ run_backtest.py # Basic backtest
βββ run_4h_backtest.py # 4H timeframe backtest
βββ run_daily_backtest.py # Daily timeframe backtest
βββ run_long_backtest.py # 25-year long-term backtest
βββ run_swing_backtest.py # Swing strategy backtest
βββ run_optimized_backtest.py # Optimized strategy backtest
βββ run_optimized_v2_backtest.py # Optimized V2 backtest
βββ test_integration.py # Integration tests
βββ requirements.txt # Python dependencies
βββ .gitignore
conda create -n gold_quant python=3.11 -y
conda activate gold_quantpip install -r requirements.txtDependencies:
| Package | Purpose |
|---|---|
pandas |
Data processing |
numpy |
Numerical computation |
ta |
Technical indicators (ADX, Bollinger Bands, Donchian, etc.) |
backtrader |
Backtesting engine |
yfinance |
Yahoo Finance data source |
python-dotenv |
Environment variable management |
Note:
MetaTrader5is Windows-only. On Linux,yfinanceis used as the data source.
Create a .env file for configuration (e.g., MT5 account credentials):
cp .env.example .env # Edit as neededAnalyzes gold price data across 1H / 4H / Daily / Weekly timeframes using 4 independent strategies, then aggregates results into a multi-strategy consensus.
| Strategy | CLI Name | Core Method | Best For |
|---|---|---|---|
| Linear Regression | regression |
Regression slope + RΒ² + ADX | Trend strength quantification |
| Bollinger Bands | bollinger |
SMA(20) Β± 2Ο, bandwidth + %B | Volatility + overbought/oversold |
| Donchian Channel | donchian |
N-period highest high / lowest low | Breakout confirmation |
| Trendline | trendline |
Pivot high/low regression | Closest to manual chart drawing |
| Type | Condition |
|---|---|
| π Uptrend Channel | Positive trend indicators + sufficient strength |
| π Downtrend Channel | Negative trend indicators + sufficient strength |
| No clear trend or insufficient strength | |
| π Transitioning | Direction emerging but strength not confirmed |
# Run all strategies (default)
conda run -n gold_quant python run_channel_analysis.py
# Select a single strategy
conda run -n gold_quant python run_channel_analysis.py --strategy bollinger
# Select multiple strategies (comma-separated)
conda run -n gold_quant python run_channel_analysis.py --strategy regression,donchian
# Analyze a different symbol
conda run -n gold_quant python run_channel_analysis.py --symbol SI=F # Silverββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β XAUUSD Multi-Timeframe Β· Multi-Strategy Report β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£
β π 1H (Intraday) β
β Regression βοΈ Sideways Pos:59% 5298/5062 β
β Bollinger βοΈ Sideways Pos:41% 5237/5176 β
β Donchian βοΈ Sideways Pos:50% 5237/5163 β
β Trendline π Transition Pos:68% 5225/5149 β
β ββ Consensus: π Mixed (0 up / 0 down / 4 other) β
β ... β
β π Weekly (Long-term) β
β ββ Consensus: π Bullish (3/4 strategies agree) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Results are saved as timestamped JSON files in output/:
output/channel_gcf_20260226_152500_all.json
Built on the Backtrader engine, supports multiple trading strategies across different timeframes.
| Strategy | Description | Script |
|---|---|---|
| Dual MA | SMA golden/death cross | run_backtest.py |
| Enhanced MA | MA + momentum filter | run_optimized_backtest.py |
| Swing Trading | Trend following + swing capture | run_swing_backtest.py |
| Optimized Swing V2 | Improved stops + short selling | run_optimized_v2_backtest.py |
| Script | Timeframe | Description |
|---|---|---|
run_backtest.py |
1H | Quick short-period verification |
run_4h_backtest.py |
4H | Medium-period swing trading |
run_daily_backtest.py |
Daily | Medium-to-long-term trends |
run_long_backtest.py |
Daily (25 years) | Long-term robustness validation |
# Run a backtest (e.g., swing strategy)
conda run -n gold_quant python run_swing_backtest.py
# View results
cat output/gold_swing_backtest.jsonBacktest reports include: return rate, Sharpe ratio, max drawdown, trade count, win rate, and other key metrics.
- YFinance (default): Fetches
GC=F(Gold Futures) OHLCV data viayfinance. Supports 1m to 1wk intervals. Cross-platform. - MT5 (optional): Fetches
XAUUSDreal-time data via MetaTrader5 API. Windows only.
from data.data_fetcher import YFinanceDataFetcher
fetcher = YFinanceDataFetcher(symbol="GC=F")
df = fetcher.fetch_ohlcv(period="1y", interval="1d")factors/technical_indicators.py provides one-call computation of all indicators:
- Trend: SMA (multiple periods), EMA, MACD
- Momentum: RSI
- Volatility: ATR, Bollinger Bands
- Trend Strength: ADX
execution/mt5_trader.py interfaces with MetaTrader5:
- Account info query
- Market / pending order placement
- Position management & closing
- Stop-loss / take-profit configuration
β οΈ Warning: Live trading involves real financial risk. Always test thoroughly on a demo account first.
# 1. Clone the repository
git clone git@github.com:Br0wn2000/Gold_Quant_Project.git
cd Gold_Quant_Project
# 2. Setup environment
conda create -n gold_quant python=3.11 -y
conda activate gold_quant
pip install -r requirements.txt
# 3. Run channel analysis
python run_channel_analysis.py
# 4. Run strategy backtest
python run_swing_backtest.pyThis project is for educational and research purposes only. It does not constitute investment advice.