top of page

The Complete Guide Smart Order Routing (SOR)

Introduction

This article is intended to instill the reader with a basic understanding of what smart order routing is in both the traditional finance and cryptocurrency spaces. Additionally, the ‘whys’ and ‘hows’ are explored, along with some existing standout solutions in crypto.

What Are Smart Order Routers?

The phrase ‘smart order router’ can be broken down into its constituent parts for some insight into its meaning. ‘Smart’ refers to the intelligence involved in rapid and optimal decision making, an ‘order’ is some type of instruction given with the expectation that that action will be taken, and ‘routing’ refers to setting a course aimed at reaching a final destination.

In online trading, smart order routing (SOR) is an automated process for handling orders with the intent of attaining the most desirable path across trading venues. SOR algorithms do so by following a set of rules which primarily factor liquidity into calculations intended to identify the best way of executing a given trade. In general, SORs seek liquidity.

Originating from the equities market, SOR was conceived in response to the fractured liquidity caused by the ever-increasing number of electronic trading venues and platforms. Now prevalent in all areas of electronic trading, SOR takes advantage of this fragmented liquidity by finding optimal routes for orders across venues trading a number of assets in different amounts at different prices in order to minimize losses.

Although the terms are sometimes incorrectly interchanged, SORs are not trading algorithms, SORs only consider where an order is directed, and at what price. Trading algorithms on the other hand tend to deal with the ‘what’ and ‘when’ of placing orders.

Why Do We Need Smart Order Routers?

The primary objective of SOR is to provide traders with optimal chances of price improvements and occasionally even identify opportunities in which the trader stands to profit from trade imbalances. They do so by following algorithmic procedures informed by liquidity and volatility data, automatically identifying the best orders to place across venues, given the desired swap. Some routers will be entirely automated and built into the execution of trading bots, while others will require manual input and serve more as a tool to human traders.

Slippage

To understand why SOR is a necessity in the world of electronic trading, it is important to first understand the concept of slippage. Slippage refers to the difference between the expected and actual prices of a trade, it occurs in all forms of markets and is particularly problematic in times of high volatility.

0_v8J18oCFPd50pPmZ.png
The Complete Guide Smart Order Routing (SOR)
Introduction
What Are Smart Order Routers?
Why Do We Need Smart Order Routers?
Slippage
Scarce Liquidity
Fragmented Liquidity
Market Volatility and Price Inconsistencies
Nuances of Smart Order Routing
What is a Route in the Stock Market?
What is a Route in Crypto?

Source: https://www.babypips.com/forexpedia/slippage

Slippage generally occurs when the bid/ask spread (the difference between the highest price a buyer is willing to pay and the lowest price a seller is willing to accept) changes between the time an order is requested and the time that the order is executed. These changes in asset price can be self-induced when placing orders of significant magnitude, as your own alteration to the supply and demand cycle can alter the price before the trade executes due to keen-eyed traders and algorithms observing your order’s placement. Slippage can also occur when there is insufficient volume at the given price point to sustain the current bid/ask spread, i.e. when there are not enough buyers and/or sellers at the given levels.

Slippage can be positive in scenarios where the expected and actual prices of an asset benefit the trader in question, yielding a profit in cases such as the bid price increasing in short trades. However, slippage is more often negative, incurring a loss in the events such as the ask price increasing in long trades. The following hypothetical example taken from Investopedia may help in understanding the mechanics of negative slippage:

  • A given bid/ask price is $183.50/$183.57 on a broker interface

  • A market order for 100 shares is placed, intending to be filled at $183.53

  • In the time between placing the order and the order’s execution, the bid/ask spread lifts to $183.54/183.57

  • This incurs a $0.04 per share or $4 total negative slippage for the 100-share trade

 

For a more tangible and crypto-centric example of slippage, see section 2.1 of our article on DEX aggregators.

SOR primarily aims to avoid incurring slippage losses by identifying liquidity and volatility factors across a number of sources and may also include differing order types such as limit or market orders, and more complex timed strategies such as trailing stop orders. In doing so, a SOR algorithm carves out a ‘route’ between buyer and seller, a route that may span multiple venues in order to take advantage of those venues’ liquidity depth and volatility metrics.

 

Scarce Liquidity

Cryptocurrency market venues often suffer from liquidity scarcity, rendering large transactions either impossible or infeasible due to the various losses incurred. This is largely due to the issue explored in the next section but is also a symptom of the relatively small market cap of cryptocurrencies relative to traditional markets.

Fragmented Liquidity

As previously mentioned, SORs both take advantage of and aim to mend the problem of fragmented liquidity, an issue facing the various cryptocurrency spaces as the number of trading venues continues to increase. Liquidity within venues is generally somewhat endemic to that ecosystem, meaning that the liquidity of one asset in that venue is separated from the liquidity of that asset in other venues. This can dramatically reduce the readily available liquidity of any given asset, unless tools are used to bridge these venues in an intelligent manner, such as via the implementation of SOR.

Market Volatility and Price Inconsistencies

Cryptocurrencies are not generally known for their stability (barring non-algorithmic stablecoin examples). Volatile assets are extremely prone to slippage, combined with inconsistent asset pricing across venues, SOR offers a lot of value in the way of both loss mitigation and in the potential for arbitrage opportunities.

Nuances of Smart Order Routing

SORs come in many forms specialized to their own ecosystems and objectives. Generally speaking, the goal is to find the best route for an order; but this can mean different things in different use cases. SORs may break orders up, spreading orders across venues, they may favor fill speed rate over fill price or vice versa. Additionally, more advanced techniques such as dark pools (private exchanges on which transactions are not made public, useful in the aforementioned cases in which your own large order would noticeably affect the market) may be leveraged in cases where extremely large orders are being placed, with the intent of minimizing immediate market reaction; or they can be configured to optimize dynamic ratio indicators such as volume-weighted average price (VWAP) or time-weighted average price (TWAP).

What is a Route in the Stock Market?

In traditional markets, an order placed through a broker will be passed on to market makers or order flow agreement partners for execution. The broker is the middleman in this arrangement and facilitates the routes through which your order can be connected with its counterpart offer/s, often through order flow arrangements with third-party liquidity providers known as market makers.

Orders placed through electronic communication networks (ECN) are able to cut out the middleman broker, routing trades directly. ECNs use order books that automatically match buyers to sellers via algorithms that are generally centered on best price execution.

What is a Route in Crypto?

In crypto, a SOR generally searches for the most efficient route to swap tokens for another token type. SOR for centralized exchanges (CEX) is comparable to the SOR techniques used in ECN, as CEXs similarly use order books, and routing consists of matching buyers to sellers across the order book in accordance with some algorithmically optimized goal.

However, in the case of decentralized exchanges (DEX), SOR deviates somewhat in functionality from its traditional market counterpart. A more comprehensive definition of DEXs can be found in this article, but they are essentially exchanges that do away with third-party authority, enabling users to exchange assets with one another without the need to forfeit custody of their assets. This is made possible by liquidity pool smart contracts, pools of (generally two types of) tokens that users can swap for one another which use conservation functions such as the XYK model in order to algorithmically price assets — these functions are often referred to as automated market makers (AMM), as they effectively fill the same role as traditional market makers by buying and selling assets to facilitate market swaps, only they do so algorithmically. In its simplest form, a user wishing to swap token A for token B will deposit token A into an A:B liquidity pool, receiving the appropriate amount of token B in return.

DEXs are however by no means impervious to the aforementioned issues of liquidity scarcity, liquidity fragmentation, and market volatility, and as such, are susceptible to slippage all the same. In fact, in many cases these factors are amplified within the current state of DEXs, as liquidity pools are many in number, fragmenting liquidity and leaving only small amounts for each pool; furthermore, liquidity issues tend to cause price fluctuations on account of the pool’s conservation functions acting to quickly rebalance the pool’s asset ratio.

Due to the nature of AMMs in DEXs, slippage due to placing large orders is often even more of an issue than in other types of venues, as the liquidity concentration of DEX liquidity pools is generally on the smaller side relative to other sources of liquidity. When swapping tokens in a DEX, you are essentially adding one asset to the liquidity pool while simultaneously removing another, the AMM conservation function then automatically rebalances the ratio of these two tokens. This means that trades in pools with small liquidity, conservation functions can overcompensate and cause dramatic price swings as the proportional ratio will be more affected — conducive to major slippage. This can be thought of analogously as the fluid displacement of a boat (an order) in a body of water (a liquidity pool); a dingy in a river will virtually have no impact on the water level, but a yacht in a pool certainly will.

With all of these factors in play, it is generally infeasible to execute large trades in DEXs without incurring large losses to slippage. To combat these effects, DEX SORs can scour across various liquidity pools in order to find swaps with the most optimal price for the user. Additionally, they may also take advantage of the liquidity depth of several pools rather than simply relying on the available liquidity of a single pool, potentially even making use of any profitable arbitrage opportunities along the way.

0_DXXUCAXxFJlqn44P.png
Existing Solutions
Uniswap Auto Router

Source: https://www.seaquake.io/blog/smart-order-routing-the-force-behind-a-balanced-market/

Existing Solutions

 

There are a number of existing approaches toward SOR in crypto offered by various groups, in this section, we will examine a few of them.

Uniswap Auto Router

Uniswap’s automated liquidity protocol allows users to swap one asset for another. To facilitate this, they have implemented an auto-router that makes a series of swaps across multiple pairs (the ‘route’) to attain the desired asset in the event of a trader creating an order for an asset that is not included in the pair pool they are trading through.

The route from one asset to another may consist of segments (points at which tokens are exchanged for other tokens along the way) between pairs. Though each segment will contribute to the overall cost of the trade in the form of gas fees.

In the event of a large order being placed or low liquidity in the pair pool, routes may also consist of multiple paths. By hedging routes, orders are protected against a singular point of loss to slippage. Introducing multiple paths will also increase the gas fees required to complete the trade as each path adds at least one additional transaction.

0_Ybpb6IJmLBPoYXcZ.png

              Source: https://medium.com/@ValveFinance/building-blocks-for-dex-router-construction-analysis-acc03b9f15d8

Routes can be split across up to seven routes, which enables a trader to take advantage of the liquidity depth of different pools. The following diagram depicts a scenario where a trader saves $134,689 on account of the Auto Router’s splitting function.

0_W0NIrQLYYk1d3WDE.png

The Auto Router also factors gas fees into its calculations for cost efficiency — small trades will execute with minimal hops to reduce the computations which incur gas fees. This can be seen in the following example where the net cost is reduced by 10% by not making an additional trade along the route.

0_NluPlCC_R4YxG4h9.png
Uniswap V2 and V3 Support
Layer 2 Network Support
Gas Aware Route Splitting Across V2 and V3
Real-Time Gas Estimates
Smarter Auto Slippage
SwapRouter02.sol Contract
Swap and Add
Oracle Slippage
SFOX Smart Routing and Net-Price Routing

The Auto Router comes complete with a user interface through which a trader can view the path of their trade before execution.

In an analysis of 4,429 on-chain swaps from June 1, 2021, Uniswap claims the Auto Router improved pricing on 13.97% of all trades and 36.84% on trades between the top ten tokens by TVL.

As of mid-December, 2021, Uniswap updated its Auto Router by introducing the following improvements:

 

Uniswap V2 and V3 Support

The Auto Router now further improves trade prices by routing trades across all Uniswap pools, including V2 and V3 pools, while optimizing gas fees in the process — meaning that traders need not consider which version of Uniswap they are using.

Layer 2 Network Support

All Uniswap Layer 2 networks are now supported by the Auto Router, this includes Optimistic Ethereum and Arbitrum. The intent is to combine the trade pricing efficiency of the Auto Router with the low gas fees associated with Layer 2 networks.

Gas Aware Route Splitting Across V2 and V3

Trades can now be split across both V2 and V3 liquidity pools to find the best price across both, improvements brought about by the introduction of a SwapRouter smart contract. To save gas fees, old router prices are compared against new router prices, and the lower of the two is selected.

 

Real-Time Gas Estimates

Gas fees may now be estimated via the application’s interface, allowing traders to further gauge the profitability of a swap.

Smarter Auto Slippage

Gas estimates are incorporated into slippage calculations to automatically set slippage based on the trader’s expected gas cost and trade size. For smaller trades, slippage tolerance is adjusted to improve the odds of a trade successfully executing, in order to avoid situations where the gas fees involved with failed trades require additional transactions — and thus, higher gas fees.

In this update, Uniswap also introduced a number of developer tools:

Auto Router npm Package

The Auto Router has been packaged onto the npm registry, allowing developers to quickly and easily import it into their projects.

SwapRouter02.sol Contract

The SwapRouter contract is fully open source and can be viewed on the project’s public GitHub repository.

Swap and Add

The router contract now offers atomic swap-and-add functionality, allowing liquidity provision of only one of the assets in a pool, or any ratio of the two assets (as opposed to the traditional requirement of offering a set ratio of both assets in a pool as liquidity provision).

Oracle Slippage

Auto Router allows a limit to be set on the relative price impact of trades as measured against V3 oracles, rather than only measuring against on-chain liquidity. For example: “a user swapping ETH for USDC could specify that their swap can only execute within one percentage point of the five-minute ETH/USDC time-weighted average price”.

SFOX Smart Routing and Net-Price Routing
0_hebBjhhDB9gwOHAe.png
1Inch Pathfinder

SFOX approaches SOR with its Smart Routing algorithm, which handles orders in such a way that aims to take the best opportunity throughout a range of different trading venues.

In 2020, SFOX improved upon its Smart Routing solution by introducing its new Net-Price Routing (NPR), which touts a supposed 28% average saving on trading fees. NPR aims to address issues relating to uneven fees in crypto, and the fact that many smart order routers calculate using the gross prices of assets. NPR finds the route to execute a trade that yields the best net price, factoring fees in.

 

1Inch Pathfinder
0__6WpUTPdyVUzozHD.png
Balancer Smart Order Router

Source: https://blog.1inch.io/introducing-1inch-v2-defis-fastest-and-most-advanced-aggregation-protocol-c42573dc3f85

1inch’s Pathfinder API includes a price discovery and routing algorithm which they use for identifying optimal paths for token swaps across liquidity pools over a number of exchanges. In particular, it is designed to take advantage of market depth to bridge between source and destination tokens when performing swaps, while considering other factors such as gas fees.

 

“Thanks to improved quotes, 1inch offers, for instance, a rate for 1 sBTC-sUSD that’s better than Uniswap’s offer by almost 98%.”

Balancer Smart Order Router

Balancer’s SOR solution is an “off-chain linear optimization of routing orders across pools for best price execution”. It takes an amount of input tokens and desired tokens to trade them for, then returns a list of pools and amounts that should be traded to maximize the amount of returned tokens. The tool is available for free as an npm package for use by developers who aim to implement order routing across pools.

Balancer sees SOR as an optimization problem where the aim is to find the path through a set of Balancer Pools with the highest net yield after gas costs. The algorithm adds pools to a trading set until there are no pools left where the net gain from trading with the pool would exceed the gas cost.

The current Balancer SOR runs off-chain but is EVM-tractable by design in preparation for a future on-chain release. In order to achieve EVM-tractability, the function used to calculate the spot price of a Balancer pool has been linearized — this is also useful in adjusting for the post-trade spot price change and slippage.

We can expect the price to increase after trading due to slippage, the larger the trade, the larger the resultant change in the spot price. This linearized approximation of the spot price is exactly that, an estimate, the following diagram shows the real (non-linear) spot price after a trade compared with the linearized approximation.

0_xV_2x71WgAeM0FfE.png

As the spot prices of all Balancer Pools are linearized, Balancer interpolates prices and amounts to simplify their optimization problem. Imagine three Balancer Pools containing the token which is being traded, the estimated prices (EP) interest are defined as sets of prices under the following conditions:

  1. There is a pool with exactly that initial spot price

 

OR

  1. The spot prices of two pools ‘cross’ at that price

0_G_m1GDviEVKWfylB.png

An Example SOR Optimization Problem

For the following walkthrough, Balancer considers only Pools 1 and 2 for their explanation.

 

“Let A be the amount of token in (Ai) to be traded on pool 1 such that its price increases from EP1 (initial spot price of pool 1) to EP2 (initial spot price of pool 2). The SOR’s solution for any amount of tokens lower than A is simply: “trade the entire amount with pool 1.”

When the amount traded is greater than A, SOR will start including pool 2 in the solution, as not doing so would mean the trader is trading some amount (Ai — A) for a higher price than they could with pool 2.

The solution for trading an amount B + C can be found by interpolating the trades that result in EP2 and EP3. By trading C with pool 1 and B with pool 2, both pools end at the same price (Final Price) which means that the best solution was found.”

0_flUTdrJ2cNisciIl.png
References

Essentially, the algorithm predicts spot prices in pools as a function of the amount to be traded, identifies price points of interest, and aims to find swaps with pools that yield greater returns depending on the amount the trader actually trades.

References

  1. “Introducing the Auto Router”, Uniswap Protocol, Sep. 16, 2021. https://uniswap.org/blog/auto-router (accessed Sep. 19, 2022).

  2. “Auto Router V2”, Uniswap Protocol, Dec. 16, 2021. https://uniswap.org/blog/auto-router-v2 (accessed Sep. 19, 2022).

  3. “Smart Order Router.” https://balancer.gitbook.io/balancer/smart-contracts/sor (accessed Sep. 19, 2022).

  4. “Better Prices, Lower Fees: Announcing Net-Price Routing by SFOX”, SFOX, Feb. 05, 2020. https://www.sfox.com/blog/sfox-pricing-crypto-trading-net-price-routing/ (accessed Sep. 19, 2022).

  5. “Introducing 1inch v2 — DeFi’s fastest and most advanced aggregation protocol”, 1inch Network, Nov. 6, 2020, https://blog.1inch.io/introducing-1inch-v2-defis-fastest-and-most-advanced-aggregation-protocol-c42573dc3f85 (accessed Sep. 19, 2022).

  6. “Slippage”, Investopedia, May. 10, 2021, https://www.investopedia.com/terms/s/slippage.asp (accessed Sep. 19, 2022).

  7. “Slippage”, Babypips, https://www.babypips.com/forexpedia/slippage (accessed Sep. 19, 2022).

  8. “Smart Order Routing (SOR)”, Babypips, https://www.babypips.com/forexpedia/sor (accessed Sep. 19, 2022).

  9. “Understanding the XYK model of pooled liquidity”, Ryan Tian, Jan. 2, 2020, https://medium.com/phoenix-finance/understanding-the-xyk-model-of-pooled-liquidity-7340fdc20d9c (accessed Sep. 19, 2022).

  10. “Smart Order Routing — the Force behind a Balanced Market”, Seauake, Jul. 7, 2020, https://www.seaquake.io/blog/smart-order-routing-the-force-behind-a-balanced-market/ (accessed Sep. 19, 2022).

bottom of page