Production-Ready Algorithmic Trading System with sentiment analysis, machine learning predictions, and comprehensive risk management. Completely refactored from a non-functional prototype into a battle-tested trading system.
New to the system? Use the automated setup:
# Clone and enter directory
git clone <repository-url>
cd algorithmic-trading-bot
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
# Automated setup (installs dependencies, creates directories, etc.)
python quick_start.py
# Follow the prompts to set up your .env file
# Then test immediately:
python backtest.py --start-date 2024-01-01 --end-date 2024-06-30
- Python 3.8+ (3.10+ recommended)
- 4GB+ RAM (8GB+ recommended for ML training)
- Alpaca Trading Account (Paper/Live)
- Reddit API credentials (optional but recommended)
# Clone the repository
git clone <repository-url>
cd algorithmic-trading-bot
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
# Install core dependencies
pip install -r requirements.txt
# Install ML dependencies (for training models)
pip install -r requirements-ml.txt
Create .env
file in the project root:
# === REQUIRED: Alpaca API (Paper Trading) ===
ALPACA_API_KEY=your_alpaca_api_key_here
ALPACA_API_SECRET=your_alpaca_secret_here
ALPACA_BASE_URL=https://paper-api.alpaca.markets
# === REQUIRED: Database ===
DATABASE_URL=postgresql://username:password@localhost:5432/quantumsentiment
# Alternative: DATABASE_URL=sqlite:///quantumsentiment.db
# === OPTIONAL: Sentiment Analysis ===
# Reddit API (highly recommended)
REDDIT_CLIENT_ID=your_reddit_client_id
REDDIT_CLIENT_SECRET=your_reddit_client_secret
REDDIT_USER_AGENT=QuantumSentiment/1.0
# News APIs (optional)
NEWSAPI_KEY=your_newsapi_key
ALPHA_VANTAGE_API_KEY=your_alpha_vantage_key
# === OPTIONAL: Alerts ===
TELEGRAM_BOT_TOKEN=your_telegram_bot_token
TELEGRAM_CHAT_ID=your_telegram_chat_id
# Option 1: PostgreSQL (Recommended for production)
# Install PostgreSQL and create database
createdb quantumsentiment
# Option 2: SQLite (Quick start)
# Just set DATABASE_URL=sqlite:///quantumsentiment.db in .env
# Create required directories
mkdir -p models data logs backups cache
# Download and prepare training data (optional - models included)
python scripts/download_quality_data.py --symbols 30
# Train models (optional - pre-trained models included)
python train_production.py --quick-start
# Basic paper trading with default settings
python src/main.py --mode paper
# Paper trading with custom config
python src/main.py --mode paper --config config/config_small_data.yaml
# Semi-automatic mode (ask for confirmation on trades)
python src/main.py --mode semi_auto
# Basic 6-month backtest
python backtest.py --start-date 2024-01-01 --end-date 2024-06-30
# Custom symbols and capital
python backtest.py --symbols AAPL GOOGL MSFT --start-date 2024-01-01 --end-date 2024-12-31 --capital 50000
# Using small data config
python backtest.py --config config/config_small_data.yaml --start-date 2024-01-01 --end-date 2024-06-30
python training/train_simple_massive.py
# or
python training/train_binary_massive.py
File | Purpose | When to Use |
---|---|---|
config/config.yaml |
Production configuration | Live/Paper trading with full features |
config/config_small_data.yaml |
Limited data configuration | Testing, limited memory, quick validation |
config/download_config.yaml |
Data download settings | Customizing data collection |
# In config.yaml
trading:
strategy_mode: "adaptive" # Options: adaptive, technical_only, sentiment_only, conservative
adaptive
β Recommended: Uses any available signals, adapts to data availabilitytechnical_only
: Requires technical indicators only (works without sentiment APIs)sentiment_only
: Requires sentiment data (Reddit/News APIs needed)conservative
: Requires both technical AND sentiment confirmation (highest confidence)
risk:
max_drawdown: 0.10 # 10% maximum portfolio drawdown
daily_loss_limit: 0.03 # 3% daily loss limit
stop_loss_pct: 0.02 # 2% stop loss per position
take_profit_pct: 0.05 # 5% take profit per position
max_positions: 10 # Maximum concurrent positions
max_position_size: 0.10 # 10% max per position
# Advanced position sizing with Kelly Criterion
# Automatically enabled - calculates optimal position sizes
position_sizer:
use_kelly_criterion: true
kelly_fraction: 0.25 # Use 25% of Kelly recommendation (safety factor)
max_position_size: 0.10 # Hard cap at 10% per position
The system includes pre-trained models ready for paper trading:
- XGBoost: Primary trading model (49.6% accuracy, +29.6% vs baseline)
- LSTM: Price sequence prediction
- CNN: Chart pattern recognition
- FinBERT: Sentiment analysis
- Ensemble: Stacked combination of all models
Model | Accuracy | Performance vs Baseline | Status |
---|---|---|---|
XGBoost | 49.6% | +29.6% | β Primary Model |
LSTM | ~41% | +21% | β Supporting Model |
CNN | ~41% | +21% | β Pattern Recognition |
Ensemble | ~44% | +24% | β Combined Prediction |
# Quick training (1 hour, good for testing)
python train_production.py --quick-start
# Production training (4-6 hours, best performance)
python train_production.py --symbols 50 --epochs 500 --use-all-data
# Memory-efficient training
python train_production.py --config config/config_small_data.yaml --symbols 20
Historical Data β Feature Engineering β Model Prediction β Signal Validation β Risk Assessment β Position Sizing β Order Execution
β β β β β β β
Alpaca API 119+ Features XGBoost/LSTM Strategy Rules VaR Analysis Kelly Criterion Simulated/Live
- Data Sources: Alpaca (prices), Reddit (sentiment), News APIs (sentiment)
- Feature Engineering: 119+ technical indicators + sentiment scores
- ML Pipeline: XGBoost primary, LSTM/CNN supporting, FinBERT sentiment
- Risk Management: Kelly Criterion sizing, VaR limits, stop-loss protection
- Execution: Smart order routing with slippage/commission simulation
- Market Data: Updated every minute
- Sentiment Analysis: Updated every 5 minutes
- Model Predictions: Updated every 15 minutes
- Risk Checks: Continuous monitoring
- Position Management: Real-time stop-loss/take-profit
- Sharpe Ratio: Risk-adjusted returns
- Sortino Ratio: Downside risk focus
- Max Drawdown: Worst portfolio decline
- Win Rate: Percentage of profitable trades
- Profit Factor: Gross profit / Gross loss
- Portfolio Level: Max drawdown (10%), daily loss limit (3%)
- Position Level: Stop-loss (2%), take-profit (5%), position size limits
- Correlation Limits: Max correlation between positions
- Sector Concentration: Limits on sector exposure
- Create Strategy Module:
# src/strategies/my_strategy.py
class MyCustomStrategy:
def validate_signal(self, signal):
# Custom signal validation logic
return signal['confidence'] > 0.8
- Configure Strategy:
# config/config.yaml
trading:
strategy_mode: "custom"
custom_strategy: "my_strategy"
The system provides REST APIs for monitoring:
# Start API server
python -m src.api.server
# Health check
curl http://localhost:8000/health
# Get current positions
curl http://localhost:8000/api/positions
# Get performance metrics
curl http://localhost:8000/api/metrics
TELEGRAM_BOT_TOKEN=your_bot_token
TELEGRAM_CHAT_ID=your_chat_id
# Access web dashboard at:
http://localhost:8000/dashboard
# Error: No trained models found
# Solution: Train models or check model path
python train_production.py --quick-start
# Error: Alpaca authentication failed
# Solution: Check .env file has correct API keys
# Verify: ALPACA_API_KEY and ALPACA_API_SECRET are set
# Error: Out of memory during model training
# Solution: Use smaller configuration
python train_production.py --config config/config_small_data.yaml --symbols 10
# In config.yaml, change cache type to memory:
cache:
type: "memory" # Instead of "redis"
# Try SQLite instead of PostgreSQL:
DATABASE_URL=sqlite:///quantumsentiment.db
# Run with debug logging
python src/main.py --mode paper --log-level DEBUG
# Enable profiling
python src/main.py --mode paper --profile
# In config.yaml - reduce frequency for slower systems:
scheduler:
market_data_update: "*/5 * * * *" # Every 5 minutes instead of 1
sentiment_update: "*/15 * * * *" # Every 15 minutes instead of 5
model_prediction: "*/30 * * * *" # Every 30 minutes instead of 15
algorithmic-trading-bot/
βββ config/ # Configuration files
β βββ config.yaml # Main production config
β βββ config_small_data.yaml # Limited data config
β βββ download_config.yaml # Data download settings
βββ src/ # Source code
β βββ main.py # Main trading bot entry point
β βββ backtesting/ # Backtesting engine
β βββ broker/ # Trading broker integrations
β βββ data/ # Data fetching and management
β βββ features/ # Feature engineering
β βββ models/ # ML models and ensemble
β βββ portfolio/ # Portfolio optimization
β βββ risk/ # Risk management
β βββ sentiment/ # Sentiment analysis
β βββ training/ # Model training pipeline
βββ scripts/ # Utility scripts
β βββ download_quality_data.py # Download training data
β βββ prepare_quality_data.py # Data preprocessing
βββ models/ # Trained model storage
βββ data/ # Historical data storage
βββ logs/ # Application logs
βββ backups/ # Configuration backups
βββ cache/ # Temporary cache
βββ backtest.py # Backtest entry point
βββ train_production.py # Model training entry point
βββ requirements.txt # Core dependencies
βββ requirements-ml.txt # ML dependencies
βββ README.md # This file
CLAUDE.md
: Development guidelines and system contextTODO.md
: Completed refactoring plan and system analysisNOTES.md
: Technical decisions and implementation notesTRAINING_GUIDE.md
: Detailed model training instructions
- REST API:
http://localhost:8000/docs
(when API server running) - Configuration: See
config/config.yaml
with inline comments - Model Architecture: See
src/models/
for implementation details
# Run unit tests
pytest tests/
# Run integration tests
pytest tests/integration/
# Test configuration validation
python -c "from src.configuration import load_config; print('Config OK')"
# Test database connection
python -c "from src.database import DatabaseManager; db = DatabaseManager(); print('DB OK')"
# Test API credentials
python scripts/test_apis.py
- Setup Production Environment:
# Use production config
cp config/config.yaml config/config_production.yaml
# Set production environment
export ENVIRONMENT=production
- Run with Process Manager:
# Using systemd (Linux)
sudo systemctl enable quantumsentiment.service
sudo systemctl start quantumsentiment.service
# Using PM2 (Node.js process manager)
pm2 start ecosystem.config.js
- Monitoring:
# View logs
tail -f logs/trading.log
# Monitor performance
python scripts/monitor_performance.py
The system can be deployed on cloud platforms:
- AWS: EC2 + RDS + ElastiCache
- Google Cloud: Compute Engine + Cloud SQL + Memorystore
- Azure: Virtual Machines + Database + Cache
- Docker: Containerized deployment available
IMPORTANT: This software is for educational and research purposes. Algorithmic trading involves substantial risk of financial loss.
- Start with Paper Trading: Always test thoroughly before risking real money
- Understand the Risks: Past performance does not guarantee future results
- Monitor Continuously: Automated systems require active monitoring
- Risk Management: Never risk more than you can afford to lose
The authors are not responsible for any financial losses incurred from using this software.
- Fork the Repository
- Create Feature Branch:
git checkout -b feature/amazing-feature
- Commit Changes:
git commit -m 'Add amazing feature'
- Push to Branch:
git push origin feature/amazing-feature
- Open Pull Request
# Install development dependencies
pip install -r requirements-dev.txt
# Install pre-commit hooks
pre-commit install
# Run linting and tests
ruff check src/
black src/
pytest tests/
This project is licensed under the MIT License - see the LICENSE file for details.
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: Check
CLAUDE.md
and inline code comments - Configuration Help: Review
config/config.yaml
comments
Happy Trading! ππ―
Built with β€οΈ by the QuantumSentiment team