An intelligent algorithmic trading bot that combines technical analysis with risk management to execute automated trades on Alpaca Markets. Built with clean architecture, comprehensive testing, and production-ready code quality.
- π Technical Analysis: RSI, SMA indicators with customizable parameters
- π‘οΈ Risk Management: Position sizing, stop-loss automation, portfolio limits
- β° Market Hours Detection: Automatic trading suspension when markets are closed
- π Comprehensive Logging: Detailed execution logs for monitoring and debugging
- π§ͺ Fully Tested: 100% test coverage on core functionality
- ποΈ Clean Architecture: SOLID principles, 97.2/100 code quality score
- Python 3.8+
- Alpaca Markets account (Sign up)
- Anthropic API key (optional, for future AI features)
# Clone the repository
git clone https://github.com/yourusername/trading-bot.git
cd trading-bot
# Install dependencies
pip install -r requirements.txt
# Configure API keys
cp .env.example .env
# Edit .env with your API keysEdit .env with your credentials:
# Alpaca API (Paper Trading)
ALPACA_API_KEY=your_alpaca_key_here
ALPACA_SECRET_KEY=your_alpaca_secret_here
ALPACA_BASE_URL=https://paper-api.alpaca.markets
# Anthropic API (Optional)
ANTHROPIC_API_KEY=your_anthropic_key_here# Run once
python run.py
# Run continuously (every 5 minutes)
./run_bot.shtrading-bot/
βββ analyzer.py # Technical analysis engine
βββ trader.py # Trading execution logic
βββ risk_manager.py # Risk management system
βββ market_data.py # Market data fetching
βββ config.py # Configuration settings
βββ logger.py # Logging setup
βββ run.py # Main entry point
βββ test_trading_bot.py # Test suite
βββ requirements.txt # Dependencies
- Market Check: Verifies market is open before trading
- Data Collection: Fetches historical price data for target symbols
- Technical Analysis: Calculates RSI, SMA indicators
- Signal Generation: Identifies buy/sell opportunities
- Risk Assessment: Validates position sizing and portfolio limits
- Order Execution: Submits market orders via Alpaca API
- Stop Loss Monitoring: Automatically exits losing positions
- Strong Buy: RSI < 30 + Uptrend (Confidence: 75%)
- Buy: Price > SMA(5) > SMA(20) (Confidence: 65%)
- Strong Sell: RSI > 70 (Confidence: 80%)
- Sell: Downtrend detected (Confidence: 70%)
- Position Size: 10% of portfolio per position
- Stop Loss: 5% below entry price
- Max Positions: 5 concurrent positions
- Min Confidence: 60% required for execution
# Run all tests
pytest test_trading_bot.py -v
# Run with coverage
pytest test_trading_bot.py --cov=. --cov-report=htmlTest Results: 11/11 passing (100%)
This project maintains high code quality standards:
- Overall Score: 97.2/100 (Grade A)
- Code Smells: 19 (all low severity)
- SOLID Violations: 0
- Test Coverage: 100% on core modules
See CODE_REVIEW_SKILL_REPORT.md for detailed analysis.
- β
API keys stored in
.env(gitignored) - β No hardcoded credentials
- β Input validation on all external data
- β Error handling with safe defaults
Important: Never commit .env file. Always use paper trading for testing.
Edit config.py to customize:
TARGET_SYMBOLS = ["AAPL", "GOOGL", "MSFT", "AMZN", "TSLA"]
HISTORY_DAYS = 30
MAX_POSITIONS = 5
POSITION_SIZE_PCT = 0.10 # 10% per position
STOP_LOSS_PCT = 0.05 # 5% stop lossThis project follows PEP 8 and uses:
- Dataclasses for parameter encapsulation
- Single Responsibility Principle
- Comprehensive error handling
- Descriptive logging
- Add calculation method to
TechnicalAnalyzer - Update
_calculate_indicators()method - Modify signal logic in
_check_buy_signals()/_check_sell_signals() - Add tests in
test_trading_bot.py
Logs are stored in logs/trading_bot.log:
# View recent activity
tail -f logs/trading_bot.log
# Search for trades
grep "BUYING\|SELLING" logs/trading_bot.logThis bot is for educational purposes only.
- Trading involves substantial risk of loss
- Past performance does not guarantee future results
- Always test with paper trading first
- Never invest more than you can afford to lose
- Consult a financial advisor before live trading
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Alpaca Markets for commission-free trading API
- Anthropic for Claude AI capabilities
- Code quality validated by Claude Code Reviewer Skill
- Issues: GitHub Issues
- Discussions: GitHub Discussions
β Star this repo if you find it helpful!
Made with β€οΈ by [Your Name]