An AI-powered quantitative trading platform for US stocks using Alpaca Markets API and DeepSeek LLM for intelligent trading decisions.
Quantitative Trading Platform with Hard Risk Control
An AI-powered quantitative trading platform for US stocks using Alpaca Markets API and DeepSeek LLM for intelligent trading decisions.
๐น Project Demo
Watch the demo video to see the platform in action:
๐ Features
Core Functionality
- ๐ฐ Account Management - Real-time account information, portfolio value, buying power, and equity tracking
- ๐ Position Management - View all positions with real-time P&L, entry prices, and market values
- โก Order Management - Place buy/sell orders with multiple order types (market, limit, stop, stop-limit)
- ๐ค AI Trading Decisions - DeepSeek LLM-powered analysis providing buy/sell/hold recommendations with confidence scores
- ๐ฏ Strategy Management - Create, manage, and execute trading strategies with automated monitoring
- ๐ Auto Trading - Automated trading execution based on AI decisions and strategy rules
- ๐ก๏ธ Risk Management - Stop loss and take profit monitoring with automatic alerts
- ๐ Trade Records - Complete history of all trades and trading signals
- โ๏ธ Configuration - Easy web-based configuration for API keys and trading settings
Trading Modes
- ๐ฎ Local Simulator (Default)
- ๐ Alpaca Paper Trading
- ๐ต Live Trading
๐ Requirements
- Python 3.8+ (recommended 3.12)
- Stable internet connection
- DeepSeek API Key (for AI features)
- Alpaca API Key (optional, for paper/live trading)
๐ง Installation
1. Clone the Repository
git clone https://github.com/xuan13hao/aiqtstock.git
cd aiqtstock
Edit .env file and configure your API keys:
# DeepSeek API (Required for AI features)
DEEPSEEKAPIKEY=yourdeepseekapikeyhere
DEEPSEEKBASEURL=https://api.deepseek.com/v1
Alpaca API (Optional, for paper/live trading)
ALPACA_ENABLED=false
ALPACAAPIKEY=youralpacaapikeyhere
ALPACAAPISECRET=youralpacaapisecrethere
ALPACA_PAPER=true # true for paper trading, false for live trading
2. Run the Application
# Using the run script
python run.py
Or directly with Streamlit
streamlit run app.py --server.port 8503
๐ณ Docker Deployment
Using Docker Compose (Recommended)
docker-compose up -d
Using Dockerfile
# Build the image
docker build -t alpaca-trading-platform .
Run the container
docker run -d -p 8503:8501 \
-v $(pwd)/.env:/app/.env \
--name alpaca-trading \
alpaca-trading-platform
๐ Usage Guide
Getting Started
- Configure API Keys
- View Account Information
- Place Orders
AI Trading Decisions
- Get AI Recommendation
- Auto Execute Trade
Strategy Management
- Add Strategy Task
- Auto Trading
- Stop Loss / Take Profit
Order Types
- Market Order - Execute immediately at current market price
- Limit Order - Execute only at specified price or better
- Stop Order - Trigger when price reaches stop price
- Stop-Limit Order - Combination of stop and limit orders
Time in Force Options
- Day - Order expires at end of trading day
- GTC - Good till canceled (remains active until filled or canceled)
- IOC - Immediate or cancel (fill immediately or cancel)
- FOK - Fill or kill (fill completely or cancel)
๐ Getting API Keys
DeepSeek API Key
- Visit https://platform.deepseek.com
- Register/Login to your account
- Navigate to API key management
- Create a new API key
- Copy the key and paste it into the configuration
Alpaca API Keys
- Visit https://alpaca.markets
- Sign up for a free account
- Go to Dashboard โ API Keys
- Create new API key (for paper trading)
- Copy Key ID and Secret Key
- Paste into configuration
๐ Project Structure
aiagents-stock/
โโโ app.py # Main Streamlit application
โโโ usstocktrading.py # Alpaca trading interface
โโโ alpacaaidecision.py # AI decision engine (DeepSeek)
โโโ alpacastrategymanager.py # Strategy management
โโโ alpacaautotrader.py # Auto trading service
โโโ config_manager.py # Configuration management
โโโ run.py # Application launcher
โโโ requirements.txt # Python dependencies
โโโ env_example.txt # Environment configuration template
โโโ Dockerfile # Docker image definition
โโโ docker-compose.yml # Docker Compose configuration
โโโ README.md # This file
๐ ๏ธ Technology Stack
- Frontend: Streamlit
- Trading API: Alpaca Markets API
- AI/LLM: DeepSeek API
- Data Source: Yahoo Finance (yfinance)
- Technical Analysis: TA-Lib (ta library)
- Database: SQLite
- Language: Python 3.8+
โ ๏ธ Important Notes
Risk Warning
- Stock trading involves real financial risk
- AI recommendations are for reference only, not investment advice
- Always test strategies with paper trading first
- Never invest more than you can afford to lose
- Use stop loss orders to manage risk
- Past performance does not guarantee future results
Best Practices
- Start with Simulator - Use the local simulator to learn the platform
- Test with Paper Trading - Use Alpaca paper trading to test strategies
- Start Small - Begin with small position sizes
- Monitor Positions - Regularly check your positions and account
- Understand Orders - Learn different order types before trading
- Keep Records - Review trade history to improve strategies
๐ Trading Policy
Risk Management Rules
- Maximum Position Size
- Portfolio Diversification
- Maximum Daily Loss Limit
- Maximum Drawdown Limit
Stop Loss Requirements
- Mandatory Stop Loss
- Stop Loss Placement
- Trailing Stop Loss
Position Management
- Entry Rules
- Exit Rules
- Position Monitoring
AI Decision Usage Policy
- AI Recommendations
- Auto Execution
- AI Limitations
Auto Trading Policy
- Auto Trading Activation
- Strategy Requirements
- Auto Trading Monitoring
Trading Hours Policy
- Regular Market Hours (Recommended)
- Extended Hours Trading
- Market Closures
Account Protection
- API Key Security
- Account Monitoring
- Capital Preservation
Compliance and Regulatory
- Regulatory Compliance
- Record Keeping
- Disclosure Requirements
Prohibited Practices
- Do Not:
- Restrictions:
Policy Updates
- Trading policies may be updated periodically
- Users are responsible for reviewing and understanding current policies
- Policy violations may result in account restrictions
- Contact maintainers for questions about trading policies
๐ Troubleshooting
Common Issues
- API Key Errors
.env file exists and is properly formatted
- Connection Issues
- Order Execution Failures
- AI Decision Failures
- Docker Issues
docker ps
- View logs: docker-compose logs -f
- Verify .env file is mounted correctly
- Check port 8503 is not in use
๐ License
MIT License
Commercial Use
โ ๏ธ Important: Commercial use of this project requires explicit permission from the author. If you intend to use this software for commercial purposes, please contact the author for authorization before proceeding.
For commercial licensing inquiries, please open an issue on GitHub or contact the maintainers.
๐ค Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.
๐ง Support
For questions or issues, please open an issue on GitHub or contact the maintainers.
Disclaimer: This platform is for educational and research purposes only. Stock trading involves substantial risk of loss. AI recommendations should not be considered as investment advice. Always conduct your own research and consult with financial advisors before making investment decisions.
