TradeAgent is a production-ready hybrid AI trading system that combines deterministic technical analysis with LLM-powered sentiment analysis. Built for Alpaca's paper trading API, it features adaptive parameter optimization, portfolio correlation monitoring, and comprehensive risk management.
- Momentum Trading: RSI, MACD, volume-based signals with dynamic thresholds
- News Sentiment Analysis: Claude 3.5 Sonnet analyzes 90+ articles per ticker
- Sentiment Trend Tracker: Detects sentiment momentum shifts and inflection points
- Defensive Core: Monthly rebalancing of VTI/VGK/GLD allocation
- Adaptive Parameter Optimizer: Grid search optimization based on rolling 30-day performance
- ML Training Pipeline: Collects labeled data for future ensemble models
- Performance Analytics: Daily & weekly reports with Sharpe ratio tracking
- Portfolio Correlation Monitor: Blocks over-concentrated positions (max 40% per sector)
- Dynamic Position Sizing: Kelly Criterion with half-Kelly safety (3-15% per position)
- Circuit Breakers: -3% daily loss limit, automatic trading halt
- Stop-Loss & Take-Profit: Bracket orders on all momentum entries
- Multi-Source News: Yahoo Finance, Finnhub, Alpha Vantage, NewsAPI
- Persistent Logging: All trades, signals, and LLM analyses stored in Supabase
- Complete Audit Trail: Track parameter changes, performance metrics, reasoning
- Python 3.12+
- Alpaca Paper Trading Account (Sign up)
- Supabase Account (Sign up)
- OpenRouter API Key (Sign up) (optional, for LLM features)
-
Clone the repository ```bash git clone https://github.com/enving/TradeAgent.git cd TradeAgent ```
-
Create virtual environment ```bash python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate ```
-
Install dependencies ```bash pip install -r requirements.txt ```
-
Configure environment variables ```bash cp .env.example .env
```
Required variables: ```env ALPACA_API_KEY=your_alpaca_key ALPACA_SECRET_KEY=your_alpaca_secret SUPABASE_URL=your_supabase_url SUPABASE_KEY=your_supabase_anon_key ```
Optional (for LLM features): ```env OPENROUTER_API_KEY=your_openrouter_key NEWS_API_KEY=your_newsapi_key FINNHUB_API_KEY=your_finnhub_key ENABLE_LLM_FEATURES=true ```
- Set up Supabase database
Run these SQL migrations in your Supabase SQL Editor (in order):
-
database/migrations/create_ml_training_data.sql -
database/migrations/add_news_and_llm_logging_v2.sql -
database/migrations/add_parameter_changes.sql -
Test the installation ```bash python3 test_integration.py ```
Run the bot autonomously with automated scheduling!
```bash
./run_docker.sh build
./run_docker.sh once
./run_docker.sh schedule
./run_docker.sh logs
./run_docker.sh stop ```
- ✅ Autonomous operation - Runs daily at 9:35 AM ET automatically
- ✅ Isolated environment - No dependency conflicts
- ✅ Auto-restart - Recovers from failures automatically
- ✅ Cross-platform - Works on Linux, macOS, Windows
- ✅ Easy updates - `git pull && ./run_docker.sh build`
For native Linux deployment or cron-based scheduling, see detailed guide:
- deployment/README.md - Full deployment guide with SystemD and Cron options
```bash python3 -m src.main ```
The bot will:
- Check market hours
- Scan for momentum signals (15 tickers)
- Analyze news sentiment (if LLM enabled)
- Apply risk filters (correlation, sector limits)
- Execute trades with bracket orders
- Monitor exit conditions for open positions
- Log performance metrics to Supabase
```bash python3 -c "import asyncio; from src.main import daily_trading_loop; asyncio.run(daily_trading_loop(allow_premarket=True))" ```
After collecting 30+ trades: ```bash
python3 run_optimizer.py --strategy momentum --lookback 30
python3 run_optimizer.py --show ```
```bash
python3 test_correlation_monitor.py
python3 test_sentiment_tracker.py ```
``` TradeAgent/ ├── src/ │ ├── strategies/ # Trading strategies │ ├── risk/ # Risk management │ ├── ml/ # Machine learning │ ├── llm/ # LLM integration │ ├── core/ # Core logic │ ├── adapters/ # External APIs │ ├── models/ # Data models │ └── database/ # Supabase client ├── database/migrations/ # SQL migrations └── tests/ # Tests ```
Entry: RSI 45-75, MACD > 0, Volume > 1.1x avg
Exit: Stop -3%, Target +8%, RSI > 80
Entry: Sentiment > 0.6, High impact, OR inflection detected
Claude 3.5 Sonnet analyzes 90+ articles/ticker
Allocation: 50% VTI, 30% VGK, 20% GLD
Rebalance: Monthly, first trading day
Tests 27+ parameter combinations on 30-day performance, selects optimal Sharpe ratio.
Detects sentiment momentum via linear regression, blocks volatile signals.
Prevents over-concentration: max 0.7 correlation, 40% sector limit.
```bash
python3 test_integration.py
python3 test_correlation_monitor.py python3 test_sentiment_tracker.py ```
Educational purposes only. Not financial advice.
- Paper trading only
- No profitability guarantees
- Trading involves risk of loss
- Use at your own risk
MIT License - see LICENSE
Built with ❤️ by algorithmic traders, for algorithmic traders.