Skip to content

Latest commit

 

History

History
394 lines (319 loc) · 10.2 KB

File metadata and controls

394 lines (319 loc) · 10.2 KB

AI-Powered Trading System

AI trading system with real-time data processing, machine learning models, risk management, and automated execution capabilities.

🚀 Features

Core Components

  • Real-time Data Ingestion: Multi-source market data collection (Yahoo Finance, Alpaca, News APIs)
  • Advanced ML Models: LSTM, Transformer, and ensemble models for price prediction
  • Risk Management: VaR calculation, stress testing, portfolio diversification
  • Portfolio Management: Automated position sizing, trade execution, performance tracking
  • Backtesting Framework: Historical strategy validation with realistic conditions
  • REST API: Complete API for system integration and monitoring
  • Monitoring & Logging: Comprehensive system monitoring with Prometheus metrics

Key Capabilities

  • ✅ Multi-asset trading (stocks, ETFs, indices)
  • ✅ Real-time signal generation and execution
  • ✅ Advanced risk controls and position management
  • ✅ Comprehensive backtesting with transaction costs
  • ✅ Production-ready monitoring and alerting
  • ✅ Scalable architecture for high-frequency trading

📁 Project Structure

ai_trading_system/
├── main.py                    # Main entry point
├── requirements.txt           # Python dependencies
├── README.md                  # This file
├── ai_trading_system_design.md # Detailed system design
├── TODO.md                    # Development tasks
├── core/                      # Core trading engine
│   ├── engine.py             # Main trading orchestrator
│   └── __init__.py
├── data/                      # Data ingestion layer
│   ├── manager.py            # Market data collection
│   └── __init__.py
├── models/                    # ML prediction models
│   ├── predictor.py          # Price prediction models
│   └── __init__.py
├── risk_management/           # Risk assessment
│   ├── manager.py            # VaR and risk controls
│   └── __init__.py
├── portfolio/                 # Portfolio management
│   ├── manager.py            # Position and execution
│   └── __init__.py
├── backtesting/               # Strategy validation
│   ├── engine.py             # Backtesting framework
│   └── __init__.py
├── api/                       # REST API layer
│   ├── app.py                # FastAPI application
│   └── __init__.py
├── utils/                     # Utilities
│   ├── logging_config.py     # Structured logging
│   ├── monitoring.py         # System monitoring
│   └── __init__.py
├── config/                    # Configuration
│   ├── settings.toml         # System settings
│   └── __init__.py
└── tests/                     # Test suite
    └── __init__.py

🛠️ Installation

Prerequisites

  • Python 3.8+
  • Git
  • Redis (optional, for caching)
  • Alpaca/Polygon account (for live trading)

Setup Instructions

  1. Clone the repository

    git clone <repository-url>
    cd ai_trading_system
  2. Create virtual environment

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

    pip install -r requirements.txt
  4. Configure settings

    # Copy and edit configuration
    cp ai_trading_system/config/settings.toml.example ai_trading_system/config/settings.toml
    # Edit with your API keys and preferences
  5. Set environment variables (optional)

    export ALPACA_API_KEY="your-api-key"
    export ALPACA_API_SECRET="your-api-secret"
    export DATABASE_URL="your-database-url"

🚦 Quick Start

Run API Server (Recommended)

python -m ai_trading_system.main --mode api --port 8000

The API will be available at http://localhost:8000 with:

  • Interactive Docs: http://localhost:8000/docs
  • Health Check: http://localhost:8000/health
  • Portfolio: http://localhost:8000/portfolio
  • Trading Signals: http://localhost:8000/signals

Run Trading Engine Only

python -m ai_trading_system.main --mode trading

Development Mode (with auto-reload)

python -m ai_trading_system.main --mode api --reload

📊 Usage Examples

Get System Status

curl http://localhost:8000/status

Execute a Trade

curl -X POST "http://localhost:8000/trade" \
  -H "Content-Type: application/json" \
  -d '{
    "symbol": "AAPL",
    "action": "buy",
    "quantity": 100,
    "price_target": 150.0
  }'

Run Backtest

curl -X POST "http://localhost:8000/backtest" \
  -H "Content-Type: application/json" \
  -d '{
    "symbols": ["AAPL", "MSFT", "GOOGL"],
    "start_date": "2023-01-01T00:00:00",
    "end_date": "2023-12-31T23:59:59",
    "strategy_name": "momentum",
    "initial_capital": 100000
  }'

Get Portfolio Information

curl http://localhost:8000/portfolio

🔧 Configuration

Main Settings (settings.toml)

[system]
name = "AI Trading System"
environment = "development"  # development, staging, production
log_level = "INFO"

[trading]
max_positions = 50
max_position_size = 0.05  # 5% of portfolio
min_trade_size = 1000     # $1000 minimum

[risk]
max_var = 0.02           # 2% daily VaR limit
stop_loss_pct = 0.05     # 5% stop loss
take_profit_pct = 0.15   # 15% take profit

[models]
prediction_horizon = 24  # hours
retrain_interval = 168   # hours (1 week)

[api]
host = "0.0.0.0"
port = 8000
workers = 4

[alpaca]
api_key = "YOUR_API_KEY"
api_secret = "YOUR_API_SECRET"
base_url = "https://paper-api.alpaca.markets"  # Paper trading

📈 Monitoring

Built-in Monitoring

  • Prometheus Metrics: Available at http://localhost:8000/metrics
  • Health Checks: http://localhost:8000/health
  • Structured Logging: All events logged with context
  • Performance Tracking: Response times, throughput, error rates

Key Metrics

  • Portfolio value and performance
  • Trading signal accuracy
  • System resource usage
  • API response times
  • Error rates and alerts

🧪 Backtesting

Available Strategies

  • Momentum: Buy when price > moving average
  • Mean Reversion: Buy when RSI < 30, sell when RSI > 70

Run Backtest via API

import requests

response = requests.post("http://localhost:8000/backtest", json={
    "symbols": ["AAPL", "MSFT"],
    "start_date": "2023-01-01T00:00:00",
    "end_date": "2023-12-31T23:59:59",
    "strategy_name": "momentum",
    "initial_capital": 100000
})

results = response.json()
print(f"Total Return: {results['total_return']:.2%}")

🔒 Risk Management

Risk Controls

  • Value at Risk (VaR): 2% daily limit
  • Position Limits: Maximum 5% per position
  • Stop Losses: Automatic 5% stop loss
  • Diversification: Sector and asset class limits
  • Stress Testing: Market crash scenario analysis

Risk Metrics

  • Sharpe ratio optimization
  • Maximum drawdown tracking
  • Portfolio correlation analysis
  • Real-time risk monitoring

🤖 Machine Learning Models

Model Types

  • LSTM Networks: Time series price prediction
  • Transformers: Multi-horizon forecasting
  • XGBoost: Ensemble price movement prediction
  • Random Forest: Feature importance analysis

Model Training

# Train models via API
requests.post("http://localhost:8000/models/train", json={
    "symbols": ["AAPL", "MSFT", "GOOGL"]
})

🔌 API Reference

Endpoints

Method Endpoint Description
GET / API information
GET /health Health check
GET /status System status
GET /portfolio Portfolio information
POST /trade Execute trade
POST /backtest Run backtest
GET /signals Current signals
GET /market-data Market data
POST /models/train Train models
GET /performance Performance metrics

🚨 Production Deployment

Docker Deployment

FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "-m", "ai_trading_system.main", "--mode", "api"]

Environment Setup

# Production environment variables
export ENVIRONMENT=production
export LOG_LEVEL=WARNING
export ALPACA_API_KEY=prod-key
export DATABASE_URL=postgresql://...

Monitoring Setup

  • Configure Sentry for error tracking
  • Set up Prometheus/Grafana for metrics
  • Configure log aggregation (ELK stack)
  • Set up alerting for critical metrics

📚 Development

Running Tests

# Install test dependencies
pip install pytest pytest-asyncio

# Run tests
pytest tests/

# Run with coverage
pytest --cov=ai_trading_system tests/

Code Quality

# Format code
black ai_trading_system/
isort ai_trading_system/

# Lint code
flake8 ai_trading_system/

🔧 Troubleshooting

Common Issues

  1. Import Errors

    # Ensure you're in the project root
    cd /path/to/ai_trading_system
    export PYTHONPATH=$PWD:$PYTHONPATH
  2. API Connection Issues

    • Check Alpaca API credentials
    • Verify network connectivity
    • Check API rate limits
  3. Model Training Issues

    • Ensure sufficient historical data
    • Check memory availability for large models
    • Verify data quality and completeness
  4. Performance Issues

    • Monitor system resource usage
    • Check log files for errors
    • Review Prometheus metrics

Debug Mode

# Enable debug logging
export LOG_LEVEL=DEBUG

# Run with detailed output
python -m ai_trading_system.main --mode api --reload

📄 License

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

🤝 Contributing

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

📞 Support

For support and questions:

  • Create an issue in the repository
  • Check the troubleshooting guide above
  • Review the API documentation at /docs

⚠️ Disclaimer: This is a sophisticated trading system for educational and research purposes. Always test thoroughly in paper trading mode before using with real money. Past performance does not guarantee future results.