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
-
Clone the repository
git clone https://github.com/your-username/xtrade-ai-framework.git cd xtrade-ai-framework
-
Create virtual environment
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Install dependencies
pip install -r requirements/base.txt
-
Set up environment variables
cp config/env.example .env # Edit .env with your configuration
-
Set up database
# Create PostgreSQL database createdb xtrade_ai # Run migrations python -m xtrade_ai.cli db migrate
-
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
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Support
- Documentation: docs/
- Issues: GitHub Issues
- Discussions: GitHub Discussions
๐ฎ 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xtrade_ai-1.1.1-py3-none-manylinux_2_17_x86_64.whl.
File metadata
- Download URL: xtrade_ai-1.1.1-py3-none-manylinux_2_17_x86_64.whl
- Upload date:
- Size: 30.2 MB
- Tags: Python 3, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1a7a68ca2e66d605e575f5970808af9252044b43bc21f40b797e82d53de77fc
|
|
| MD5 |
c6437b51e98b79c11d97f173c7f2bf74
|
|
| BLAKE2b-256 |
472cf0a73ab8708843f6f63f1dd3bd334498b4b3dd8477ad630be1c5e1f32c22
|