Skip to main content

A comprehensive reinforcement learning framework for algorithmic trading

Project description

XTrade-AI Framework

A comprehensive AI-powered trading framework built with Python, featuring machine learning models, backtesting, and live trading capabilities.

๐Ÿ—๏ธ Architecture

XTrade-AI Framework follows the Model-View-Controller (MVC) architecture pattern, providing a clean separation of concerns:

  • Models (xtrade_ai/model/): Database models and data access layer
  • Controllers (xtrade_ai/controller/): Business logic and application logic
  • Routers (xtrade_ai/router/): API endpoints and request handling
  • Database (xtrade_ai/database/): Database connection and migration management

โœจ Features

  • MVC Architecture: Clean, maintainable code structure
  • PostgreSQL + TimescaleDB: High-performance time-series database
  • FastAPI: Modern, fast web framework for building APIs
  • Machine Learning: Support for XGBoost, scikit-learn, and custom models
  • Backtesting Engine: Comprehensive backtesting with performance metrics
  • Live Trading: Real-time trading with multiple brokers
  • Data Management: Efficient market data storage and retrieval
  • User Management: Multi-user support with role-based access control
  • Migration System: Database schema versioning and management

๐Ÿš€ Quick Start

Prerequisites

  • Python 3.8+
  • PostgreSQL 12+
  • TimescaleDB extension

Installation

  1. Clone the repository

    git clone https://github.com/your-username/xtrade-ai-framework.git
    cd xtrade-ai-framework
    
  2. Create virtual environment

    python -m venv .venv
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
    
  3. Install dependencies

    pip install -r requirements/base.txt
    
  4. Set up environment variables

    cp config/env.example .env
    # Edit .env with your configuration
    
  5. Set up database

    # Create PostgreSQL database
    createdb xtrade_ai
    
    # Run migrations
    python -m xtrade_ai.cli db migrate
    
  6. Start the API server

    python -m xtrade_ai.cli api serve
    

๐Ÿ“ Project Structure

xtrade_ai/
โ”œโ”€โ”€ api.py                 # Main API application
โ”œโ”€โ”€ cli.py                 # Command-line interface
โ”œโ”€โ”€ database/              # Database layer
โ”‚   โ”œโ”€โ”€ connection.py      # Database connection manager
โ”‚   โ””โ”€โ”€ migration_manager.py # Migration system
โ”œโ”€โ”€ model/                 # Data models (MVC Model)
โ”‚   โ”œโ”€โ”€ base_model.py      # Base model class
โ”‚   โ”œโ”€โ”€ user_model.py      # User management
โ”‚   โ”œโ”€โ”€ model_model.py     # ML model management
โ”‚   โ”œโ”€โ”€ training_model.py  # Training sessions
โ”‚   โ”œโ”€โ”€ backtest_model.py  # Backtest sessions
โ”‚   โ”œโ”€โ”€ prediction_model.py # Predictions
โ”‚   โ””โ”€โ”€ market_data_model.py # Market data
โ”œโ”€โ”€ controller/            # Business logic (MVC Controller)
โ”‚   โ”œโ”€โ”€ base_controller.py # Base controller
โ”‚   โ”œโ”€โ”€ user_controller.py # User management logic
โ”‚   โ”œโ”€โ”€ model_controller.py # Model management logic
โ”‚   โ”œโ”€โ”€ training_controller.py # Training logic
โ”‚   โ”œโ”€โ”€ backtest_controller.py # Backtest logic
โ”‚   โ”œโ”€โ”€ prediction_controller.py # Prediction logic
โ”‚   โ””โ”€โ”€ market_data_controller.py # Market data logic
โ”œโ”€โ”€ router/                # API endpoints (MVC View)
โ”‚   โ”œโ”€โ”€ base_router.py     # Base router
โ”‚   โ”œโ”€โ”€ user_router.py     # User endpoints
โ”‚   โ”œโ”€โ”€ model_router.py    # Model endpoints
โ”‚   โ”œโ”€โ”€ training_router.py # Training endpoints
โ”‚   โ”œโ”€โ”€ backtest_router.py # Backtest endpoints
โ”‚   โ”œโ”€โ”€ prediction_router.py # Prediction endpoints
โ”‚   โ””โ”€โ”€ market_data_router.py # Market data endpoints
โ”œโ”€โ”€ migrations/            # Database migrations
โ”‚   โ””โ”€โ”€ migration_001_initial_schema.sql
โ””โ”€โ”€ utils/                 # Utility functions
    โ”œโ”€โ”€ logger.py          # Logging configuration
    โ””โ”€โ”€ ...                # Other utilities

๐Ÿ—„๏ธ Database Schema

The framework uses PostgreSQL with TimescaleDB for efficient time-series data storage:

  • Users: User authentication and management
  • Models: Machine learning model registry
  • Training Sessions: Model training tracking
  • Backtest Sessions: Backtesting results and metrics
  • Market Data: OHLCV data with TimescaleDB hypertables
  • Predictions: Model prediction requests and results

๐Ÿ”ง CLI Commands

Database Management

# Check migration status
python -m xtrade_ai.cli db status

# Run migrations
python -m xtrade_ai.cli db migrate

# Rollback migrations
python -m xtrade_ai.cli db rollback --steps 1

# Create new migration
python -m xtrade_ai.cli db create-migration add_new_table

# Test database connection
python -m xtrade_ai.cli db connect

API Management

# Start API server
python -m xtrade_ai.cli api serve

# Open API documentation
python -m xtrade_ai.cli api docs

General

# Show framework information
python -m xtrade_ai.cli info

๐ŸŒ API Endpoints

The framework provides RESTful API endpoints:

  • Authentication: /api/v1/users/auth/*
  • User Management: /api/v1/users/*
  • Models: /api/v1/models/*
  • Training: /api/v1/training/*
  • Backtesting: /api/v1/backtest/*
  • Predictions: /api/v1/predictions/*
  • Market Data: /api/v1/market-data/*

๐Ÿ” Authentication

The framework supports:

  • JWT-based authentication
  • Role-based access control (User/Admin)
  • API key management
  • Secure password hashing with bcrypt

๐Ÿ“Š Data Management

  • Market Data: Efficient storage with TimescaleDB hypertables
  • Model Storage: Local and cloud storage support (S3, GCS)
  • Data Validation: Pydantic models for data validation
  • Bulk Operations: Optimized for high-volume data processing

๐Ÿงช Testing

# Run tests
python -m pytest tests/

# Run with coverage
python -m pytest --cov=xtrade_ai tests/

๐Ÿ“ˆ Performance

  • Database: Connection pooling and optimized queries
  • API: Async FastAPI with high concurrency
  • Data Processing: Efficient pandas operations
  • Caching: Redis integration for performance

๐Ÿš€ Deployment

Docker

# Build image
docker build -t xtrade-ai .

# Run container
docker run -p 8000:8000 xtrade-ai

Production

  • Use production-grade PostgreSQL
  • Enable SSL/TLS
  • Set up proper logging and monitoring
  • Configure backup strategies

๐Ÿค Contributing

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

๐Ÿ“„ License

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

๐Ÿ†˜ Support

๐Ÿ”ฎ Roadmap

  • Advanced ML model support (PyTorch, TensorFlow)
  • Real-time streaming data
  • Advanced risk management
  • Multi-asset portfolio optimization
  • Cloud-native deployment
  • Advanced analytics dashboard

Built with โค๏ธ for the trading community

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xtrade_ai-1.1.1-py3-none-manylinux_2_17_x86_64.whl (30.2 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

File details

Details for the file xtrade_ai-1.1.1-py3-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for xtrade_ai-1.1.1-py3-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 e1a7a68ca2e66d605e575f5970808af9252044b43bc21f40b797e82d53de77fc
MD5 c6437b51e98b79c11d97f173c7f2bf74
BLAKE2b-256 472cf0a73ab8708843f6f63f1dd3bd334498b4b3dd8477ad630be1c5e1f32c22

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page