Skip to content

ziscross-gh/quant-trading

Repository files navigation

πŸ† Autonomous Gold/USD Quantitative Trading System

A production-ready, fully autonomous quantitative trading system for Gold/USD (XAU/USD) with Python and Rust implementations. Features news sentiment analysis, real-time notifications, database logging, and professional broker integrations.

πŸ”₯ Latest Updates

v3.0 - Advanced Strategies & Real-Time Dashboard!

  • πŸ†• Real-Time Web Dashboard - Professional monitoring with WebSocket live updates
  • πŸ†• 7 Advanced Strategies - ATR Volatility, Multi-Timeframe, Ensemble, and more
  • πŸ†• Strategy Comparison Tool - Backtest all strategies side-by-side
  • πŸ†• Interactive Charts - Equity curve, drawdown, and strategy performance visualization
  • βœ… OANDA Broker Integration - Professional forex trading with demo & live modes
  • βœ… News Sentiment Analysis - Yahoo Finance news with Gold-specific AI sentiment
  • βœ… Telegram & Email Alerts - Real-time notifications for all trading events
  • βœ… PostgreSQL Logging - Complete trade history and performance analytics
  • βœ… Health Monitoring - Auto-recovery and system health checks
  • βœ… Rust Performance - 10-100x faster than Python with async execution

🌟 Key Features

Trading & Execution

  • πŸ€– Fully Autonomous - Runs 24/7 with no manual intervention
  • πŸ“ˆ Multi-Indicator Strategy - MA, RSI, Bollinger Bands + News Sentiment
  • πŸ’± OANDA Integration - Professional REST API v20 broker (demo & live)
  • πŸ›‘οΈ Risk Management - Position sizing, stop-loss, take-profit, drawdown limits
  • πŸ“Š Backtesting - Historical validation with slippage and commission

Intelligence & Analysis

  • πŸ“° News Sentiment - Fetches & analyzes Gold/USD news from Yahoo Finance
  • 🧠 60+ Keywords - Financial sentiment lexicon with Gold-specific terms
  • βš–οΈ Signal Blending - Combines technical indicators with news sentiment
  • πŸ“‰ Real-time Analytics - Live performance tracking and equity curves

Notifications & Monitoring

  • πŸ“Š Real-Time Dashboard - Web UI with live charts and WebSocket updates
  • πŸ“ˆ Interactive Visualizations - Equity curve, drawdown, strategy comparison
  • πŸ“± Telegram Bot - Instant alerts with emoji status indicators
  • πŸ“§ HTML Emails - Beautiful formatted trade notifications
  • πŸ’Ύ PostgreSQL - Persistent storage for trades and equity curve
  • ❀️ Health Checks - Automatic error recovery and heartbeat monitoring

Developer Experience

  • ⚑ Rust Speed - 10-100x faster with zero-cost abstractions
  • πŸ”§ Easy Setup - One-command installation script
  • πŸ“– Complete Docs - Step-by-step setup guide
  • πŸ§ͺ Well Tested - Unit tests and integration tests

Python Version

This directory contains the Python implementation. For the high-performance Rust version, see rust/ directory.

✨ Features

  • Autonomous Trading: Continuously monitors markets and executes trades automatically
  • Advanced Strategy: Momentum-based strategy using multiple technical indicators:
    • Moving Average Crossover (Fast/Slow MA)
    • Relative Strength Index (RSI)
    • Bollinger Bands
    • Volume confirmation
    • Trend strength analysis
  • Comprehensive Risk Management:
    • Position sizing based on risk parameters
    • Stop loss and take profit levels
    • Trailing stops
    • Maximum drawdown limits
    • Daily loss limits
  • Backtesting Framework: Test strategies on historical data with detailed performance metrics
  • Paper Trading: Test the system in simulation mode before going live
  • Configurable: YAML-based configuration for easy customization
  • Logging: Comprehensive logging with daily rotation and error tracking

πŸ“‹ Requirements

  • Python 3.8+
  • See requirements.txt for all dependencies

πŸš€ Quick Start

1. Installation

# Clone the repository
git clone <repository-url>
cd quant-trading

# Create virtual environment (recommended)
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

2. Configuration

Copy the example environment file and configure your API keys:

cp .env.example .env
# Edit .env with your API keys

Customize the trading parameters in config/config.yaml:

  • Adjust strategy parameters (MA periods, RSI thresholds, etc.)
  • Set risk management limits
  • Configure capital and position sizing
  • Set data source and intervals

3. Run Backtest

Test the strategy on historical data:

python run_backtest.py

This will:

  • Fetch historical Gold/USD data
  • Run the strategy on past data
  • Generate performance metrics
  • Save results to results/ directory
  • Display equity curve and trading signals

4. Run Autonomous Trader

Paper Trading (Simulation)

python run_autonomous_trader.py --mode paper

Live Trading (Real Money - Use with Caution!)

python run_autonomous_trader.py --mode live

Single Iteration (Testing)

python run_autonomous_trader.py --once

πŸ“ Project Structure

quant-trading/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ data/                 # Data fetching and caching
β”‚   β”‚   β”œβ”€β”€ data_fetcher.py   # Market data acquisition
β”‚   β”‚   └── data_cache.py     # Data caching utilities
β”‚   β”œβ”€β”€ strategies/           # Trading strategies
β”‚   β”‚   β”œβ”€β”€ base_strategy.py  # Base strategy class
β”‚   β”‚   └── gold_momentum_strategy.py  # Gold momentum strategy
β”‚   β”œβ”€β”€ backtesting/          # Backtesting framework
β”‚   β”‚   β”œβ”€β”€ backtest_engine.py  # Backtesting engine
β”‚   β”‚   └── performance_metrics.py  # Performance calculations
β”‚   β”œβ”€β”€ execution/            # Order execution
β”‚   β”‚   β”œβ”€β”€ autonomous_trader.py  # Autonomous trading engine
β”‚   β”‚   └── order_executor.py     # Order execution logic
β”‚   β”œβ”€β”€ risk_management/      # Risk management
β”‚   β”‚   β”œβ”€β”€ risk_manager.py   # Risk control system
β”‚   β”‚   └── position_sizer.py # Position sizing utilities
β”‚   └── utils/                # Utilities
β”‚       β”œβ”€β”€ config_loader.py  # Configuration management
β”‚       └── logger.py         # Logging setup
β”œβ”€β”€ config/
β”‚   └── config.yaml           # Main configuration file
β”œβ”€β”€ data/                     # Data storage
β”‚   β”œβ”€β”€ raw/                  # Raw market data
β”‚   └── processed/            # Processed data
β”œβ”€β”€ logs/                     # Log files
β”œβ”€β”€ results/                  # Backtest results
β”œβ”€β”€ notebooks/                # Jupyter notebooks for analysis
β”œβ”€β”€ tests/                    # Unit tests
β”œβ”€β”€ run_backtest.py          # Backtest runner
β”œβ”€β”€ run_autonomous_trader.py # Autonomous trader runner
β”œβ”€β”€ requirements.txt         # Python dependencies
β”œβ”€β”€ .env.example            # Example environment variables
└── README.md               # This file

🎯 Strategy Overview

Gold Momentum Strategy

The strategy uses multiple technical indicators to identify momentum in Gold/USD prices:

  1. Moving Average Crossover:

    • Fast MA (20-period) crosses Slow MA (50-period)
    • Generates primary trend signals
  2. RSI (Relative Strength Index):

    • Identifies overbought (>70) and oversold (<30) conditions
    • Confirms momentum and potential reversals
  3. Bollinger Bands:

    • Measures volatility
    • Identifies breakout and mean reversion opportunities
  4. Volume Confirmation:

    • Confirms signals with volume analysis
    • Filters false breakouts
  5. Trend Strength:

    • Measures the strength of the current trend
    • Helps determine position sizing

Signal Generation

Signals are generated based on a scoring system:

  • Multiple indicators must align for a trade signal
  • Buy signal requires 3+ bullish indicators
  • Sell signal requires 3+ bearish indicators
  • Otherwise, hold position

πŸ›‘οΈ Risk Management

The system implements comprehensive risk controls:

  • Position Sizing: Based on risk per trade (default 1% of capital)
  • Stop Loss: Automatic stop loss at 2% per trade
  • Take Profit: Automatic take profit at 5% per trade
  • Trailing Stop: Dynamic trailing stop at 1.5%
  • Maximum Drawdown: System halts at 15% drawdown
  • Daily Loss Limit: Maximum 5% loss per day
  • Position Limits: Maximum 3 concurrent positions

πŸ“Š Performance Metrics

The backtesting framework calculates:

  • Return Metrics: Total return, annualized return, daily returns
  • Risk Metrics: Sharpe ratio, Sortino ratio, Calmar ratio, max drawdown
  • Win/Loss Metrics: Win rate, profit factor, average win/loss
  • Trade Statistics: Number of trades, consecutive wins/losses, trade duration

πŸ“ˆ Example Usage

Backtest Example

from src.utils.config_loader import load_config
from src.data.data_fetcher import DataFetcher
from src.strategies.gold_momentum_strategy import GoldMomentumStrategy
from src.backtesting.backtest_engine import BacktestEngine

# Load configuration
config = load_config()

# Initialize components
data_fetcher = DataFetcher(config)
strategy = GoldMomentumStrategy(config)
backtest = BacktestEngine(strategy, config)

# Fetch data and run backtest
data = data_fetcher.fetch_historical_data(start_date='2020-01-01', end_date='2024-12-31')
results = backtest.run(data)

# View results
print(results['metrics'])
backtest.plot_results()

Live Trading Example

from src.utils.config_loader import load_config
from src.strategies.gold_momentum_strategy import GoldMomentumStrategy
from src.execution.autonomous_trader import AutonomousTrader

# Load configuration
config = load_config()
config['trading']['trading_mode'] = 'paper'  # Use 'live' for real trading

# Initialize and start
strategy = GoldMomentumStrategy(config)
trader = AutonomousTrader(strategy, config)
trader.start()

βš™οΈ Configuration

Key configuration parameters in config/config.yaml:

trading:
  symbol: "GC=F"           # Gold Futures
  initial_capital: 100000   # Starting capital
  position_size: 0.1        # 10% per trade
  trading_mode: "paper"     # paper or live

strategy:
  fast_ma: 20              # Fast moving average
  slow_ma: 50              # Slow moving average
  rsi_period: 14           # RSI period
  rsi_oversold: 30         # RSI oversold threshold
  rsi_overbought: 70       # RSI overbought threshold

risk:
  max_drawdown_pct: 15     # Maximum drawdown
  stop_loss_pct: 2         # Stop loss per trade
  take_profit_pct: 5       # Take profit per trade
  risk_per_trade_pct: 1    # Risk per trade

πŸ” Security

  • Store API keys in .env file (never commit this file)
  • Use paper trading mode for testing
  • Start with small capital in live mode
  • Monitor the system regularly
  • Set appropriate risk limits

πŸ“ Logging

Logs are stored in the logs/ directory:

  • trading_YYYY-MM-DD.log: Daily trading logs
  • trading_errors_YYYY-MM-DD.log: Error logs
  • Automatic rotation and compression
  • 30-day retention for regular logs, 90 days for errors

πŸ§ͺ Testing

Run tests:

pytest tests/

🀝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

⚠️ Disclaimer

IMPORTANT: This software is for educational purposes only. Trading involves substantial risk of loss. Past performance is not indicative of future results. The authors are not responsible for any financial losses incurred through the use of this software.

  • Always test thoroughly in paper trading mode first
  • Start with small amounts in live trading
  • Never trade with money you cannot afford to lose
  • Consult with a financial advisor before trading

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ“§ Contact

For questions, issues, or contributions, please open an issue on GitHub.

πŸ™ Acknowledgments

  • Market data provided by Yahoo Finance
  • Built with Python and various open-source libraries
  • Inspired by quantitative trading research and best practices

Happy Trading! πŸ“ˆπŸ’°

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors