Skip to content

About

AI-Powered Trading System with Databento, Qlib, and Machine Learning

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

Β 

History

5 Commits

Folders and files

Repository files navigation

πŸš€ AI-Powered Trading System

A comprehensive trading system that integrates Databento market data, Qlib analysis, machine learning models, and advanced backtesting capabilities through a modern web interface.

✨ Features

  • πŸ“Š Data Management: Fetch and manage market data from Databento
  • πŸ” Advanced Analysis: Qlib-powered analysis and ML predictions
  • πŸ“ˆ Backtesting Engine: Comprehensive backtesting with performance metrics
  • 🎯 Trading Strategies: Momentum and mean reversion strategies
  • πŸ“± Modern UI: Intuitive Gradio-based web interface
  • βš™οΈ Configurable: YAML-based configuration system
  • πŸ§ͺ Testable: Comprehensive test suite
  • πŸš€ Multiple Startup Options: Choose the best interface for your needs

πŸ—οΈ Architecture

qlibtrader/
β”œβ”€β”€ πŸš€ working_trading_app.py      # Main working application (RECOMMENDED)
β”œβ”€β”€ πŸ”§ start_simple_trading.py     # Simplified interface launcher
β”œβ”€β”€ πŸ§ͺ minimal_working_app.py      # Basic test interface
β”œβ”€β”€ πŸ“Š diagnose_gradio.py          # Diagnostic tool
β”œβ”€β”€ πŸ› οΈ setup.sh                   # Setup script
β”œβ”€β”€ ui/                            # Web interfaces
β”‚   β”œβ”€β”€ simple_gradio_app.py      # Simplified Gradio app
β”‚   └── __init__.py
β”œβ”€β”€ utils/                         # Utilities
β”‚   β”œβ”€β”€ config.py                 # Configuration management
β”‚   β”œβ”€β”€ logger.py                 # Logging utilities
β”‚   β”œβ”€β”€ validators.py             # Data validation
β”‚   └── __init__.py
β”œβ”€β”€ configs/                       # Configuration files
β”œβ”€β”€ data/                          # Data storage
└── logs/                          # Log files

πŸš€ Quick Start

Prerequisites

  • Python 3.8+
  • Databento API key

Installation

  1. Clone the repository

    git clone <repository-url>
    cd qlibtrader
  2. Install dependencies

    pip install -r requirements.txt
  3. Set up environment variables

    cp env_template .env
    # Edit .env and add your DATABENTO_API_KEY
  4. Run the application

    πŸš€ Recommended: Working Trading System (Stable)

    python working_trading_app.py
    • Access at: http://127.0.0.1:7860
    • Full functionality with stable interface

    πŸ”§ Alternative: Simplified Trading System

    python start_simple_trading.py
    • Access at: http://0.0.0.0:7860
    • Simplified interface for development

    πŸ§ͺ Testing: Minimal Interface

    python minimal_working_app.py
    • Access at: http://127.0.0.1:7860
    • Basic functionality test

    🏭 Production: FastAPI Server

    python start_simple_trading.py --mode server
    • Access at: http://0.0.0.0:8000
    • Production-ready FastAPI server
  5. Open your browser

    • Working/Simplified: http://127.0.0.1:7860
    • Production: http://0.0.0.0:8000

πŸ“– Usage

1. Data Management

  • Enter symbols (e.g., AAPL, MSFT, GOOGL)
  • Set date range and data schema
  • Fetch and cache market data

2. Analysis & Signals

  • Choose analysis type (Qlib, ML, or Strategy)
  • Configure parameters
  • Generate trading signals

3. Backtesting

  • Set backtest parameters (capital, commission, etc.)
  • Run backtests on historical data
  • Analyze performance metrics

4. Results & Reports

  • View comprehensive reports
  • Export results in various formats
  • Monitor performance metrics

βš™οΈ Configuration

The system uses YAML configuration files:

  • configs/data_config.yaml - Data source settings
  • configs/strategy_config.yaml - Strategy parameters
  • configs/model_config.yaml - ML model settings

Environment Variables

Create a .env file with:

DATABENTO_API_KEY=your_api_key_here
LOG_LEVEL=INFO

πŸ§ͺ Testing

Run the test suite:

pytest tests/

Run specific test files:

pytest tests/test_data_fetcher.py
pytest tests/test_analyzer.py
pytest tests/test_backtester.py

Run Gradio diagnostic tool:

python diagnose_gradio.py

πŸ”§ Troubleshooting

Common Issues

1. Gradio Interface Disconnects

  • Use the working trading app: python working_trading_app.py
  • Check Gradio version: pip show gradio
  • Run diagnostics: python diagnose_gradio.py

2. Missing Dependencies

pip install -r requirements.txt
# Or install specific packages:
pip install plotly scikit-learn pyyaml gradio

3. Port Already in Use

# Use a different port:
python working_trading_app.py --port 8080
# Or kill existing processes:
pkill -f "python.*trading"

4. API Key Issues

  • Create .env file: cp env_template .env
  • Add your Databento API key to .env
  • Restart the application

Startup Options

Script Purpose Stability Features
working_trading_app.py Recommended βœ… High Full functionality
start_simple_trading.py Development βœ… High Simplified UI
minimal_working_app.py Testing βœ… High Basic features

πŸ“Š Strategies

Momentum Strategy

  • Identifies trending price movements
  • Uses RSI, MACD, and volume confirmation
  • Configurable lookback periods and thresholds

Mean Reversion Strategy

  • Detects overbought/oversold conditions
  • Uses Bollinger Bands and Z-score
  • Volume confirmation for signal validation

πŸš€ Deployment Options

Docker Deployment

Build and run with Docker:

# Build the image
docker build -t trading-system .

# Run with environment variables
docker run -p 8000:8000 \
  -e DATABENTO_API_KEY=your_key_here \
  trading-system

Using Docker Compose:

# Create .env file with your API key
echo "DATABENTO_API_KEY=your_key_here" > .env

# Start the application
docker-compose up -d

# With reverse proxy
docker-compose --profile proxy up -d

Production Deployment

Using Gunicorn with Uvicorn workers:

pip install gunicorn
gunicorn server:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000

Environment Variables for Production:

export HOST=0.0.0.0
export PORT=8000
export WORKERS=4
export LOG_LEVEL=warning
export ACCESS_LOG=true

Development vs Production

Development Mode:

python start_server.py --reload --log-level debug

Production Mode:

python start_server.py --production --workers 4

πŸ”§ Development

Adding New Strategies

  1. Create a new strategy class inheriting from BaseStrategy
  2. Implement generate_signals() method
  3. Add strategy to the UI configuration

Adding New Analysis Methods

  1. Extend the QlibAnalyzer or MLAnalyzer classes
  2. Implement analysis methods
  3. Update the UI to include new options

Customizing the UI

  1. Modify panel components in ui/components/
  2. Add new tabs or features to ui/gradio_app.py
  3. Update styling in the CSS sections

πŸ“ˆ Performance Metrics

The backtesting engine calculates:

  • Total Return and Annualized Return
  • Sharpe Ratio and Sortino Ratio
  • Maximum Drawdown
  • Win Rate and Trade Statistics
  • Risk-adjusted metrics

πŸ”’ Security

  • API keys stored in environment variables
  • Input validation for all user inputs
  • Secure data caching and storage
  • Error handling and logging

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Submit a pull request

πŸ“„ License

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

πŸ™ Acknowledgments

πŸ“ž Support

For questions and support:

  • Create an issue on GitHub
  • Check the documentation
  • Review the test examples

πŸ”„ Changelog

v0.1.0

  • Initial release
  • Basic data fetching and analysis
  • Momentum and mean reversion strategies
  • Gradio web interface
  • Comprehensive backtesting engine

About

AI-Powered Trading System with Databento, Qlib, and Machine Learning

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages