Skip to content

Chapter 347: Dynamic Graph Neural Networks for Trading

Chapter 347: Dynamic Graph Neural Networks for Trading

Overview

Dynamic Graph Neural Networks (Dynamic GNNs) represent a cutting-edge approach to modeling financial markets by capturing the evolving relationships between assets, market participants, and external factors over time. Unlike static graph neural networks that assume fixed graph structures, Dynamic GNNs adapt their topology and edge weights in response to changing market conditions.

Table of Contents

  1. Introduction
  2. Theoretical Foundation
  3. Dynamic Graph Construction
  4. Architecture Components
  5. Temporal Evolution Mechanisms
  6. Application to Cryptocurrency Trading
  7. Implementation Strategy
  8. Risk Management
  9. Performance Metrics
  10. References

Introduction

Financial markets are inherently dynamic systems where relationships between assets constantly evolve. Traditional machine learning approaches often fail to capture these complex, time-varying interdependencies. Dynamic GNNs address this limitation by:

  • Modeling evolving correlations: Asset correlations change during different market regimes
  • Capturing market microstructure: Order flow and liquidity relationships shift continuously
  • Adapting to regime changes: Bull, bear, and sideways markets have different structural properties
  • Learning temporal patterns: Price movements follow complex temporal dependencies

Why Dynamic GNNs for Trading?

┌─────────────────────────────────────────────────────────────────┐
│ Market as a Dynamic Graph │
├─────────────────────────────────────────────────────────────────┤
│ │
│ BTC ←──────→ ETH Time t₁: High correlation │
│ ↑ ↑ │
│ │ │ │
│ ↓ ↓ │
│ USDT ←──────→ SOL │
│ │
│ ════════════════════════════════════════════ │
│ │
│ BTC ←─ ─ ─ → ETH Time t₂: Decorrelation │
│ ↑ │
│ │ ╲ │
│ ↓ ╲ │
│ USDT → SOL New relationships emerge │
│ │
└─────────────────────────────────────────────────────────────────┘

Theoretical Foundation

Graph Representation of Markets

A financial market at time $t$ can be represented as a graph $G_t = (V, E_t, X_t)$ where:

  • V: Set of nodes (assets, exchanges, traders)
  • E_t: Time-varying edges (correlations, order flow, arbitrage opportunities)
  • X_t: Node features (price, volume, volatility, order book depth)

Message Passing Framework

The core GNN operation follows the message passing paradigm:

$$h_v^{(l+1)} = \text{UPDATE}\left(h_v^{(l)}, \text{AGGREGATE}\left({m_{uv} : u \in \mathcal{N}(v)}\right)\right)$$

Where:

  • $h_v^{(l)}$ is the hidden state of node $v$ at layer $l$
  • $m_{uv}$ is the message from node $u$ to node $v$
  • $\mathcal{N}(v)$ is the neighborhood of node $v$

Temporal Graph Networks (TGN)

For dynamic graphs, we extend this to temporal graphs:

$$h_v(t) = \text{GNN}\left(h_v(t^-), {(h_u(t^-), e_{uv}(t), \Delta t_{uv}) : u \in \mathcal{N}_t(v)}\right)$$

Where $\Delta t_{uv}$ represents the time since the last interaction.

Dynamic Graph Construction

Correlation-Based Graphs

Rolling Correlation Matrix → Adjacency Matrix → Graph Structure
BTC ETH SOL AVAX
BTC 1.00 0.85 0.72 0.68
ETH 0.85 1.00 0.78 0.71
SOL 0.72 0.78 1.00 0.82
AVAX 0.68 0.71 0.82 1.00
Threshold: ρ > 0.7 creates edge

Order Flow Graphs

┌────────────────────────────────────────────────┐
│ Order Flow Graph Construction │
├────────────────────────────────────────────────┤
│ │
│ Exchange A ─────────→ Exchange B │
│ │ flow │ │
│ │ │ │
│ ↓ ↓ │
│ Whale Addr ←───────── Retail Pool │
│ │
│ Edge weight = Volume × Frequency × Recency │
│ │
└────────────────────────────────────────────────┘

Multi-Resolution Graphs

We construct graphs at multiple time scales:

  1. Tick-level (milliseconds): Microstructure relationships
  2. Minute-level: Short-term momentum
  3. Hour-level: Intraday patterns
  4. Daily-level: Macro trends

Architecture Components

1. Dynamic Edge Predictor

Predicts which edges should exist at time $t+1$:

Input: Node embeddings h_u(t), h_v(t)
Historical edge existence
Market regime features
Output: P(edge_uv exists at t+1)

2. Attention-Based Aggregation

α_uv = softmax(LeakyReLU(a^T [Wh_u || Wh_v || e_uv]))
h_v' = σ(Σ α_uv · Wh_u)

3. Temporal Memory Module

┌─────────────────────────────────────────────────┐
│ Temporal Memory Cell │
├─────────────────────────────────────────────────┤
│ │
│ Memory(t) = Memory(t-1) ⊙ forget_gate │
│ + new_info ⊙ input_gate │
│ │
│ Where: │
│ - forget_gate = σ(W_f · [h(t), Δt]) │
│ - input_gate = σ(W_i · [h(t), Δt]) │
│ - new_info = tanh(W_c · [h(t), event]) │
│ │
└─────────────────────────────────────────────────┘

4. Graph Evolution Layer

G(t+1) = GraphEvolution(G(t), market_events, time_decay)
Components:
├── Edge Addition: New correlations detected
├── Edge Removal: Correlations broken
├── Weight Update: Strength changes
└── Node Features: Price/volume updates

Temporal Evolution Mechanisms

Continuous-Time Dynamic Graphs (CTDG)

Instead of discrete snapshots, model the graph as a continuous process:

$$\frac{dh_v}{dt} = f(h_v, {h_u : u \in \mathcal{N}(v)}, t)$$

Event-Driven Updates

Market Event Types:
├── Trade execution → Update price nodes
├── Order placement → Update order book graph
├── Large transfer → Update whale tracking graph
├── News/announcement → Update sentiment edges
└── Liquidation → Update risk propagation edges

Time Encoding

Encode temporal information using:

$$\phi(t) = [\cos(\omega_1 t), \sin(\omega_1 t), …, \cos(\omega_d t), \sin(\omega_d t)]$$

Application to Cryptocurrency Trading

Bybit Market Structure

┌─────────────────────────────────────────────────────────────┐
│ Bybit Trading Graph │
├─────────────────────────────────────────────────────────────┤
│ │
│ Spot Markets Perpetual Futures │
│ ┌─────────┐ ┌─────────────────┐ │
│ │ BTC/USDT│←─────────→│ BTCUSDT Perp │ │
│ │ ETH/USDT│←─────────→│ ETHUSDT Perp │ │
│ │ SOL/USDT│←─────────→│ SOLUSDT Perp │ │
│ └─────────┘ └─────────────────┘ │
│ ↑ ↑ │
│ │ Funding Rate │ │
│ └────────────────────────┘ │
│ │
│ Cross-Asset Signals: │
│ • BTC dominance changes → altcoin rotation │
│ • Funding rate divergence → arbitrage opportunities │
│ • Open interest changes → leverage indicators │
│ │
└─────────────────────────────────────────────────────────────┘

Feature Engineering for Crypto

Feature CategoryFeaturesUpdate Frequency
PriceOHLCV, returns, volatilityReal-time
Order BookBid-ask spread, depth, imbalanceReal-time
FundingFunding rate, predicted funding8 hours
Open InterestOI, OI change, long/short ratioReal-time
On-ChainWhale movements, exchange flowsMinutes
SentimentFear & Greed, social volumeHours

Trading Signal Generation

┌────────────────────────────────────────────────────────────┐
│ Signal Generation Pipeline │
├────────────────────────────────────────────────────────────┤
│ │
│ 1. Graph Update │
│ └── Ingest new market data │
│ └── Update node features │
│ └── Recalculate edge weights │
│ │
│ 2. GNN Forward Pass │
│ └── Message passing across updated graph │
│ └── Temporal aggregation │
│ └── Generate node embeddings │
│ │
│ 3. Signal Extraction │
│ └── Apply prediction heads │
│ ├── Direction: P(up), P(down), P(sideways) │
│ ├── Magnitude: Expected return │
│ └── Confidence: Model uncertainty │
│ │
│ 4. Position Sizing │
│ └── Kelly criterion with risk constraints │
│ └── Portfolio optimization │
│ └── Generate trade orders │
│ │
└────────────────────────────────────────────────────────────┘

Implementation Strategy

Module Architecture

dynamic_gnn_trading/
├── src/
│ ├── lib.rs # Library root
│ ├── graph/
│ │ ├── mod.rs # Graph module
│ │ ├── node.rs # Node definitions
│ │ ├── edge.rs # Edge definitions
│ │ └── dynamic.rs # Dynamic graph operations
│ ├── gnn/
│ │ ├── mod.rs # GNN module
│ │ ├── layers.rs # GNN layers
│ │ ├── attention.rs # Attention mechanisms
│ │ └── temporal.rs # Temporal components
│ ├── data/
│ │ ├── mod.rs # Data module
│ │ ├── bybit.rs # Bybit API client
│ │ ├── orderbook.rs # Order book processing
│ │ └── features.rs # Feature engineering
│ ├── strategy/
│ │ ├── mod.rs # Strategy module
│ │ ├── signals.rs # Signal generation
│ │ └── execution.rs # Order execution
│ └── utils/
│ ├── mod.rs # Utilities
│ └── metrics.rs # Performance metrics
├── examples/
│ ├── basic_gnn.rs # Basic GNN example
│ ├── live_trading.rs # Live trading demo
│ └── backtest.rs # Backtesting example
└── tests/
└── integration.rs # Integration tests

Key Design Principles

  1. Modularity: Each component is independent and testable
  2. Real-time Processing: Designed for streaming data
  3. Memory Efficiency: Incremental graph updates
  4. Type Safety: Leverage Rust’s type system
  5. Error Handling: Comprehensive error types

Risk Management

Graph-Based Risk Metrics

Risk Propagation Model:
Node Risk = Local Risk + Σ (Edge Weight × Neighbor Risk)
Where:
- Local Risk = VaR + Liquidity Risk + Concentration Risk
- Edge Weight = Correlation × Contagion Factor
- Neighbor Risk = Counterparty/Asset cluster risk

Position Limits

┌────────────────────────────────────────┐
│ Risk Constraints │
├────────────────────────────────────────┤
│ Max Position Size: 5% of portfolio │
│ Max Correlated Exposure: 15% │
│ Max Drawdown Trigger: 10% │
│ Leverage Limit: 3x │
│ Liquidation Buffer: 20% │
└────────────────────────────────────────┘

Circuit Breakers

  1. Volatility Spike: Pause trading if hourly vol > 3σ
  2. Correlation Breakdown: Reduce size if graph structure changes dramatically
  3. Liquidity Crisis: Exit positions if spread > threshold
  4. Model Divergence: Stop if prediction accuracy drops

Performance Metrics

Model Metrics

MetricDescriptionTarget
Graph Prediction AUCEdge existence prediction> 0.75
Direction AccuracyPrice direction prediction> 55%
Sharpe RatioRisk-adjusted returns> 2.0
Max DrawdownLargest peak-to-trough< 15%
Calmar RatioReturn / Max Drawdown> 1.5

Latency Requirements

┌─────────────────────────────────────────┐
│ Latency Budget │
├─────────────────────────────────────────┤
│ Data Ingestion: < 10ms │
│ Graph Update: < 50ms │
│ GNN Inference: < 100ms │
│ Signal Generation: < 20ms │
│ Order Submission: < 50ms │
├─────────────────────────────────────────┤
│ Total Round Trip: < 230ms │
└─────────────────────────────────────────┘

References

  1. Rossi, E., et al. (2020). “Temporal Graph Networks for Deep Learning on Dynamic Graphs.” ICML Workshop on Graph Representation Learning

  2. Xu, D., et al. (2020). “Inductive Representation Learning on Temporal Graphs.” ICLR

  3. Pareja, A., et al. (2020). “EvolveGCN: Evolving Graph Convolutional Networks for Dynamic Graphs.” AAAI

  4. Sankar, A., et al. (2020). “DySAT: Deep Neural Representation Learning on Dynamic Graphs via Self-Attention Networks.” WSDM

  5. Kumar, S., et al. (2019). “Predicting Dynamic Embedding Trajectory in Temporal Interaction Networks.” KDD

  6. Kazemi, S.M., et al. (2020). “Representation Learning for Dynamic Graphs: A Survey.” JMLR

  7. Chen, J., et al. (2021). “Graph Neural Networks for Financial Market Prediction.” IEEE Transactions on Neural Networks


Next Steps


Chapter 347 of Machine Learning for Trading