Graph Algorithms for Arbitrage Detection: From Bellman-Ford to RICH

MarketMaker.cc Team
البحوث والاستراتيجيات الكمية

MarketMaker.cc Team
البحوث والاستراتيجيات الكمية
Part 1 of the series "Complex Arbitrage Chains Between Futures and Spot"
Imagine the cryptocurrency market as a living, breathing organism where thousands of prices change every second across hundreds of exchanges. In this chaos, temporary price discrepancies arise—"inefficiencies"—that allow for risk-free profit. This is arbitrage. But we are not talking about simple two-step swaps. We are diving into complex multi-asset chains where a profit might only be found by jumping from BTC to ETH, then to SOL, then to USDT, and finally back to BTC.
How do we find these chains among millions of possibilities in real-time? The answer lies in Graph Theory.
In this article, we will traverse the path from classical algorithms to cutting-edge academic research, implementing everything in Rust for maximum performance.
A high-tech visualization of an arbitrage graph: nodes represent assets, and edges represent trading pairs. A highlighted cycle represents a detected profitable opportunity.
To apply graph algorithms, we must first represent the market correctly.
If we have a rate (how much of asset we get for 1 unit of asset ), an arbitrage opportunity exists if a cycle produces:
Computers are much faster at adding than multiplying, and most shortest-path algorithms are designed for sums. We use a simple mathematical trick: the logarithm. Since , the condition becomes: Or, by flipping the sign to find a negative cycle:
Now, every edge has a weight . Our task is to find a cycle with a negative total weight.
The Bellman-Ford algorithm is the "Hello World" of arbitrage detection. It is designed to find shortest paths and can naturally detect negative cycles.
Using the petgraph crate, we can implement this efficiently:
use petgraph::graph::{DiGraph, NodeIndex};
use petgraph::algo::bellman_ford;
fn find_arbitrage_bellman_ford(
graph: &DiGraph<&str, f64>,
start_node: NodeIndex
) -> Option<Vec<NodeIndex>> {
// Bellman-Ford returns distances or an error if a negative cycle is found
match bellman_ford(graph, start_node) {
Ok(_) => None, // No negative cycles
Err(error) => {
// In a real implementation, we would reconstruct the cycle
// using the predecessor map from the error
println!("Arbitrage detected!");
None
}
}
}
Bellman-Ford runs in , where is the number of assets and is the number of trading pairs.
The Shortest Path Faster Algorithm (SPFA) is an optimized version of Bellman-Ford that uses a queue to avoid redundant calculations.
use std::collections::VecDeque;
fn spfa_negative_cycle(n: usize, adj: &Vec<Vec<(usize, f64)>>) -> bool {
let mut dist = vec![0.0; n];
let mut count = vec![0; n];
let mut in_queue = vec![false; n];
let mut queue = VecDeque::new();
for i in 0..n {
in_queue[i] = true;
queue.push_back(i);
}
while let Some(u) = queue.pop_front() {
in_queue[u] = false;
for &(v, weight) in &adj[u] {
if dist[v] > dist[u] + weight {
dist[v] = dist[u] + weight;
count[v] = count[u] + 1;
if count[v] >= n {
return true; // Negative cycle detected
}
if !in_queue[v] {
queue.push_back(v);
in_queue[v] = true;
}
}
}
}
false
}
In practice, SPFA often runs in where , making it much faster for sparse market graphs.
In 2024, researchers proposed the RICH (Rapid Identification of Cyclic High-profitability) algorithm. Unlike Bellman-Ford, RICH is specifically optimized for financial graphs where:
Real trading is not free. A multi-leg cycle incurs three separate trading fees.
We must adjust our edge weights: This significantly prunes the graph, as many theoretical cycles are killed by the cost of execution.
As you buy more of an asset, the price goes up (slippage). A cycle that looks profitable for 10,000. Advanced graph models use parametric weights, where is a function of the volume . This turns the problem from a simple shortest-path search into a Convex Optimization problem on a graph.
In the world of arbitrage, 100 microseconds is the difference between profit and a "skipped" opportunity.
Graph algorithms are the engine behind modern crypto arbitrage. While Bellman-Ford provides the foundation, modern systems use optimized variants like SPFA or specialized algorithms like RICH.
In the next part of this series, we will look at Futures-Spot Arbitrage, where we expand our graph from simple asset swaps to include funding rates and cash-and-carry strategies.
Are you building low-latency trading systems? Check our Open Source Rust HFT Template.