An AI-powered automated trading bot with a professional Tkinter GUI dashboard, Alpaca brokerage integration, and OpenAI GPT portfolio analysis.
- Automated DCA Strategy: Implements a Dollar Cost Averaging (DCA) laddering system with Martingale position sizing.
- AI Portfolio Manager: Chat with a GPT-4 powered assistant that analyzes your real-time risk, exposure, and PnL.
- Real-time Dashboard: Monitor positions, buying power, and total unrealized PnL in a dark-themed trading terminal.
- Persistent Configuration: Manage your target equities through a local JSON persistence system.
- Background Engine: Multi-threaded architecture ensures the UI remains responsive while the bot monitors markets.
ai_trading_bot/
│
├── bot.py # Main entry point
├── equities.json # Persistent equity settings
├── .env # API Keys (Git ignored)
├── requirements.txt # Dependencies
│
├── core/ # Business Logic
│ ├── alpaca_client.py # Alpaca API wrapper
│ ├── trading_engine.py # DCA/Martingale logic
│ ├── order_manager.py # Order tracking
│ ├── portfolio_manager.py# Data aggregation
│ ├── ai_manager.py # OpenAI integration
│ ├── persistence.py # JSON handler
│ └── refresh_engine.py # Threading logic
│
├── gui/ # UI Components
│ ├── main_window.py # Terminal assembly
│ ├── portfolio_frame.py # Stats & Table
│ ├── chat_frame.py # AI interaction
│ ├── controls_frame.py # CRUD operations
│ └── logs_frame.py # System logs
│
└── notebooks/ # API Testing
├── alpaca.ipynb
└── openai.ipynb
-
Clone the repository:
git clone <repo-url> cd ai_trading_bot
-
Install Dependencies:
pip install -r requirements.txt
-
Configure API Keys: Create a
.envfile in the root directory (template provided):ALPACA_API_KEY=your_key ALPACA_SECRET_KEY=your_secret OPENAI_API_KEY=your_openai_key BASE_URL=https://paper-api.alpaca.markets -
Run the Bot:
python bot.py
When a symbol is added and enabled:
- The bot checks for existing positions.
- If none, it places an initial market order.
- It then generates a "ladder" of limit buy orders at specific drawdown percentages (e.g., 2%, 4%, 6% below entry).
- Each level increases the order size (Martingale) to lower the average cost basis.
This bot is for educational purposes only. Automated trading involves significant risk of loss. Always use Paper Trading (Sandbox) environments before considering live capital.
Architecture based on the trading systems tutorial by Roman Paolucci.