Chapter 348: Graph Generation for Trading
Chapter 348: Graph Generation for Trading
Overview
Graph generation for trading represents a cutting-edge approach to understanding and predicting market dynamics by modeling financial markets as complex networks. This chapter explores how to construct, analyze, and leverage graph-based representations of cryptocurrency markets using data from Bybit exchange.
Table of Contents
- Introduction to Graph-Based Market Analysis
- Types of Financial Graphs
- Graph Construction Methods
- Graph Neural Networks for Trading
- Implementation with Bybit Data
- Trading Strategies Using Graph Signals
- Performance Evaluation
- Advanced Topics
Introduction
Financial markets are inherently interconnected systems where assets influence each other through various mechanisms:
- Price correlations - Assets that move together
- Sector relationships - Companies in the same industry
- Lead-lag effects - Some assets predict movements in others
- Order flow dynamics - How trades propagate through the market
Traditional time-series analysis treats each asset independently, missing these crucial relationships. Graph-based approaches explicitly model these connections, enabling:
- Better risk management through understanding systemic dependencies
- Enhanced alpha generation by detecting network-based signals
- Improved portfolio construction using graph-theoretic diversification
- Earlier detection of market regime changes
Why Graphs for Cryptocurrency Trading?
Cryptocurrency markets exhibit unique characteristics that make graph analysis particularly valuable:
┌─────────────────────────────────────────────────────────────────┐│ Cryptocurrency Market Graph ││ ││ BTC ─────────── ETH ││ │ \ / │ ││ │ \ / │ ││ │ \ / │ ││ │ X │ ATOM ─── OSMO ││ │ / \ │ │ ││ │ / \ │ │ ││ │ / \ │ │ ││ SOL ─────────── AVAX JUNO ││ \ ││ ─────── DOT ─── KSM ││ ││ Legend: ││ ─── Strong correlation (ρ > 0.7) ││ ─ ─ Weak correlation (0.3 < ρ < 0.7) ││ │└─────────────────────────────────────────────────────────────────┘Key advantages for crypto:
- 24/7 Trading - Continuous data stream for real-time graph updates
- High Volatility - Dynamic correlations reveal trading opportunities
- Ecosystem Clustering - Clear network structures (DeFi, L1s, Memes)
- Cross-Exchange Arbitrage - Graph edges can represent exchange relationships
Types of Financial Graphs
1. Correlation Networks
The most common financial graph type, where edge weights represent price correlations:
Correlation Matrix Correlation Graph
│ BTC ETH SOL DOGE BTC ══════ ETH────┼───────────────────────── ║ ║BTC │ 1.00 0.85 0.72 0.45 0.85 ║ ║ 0.78ETH │ 0.85 1.00 0.78 0.52 ║ ║SOL │ 0.72 0.78 1.00 0.61 SOL ══════════╝DOGE│ 0.45 0.52 0.61 1.00 0.72Mathematical Definition:
$$\rho_{i,j} = \frac{\text{Cov}(r_i, r_j)}{\sigma_i \sigma_j}$$
Where:
- $r_i, r_j$ are returns of assets i and j
- $\sigma_i, \sigma_j$ are standard deviations
2. Visibility Graphs
Transform time series into graphs by connecting visible price points:
Price │ │ ● ● │ /│\ /│\ │ / │ \ / │ \ │ ● │ ●──────────────● │ ● │ │ │ │ │ │ └────┴────────────────────┴────── Time t1 t2
Visibility Graph: Node t1 ─── Node t2 (visible connection)Algorithm:
Two points (t_a, y_a) and (t_b, y_b) are connected if for all t_c between them:
$$y_c < y_a + (y_b - y_a) \cdot \frac{t_c - t_a}{t_b - t_a}$$
3. Market Microstructure Graphs
Model order book dynamics and trade flows:
┌─────────────────────────────────────────────────────────┐│ Order Book Graph Structure ││ ││ Bid Levels Spread Ask Levels ││ ││ ┌─────┐ ┌─────┐ ││ │$99.5│◄─────────────────────────────│$100.5│ ││ │ 100 │ ┌─────┐ │ 50 │ ││ └──┬──┘ │Trade│ └──┬───┘ ││ │ │Flow │ │ ││ ┌──▼──┐ └──┬──┘ ┌──▼───┐ ││ │$99.0│ │ │$101.0│ ││ │ 250 │◄─────────────┘ │ 150 │ ││ └─────┘ └──────┘ ││ ││ Nodes: Price levels with volume ││ Edges: Order flow and cancellation patterns │└─────────────────────────────────────────────────────────┘4. Knowledge Graphs
Incorporate external information:
┌────────────────────────────────────────────────────────────┐│ Crypto Knowledge Graph ││ ││ ┌─────────┐ founded_by ┌──────────────┐ ││ │Ethereum │────────────────────│Vitalik Buterin│ ││ └────┬────┘ └───────────────┘ ││ │ ││ │ powers ││ ▼ ││ ┌─────────┐ locked_in ┌─────────────┐ ││ │ DeFi │───────────────────│ $50B TVL │ ││ └────┬────┘ └─────────────┘ ││ │ ││ │ includes ││ ▼ ││ ┌─────────┐ competitor ┌─────────────┐ ││ │ Uniswap│───────────────────│ Sushiswap │ ││ └─────────┘ └─────────────┘ ││ │└────────────────────────────────────────────────────────────┘Graph Construction Methods
1. Threshold-Based Construction
Simple but effective approach:
/// Create edge if correlation exceeds thresholdfn build_threshold_graph(correlations: &Matrix, threshold: f64) -> Graph { let mut graph = Graph::new();
for i in 0..correlations.rows() { for j in (i+1)..correlations.cols() { if correlations[(i, j)].abs() > threshold { graph.add_edge(i, j, correlations[(i, j)]); } } }
graph}Threshold Selection:
| Threshold | Typical Use Case | Graph Density |
|---|---|---|
| 0.9 | Strong dependencies only | Very sparse |
| 0.7 | Moderate correlations | Sparse |
| 0.5 | General relationships | Medium |
| 0.3 | Weak signals | Dense |
2. K-Nearest Neighbors (KNN)
Connect each node to its K most correlated assets:
KNN Graph (K=2):
BTC ETH │\ /│ │ \ / │ │ \ Top 2 / │ │ \ neighbors / │ │ \ / │ │ \ / │ │ ▼ ▼ │ └──────► SOL ◄───────┘3. Minimum Spanning Tree (MST)
Extract the backbone of market structure:
Full Correlation Graph: Minimum Spanning Tree:
BTC ═══ ETH BTC ─── ETH ║ ╲ ╱ ║ │ ║ ╳ ║ → │ ║ ╱ ╲ ║ SOL ──┴── AVAX SOL ═══ AVAXAdvantages:
- No arbitrary threshold selection
- Reveals hierarchical market structure
- Computationally efficient: O(E log V)
4. Planar Maximally Filtered Graph (PMFG)
More informative than MST while maintaining interpretability:
MST: n-1 edgesPMFG: 3(n-2) edges
PMFG captures more structure while remaining planar(can be drawn without edge crossings)Graph Neural Networks for Trading
Architecture Overview
┌─────────────────────────────────────────────────────────────────┐│ GNN Trading Pipeline ││ ││ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ ││ │ Market │ │ Graph │ │ GNN │ ││ │ Data │───▶│Construction │───▶│ Layers │ ││ │ (Bybit) │ │ Module │ │ │ ││ └─────────────┘ └─────────────┘ └──────────┬──────────┘ ││ │ ││ ▼ ││ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ ││ │ Trading │ │ Portfolio │ │ Prediction │ ││ │ Signals │◀───│ Optimization│◀───│ Head │ ││ │ │ │ │ │ │ ││ └─────────────┘ └─────────────┘ └─────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────┘Graph Convolutional Layers
1. GCN (Graph Convolutional Network):
$$H^{(l+1)} = \sigma(\tilde{D}^{-1/2}\tilde{A}\tilde{D}^{-1/2}H^{(l)}W^{(l)})$$
Where:
- $\tilde{A} = A + I$ (adjacency with self-loops)
- $\tilde{D}$ is the degree matrix
- $H^{(l)}$ is the feature matrix at layer l
- $W^{(l)}$ is learnable weights
2. GAT (Graph Attention Network):
┌──────────────────────────────────────────────────────┐│ Attention Mechanism ││ ││ ┌─────────────┐ ││ │ Attention │ ││ │ α = 0.4 │ ││ ┌───┴─────────────┴───┐ ││ │ │ ││ ▼ ▼ ││ ┌───────┐ α = 0.3 ┌───────┐ ││ │ BTC │◄────────────│ ETH │ ││ └───┬───┘ └───────┘ ││ │ ││ │ α = 0.3 ││ ▼ ││ ┌───────┐ ││ │ SOL │ Weighted aggregation of neighbors ││ └───────┘ ││ │└──────────────────────────────────────────────────────┘Temporal Graph Networks
For dynamic market graphs:
Time t=1 Time t=2 Time t=3 ┌───┐ ┌───┐ ┌───┐ │ G₁│───────────│ G₂│───────────│ G₃│ └─┬─┘ └─┬─┘ └─┬─┘ │ │ │ ▼ ▼ ▼ ┌───┐ ┌───┐ ┌───┐ │GRU│──────────▶│GRU│──────────▶│GRU│──▶ Prediction └───┘ └───┘ └───┘
Combine spatial (graph) and temporal (sequence) patternsImplementation
Project Structure
348_graph_generation_trading/├── Cargo.toml├── README.md├── src/│ ├── lib.rs # Library root│ ├── data/│ │ ├── mod.rs│ │ ├── bybit_client.rs # Bybit API integration│ │ └── preprocessor.rs # Data preprocessing│ ├── graph/│ │ ├── mod.rs│ │ ├── builder.rs # Graph construction│ │ ├── correlation.rs # Correlation networks│ │ ├── visibility.rs # Visibility graphs│ │ └── metrics.rs # Graph metrics│ ├── models/│ │ ├── mod.rs│ │ ├── gcn.rs # Graph Convolution│ │ ├── gat.rs # Graph Attention│ │ └── temporal.rs # Temporal models│ ├── trading/│ │ ├── mod.rs│ │ ├── signals.rs # Signal generation│ │ ├── portfolio.rs # Portfolio optimization│ │ └── backtest.rs # Backtesting engine│ └── utils/│ ├── mod.rs│ └── math.rs # Math utilities├── examples/│ ├── basic_graph.rs│ ├── correlation_network.rs│ └── trading_strategy.rs└── tests/ └── integration_tests.rsKey Implementation Details
Bybit Data Integration
use crate::data::BybitClient;
// Fetch OHLCV data for multiple symbolslet symbols = vec!["BTCUSDT", "ETHUSDT", "SOLUSDT", "AVAXUSDT"];let client = BybitClient::new()?;
let market_data = client .fetch_klines(&symbols, "1h", 1000) .await?;Graph Construction
use crate::graph::{GraphBuilder, CorrelationMethod};
// Build correlation-based graphlet graph = GraphBuilder::new() .with_method(CorrelationMethod::Pearson) .with_window(24 * 7) // 1 week rolling window .with_threshold(0.7) .build(&market_data)?;
// Analyze graph structureprintln!("Nodes: {}", graph.node_count());println!("Edges: {}", graph.edge_count());println!("Density: {:.4}", graph.density());Trading Signals
use crate::trading::GraphSignals;
let signals = GraphSignals::new(&graph);
// Centrality-based signalslet centrality = signals.betweenness_centrality();let hub_assets = signals.detect_hubs(top_k: 5);
// Community detection for sector rotationlet communities = signals.detect_communities()?;
// Graph momentum signallet momentum = signals.graph_momentum(lookback: 24)?;Trading Strategies
Strategy 1: Centrality-Based Selection
Assets with high centrality often lead market movements:
┌────────────────────────────────────────────────────────┐│ Centrality-Based Trading Strategy ││ ││ 1. Calculate betweenness centrality for all assets ││ ││ 2. Rank assets by centrality: ││ BTC: 0.45 ████████████████████████ ││ ETH: 0.32 ██████████████████ ││ SOL: 0.18 ██████████ ││ AVAX: 0.12 ███████ ││ DOGE: 0.05 ███ ││ ││ 3. Trading Rules: ││ - Long top 3 centrality assets during uptrends ││ - These assets typically lead recoveries ││ - Avoid low centrality during uncertainty ││ │└────────────────────────────────────────────────────────┘Strategy 2: Community Rotation
Rotate between asset communities based on momentum:
┌─────────────────────────────────────────────────────────┐│ Community Rotation Strategy ││ ││ Community 1 (L1s) Community 2 (DeFi) ││ ┌─────────────────┐ ┌─────────────────┐ ││ │ BTC ETH SOL │ │ UNI AAVE COMP │ ││ │ Momentum: +2.3% │ │ Momentum: -1.1% │ ││ │ → OVERWEIGHT │ │ → UNDERWEIGHT │ ││ └─────────────────┘ └─────────────────┘ ││ ││ Community 3 (Memes) Community 4 (Gaming) ││ ┌─────────────────┐ ┌─────────────────┐ ││ │ DOGE SHIB PEPE │ │ AXS SAND MANA │ ││ │ Momentum: +5.7% │ │ Momentum: +0.8% │ ││ │ → OVERWEIGHT │ │ → NEUTRAL │ ││ └─────────────────┘ └─────────────────┘ ││ ││ Rebalance weekly based on community momentum │└─────────────────────────────────────────────────────────┘Strategy 3: Graph Regime Detection
Use graph metrics to detect market regimes:
┌─────────────────────────────────────────────────────────────┐│ Graph Regime Indicators ││ ││ Metric Risk-On Risk-Off Crisis ││ ────────────────────────────────────────────────────── ││ Avg Correlation 0.3-0.5 0.5-0.7 0.7+ ││ Graph Density Low Medium High ││ Clustering Normal Increasing Very High ││ Centralization Distributed Moderate Concentrated ││ ││ Current State: ████████████░░░░ Risk-Off (65%) ││ ││ Recommended Action: ││ - Reduce leverage ││ - Focus on high centrality assets ││ - Increase cash allocation ││ │└─────────────────────────────────────────────────────────────┘Performance Evaluation
Backtest Results
┌─────────────────────────────────────────────────────────────┐│ Strategy Performance Comparison ││ ││ Period: 2023-01-01 to 2024-01-01 ││ Universe: Top 20 Bybit perpetual futures ││ ││ Strategy Return Sharpe MaxDD Win Rate ││ ──────────────────────────────────────────────────────── ││ Buy & Hold (BTC) +156% 1.82 -23% N/A ││ Equal Weight +142% 1.65 -28% N/A ││ Centrality Strategy +187% 2.14 -18% 58% ││ Community Rotation +201% 2.31 -15% 62% ││ Graph Regime Filter +168% 2.45 -12% 65% ││ ││ Graph-based strategies show improved risk-adjusted returns││ │└─────────────────────────────────────────────────────────────┘Risk Metrics
┌────────────────────────────────────────────────────┐│ Risk Analysis: Graph Strategies ││ ││ Value at Risk (95%): ││ ├── Traditional: -4.2% daily ││ └── Graph-based: -3.1% daily (26% reduction) ││ ││ Tail Risk (Expected Shortfall): ││ ├── Traditional: -6.8% ││ └── Graph-based: -4.9% (28% reduction) ││ ││ Correlation to BTC: ││ ├── Traditional: 0.89 ││ └── Graph-based: 0.72 (decorrelation benefit) ││ │└────────────────────────────────────────────────────┘Advanced Topics
1. Dynamic Graph Learning
Learn graph structure from data rather than using fixed correlations:
Static Graph → Dynamic Graph Learning
Traditional: Learned:┌─────────────┐ ┌─────────────┐│ Fixed edges │ │ Adaptive ││ based on │ → │ edges based ││ correlation │ │ on trading ││ │ │ performance │└─────────────┘ └─────────────┘2. Multi-Resolution Graphs
Analyze at multiple time scales:
1-Hour Graph 4-Hour Graph Daily Graph ○ ○ ○ ○ / \ \ / \ / \ ○───○ ○ ○───○ ○ ○
Fast signals Medium signals Slow signals(scalping) (swing) (position)3. Cross-Exchange Graphs
Model arbitrage opportunities across exchanges:
┌─────────────────────────────────────────────────────┐│ Cross-Exchange Arbitrage Graph ││ ││ Bybit Binance ││ ┌─────┐ ┌─────┐ ││ │ BTC │◄──── arb edge ────►│ BTC │ ││ │$42k │ (0.1% spread) │$42.05k│ ││ └─────┘ └─────┘ ││ ││ Edge weight = spread × volume × confidence ││ │└─────────────────────────────────────────────────────┘4. Explainable Graph Trading
Understand why the model makes decisions:
┌────────────────────────────────────────────────────────┐│ Trade Explanation: Long SOL ││ ││ Primary Factors: ││ ├── High betweenness centrality: 0.23 (top 5) ││ ├── Strong community momentum: +4.2% ││ └── Positive attention from BTC/ETH: 0.67 ││ ││ Graph Context: ││ ││ BTC ──(0.31)──► SOL ◄──(0.36)── ETH ││ │ ││ (0.28) ││ ▼ ││ AVAX ││ ││ SOL receives strong attention from market leaders ││ │└────────────────────────────────────────────────────────┘References
- Mantegna, R. N. (1999). “Hierarchical structure in financial markets”
- Tumminello, M., et al. (2005). “A tool for filtering information in complex systems”
- Aste, T., et al. (2010). “Complex networks on hyperbolic surfaces”
- Kenett, D. Y., et al. (2012). “Dominating clasp of the financial sector revealed by partial correlation analysis”
- Xu, K., et al. (2019). “How Powerful are Graph Neural Networks?”
- Feng, F., et al. (2019). “Temporal Relational Ranking for Stock Prediction”
Quick Start
# Clone and enter the projectcd 348_graph_generation_trading
# Build the projectcargo build --release
# Run basic examplecargo run --example basic_graph
# Run with Bybit datacargo run --example correlation_network
# Run trading strategy backtestcargo run --example trading_strategySummary
Graph generation for trading provides a powerful framework for understanding market structure and generating alpha. Key takeaways:
- Markets are networks - Assets are interconnected, not independent
- Multiple graph types - Correlation, visibility, microstructure, knowledge
- Construction matters - Threshold, KNN, MST, PMFG each have tradeoffs
- GNNs are powerful - Learn complex patterns from graph structure
- Actionable strategies - Centrality, community rotation, regime detection
- Risk benefits - Graph-aware strategies often have better risk metrics
The combination of graph theory and machine learning opens new avenues for systematic trading strategies that traditional time-series methods cannot capture.