File size: 10,754 Bytes
9f44dc9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# UI Integration Guide
This guide covers the comprehensive UI system for the Algorithmic Trading project, providing multiple interface options for different use cases.
## π― UI Options Overview
### 1. **Streamlit UI** - Quick Prototyping
- **Best for**: Data scientists, quick experiments, rapid prototyping
- **Features**: Interactive widgets, real-time data visualization, easy configuration
- **Port**: 8501
- **URL**: http://localhost:8501
### 2. **Dash UI** - Enterprise Dashboards
- **Best for**: Production dashboards, real-time monitoring, complex analytics
- **Features**: Advanced charts, real-time updates, professional styling
- **Port**: 8050
- **URL**: http://localhost:8050
### 3. **Jupyter UI** - Interactive Notebooks
- **Best for**: Research, experimentation, educational purposes
- **Features**: Interactive widgets, code execution, rich documentation
- **Port**: 8888
- **URL**: http://localhost:8888
### 4. **WebSocket Server** - Real-time Data
- **Best for**: Real-time trading signals, live data streaming
- **Features**: WebSocket API, real-time updates, trading signals
- **Port**: 8765
- **URL**: ws://localhost:8765
## π Quick Start
### Prerequisites
```bash
# Install UI dependencies
pip install -r requirements.txt
# Verify installation
python -c "import streamlit, dash, plotly, ipywidgets; print('β
All UI dependencies installed')"
```
### Launch Individual UIs
#### Streamlit (Recommended for beginners)
```bash
python ui_launcher.py streamlit
```
#### Dash (Recommended for production)
```bash
python ui_launcher.py dash
```
#### Jupyter Lab
```bash
python ui_launcher.py jupyter
```
#### WebSocket Server
```bash
python ui_launcher.py websocket
```
#### Launch All UIs
```bash
python ui_launcher.py all
```
## π Streamlit UI Features
### Dashboard
- **System Status**: Real-time trading status, portfolio value, P&L
- **Configuration Management**: Load and modify trading parameters
- **Quick Actions**: One-click data loading, Alpaca connection, model training
### Data Ingestion
- **Multiple Sources**: CSV, Alpaca API, Synthetic data
- **Data Validation**: Automatic data quality checks
- **Technical Indicators**: Automatic calculation of moving averages, RSI, MACD
- **Interactive Charts**: Candlestick, line, volume charts with Plotly
### Alpaca Integration
- **Account Connection**: Secure API key management
- **Market Status**: Real-time market hours and status
- **Position Monitoring**: Current positions and portfolio value
- **Order Management**: Buy/sell order execution
### FinRL Training
- **Algorithm Selection**: PPO, A2C, DDPG, TD3
- **Hyperparameter Tuning**: Learning rate, batch size, training steps
- **Training Progress**: Real-time training metrics and progress
- **Model Evaluation**: Performance metrics and backtesting
### Trading Controls
- **Live Trading**: Start/stop live trading with Alpaca
- **Backtesting**: Historical strategy testing
- **Risk Management**: Position sizing and drawdown limits
- **Emergency Stop**: Immediate trading halt
### Portfolio Monitoring
- **Real-time Portfolio**: Live portfolio value and P&L
- **Position Analysis**: Individual position performance
- **Allocation Charts**: Portfolio allocation visualization
- **Risk Metrics**: Sharpe ratio, drawdown analysis
## π Dash UI Features
### Enterprise Dashboard
- **Professional Styling**: Bootstrap themes and responsive design
- **Real-time Updates**: Live data streaming and updates
- **Advanced Charts**: Interactive Plotly charts with zoom, pan, hover
- **Multi-page Navigation**: Tabbed interface for different functions
### Advanced Analytics
- **Technical Analysis**: Advanced charting with indicators
- **Performance Metrics**: Comprehensive trading performance analysis
- **Risk Management**: Advanced risk monitoring and alerts
- **Strategy Comparison**: Multiple strategy backtesting and comparison
### Real-time Monitoring
- **Live Trading Activity**: Real-time trade execution monitoring
- **System Alerts**: Automated alerts for important events
- **Portfolio Tracking**: Live portfolio updates and analysis
- **Market Data**: Real-time market data visualization
## π Jupyter UI Features
### Interactive Development
- **Widget-based Interface**: Interactive controls for all functions
- **Code Execution**: Direct Python code execution and experimentation
- **Data Exploration**: Interactive data analysis and visualization
- **Model Development**: Iterative model training and testing
### Research Tools
- **Notebook Integration**: Rich documentation and code examples
- **Data Analysis**: Pandas and NumPy integration
- **Visualization**: Matplotlib, Seaborn, Plotly integration
- **Experiment Tracking**: Training history and model comparison
## π WebSocket API
### Real-time Data Streaming
```javascript
// Connect to WebSocket server
const ws = new WebSocket('ws://localhost:8765');
// Listen for market data updates
ws.onmessage = function(event) {
const data = JSON.parse(event.data);
if (data.type === 'market_data') {
console.log('Price:', data.price);
console.log('Volume:', data.volume);
}
if (data.type === 'trading_signal') {
console.log('Signal:', data.signal);
}
if (data.type === 'portfolio_update') {
console.log('Portfolio:', data.account);
}
};
```
### Available Message Types
- `market_data`: Real-time price and volume data
- `trading_signal`: FinRL model trading signals
- `portfolio_update`: Account and position updates
- `trading_status`: Trading system status
- `system_alert`: System alerts and notifications
## π οΈ Configuration
### Environment Variables
```bash
# Alpaca API credentials
export ALPACA_API_KEY="your_api_key"
export ALPACA_SECRET_KEY="your_secret_key"
# UI configuration
export STREAMLIT_SERVER_PORT=8501
export DASH_SERVER_PORT=8050
export JUPYTER_PORT=8888
export WEBSOCKET_PORT=8765
```
### Configuration File
```yaml
# config.yaml
ui:
streamlit:
server_port: 8501
server_address: "0.0.0.0"
theme: "light"
dash:
server_port: 8050
server_address: "0.0.0.0"
theme: "bootstrap"
jupyter:
port: 8888
ip: "0.0.0.0"
token: ""
websocket:
host: "0.0.0.0"
port: 8765
max_connections: 100
```
## π§ Customization
### Adding Custom Charts
```python
# In ui/streamlit_app.py
def create_custom_chart(data):
fig = go.Figure()
fig.add_trace(go.Scatter(
x=data['timestamp'],
y=data['custom_indicator'],
name='Custom Indicator'
))
return fig
```
### Custom Trading Strategies
```python
# In ui/dash_app.py
def custom_strategy(data, config):
# Implement your custom strategy
signals = []
for i in range(len(data)):
if data['sma_20'][i] > data['sma_50'][i]:
signals.append('BUY')
else:
signals.append('SELL')
return signals
```
### WebSocket Custom Messages
```python
# In ui/websocket_server.py
async def broadcast_custom_message(self, message_type, data):
message = {
"type": message_type,
"timestamp": datetime.now().isoformat(),
"data": data
}
await self.broadcast(message)
```
## π Deployment
### Docker Deployment
```bash
# Build UI-enabled Docker image
docker build -t trading-ui .
# Run with UI ports exposed
docker run -p 8501:8501 -p 8050:8050 -p 8888:8888 -p 8765:8765 trading-ui
```
### Production Deployment
```bash
# Using Gunicorn for production
pip install gunicorn
# Start Dash app with Gunicorn
gunicorn -w 4 -b 0.0.0.0:8050 ui.dash_app:app
# Start Streamlit with production settings
streamlit run ui/streamlit_app.py --server.port 8501 --server.address 0.0.0.0
```
### Cloud Deployment
```bash
# Deploy to Heroku
heroku create trading-ui-app
git push heroku main
# Deploy to AWS
aws ecs create-service --cluster trading-cluster --service-name trading-ui
```
## π Troubleshooting
### Common Issues
#### Port Already in Use
```bash
# Find process using port
lsof -i :8501
# Kill process
kill -9 <PID>
# Or use different port
python ui_launcher.py streamlit --port 8502
```
#### Missing Dependencies
```bash
# Install missing packages
pip install streamlit dash plotly ipywidgets
# Or reinstall all requirements
pip install -r requirements.txt
```
#### Alpaca Connection Issues
```bash
# Check API credentials
echo $ALPACA_API_KEY
echo $ALPACA_SECRET_KEY
# Test connection
python -c "from agentic_ai_system.alpaca_broker import AlpacaBroker; print('Connection test')"
```
### Debug Mode
```bash
# Enable debug logging
export LOG_LEVEL=DEBUG
# Run with debug output
python ui_launcher.py streamlit --debug
```
## π API Reference
### Streamlit Functions
- `create_streamlit_app()`: Create Streamlit application
- `TradingUI.run()`: Run the main UI application
- `load_configuration()`: Load trading configuration
- `display_system_status()`: Show system status
### Dash Functions
- `create_dash_app()`: Create Dash application
- `TradingDashApp.setup_layout()`: Setup dashboard layout
- `TradingDashApp.setup_callbacks()`: Setup interactive callbacks
### Jupyter Functions
- `create_jupyter_interface()`: Create Jupyter interface
- `TradingJupyterUI.display_interface()`: Display interactive widgets
- `TradingJupyterUI.update_chart()`: Update chart displays
### WebSocket Functions
- `create_websocket_server()`: Create WebSocket server
- `TradingWebSocketServer.broadcast()`: Broadcast messages
- `TradingWebSocketServer.handle_client_message()`: Handle client messages
## π€ Contributing
### Adding New UI Features
1. Create feature branch: `git checkout -b feature/new-ui-feature`
2. Implement feature in appropriate UI module
3. Add tests in `tests/ui/` directory
4. Update documentation
5. Submit pull request
### UI Development Guidelines
- Follow PEP 8 style guidelines
- Add type hints for all functions
- Include docstrings for all classes and methods
- Write unit tests for new features
- Update documentation for new features
## π Support
For UI-related issues:
1. Check the troubleshooting section
2. Review the logs in `logs/ui/` directory
3. Create an issue on GitHub with detailed error information
4. Include system information and error logs
## π Updates
### UI Version History
- **v1.0.0**: Initial UI implementation with Streamlit, Dash, Jupyter, and WebSocket
- **v1.1.0**: Added real-time data streaming and advanced charts
- **v1.2.0**: Enhanced portfolio monitoring and risk management
- **v1.3.0**: Added custom strategy development tools
### Upcoming Features
- **v1.4.0**: Machine learning model visualization
- **v1.5.0**: Advanced backtesting interface
- **v1.6.0**: Multi-asset portfolio management
- **v1.7.0**: Social trading features |