2026-01-30 16:30:43 -07:00
|
|
|
# Alpaca Trader
|
|
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
Advanced algorithmic trading bot built for Alpaca Markets.
|
2026-01-30 16:30:43 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
Designed for automated strategy execution, multi-layer technical analysis, and research-driven trading experimentation.
|
2026-01-30 16:30:43 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
Supports configurable strategies, risk management automation, and detailed performance logging.
|
2026-01-30 16:30:43 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
---
|
2026-01-30 16:30:43 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
## 🚀 Quick Start
|
2026-01-30 16:30:43 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
Clone and install:
|
2026-01-30 16:30:43 -07:00
|
|
|
|
|
|
|
|
```bash
|
2026-02-08 13:23:21 -07:00
|
|
|
git clone https://github.com/YOUR_REPO/alpaca-trader.git
|
|
|
|
|
cd alpaca-trader
|
2026-01-30 16:30:43 -07:00
|
|
|
pip install -r requirements.txt
|
|
|
|
|
```
|
|
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
Run:
|
2026-01-30 16:30:43 -07:00
|
|
|
|
|
|
|
|
```bash
|
2026-01-30 16:36:12 -07:00
|
|
|
python3 run.py
|
2026-01-30 16:30:43 -07:00
|
|
|
```
|
|
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
On first launch, the bot will create:
|
2026-01-30 16:30:43 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
```
|
|
|
|
|
alpaca_trader/.env
|
2026-01-30 16:30:43 -07:00
|
|
|
```
|
|
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
Add your Alpaca API keys:
|
2026-01-30 16:30:43 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
```
|
|
|
|
|
APCA_API_KEY_ID="your_key"
|
|
|
|
|
APCA_API_SECRET_KEY="your_secret"
|
|
|
|
|
APCA_API_BASE_URL="https://paper-api.alpaca.markets"
|
|
|
|
|
```
|
2026-02-04 14:33:15 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
---
|
2026-02-02 14:13:54 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
## 🎯 Features
|
|
|
|
|
|
|
|
|
|
### Core Trading Engine
|
|
|
|
|
|
|
|
|
|
- Automated signal evaluation loop
|
|
|
|
|
- Multi-strategy architecture
|
|
|
|
|
- Risk-aware position sizing
|
|
|
|
|
- Market regime detection
|
|
|
|
|
- Config-driven behavior (no code changes required)
|
2026-02-04 14:33:15 -07:00
|
|
|
|
|
|
|
|
### Technical Indicators
|
2026-02-08 13:23:21 -07:00
|
|
|
|
|
|
|
|
- SMA / EMA
|
|
|
|
|
- RSI
|
|
|
|
|
- MACD
|
|
|
|
|
- ADX
|
|
|
|
|
- ATR
|
|
|
|
|
- Bollinger Bands
|
|
|
|
|
- Multi-timeframe signal confirmation
|
|
|
|
|
|
|
|
|
|
### Strategy System
|
|
|
|
|
|
|
|
|
|
Supports multiple strategy modes:
|
|
|
|
|
|
|
|
|
|
- Moving Average crossover (default)
|
|
|
|
|
- Opening Range + Fair Value Gap (OR/FVG)
|
|
|
|
|
- Regime-filtered execution
|
|
|
|
|
|
|
|
|
|
### Risk Management
|
|
|
|
|
|
|
|
|
|
- ATR-based stop loss
|
|
|
|
|
- Trailing stop logic
|
|
|
|
|
- Multi-level take profits
|
|
|
|
|
- Risk-per-trade sizing
|
|
|
|
|
- Max drawdown protection
|
|
|
|
|
- Risk/reward validation
|
|
|
|
|
- Position hold-time limits
|
|
|
|
|
|
|
|
|
|
### Execution Controls
|
|
|
|
|
|
|
|
|
|
- Market or limit orders
|
|
|
|
|
- Slippage simulation
|
|
|
|
|
- Commission modeling
|
|
|
|
|
- Cash account compatibility
|
|
|
|
|
- T+1 settlement handling
|
|
|
|
|
- PDT rule awareness
|
|
|
|
|
|
|
|
|
|
### Market Filters
|
|
|
|
|
|
|
|
|
|
- Market regime classification
|
|
|
|
|
- Volume filters
|
|
|
|
|
- 200 SMA trend filter
|
|
|
|
|
- VIX volatility filter
|
|
|
|
|
- Candle confirmation
|
|
|
|
|
- MACD confirmation layer
|
|
|
|
|
|
|
|
|
|
### Analytics & Logging
|
|
|
|
|
|
|
|
|
|
Automatically generates:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
logs/
|
|
|
|
|
├── trading.log
|
|
|
|
|
├── debug.log
|
|
|
|
|
|
|
|
|
|
data/
|
|
|
|
|
├── trades.csv
|
|
|
|
|
├── signals.csv
|
|
|
|
|
├── performance.csv
|
|
|
|
|
├── indicators.csv
|
|
|
|
|
├── session.csv
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## 🧠 Strategy Overview
|
|
|
|
|
|
|
|
|
|
### Moving Average Strategy
|
|
|
|
|
|
|
|
|
|
Primary signal generated when:
|
|
|
|
|
|
|
|
|
|
- Short MA crosses long MA
|
|
|
|
|
- Trend filters confirm
|
|
|
|
|
- Risk/reward meets threshold
|
|
|
|
|
- Market regime supports trade
|
|
|
|
|
|
|
|
|
|
Optional confirmation:
|
|
|
|
|
|
|
|
|
|
- MACD alignment
|
|
|
|
|
- RSI thresholds
|
|
|
|
|
- Volume confirmation
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
### Opening Range + Fair Value Gap Strategy
|
|
|
|
|
|
|
|
|
|
Designed for intraday momentum:
|
|
|
|
|
|
|
|
|
|
1. Detect opening range window.
|
|
|
|
|
2. Identify Fair Value Gap structures.
|
|
|
|
|
3. Validate volume and direction.
|
|
|
|
|
4. Execute with ATR-based risk controls.
|
|
|
|
|
|
|
|
|
|
Configurable parameters:
|
|
|
|
|
|
|
|
|
|
- Opening range duration
|
|
|
|
|
- Minimum gap size
|
|
|
|
|
- Entry timeframe
|
|
|
|
|
- Risk/reward target
|
|
|
|
|
- Maximum entry window
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## ⚙️ Configuration
|
|
|
|
|
|
|
|
|
|
All trading behavior controlled via:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
alpaca_trader/config.json
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Key sections:
|
|
|
|
|
|
|
|
|
|
### Strategy
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
STRATEGY_MODE
|
|
|
|
|
OR_FVG_ENABLED
|
|
|
|
|
OR_FVG_OPENING_RANGE_MINUTES
|
|
|
|
|
OR_FVG_MIN_GAP_SIZE
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Risk
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
RISK_PER_TRADE
|
|
|
|
|
ATR_STOP_MULTIPLIER
|
|
|
|
|
MAX_DRAWDOWN
|
|
|
|
|
MIN_RISK_REWARD
|
|
|
|
|
```
|
2026-02-02 14:13:54 -07:00
|
|
|
|
|
|
|
|
### Filters
|
2026-02-04 14:33:15 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
```
|
|
|
|
|
REGIME_DETECTION
|
|
|
|
|
USE_200_SMA_FILTER
|
|
|
|
|
USE_VIX_FILTER
|
|
|
|
|
MULTIFRAME_FILTER
|
|
|
|
|
```
|
2026-02-04 14:33:15 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
### Execution
|
2026-02-04 14:33:15 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
```
|
|
|
|
|
USE_LIMIT_ORDERS
|
|
|
|
|
LIMIT_ORDER_TIMEOUT
|
|
|
|
|
SLIPPAGE_PCT
|
|
|
|
|
COMMISSION_PCT
|
|
|
|
|
```
|
2026-01-30 16:30:43 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
---
|
2026-01-30 16:30:43 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
## 🏗 Architecture
|
2026-02-04 14:33:15 -07:00
|
|
|
|
|
|
|
|
```
|
|
|
|
|
alpaca_trader/
|
2026-02-08 13:23:21 -07:00
|
|
|
├── api.py # Alpaca API interface
|
|
|
|
|
├── engine.py # Core trading loop
|
|
|
|
|
├── indicators.py # Technical analysis
|
|
|
|
|
├── filters.py # Market condition filters
|
|
|
|
|
├── risk.py # Risk & position sizing
|
|
|
|
|
├── utils.py # Helpers
|
|
|
|
|
├── cli.py # CLI interface
|
|
|
|
|
├── config.json # Main configuration
|
2026-02-04 14:33:15 -07:00
|
|
|
```
|
2026-01-30 16:30:43 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
---
|
2026-01-30 16:30:43 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
## 🔄 How It Works
|
2026-02-02 14:13:54 -07:00
|
|
|
|
2026-02-08 13:23:21 -07:00
|
|
|
1. Load configuration and API credentials
|
|
|
|
|
2. Fetch historical market data
|
|
|
|
|
3. Calculate indicators
|
|
|
|
|
4. Evaluate market regime
|
|
|
|
|
5. Generate trading signals
|
|
|
|
|
6. Validate risk constraints
|
|
|
|
|
7. Execute trades via Alpaca API
|
|
|
|
|
8. Log analytics data
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## 📊 Design Philosophy
|
|
|
|
|
|
|
|
|
|
- Config-first architecture
|
|
|
|
|
- Strategy isolation
|
|
|
|
|
- Risk before execution
|
|
|
|
|
- Modular extensibility
|
|
|
|
|
- Research-friendly logging
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## ⚠️ Important Notes
|
|
|
|
|
|
|
|
|
|
- Use paper trading first.
|
|
|
|
|
- Algorithmic trading involves financial risk.
|
|
|
|
|
- No strategy guarantees profit.
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## 🛠 Roadmap (Example)
|
|
|
|
|
|
|
|
|
|
- Strategy plug-in system
|
|
|
|
|
- ML signal scoring
|
|
|
|
|
- Portfolio-level risk controls
|
|
|
|
|
- Multi-symbol scanning
|
|
|
|
|
- Performance dashboard
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Disclaimer
|
|
|
|
|
|
|
|
|
|
This software is provided for educational and research purposes only.
|
|
|
|
|
|
|
|
|
|
Not financial advice.
|