Smart Order Routing (SOR) Engine
December 2024
The Fragmented Trade Tool is a high-precision Smart Order Routing (SOR) engine designed to minimize the total execution cost of large block trades. It utilizes a Hybrid Optimization Model that combines continuous mathematical modeling with discrete real-world verification.
TLDR
The fragmented trade tool creates a non-linear optimization problem, assuming slippage increases linearly across each DEX. For example, if a 100k bid has 10bps of slippage, it will assume a 10k bid will have 1bps of slippage. Although in reality orderbooks are discrete, jagged step functions, optimizing for this would require a brute force method that is computationally too expensive for real-time trading, so we use a smooth convex approximation to instantly find a near-optimal candidate.
1. The Optimization Problem
We model the trade execution as a Constrained Non-Linear Optimization Problem.
Variables
- : Total trade size (in USD).
- : Number of available exchanges (venues).
- : The trade volume allocated to exchange .
- : Taker fee rate for exchange (e.g., ).
- : Liquidity coefficient (slope) for exchange , derived from real-time order book snapshots.
Cost Function
The total cost of execution is the sum of Fees (linear) and Slippage (non-linear). We assume a linear market impact model where Price Impact scales linearly with trade size, making the total slippage cost quadratic.
The cost function for a single venue is:
Objective
We seek to minimize the global cost function subject to filling the order completely:
2. The Analytical Solution
Since the objective function is convex (assuming positive liquidity ), we solve it analytically using the method of Lagrange Multipliers. This avoids the computational cost and inaccuracy of iterative approximation methods.
We introduce a Lagrange multiplier , representing the System-Wide Marginal Cost. At the optimal solution, the marginal cost of trading one additional dollar (Fee + Marginal Slippage) must be identical across all active venues.
The "Water Level" (λ)
We calculate by enforcing the constraint :
Optimal Allocation Formula
Once is known, the exact optimal allocation for each exchange is:
If any venue yields a negative allocation (), it implies that venue is "too expensive" (). The venue is removed from the active set, and is recalculated for the remaining exchanges.
3. The "Reality Gap" & Hybrid Logic
While the QP model provides a theoretical optimum, real-world order books are discrete step functions, not smooth quadratic curves.
- The Issue: An exchange might have a massive "liquidity wall" (e.g., $10M available at zero slippage). The quadratic model () often underestimates the efficiency of these walls, effectively "hallucinating" slippage where there is none.
- The Fix: We implement a Tournament System (Hybrid Decision Rule).
The engine calculates two competing strategies and executes the cheaper one:
- The Challenger (Fragmented Split): The result of the Lagrange optimization .
- The Champion (Winner-Takes-All): The cost of sending 100% of to the single best exchange.
This guarantees that the SOR never performs worse than a standard, non-fragmented trade.
4. Execution Workflow
When calculateFragmentedTrade(orderbooks, tradeSize) is called, the engine follows this logic:
- Benchmark Phase:
- Iterate through all connected orderbooks.
- Simulate a trade of size tradeSize on each book individually.
- Identify the bestSingleRoute (lowest total cost).
- Modeling Phase:
- Calculate the slope for each book based on current depth.
- Feed parameters into the Lagrange solver.
- Optimization Phase:
- Compute (Marginal Cost).
- Determine allocations .
- Iteratively filter out expensive venues until all .
- Verification Phase:
- Take the theoretical allocations and simulate them against the actual discrete order books.
- Calculate the real-world cost of the split (Total Weighted Cost).
- Final Showdown:
- Compare Real Split Cost vs. Benchmark Cost.
- Return the routing instructions for the winner.
5. Visual Example
Imagine a trade of $1,000,000.
| Strategy | Allocation | Effective Cost | Note |
|---|---|---|---|
| Strategy A (Math Model) | Split $500k Hyperliquid / $500k Lighter | 15 bps | The model assumes smooth curves. |
| Strategy B (Winner-Takes-All) | $1M on Hyperliquid | 12 bps | Hyperliquid happened to have a massive liquidity wall today. |
Outcome: The algorithm detects that Strategy B is cheaper (12 bps < 15 bps) and ignores the split, routing 100% to Hyperliquid.