Edwin Salguero
commited on
Commit
Β·
1e265d1
1
Parent(s):
6b4e60d
docs: update README with comprehensive documentation and Apache 2.0 license
Browse files
README.md
CHANGED
@@ -10,47 +10,603 @@ pinned: false
|
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
-
# Algorithmic Trading System
|
14 |
|
15 |
-
A
|
16 |
|
17 |
-
## Features
|
18 |
|
19 |
-
|
20 |
-
- π **Real-time Data**: Market data ingestion and processing
|
21 |
-
- π§ **FinRL Integration**: Deep reinforcement learning for trading strategies
|
22 |
-
- π― **Multiple UIs**: Streamlit, Dash, Jupyter, and WebSocket interfaces
|
23 |
-
- π **Backtesting**: Comprehensive backtesting and performance analysis
|
24 |
-
- π **Risk Management**: Built-in risk controls and monitoring
|
25 |
-
- π **Deployment Ready**: Docker and cloud deployment support
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
```bash
|
30 |
-
|
31 |
-
git clone https://github.com/EAName/algorithmic_trading.git
|
32 |
cd algorithmic_trading
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
pip install -r requirements.txt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
```
|
40 |
|
41 |
-
## Architecture
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
- **Data Pipeline**: Real-time market data ingestion and processing
|
45 |
-
- **UI Layer**: Multiple interface options for different use cases
|
46 |
-
- **Deployment**: Docker and cloud-ready configuration
|
47 |
|
48 |
-
##
|
49 |
|
50 |
-
-
|
51 |
-
-
|
52 |
-
-
|
|
|
53 |
|
54 |
-
##
|
55 |
|
56 |
-
|
|
|
|
|
|
|
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
+
# Algorithmic Trading System with FinRL and Alpaca Integration
|
14 |
|
15 |
+
A sophisticated algorithmic trading system that combines reinforcement learning (FinRL) with real-time market data and order execution through Alpaca Markets. This system supports both paper trading and live trading with advanced risk management and technical analysis.
|
16 |
|
17 |
+
## π Features
|
18 |
|
19 |
+
### Core Trading System
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
- **Multi-source Data Ingestion**: CSV files, Alpaca Markets API, and synthetic data generation
|
22 |
+
- **Technical Analysis**: 20+ technical indicators including RSI, MACD, Bollinger Bands, and more
|
23 |
+
- **Risk Management**: Position sizing, drawdown limits, and portfolio protection
|
24 |
+
- **Real-time Execution**: Live order placement and portfolio monitoring
|
25 |
+
|
26 |
+
### FinRL Reinforcement Learning
|
27 |
+
|
28 |
+
- **Multiple Algorithms**: PPO, A2C, DDPG, and TD3 support
|
29 |
+
- **Custom Trading Environment**: Gymnasium-compatible environment for RL training
|
30 |
+
- **Real-time Integration**: Can execute real trades during training and inference
|
31 |
+
- **Model Persistence**: Save and load trained models for consistent performance
|
32 |
+
|
33 |
+
### Alpaca Broker Integration
|
34 |
+
|
35 |
+
- **Paper Trading**: Risk-free testing with virtual money
|
36 |
+
- **Live Trading**: Real market execution (use with caution!)
|
37 |
+
- **Market Data**: Real-time and historical data from Alpaca
|
38 |
+
- **Account Management**: Portfolio monitoring and position tracking
|
39 |
+
- **Order Types**: Market orders, limit orders, and order cancellation
|
40 |
+
|
41 |
+
### π¨ Comprehensive UI System
|
42 |
+
|
43 |
+
- **Streamlit UI**: Quick prototyping and data science workflows
|
44 |
+
- **Dash UI**: Enterprise-grade interactive dashboards
|
45 |
+
- **Jupyter UI**: Interactive notebook-based interfaces
|
46 |
+
- **WebSocket API**: Real-time trading data streaming
|
47 |
+
- **Multi-interface Support**: Choose the right UI for your needs
|
48 |
+
|
49 |
+
### Advanced Features
|
50 |
+
|
51 |
+
- **Docker Support**: Containerized deployment for consistency
|
52 |
+
- **Comprehensive Logging**: Detailed logs for debugging and performance analysis
|
53 |
+
- **Backtesting Engine**: Historical performance evaluation
|
54 |
+
- **Live Trading Simulation**: Real-time trading with configurable duration
|
55 |
+
- **Performance Metrics**: Returns, Sharpe ratio, drawdown analysis
|
56 |
+
|
57 |
+
## π Prerequisites
|
58 |
+
|
59 |
+
- Python 3.8+
|
60 |
+
- Alpaca Markets account (free paper trading available)
|
61 |
+
- Docker (optional, for containerized deployment)
|
62 |
+
|
63 |
+
## π οΈ Installation
|
64 |
+
|
65 |
+
### 1. Clone the Repository
|
66 |
|
67 |
```bash
|
68 |
+
git clone https://github.com/ParallelLLC/algorithmic_trading.git
|
|
|
69 |
cd algorithmic_trading
|
70 |
+
```
|
71 |
+
|
72 |
+
### 2. Install Dependencies
|
73 |
|
74 |
+
```bash
|
75 |
pip install -r requirements.txt
|
76 |
+
```
|
77 |
+
|
78 |
+
### 3. Set Up Alpaca API Credentials
|
79 |
+
|
80 |
+
Create a `.env` file in the project root:
|
81 |
+
|
82 |
+
```bash
|
83 |
+
cp env.example .env
|
84 |
+
```
|
85 |
+
|
86 |
+
Edit `.env` with your Alpaca credentials:
|
87 |
+
|
88 |
+
```env
|
89 |
+
# Get these from https://app.alpaca.markets/paper/dashboard/overview
|
90 |
+
ALPACA_API_KEY=your_paper_api_key_here
|
91 |
+
ALPACA_SECRET_KEY=your_paper_secret_key_here
|
92 |
+
|
93 |
+
# For live trading (use with caution!)
|
94 |
+
# ALPACA_API_KEY=your_live_api_key_here
|
95 |
+
# ALPACA_SECRET_KEY=your_live_secret_key_here
|
96 |
+
```
|
97 |
+
|
98 |
+
### 4. Configure Trading Parameters
|
99 |
+
|
100 |
+
Edit `config.yaml` to customize your trading strategy:
|
101 |
+
|
102 |
+
```yaml
|
103 |
+
# Data source configuration
|
104 |
+
data_source:
|
105 |
+
type: 'alpaca' # Options: 'alpaca', 'csv', 'synthetic'
|
106 |
+
|
107 |
+
# Trading parameters
|
108 |
+
trading:
|
109 |
+
symbol: 'AAPL'
|
110 |
+
timeframe: '1m'
|
111 |
+
capital: 100000
|
112 |
+
|
113 |
+
# Risk management
|
114 |
+
risk:
|
115 |
+
max_position: 100
|
116 |
+
max_drawdown: 0.05
|
117 |
+
|
118 |
+
# Execution settings
|
119 |
+
execution:
|
120 |
+
broker_api: 'alpaca_paper' # Options: 'paper', 'alpaca_paper', 'alpaca_live'
|
121 |
+
order_size: 10
|
122 |
+
|
123 |
+
# FinRL configuration
|
124 |
+
finrl:
|
125 |
+
algorithm: 'PPO'
|
126 |
+
learning_rate: 0.0003
|
127 |
+
training:
|
128 |
+
total_timesteps: 100000
|
129 |
+
save_best_model: true
|
130 |
+
```
|
131 |
+
|
132 |
+
## π Quick Start
|
133 |
+
|
134 |
+
### 1. Launch the UI (Recommended)
|
135 |
+
|
136 |
+
```bash
|
137 |
+
# Launch Streamlit UI (best for beginners)
|
138 |
+
python ui_launcher.py streamlit
|
139 |
+
|
140 |
+
# Launch Dash UI (best for production)
|
141 |
+
python ui_launcher.py dash
|
142 |
+
|
143 |
+
# Launch Jupyter Lab
|
144 |
+
python ui_launcher.py jupyter
|
145 |
+
|
146 |
+
# Launch all UIs
|
147 |
+
python ui_launcher.py all
|
148 |
+
```
|
149 |
+
|
150 |
+
### 2. Run the Demo
|
151 |
+
|
152 |
+
```bash
|
153 |
+
python demo.py
|
154 |
+
```
|
155 |
+
|
156 |
+
This will:
|
157 |
+
- Test data ingestion from Alpaca
|
158 |
+
- Demonstrate FinRL training
|
159 |
+
- Show trading workflow execution
|
160 |
+
- Run backtesting on historical data
|
161 |
+
|
162 |
+
### 3. Start Paper Trading
|
163 |
+
|
164 |
+
```bash
|
165 |
+
python -m agentic_ai_system.main --mode live --duration 60
|
166 |
+
```
|
167 |
+
|
168 |
+
### 4. Run Backtesting
|
169 |
+
|
170 |
+
```bash
|
171 |
+
python -m agentic_ai_system.main --mode backtest --start-date 2024-01-01 --end-date 2024-01-31
|
172 |
+
```
|
173 |
+
|
174 |
+
## π Usage Examples
|
175 |
+
|
176 |
+
### Basic Trading Workflow
|
177 |
+
|
178 |
+
```python
|
179 |
+
from agentic_ai_system.main import load_config
|
180 |
+
from agentic_ai_system.orchestrator import run
|
181 |
+
|
182 |
+
# Load configuration
|
183 |
+
config = load_config()
|
184 |
+
|
185 |
+
# Run single trading cycle
|
186 |
+
result = run(config)
|
187 |
+
print(f"Trading result: {result}")
|
188 |
+
```
|
189 |
+
|
190 |
+
### FinRL Training
|
191 |
+
|
192 |
+
```python
|
193 |
+
from agentic_ai_system.finrl_agent import FinRLAgent, FinRLConfig
|
194 |
+
from agentic_ai_system.data_ingestion import load_data
|
195 |
+
|
196 |
+
# Load data and configuration
|
197 |
+
config = load_config()
|
198 |
+
data = load_data(config)
|
199 |
+
|
200 |
+
# Initialize FinRL agent
|
201 |
+
finrl_config = FinRLConfig(algorithm='PPO', learning_rate=0.0003)
|
202 |
+
agent = FinRLAgent(finrl_config)
|
203 |
+
|
204 |
+
# Train the agent
|
205 |
+
result = agent.train(
|
206 |
+
data=data,
|
207 |
+
config=config,
|
208 |
+
total_timesteps=100000,
|
209 |
+
use_real_broker=False # Use simulation for training
|
210 |
+
)
|
211 |
+
|
212 |
+
print(f"Training completed: {result}")
|
213 |
+
```
|
214 |
+
|
215 |
+
### Alpaca Integration
|
216 |
+
|
217 |
+
```python
|
218 |
+
from agentic_ai_system.alpaca_broker import AlpacaBroker
|
219 |
+
|
220 |
+
# Initialize Alpaca broker
|
221 |
+
config = load_config()
|
222 |
+
broker = AlpacaBroker(config)
|
223 |
+
|
224 |
+
# Get account information
|
225 |
+
account_info = broker.get_account_info()
|
226 |
+
print(f"Account balance: ${account_info['buying_power']:,.2f}")
|
227 |
+
|
228 |
+
# Place a market order
|
229 |
+
result = broker.place_market_order(
|
230 |
+
symbol='AAPL',
|
231 |
+
quantity=10,
|
232 |
+
side='buy'
|
233 |
+
)
|
234 |
+
print(f"Order result: {result}")
|
235 |
+
```
|
236 |
+
|
237 |
+
### Real-time Trading with FinRL
|
238 |
+
|
239 |
+
```python
|
240 |
+
from agentic_ai_system.finrl_agent import FinRLAgent
|
241 |
+
|
242 |
+
# Load trained model
|
243 |
+
agent = FinRLAgent(FinRLConfig())
|
244 |
+
agent.model = agent._load_model('models/finrl_best/best_model', config)
|
245 |
+
|
246 |
+
# Make predictions with real execution
|
247 |
+
result = agent.predict(
|
248 |
+
data=recent_data,
|
249 |
+
config=config,
|
250 |
+
use_real_broker=True # Execute real trades!
|
251 |
+
)
|
252 |
+
```
|
253 |
+
|
254 |
+
## ποΈ Architecture
|
255 |
+
|
256 |
+
### System Components
|
257 |
+
|
258 |
+
```
|
259 |
+
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
|
260 |
+
β Data Sources β β Strategy Agent β β Execution Agent β
|
261 |
+
β β β β β β
|
262 |
+
β β’ Alpaca API βββββΆβ β’ Technical βββββΆβ β’ Alpaca Broker β
|
263 |
+
β β’ CSV Files β β Indicators β β β’ Order Mgmt β
|
264 |
+
β β’ Synthetic β β β’ Signal Gen β β β’ Risk Control β
|
265 |
+
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
|
266 |
+
β β β
|
267 |
+
βΌ βΌ βΌ
|
268 |
+
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
|
269 |
+
β Data Ingestion β β FinRL Agent β β Portfolio β
|
270 |
+
β β β β β Management β
|
271 |
+
β β’ Validation β β β’ PPO/A2C/DDPG β β β’ Positions β
|
272 |
+
β β’ Indicators β β β’ Training β β β’ P&L Tracking β
|
273 |
+
β β’ Preprocessing β β β’ Prediction β β β’ Risk Metrics β
|
274 |
+
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
|
275 |
+
```
|
276 |
+
|
277 |
+
### Data Flow
|
278 |
|
279 |
+
1. **Data Ingestion**: Market data from Alpaca, CSV, or synthetic sources
|
280 |
+
2. **Preprocessing**: Technical indicators, data validation, and feature engineering
|
281 |
+
3. **Strategy Generation**: Traditional technical analysis or FinRL predictions
|
282 |
+
4. **Risk Management**: Position sizing and portfolio protection
|
283 |
+
5. **Order Execution**: Real-time order placement through Alpaca
|
284 |
+
6. **Performance Tracking**: Continuous monitoring and logging
|
285 |
+
|
286 |
+
## π Project Directory Structure
|
287 |
+
|
288 |
+
```
|
289 |
+
algorithmic_trading/
|
290 |
+
βββ π README.md # Project documentation
|
291 |
+
βββ π LICENSE # Apache License 2.0
|
292 |
+
βββ π requirements.txt # Python dependencies
|
293 |
+
βββ π config.yaml # Main configuration file
|
294 |
+
βββ π env.example # Environment variables template
|
295 |
+
βββ π .gitignore # Git ignore rules
|
296 |
+
βββ π pytest.ini # Test configuration
|
297 |
+
β
|
298 |
+
βββ π³ Docker/
|
299 |
+
β βββ π Dockerfile # Container definition
|
300 |
+
β βββ π docker-entrypoint.sh # Container startup script
|
301 |
+
β βββ π .dockerignore # Docker ignore rules
|
302 |
+
β βββ π docker-compose.yml # Default compose file
|
303 |
+
β βββ π docker-compose.dev.yml # Development environment
|
304 |
+
β βββ π docker-compose.prod.yml # Production environment
|
305 |
+
β βββ π docker-compose.hub.yml # Docker Hub deployment
|
306 |
+
β βββ π docker-compose.prod.yml # Production environment
|
307 |
+
β
|
308 |
+
βββ π€ agentic_ai_system/ # Core AI trading system
|
309 |
+
β βββ π main.py # Main entry point
|
310 |
+
β βββ π orchestrator.py # System coordination
|
311 |
+
β βββ π agent_base.py # Base agent class
|
312 |
+
β βββ π data_ingestion.py # Market data processing
|
313 |
+
β βββ π strategy_agent.py # Trading strategy logic
|
314 |
+
β βββ π execution_agent.py # Order execution
|
315 |
+
β βββ π finrl_agent.py # FinRL reinforcement learning
|
316 |
+
β βββ π alpaca_broker.py # Alpaca API integration
|
317 |
+
β βββ π synthetic_data_generator.py # Test data generation
|
318 |
+
β βββ π logger_config.py # Logging configuration
|
319 |
+
β
|
320 |
+
βββ π¨ ui/ # User interface system
|
321 |
+
β βββ π __init__.py # UI package initialization
|
322 |
+
β βββ π streamlit_app.py # Streamlit web application
|
323 |
+
β βββ π dash_app.py # Dash enterprise dashboard
|
324 |
+
β βββ π jupyter_widgets.py # Jupyter interactive widgets
|
325 |
+
β βββ π websocket_server.py # Real-time WebSocket server
|
326 |
+
β
|
327 |
+
βββ π§ͺ tests/ # Test suite
|
328 |
+
β βββ π __init__.py
|
329 |
+
β βββ π test_data_ingestion.py
|
330 |
+
β βββ π test_strategy_agent.py
|
331 |
+
β βββ π test_execution_agent.py
|
332 |
+
β βββ π test_finrl_agent.py
|
333 |
+
β βββ π test_synthetic_data_generator.py
|
334 |
+
β βββ π test_integration.py
|
335 |
+
β
|
336 |
+
βββ π data/ # Market data storage
|
337 |
+
β βββ π synthetic_market_data.csv
|
338 |
+
β
|
339 |
+
βββ π§ models/ # Trained AI models
|
340 |
+
β βββ π finrl_best/ # Best FinRL models
|
341 |
+
β
|
342 |
+
βββ π plots/ # Generated charts/visualizations
|
343 |
+
β
|
344 |
+
βββ π logs/ # System logs
|
345 |
+
β βββ π trading_system.log
|
346 |
+
β βββ π trading.log
|
347 |
+
β βββ π performance.log
|
348 |
+
β βββ π errors.log
|
349 |
+
β βββ π finrl_tensorboard/ # FinRL training logs
|
350 |
+
β βββ π finrl_eval/ # Model evaluation logs
|
351 |
+
β
|
352 |
+
βββ π§ scripts/ # Utility scripts
|
353 |
+
β βββ π docker-build.sh # Docker build automation
|
354 |
+
β βββ π docker-hub-deploy.sh # Docker Hub deployment
|
355 |
+
β
|
356 |
+
βββ π demo.py # Main demo script
|
357 |
+
βββ π finrl_demo.py # FinRL-specific demo
|
358 |
+
βββ π ui_launcher.py # UI launcher script
|
359 |
+
βββ π UI_SETUP.md # UI setup documentation
|
360 |
+
βββ π DOCKER_HUB_SETUP.md # Docker Hub documentation
|
361 |
+
β
|
362 |
+
βββ π .venv/ # Python virtual environment
|
363 |
```
|
364 |
|
365 |
+
## ποΈ Architecture Overview
|
366 |
+
|
367 |
+
### Core Components:
|
368 |
+
|
369 |
+
- **Data Layer**: Market data ingestion and preprocessing
|
370 |
+
- **Strategy Layer**: Technical analysis and signal generation
|
371 |
+
- **AI Layer**: FinRL reinforcement learning agents
|
372 |
+
- **Execution Layer**: Order management and broker integration
|
373 |
+
- **Orchestration**: System coordination and workflow management
|
374 |
+
|
375 |
+
### Key Features:
|
376 |
+
|
377 |
+
- **Modular Design**: Each component is independent and testable
|
378 |
+
- **Docker Support**: Complete containerization for deployment
|
379 |
+
- **Testing**: Comprehensive test suite for all components
|
380 |
+
- **Logging**: Detailed logging for monitoring and debugging
|
381 |
+
- **Configuration**: Centralized configuration management
|
382 |
+
- **Documentation**: Extensive documentation and examples
|
383 |
+
|
384 |
+
### Development Workflow:
|
385 |
+
|
386 |
+
1. **Data Ingestion** β Market data from Alpaca/CSV/synthetic sources
|
387 |
+
2. **Strategy Generation** β Technical indicators and FinRL predictions
|
388 |
+
3. **Risk Management** β Position sizing and portfolio protection
|
389 |
+
4. **Order Execution** β Real-time trading through Alpaca
|
390 |
+
5. **Performance Tracking** β Continuous monitoring and logging
|
391 |
+
|
392 |
+
## π§ Configuration
|
393 |
+
|
394 |
+
### Alpaca Settings
|
395 |
+
|
396 |
+
```yaml
|
397 |
+
alpaca:
|
398 |
+
api_key: '' # Set via environment variable
|
399 |
+
secret_key: '' # Set via environment variable
|
400 |
+
paper_trading: true
|
401 |
+
base_url: 'https://paper-api.alpaca.markets'
|
402 |
+
live_url: 'https://api.alpaca.markets'
|
403 |
+
data_url: 'https://data.alpaca.markets'
|
404 |
+
account_type: 'paper' # 'paper' or 'live'
|
405 |
+
```
|
406 |
+
|
407 |
+
### FinRL Settings
|
408 |
+
|
409 |
+
```yaml
|
410 |
+
finrl:
|
411 |
+
algorithm: 'PPO' # PPO, A2C, DDPG, TD3
|
412 |
+
learning_rate: 0.0003
|
413 |
+
batch_size: 64
|
414 |
+
buffer_size: 1000000
|
415 |
+
training:
|
416 |
+
total_timesteps: 100000
|
417 |
+
eval_freq: 10000
|
418 |
+
save_best_model: true
|
419 |
+
model_save_path: 'models/finrl_best/'
|
420 |
+
inference:
|
421 |
+
use_trained_model: false
|
422 |
+
model_path: 'models/finrl_best/best_model'
|
423 |
+
```
|
424 |
+
|
425 |
+
### Risk Management
|
426 |
+
|
427 |
+
```yaml
|
428 |
+
risk:
|
429 |
+
max_position: 100
|
430 |
+
max_drawdown: 0.05
|
431 |
+
stop_loss: 0.02
|
432 |
+
take_profit: 0.05
|
433 |
+
```
|
434 |
+
|
435 |
+
## π¨ User Interface System
|
436 |
+
|
437 |
+
The project includes a comprehensive UI system with multiple interface options:
|
438 |
+
|
439 |
+
### Available UIs
|
440 |
+
|
441 |
+
#### Streamlit UI (Recommended for beginners)
|
442 |
+
- **URL**: http://localhost:8501
|
443 |
+
- **Features**: Interactive widgets, real-time data visualization, easy configuration
|
444 |
+
- **Best for**: Data scientists, quick experiments, rapid prototyping
|
445 |
+
|
446 |
+
#### Dash UI (Recommended for production)
|
447 |
+
- **URL**: http://localhost:8050
|
448 |
+
- **Features**: Enterprise-grade dashboards, advanced charts, professional styling
|
449 |
+
- **Best for**: Production dashboards, real-time monitoring, complex analytics
|
450 |
+
|
451 |
+
#### Jupyter UI (For research)
|
452 |
+
- **URL**: http://localhost:8888
|
453 |
+
- **Features**: Interactive notebooks, code execution, rich documentation
|
454 |
+
- **Best for**: Research, experimentation, educational purposes
|
455 |
+
|
456 |
+
#### WebSocket API (For developers)
|
457 |
+
- **URL**: ws://localhost:8765
|
458 |
+
- **Features**: Real-time data streaming, trading signals, portfolio updates
|
459 |
+
- **Best for**: Real-time trading signals, live data streaming
|
460 |
+
|
461 |
+
### Quick UI Launch
|
462 |
+
|
463 |
+
```bash
|
464 |
+
# Launch individual UIs
|
465 |
+
python ui_launcher.py streamlit # Streamlit UI
|
466 |
+
python ui_launcher.py dash # Dash UI
|
467 |
+
python ui_launcher.py jupyter # Jupyter Lab
|
468 |
+
python ui_launcher.py websocket # WebSocket server
|
469 |
+
|
470 |
+
# Launch all UIs at once
|
471 |
+
python ui_launcher.py all
|
472 |
+
```
|
473 |
+
|
474 |
+
### UI Features
|
475 |
+
|
476 |
+
- **Real-time Data Visualization**: Live market data charts and indicators
|
477 |
+
- **Portfolio Monitoring**: Real-time portfolio value and P&L tracking
|
478 |
+
- **Trading Controls**: Start/stop trading, backtesting, risk management
|
479 |
+
- **FinRL Training**: Interactive model training and evaluation
|
480 |
+
- **Alpaca Integration**: Account management and order execution
|
481 |
+
- **Configuration Management**: Easy parameter tuning and strategy setup
|
482 |
+
|
483 |
+
For detailed UI documentation, see [UI_SETUP.md](UI_SETUP.md).
|
484 |
+
|
485 |
+
## π Performance Monitoring
|
486 |
+
|
487 |
+
### Logging
|
488 |
+
|
489 |
+
The system provides comprehensive logging:
|
490 |
+
|
491 |
+
- `logs/trading_system.log`: Main system logs
|
492 |
+
- `logs/trading.log`: Trading-specific events
|
493 |
+
- `logs/performance.log`: Performance metrics
|
494 |
+
- `logs/finrl_tensorboard/`: FinRL training logs
|
495 |
+
|
496 |
+
### Metrics Tracked
|
497 |
+
|
498 |
+
- Portfolio value and returns
|
499 |
+
- Trade execution statistics
|
500 |
+
- Risk metrics (Sharpe ratio, drawdown)
|
501 |
+
- FinRL training progress
|
502 |
+
- Alpaca account status
|
503 |
+
|
504 |
+
### Real-time Monitoring
|
505 |
+
|
506 |
+
```python
|
507 |
+
# Get account information
|
508 |
+
account_info = broker.get_account_info()
|
509 |
+
print(f"Portfolio Value: ${account_info['portfolio_value']:,.2f}")
|
510 |
+
|
511 |
+
# Get current positions
|
512 |
+
positions = broker.get_positions()
|
513 |
+
for pos in positions:
|
514 |
+
print(f"{pos['symbol']}: {pos['quantity']} shares")
|
515 |
+
|
516 |
+
# Check market status
|
517 |
+
market_open = broker.is_market_open()
|
518 |
+
print(f"Market: {'OPEN' if market_open else 'CLOSED'}")
|
519 |
+
```
|
520 |
+
|
521 |
+
## π³ Docker Deployment
|
522 |
+
|
523 |
+
### Build and Run
|
524 |
+
|
525 |
+
```bash
|
526 |
+
# Build the image
|
527 |
+
docker build -t algorithmic-trading .
|
528 |
+
|
529 |
+
# Run with environment variables
|
530 |
+
docker run -it --env-file .env algorithmic-trading
|
531 |
+
|
532 |
+
# Run with Jupyter Lab for development
|
533 |
+
docker-compose -f docker-compose.dev.yml up
|
534 |
+
```
|
535 |
+
|
536 |
+
### Production Deployment
|
537 |
+
|
538 |
+
```bash
|
539 |
+
# Use production compose file
|
540 |
+
docker-compose -f docker-compose.prod.yml up -d
|
541 |
+
|
542 |
+
# Monitor logs
|
543 |
+
docker-compose -f docker-compose.prod.yml logs -f
|
544 |
+
```
|
545 |
+
|
546 |
+
## π§ͺ Testing
|
547 |
+
|
548 |
+
### Run All Tests
|
549 |
+
|
550 |
+
```bash
|
551 |
+
pytest tests/ -v
|
552 |
+
```
|
553 |
+
|
554 |
+
### Test Specific Components
|
555 |
+
|
556 |
+
```bash
|
557 |
+
# Test Alpaca integration
|
558 |
+
pytest tests/test_alpaca_integration.py -v
|
559 |
+
|
560 |
+
# Test FinRL agent
|
561 |
+
pytest tests/test_finrl_agent.py -v
|
562 |
+
|
563 |
+
# Test trading workflow
|
564 |
+
pytest tests/test_integration.py -v
|
565 |
+
```
|
566 |
+
|
567 |
+
## β οΈ Important Notes
|
568 |
+
|
569 |
+
### Paper Trading vs Live Trading
|
570 |
+
|
571 |
+
- **Paper Trading**: Uses virtual money, safe for testing
|
572 |
+
- **Live Trading**: Uses real money, use with extreme caution
|
573 |
+
- Always test strategies thoroughly in paper trading before going live
|
574 |
+
|
575 |
+
### Risk Management
|
576 |
+
|
577 |
+
- Set appropriate position limits and drawdown thresholds
|
578 |
+
- Monitor your portfolio regularly
|
579 |
+
- Use stop-loss orders to limit potential losses
|
580 |
+
- Never risk more than you can afford to lose
|
581 |
+
|
582 |
+
### API Rate Limits
|
583 |
+
|
584 |
+
- Alpaca has rate limits on API calls
|
585 |
+
- The system includes built-in delays to respect these limits
|
586 |
+
- Monitor your API usage in the Alpaca dashboard
|
587 |
+
|
588 |
+
## π€ Contributing
|
589 |
+
|
590 |
+
1. Fork the repository
|
591 |
+
2. Create a feature branch
|
592 |
+
3. Make your changes
|
593 |
+
4. Add tests for new functionality
|
594 |
+
5. Submit a pull request
|
595 |
+
|
596 |
+
## π License
|
597 |
|
598 |
+
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.
|
|
|
|
|
|
|
599 |
|
600 |
+
## π Support
|
601 |
|
602 |
+
- **Documentation**: Check the logs and configuration files
|
603 |
+
- **Issues**: Report bugs and feature requests on GitHub
|
604 |
+
- **Alpaca Support**: Contact Alpaca for API-related issues
|
605 |
+
- **Community**: Join our Discord/Telegram for discussions
|
606 |
|
607 |
+
## π Useful Links
|
608 |
|
609 |
+
- [Alpaca Markets Documentation](https://alpaca.markets/docs/)
|
610 |
+
- [FinRL Documentation](https://finrl.readthedocs.io/)
|
611 |
+
- [Stable Baselines3 Documentation](https://stable-baselines3.readthedocs.io/)
|
612 |
+
- [Gymnasium Documentation](https://gymnasium.farama.org/)
|