Extract timestamp generation to shared helper, fix attention gating from broken softmax to sigmoid per-feature gates, and detach LSTM hidden state to prevent gradient accumulation. Backtester now uses real prices, proper position sizing, and sell logic instead of placeholder buys. Alpha calculation uses consistent geometric annualization. Add optional mark-to-market pricing to paper broker and close-position safety in real- time trader. Add sqlite3 import and fix pandas view warning in live data.
StockGNN R9700 — AMD-Optimized Graph Neural Network Trading System
A real-time intraday stock trading system built around a Graph Neural Network (GNN) with Temporal Attention, optimized for the AMD Radeon R9700 AI Pro GPU (32GB). The system ingests alternative data (news sentiment, social media), price bars, and corporate actions to generate trade signals across a universe of stocks.
📋 Table of Contents
- Overview
- Architecture
- Key Features
- Project Structure
- Quick Start
- Configuration
- Training
- Live Trading
- Web Dashboard
- API Reference
- AMD Optimizations
- Memory Management
- Data Pipeline
- Trading System
- Benchmarking
- Development
- Troubleshooting
- License
Overview
StockGNN uses a Graph Attention Network (GAT) with:
- Temporal Attention over multi-source feature sequences (price, news, social media)
- Corporate Action Awareness for splits, dividends, and delistings
- Intraday GNN variant with stateful LSTM for real-time inference
- Online Learning to adapt to regime changes during market hours
- AMD-specific ROCm optimizations (bf16 mixed precision, gradient checkpointing, pinned memory)
Architecture
┌─────────────────────────────────────────────────────────────────┐
│ Data Sources │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │
│ │ Polygon │ │ yfinance │ │ NewsAPI │ │ Twitter/ │ │
│ │ WebSocket│ │ Historical│ │ Sentiment│ │ Reddit │ │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ └──────┬───────┘ │
└───────┼────────────┼─────────────┼───────────────┼─────────────┘
│ │ │ │
▼ ▼ ▼ ▼
┌─────────────────────────────────────────────────────────────────┐
│ Data Pipeline │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │
│ │ Price │ │ Corporate│ │ Sector │ │ Alternative │ │
│ │ Data │ │ Actions │ │ Data │ │ Data │ │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ └──────┬───────┘ │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ ┌───────────────────────────────────────────────────────┐ │
│ │ SQLite Database (WAL mode) │ │
│ │ price_data | corporate_actions | features | trades │ │
│ └────────────────────────┬──────────────────────────────┘ │
└─────────────────────────┼──────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ GNN Models │
│ ┌────────────────────┐ ┌────────────────────┐ │
│ │ CorporateAction- │ │ IntradayGNN │ │
│ │ AwareGNN │ │ (stateful LSTM) │ │
│ │ │ │ │ │
│ │ TemporalAttention │ │ TemporalAttention │ │
│ │ GAT Conv Layers │ │ GAT Conv Layers │ │
│ │ LSTM │ │ LSTM │ │
│ │ Alternative Data │ │ Real-time Features │ │
│ │ Fusion │ │ │ │
│ └────────────────────┘ └────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ Trading Execution │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Paper Broker │ │ IB Broker │ │ Risk Manager │ │
│ │ (Simulation) │ │ (Interactive │ │ (Position │ │
│ │ │ │ Brokers) │ │ Sizing) │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ Web Dashboard │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Metrics │ │ Charts │ │ Trading │ │ Logs │ │
│ │ (Live) │ │ (Memory, │ │ Controls │ │ (System) │ │
│ │ │ │ Portfolio)│ │ │ │ │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
└─────────────────────────────────────────────────────────────────┘
Key Features
| Feature | Description |
|---|---|
| AMD Optimizations | ROCm bf16 mixed precision, gradient checkpointing, Flash Attention, memory-aware batching |
| GNN Architecture | Graph Attention Networks with temporal sequence processing and corporate action awareness |
| Live Data | Polygon.io WebSocket for real-time trades/quotes with automatic reconnection |
| Alternative Data | News sentiment (FinBERT) and social media (Twitter/Reddit) integrated into node features |
| Online Learning | Periodic model updates during market hours to adapt to regime shifts |
| Risk Management | Max drawdown, daily loss limits, volatility-target position sizing |
| Web Dashboard | Single-page application with real-time WebSocket metrics and Chart.js visualizations |
| Backtesting | Full backtesting framework with benchmark comparison and performance metrics |
Project Structure
stock_gnn_r9700/
├── config.py # Central configuration
├── requirements.txt # Python dependencies
├── main.py # Main training & backtesting script
├── live_trading.py # Live intraday trading system
├── benchmark.py # Performance benchmarking
├── README.md # This file
│
├── data/
│ ├── raw/ # Raw downloaded data
│ ├── processed/ # Processed datasets
│ └── external/ # External datasets (delisted stocks)
│
├── models/ # Saved model weights
│
├── src/
│ ├── amd/
│ │ └── optimizations.py # AMDOptimizer, AMDSparseAttention, AMDGATConv
│ ├── data/
│ │ ├── pipeline.py # StockDataPipeline (SQLite, yfinance, features)
│ │ ├── live_data.py # LiveDataService (Polygon WebSocket)
│ │ ├── news_processor.py # News data processor
│ │ ├── social_processor.py # Social media processor
│ │ └── sentiment.py # Sentiment analyzer
│ ├── models/
│ │ ├── gnn_model.py # CorporateActionAwareGNN + TemporalAttention
│ │ ├── intraday_gnn.py # IntradayGNN (stateful LSTM variant)
│ │ └── trainer.py # GNNTrainer with AMP & gradient clipping
│ ├── trading/
│ │ ├── broker.py # Abstract broker interface
│ │ ├── paper_broker.py # Paper trading simulation
│ │ ├── ib_broker.py # Interactive Brokers implementation
│ │ └── real_time_trader.py # Real-time trading execution
│ ├── evaluation/
│ │ ├── backtester.py # GNNBacktester
│ │ ├── intraday_backtester.py
│ │ └── metrics.py # Sharpe, Sortino, Calmar, IR, etc.
│ ├── utils/
│ │ ├── memory_manager.py # MemoryManager (AMD GPU memory tracking)
│ │ ├── helpers.py # generate_intraday_timestamps
│ │ ├── visualization.py # Plotting utilities
│ │ └── text_processing.py # Text cleaning
│ └── web/
│ ├── app.py # FastAPI entrypoint
│ ├── services/
│ │ └── state.py # Shared application state
│ ├── api/
│ │ ├── dashboard.py # Dashboard API endpoints
│ │ ├── trading_endpoints.py
│ │ ├── models_endpoints.py
│ │ └── data_endpoints.py
│ ├── templates/
│ │ └── index.html # Main dashboard template
│ └── static/
│ ├── css/style.css # Dark-themed responsive UI
│ └── js/app.js # Chart.js, WebSocket, API client
│
└── notebooks/ # Jupyter notebooks for exploration
Quick Start
Prerequisites
- Python 3.10+
- AMD GPU with ROCm 5.6+ (or NVIDIA GPU with CUDA)
- 32GB+ GPU memory recommended (configurable)
Installation
# Clone repository
git clone <repository-url>
cd stock_gnn_r9700
# Create virtual environment
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# For AMD ROCm specifically:
# pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm5.6
# pip install torch-geometric torch-scatter torch-sparse -f https://data.pyg.org/whl/torch-2.1.0+rocm5.6.html
Run Training
python main.py
This will:
- Initialize the data pipeline and download historical data
- Create training/validation datasets
- Train the GNN model with AMD optimizations
- Run backtesting on validation data
- Generate performance plots and metrics
Run Live Trading (Paper)
python live_trading.py
Run Web Dashboard
python -m uvicorn src.web.app:app --host 0.0.0.0 --port 8000
Then open http://localhost:8000 in your browser.
Configuration
All settings are centralized in config.py. Key parameters:
# GPU Settings
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
AMD_GPU = True
GPU_MEMORY_LIMIT = 0.9 # Use 90% of 32GB = ~28.8GB
MIXED_PRECISION = True
PRECISION = 'bf16' # 'fp16' or 'bf16'
ROCM_OPT_LEVEL = 'O2'
# Model Architecture
HIDDEN_CHANNELS = 128
NUM_HEADS = 16
DROPOUT = 0.3
LEARNING_RATE = 0.0005
BATCH_SIZE = 128
SEQUENCE_LENGTH = 60
# Intraday Trading
TRADING_FREQUENCY = '5min' # '1min', '5min', '15min', '30min', '1h'
INITIAL_CAPITAL = 100000
MAX_POSITION_SIZE = 0.03 # 3% of portfolio per position
# Live Data
DATA_PROVIDER = 'polygon' # 'polygon', 'alphavantage', 'ib'
POLYGON_API_KEY = 'your_key'
Training
The training loop (src/models/trainer.py) supports:
- Mixed Precision Training (bf16/fp16) via
torch.cuda.amp - Gradient Checkpointing for memory-efficient large models
- AdamW Optimizer with weight decay
- ReduceLROnPlateau scheduler
- Automatic batch skipping when GPU memory is constrained
- Per-epoch benchmarking and checkpoint saving
Training is triggered via the Web Dashboard or main.py.
Live Trading
The live trading system (live_trading.py) runs an async event loop with:
- LiveDataService — connects to Polygon.io WebSocket for real-time market data
- RealTimeTrader — generates signals on configurable intervals (1min–1h)
- PaperTradingBroker — simulates execution (or swap for InteractiveBrokersBroker)
- Online Learning — periodic model updates every hour
- Memory Monitor — automatic GPU cache clearing when usage exceeds 85%
Web Dashboard
The dashboard provides real-time monitoring and control:
| Feature | Description |
|---|---|
| Live Metrics | GPU memory %, portfolio value, cash, model status via WebSocket |
| Charts | Memory usage and portfolio value Chart.js graphs (60-point history) |
| Trading Controls | Start/stop trading, view positions/orders, close positions |
| Model Controls | Train, save, load, benchmark models |
| Data Controls | View tickers, trigger data updates |
| System Logs | Real-time log streaming with clear button |
API Reference
Dashboard
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/dashboard/metrics |
Current metrics (memory, account, model) |
| GET | /api/dashboard/logs?limit=100 |
Recent log entries |
| POST | /api/dashboard/logs/clear |
Clear stored logs |
Trading
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/trading/status |
Trading status, cash, positions |
| POST | /api/trading/start |
Start live trading |
| POST | /api/trading/stop |
Stop live trading |
| GET | /api/trading/orders |
List all orders |
| POST | /api/trading/order |
Submit manual order |
| POST | /api/trading/cancel/{id} |
Cancel order |
| POST | /api/trading/positions/close/{ticker} |
Close position |
| POST | /api/trading/positions/close-all |
Close all positions |
Models
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/models/status |
Model configuration status |
| POST | /api/models/train |
Start training run |
| POST | /api/models/train/stop |
Stop training |
| POST | /api/models/save |
Save model weights |
| POST | /api/models/load |
Load model weights |
| POST | /api/models/benchmark |
Run performance benchmark |
Data
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/data/tickers |
List tracked tickers |
| GET | /api/data/pipeline/status |
Pipeline status |
| POST | /api/data/update |
Trigger data update |
| GET | /api/data/features/{ticker} |
Latest features for ticker |
| GET | /api/data/price/{ticker} |
Latest price for ticker |
| GET | /api/data/prices/{ticker}?limit=30 |
Historical prices |
WebSocket
| Event | Direction | Description |
|---|---|---|
metrics |
Server → Client | Real-time metrics broadcast (every 2s) |
ping |
Client → Server | Keep-alive / latency check |
AMD Optimizations
src/amd/optimizations.py provides:
- AMDOptimizer: Configures ROCm, enables Flash Attention, sets memory limits
- AMDSparseAttention: Custom attention implementation optimized for AMD GPUs
- AMDGATConv: GAT layer using
torch_geometric.nn.MessagePassingwithsoftmax
Key optimizations applied:
| Technique | Benefit |
|---|---|
| bf16 Mixed Precision | 2x memory reduction, faster compute on MI200/RX7000 |
| Gradient Checkpointing | Train larger models with same GPU memory |
| Flash Attention | O(N) memory vs O(N²) for long sequences |
| SiLU Activation | Better AMD GPU utilization vs ReLU |
| Pinned Memory | Faster CPU→GPU data transfers |
Memory Management
src/utils/memory_manager.py provides:
- GPU Memory Limiting — caps usage to 90% of total memory
- Automatic Cache Clearing — triggers at configurable thresholds (default 85%)
- Per-Operation Memory Checks — skips batches if insufficient memory
- Background Monitoring — logs usage every 60 seconds
- Model Memory Estimation — estimates activation memory before training
Data Pipeline
src/data/pipeline.py handles:
- SQLite Database with WAL mode for concurrent access
- Price Data from yfinance (daily OHLCV, auto-adjusted)
- Corporate Actions (splits, dividends, delistings)
- Sector/Industry classifications
- Alternative Data (news sentiment, social media volume)
- Graph Construction — edges based on sector correlation
The pipeline supports incremental updates: only fetches new data since last run.
Trading System
Signal Generation
The trading system generates signals when:
- Market is open (9:30 AM – 4:00 PM ET)
- Interval matches
TRADING_FREQUENCY(e.g., every 5 minutes) - Model prediction > +0.2% → BUY
- Model prediction < -0.2% and position exists → SELL
Risk Management
| Rule | Default |
|---|---|
| Max Position Size | 3% of portfolio |
| Max Daily Loss | 1% |
| Max Drawdown | 5% |
| Max Daily Positions | 50 |
| Max Hold Time | 4 hours |
| Min Hold Time | 10 minutes |
Execution
- VWAP execution algorithm (configurable)
- Paper Broker for simulation
- Interactive Brokers adapter for live trading
Benchmarking
benchmark.py measures:
| Metric | Description |
|---|---|
| Inference Time | Average forward pass latency |
| Training Time | Average full training step latency |
| Throughput | Samples/second for inference and training |
| Memory Usage | Peak GPU memory during benchmark |
Run with:
python benchmark.py
Results are logged to benchmark_r9700.log.
Development
Adding a New Data Source
- Create a processor in
src/data/(e.g.,src/data/earnings_processor.py) - Implement
fetch_earnings(tickers, start, end)andget_earnings_features(ticker, date) - Register in
StockDataPipeline - Update
config.pywith API keys and feature definitions
Adding a New Model Variant
- Inherit from
nn.Moduleinsrc/models/ - Use
torch.utils.checkpoint.checkpointfor memory efficiency - Wrap with
AMDOptimizer.optimize_model()before training - Register in
GNNTrainer
Adding a New Trading Strategy
- Subclass
Brokerfor execution - Implement
_generate_trading_signals()inRealTimeTrader - Configure thresholds in
config.py
Troubleshooting
GPU Out of Memory
- Reduce
BATCH_SIZEinconfig.py - Reduce
SEQUENCE_LENGTHorHIDDEN_CHANNELS - Enable gradient checkpointing (
MIXED_PRECISION = True) - Lower
GPU_MEMORY_LIMITto trigger earlier cache clearing
ROCm Installation Issues
# Verify ROCm is installed
rocminfo
# Check PyTorch sees the GPU
python -c "import torch; print(torch.cuda.is_available())"
Data Pipeline Empty
- Ensure
yfinancecan connect to Yahoo Finance - Check
data/processed/directory permissions - Verify ticker symbols are valid
WebSocket Disconnects
- Check
WEBSOCKET_MAX_RETRIESandWEBSOCKET_RECONNECT_DELAYin config - Verify Polygon.io API key is valid
- Check firewall rules for WebSocket connections
Model Not Loading
- Verify
models/stock_gnn_r9700.ptexists - Check
config.pyMODEL_DIRpath - Ensure
DEVICEmatches training device (CPU vs CUDA)
License
MIT License — see LICENSE for details.
Acknowledgments
- PyTorch Geometric for GNN implementations
- yfinance for historical market data
- Polygon.io for real-time market data
- Chart.js for dashboard visualizations