sequence-algo-sdk
Write ultra-low-latency trading algos in Rust, compile to WASM, deploy to Sequence Markets. Zero dependencies. The SDK provides two algo traits — single-venue and multi-venue — along with order book types, action buffers, and everything you need to build trading algorithms that run on Sequence's edge infrastructure.
Quick Start
# Cargo.toml
[]
= "my-algo"
= "0.1.0"
= "2021"
[]
= ["cdylib"]
[]
= { = "0.3", = false }
[]
= "z"
= true
= 1
= "abort"
Single-Venue Algo
Receives one venue's order book. Good for market making, momentum, mean reversion on a single exchange.
// src/lib.rs
extern crate alloc;
use *;
export_algo!;
!
Multi-Venue Algo
Receives an NbboSnapshot with the best bid/ask across all connected venues (up to 16 CEX + DEX), plus VenueBooks with full 20-level depth per venue. Use it for cross-venue arbitrage, multi-venue market making, or best-execution strategies.
// src/lib.rs
extern crate alloc;
use *;
export_multi_venue_algo!;
!
Install the CLI
macOS / Linux
|
This detects your platform, downloads the latest sequence binary, verifies the SHA-256 checksum, and installs to ~/.local/bin.
Windows (PowerShell)
irm https://raw.githubusercontent.com/Bai-Funds/algo-sdk/main/install.ps1 | iex
Installs to ~/.sequence/bin. You may need to add this directory to your PATH.
Windows (Git Bash / WSL)
The shell installer also works in Git Bash, MSYS2, and WSL:
|
Manual Download
Download the archive for your platform from GitHub Releases (look for tags starting with cli/v):
| Platform | Archive |
|---|---|
| macOS (Apple Silicon) | sequence-aarch64-apple-darwin.tar.gz |
| macOS (Intel) | sequence-x86_64-apple-darwin.tar.gz |
| Linux (x86_64) | sequence-x86_64-unknown-linux-gnu.tar.gz |
| Linux (ARM64) | sequence-aarch64-unknown-linux-gnu.tar.gz |
| Windows (x86_64) | sequence-x86_64-pc-windows-msvc.zip |
Extract and place sequence (or sequence.exe on Windows) somewhere on your PATH.
Verify Installation
Update to Latest Version
This checks GitHub Releases for a newer cli/v* tag, downloads it, verifies the checksum, and replaces the current binary in-place. Works on macOS, Linux, and Windows.
Build & Deploy
# Single-venue algo
# Multi-venue algo
Or build manually:
Key Types
Core
| Type | Description |
|---|---|
Algo |
Single-venue trait — on_book, on_fill, on_reject, on_shutdown |
MultiVenueAlgo |
Multi-venue trait — on_nbbo, on_fill, on_reject, on_shutdown |
L2Book |
20-level order book (656 bytes, fits in L1 cache) |
AlgoState |
Position, open orders, session PnL, symbol metadata, and risk limits |
Actions |
Buffer for placing/canceling orders (up to 16 per tick) |
Fill |
Fill event with price, quantity, and timing |
Reject |
Rejection with typed error codes |
Multi-Venue
| Type | Description |
|---|---|
NbboSnapshot |
Cross-venue NBBO with per-venue BBO, staleness tracking, up to 16 venues |
VenueBooks |
Merged + per-venue 20-level depth books (~11KB) |
PoolBooks |
Per-pool depth from DEX liquidity pools (~23KB, up to 32 pools) |
PoolMeta |
Pool identity: address, chain, protocol, fee tier (40 bytes) |
MAX_VENUES |
Maximum venues in an NbboSnapshot (16) |
MAX_POOLS |
Maximum individual pool slots in a PoolBooks (32) |
Venue Reference
Every venue has a constant ID you can use for filtering, order routing, and depth lookups.
CEX Venues
| Constant | Value | Name | Description |
|---|---|---|---|
VENUE_KRAKEN |
1 | "kraken" |
Kraken exchange |
VENUE_COINBASE |
2 | "coinbase" |
Coinbase exchange |
VENUE_BINANCE |
3 | "binance" |
Binance exchange |
VENUE_BITGET |
4 | "bitget" |
Bitget exchange |
VENUE_CRYPTOCOM |
5 | "cryptocom" |
Crypto.com exchange |
VENUE_BITMART |
6 | "bitmart" |
BitMart exchange |
VENUE_OKX |
8 | "okx" |
OKX exchange |
VENUE_BYBIT |
9 | "bybit" |
Bybit exchange |
VENUE_UNKNOWN |
10 | "unknown" |
Unknown/unrecognized venue |
DEX Venues
| Constant | Value | Name | Chain | Supported Protocols |
|---|---|---|---|---|
VENUE_DEX |
7 | "dex" |
Aggregated | All chains combined |
VENUE_DEX_ETH |
11 | "dex-eth" |
Ethereum | Uniswap V2/V3, Curve, Balancer V2 |
VENUE_DEX_ARB |
12 | "dex-arb" |
Arbitrum | Uniswap V3, Camelot (Algebra V3) |
VENUE_DEX_BASE |
13 | "dex-base" |
Base | Uniswap V3, Aerodrome (Solidly) |
VENUE_DEX_OP |
14 | "dex-op" |
Optimism | Uniswap V3, Velodrome (Solidly) |
VENUE_DEX_POLY |
15 | "dex-poly" |
Polygon | Uniswap V2, Balancer V2, Curve |
VENUE_DEX_SOL |
16 | "dex-sol" |
Solana | Raydium CLMM, Orca Whirlpool (via Jupiter) |
Note: Value 10 is reserved. Per-chain DEX venues (11-16) provide chain-specific depth, while VENUE_DEX (7) aggregates across all chains.
Venue Helper Functions
// Classification
is_dex // true — any DEX venue (7, 11-16)
is_dex // true — Solana is a DEX venue
is_cex // true — any CEX venue (1-6, 8-9)
is_dex // false
// Name lookup
venue_name // "kraken"
venue_name // "dex-arb"
venue_name // "dex-sol"
venue_name // "unknown"
Filtering by Venue Type
VenueBooks
VenueBooks provides full 20-level depth from every connected venue, plus a merged cross-venue book. Available in on_nbbo() for multi-venue algos.
Fields
| Field | Type | Description |
|---|---|---|
merged |
L2Book |
Cross-venue merged depth (same-price levels aggregated) |
book_ct |
u8 |
Number of venues with depth data |
venue_ids |
[u8; 16] |
VenueId per slot (same order as NbboSnapshot) |
books |
[L2Book; 16] |
Per-venue L2Book per slot |
Methods
// Look up a specific venue's book by VenueId constant
books.book_for_venue // Direct slot access (faster, no scan)
books.book_at_slot .venue_id_at // Check if a venue has depth data
books.has_depth_for // Count by venue type
books.cex_count .dex_count
Using the Merged Book
The merged field aggregates depth from all venues. Same-price levels from different venues have their sizes summed. Levels are sorted (bids descending, asks ascending) and truncated to 20 per side.
// Use merged book for reference pricing
let mid = books.merged.mid_px_1e9;
let spread = books.merged.spread_bps;
// Total visible liquidity across all venues
let total_bid_depth: u64 =
.map
.sum;
PoolBooks
PoolBooks gives per-pool granularity for DEX liquidity. While VenueBooks gives per-chain depth (aggregated across all pools on a chain), PoolBooks breaks it down to individual Uniswap V3, Curve, Raydium, etc. pools. Available at WASM offset 0x13000, read on-demand during on_nbbo().
Fields
| Field | Type | Description |
|---|---|---|
pool_ct |
u8 |
Number of active pool slots (0..32) |
metas |
[PoolMeta; 32] |
Pool identity per slot |
books |
[L2Book; 32] |
Per-pool L2Book per slot |
PoolMeta
| Field | Type | Description |
|---|---|---|
address |
[u8; 32] |
Pool contract address (EVM: first 20 bytes, Solana: all 32) |
pair_index |
u16 |
For multi-asset pools like Curve (0 for standard 2-token pools) |
fee_bps |
u16 |
Pool fee in basis points (e.g., 5 = 0.05% for Uniswap V3) |
venue_id |
u8 |
Chain identifier (VENUE_DEX_ARB, VENUE_DEX_SOL, etc.) |
protocol_id |
u8 |
Protocol: 1=uniswap_v2, 2=uniswap_v3, 3=curve, 4=balancer_v2, 5=aerodrome, 6=velodrome, 7=camelot, 8=raydium_clmm, 9=orca_whirlpool |
Methods
// Look up a pool by contract address and pair index
pool_books.book_for_pool // Direct slot access (faster, no scan)
pool_books.book_at_slot .meta_at_slot
Example: Pool-Level Analysis
NbboSnapshot
The NbboSnapshot is the core data structure for multi-venue algos. It contains:
- Global NBBO: Best bid/ask price and size across all venues
- Venue identification: Which venue has the best bid, which has the best ask
- Per-venue BBO: Individual bid/ask for each connected venue (struct-of-arrays layout for cache efficiency)
- Staleness tracking: Milliseconds since last update per venue — reject stale data before trading
Important index convention:
nbbo_bid_venue/nbbo_ask_venueare array slot indices (0..venue_ct-1), not VenueIds- Use
venue_ids[nbbo_bid_venue]to get the actual VenueId for order routing Action.venue_idtakes the VenueId value, not the array slot
Helper methods:
nbbo_spread_bps()— spread in basis points (negative = crossed market)is_crossed()— true when best bid > best ask (arb opportunity)is_venue_stale(slot, max_ms)— true when venue data is older thanmax_msslot_for_venue(venue_id)— find the array slot for a given VenueIdbest_bid_venue_id()— the VenueId (not slot) of the best bid venuebest_ask_venue_id()— the VenueId (not slot) of the best ask venue
Order Routing
Single-venue algos use actions.buy() / actions.sell() — orders go to the algo's assigned venue.
Multi-venue algos use venue-targeted methods:
actions.buy_on; // Limit buy on specific venue
actions.sell_on; // Limit sell on specific venue
actions.ioc_buy_on; // IOC buy on specific venue
actions.ioc_sell_on; // IOC sell on specific venue
Set venue_id = 0 for default venue routing (same as single-venue behavior).
DEX Depth: How It Works
DEX order books are synthetic — constructed by probing AMM liquidity pools, not from a traditional order book. Understanding the methodology helps you use DEX depth data correctly.
Construction
DEX depth is built by simulating swaps at 16 discrete USD sizes:
$10, $25, $50, $100, $250, $500, $750, $1K, $2.5K, $5K, $10K, $25K, $50K, $100K, $250K, $500K
Each probe returns an effective price at that size, which is converted into a bid/ask level. The result is a 20-level L2Book that approximates the continuous AMM curve as discrete levels.
Gas Adjustment
All DEX prices are gas-adjusted: they include estimated swap gas cost at the current base fee. This means the prices you see already account for execution costs — a bid of $1000 on DEX means $1000 after gas.
Routing
- Below $10K: Single best-pool routing (lowest-impact single pool)
- Above $10K: Greedy multi-pool split to reduce price impact across multiple pools
Accuracy
DEX depth is validated every 10th emission cycle:
- Suppressed if the $100 probe has >25bps error vs. on-chain simulation
- Suppressed if the $10K probe has >75bps error
- When suppressed, the last valid book is held (no stale data pushed)
Staleness
DEX data updates more slowly than CEX data. The venue_update_ms field in NbboSnapshot tracks freshness:
- CEX staleness threshold: ~10% outlier tolerance
- DEX staleness threshold: ~25% outlier tolerance (wider due to block times)
Use nbbo.is_venue_stale(slot, max_ms) to gate on freshness before trading DEX venues.
DEX vs CEX in Your Algo
Fixed-Point Format
All prices and quantities use fixed-point integers for deterministic, allocation-free arithmetic:
- Prices:
px_1e9— multiply by 10^9 (e.g., $50,000.00 =50_000_000_000_000) - Quantities:
qty_1e8— multiply by 10^8 (e.g., 1.0 BTC =100_000_000)
Examples
See examples/ for complete working algos:
- mm-algo — Single-venue market maker with spread-based quoting
- arb-algo — Multi-venue arbitrage with crossed-market detection and staleness gating
- test-algo — Minimal algo for testing and validation
- speed-test-algo — Performance benchmarking
- xrp-5usd-mm — XRP-specific market maker
Migrating from v0.2
v0.3 adds per-venue depth alongside the existing merged 20-level L2Book.
Previously, multi-venue algos received a single merged L2Book aggregating all venues.
Now VenueBooks gives access to both the merged view and individual venue order books.
v0.3 changes the MultiVenueAlgo trait signature:
// v0.2
;
// v0.3
;
The old merged L2Book is still available as books.merged. To migrate:
- Change
book: &L2Booktobooks: &VenueBooksin youron_nbbosignature - Replace any
book.references withbooks.merged.(e.g.,book.mid_px_1e9()becomesbooks.merged.mid_px_1e9()) - Optionally, start using per-venue depth via
books.book_for_venue(VENUE_*)for smarter sizing
Pre-compiled v0.2 WASM binaries continue to work without recompilation.
License
Licensed under either of Apache License, Version 2.0 or MIT License at your option.