Documentation Index Fetch the complete documentation index at: https://mathematicalcompany.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Cross-Venue Liquidity
The same prediction can trade on Polymarket, Kalshi, and other platforms at different prices. Horizon’s liquidity aggregation module merges orderbooks from multiple venues into a unified view, routes orders to the best venue, and enables cross-venue market making. All aggregation math is in Rust.
Overview
Book Aggregation hz.aggregate_books() merges bid/ask levels from multiple venues.
Smart Routing hz.smart_route() picks the best venue for a given order.
Best Venue hz.best_venue() returns the venue with the best price for a side.
Cross-Venue MM hz.cross_venue_mm() market-makes using the aggregated book.
Core Functions
hz.aggregate_books
Merge bid/ask levels from multiple venues into a unified orderbook.
import horizon as hz
book = hz.aggregate_books(
bids = [( "polymarket" , 0.55 , 1000 ), ( "kalshi" , 0.54 , 500 )],
asks = [( "polymarket" , 0.57 , 800 ), ( "kalshi" , 0.56 , 600 )],
)
print ( f "Best bid: { book.best_bid } | Best ask: { book.best_ask } " )
print ( f "Mid: { book.mid_price :.4f} " )
print ( f "Bid depth: { book.total_bid_size } | Ask depth: { book.total_ask_size } " )
Parameter Type Description bidslist[tuple[str, float, float]](venue, price, size) tuples askslist[tuple[str, float, float]](venue, price, size) tuples
Returns an AggregatedBook with fields: bids, asks, best_bid, best_ask, mid_price, total_bid_size, total_ask_size.
hz.smart_route
Route an order to the venue(s) offering best execution.
decision = hz.smart_route(book, side = "buy" , size = 500.0 )
print ( f "Route to: { decision.venue } at { decision.price } " )
print ( f "Expected cost: { decision.expected_cost :.2f} " )
for fill in decision.fills_across_venues:
print ( f " { fill[ 0 ] } : { fill[ 2 ] } @ { fill[ 1 ] } " )
Parameter Type Description bookAggregatedBookAggregated orderbook sidestr"buy" or "sell"sizefloatOrder size
hz.best_venue
Return the venue with the best price for a given side.
venue, price = hz.best_venue(book, "buy" )
print ( f "Best venue to buy: { venue } at { price } " )
hz.merged_mid_price
Compute the size-weighted mid price across venues.
mid = hz.merged_mid_price(book)
Pipeline Functions
hz.aggregate_book
Aggregate orderbooks from multiple venue feeds each cycle.
agg = hz.aggregate_book(
venue_feeds = {
"polymarket" : { "bid_feed" : "poly_book" },
"kalshi" : { "bid_feed" : "kalshi_book" },
},
)
Parameter Type Description venue_feedsdict[str, dict[str, str]]venue to {"bid_feed": feed_name} mapping
Returns each cycle:
{
"best_bid" : 0.55 ,
"best_ask" : 0.57 ,
"mid_price" : 0.56 ,
"spread" : 0.02 ,
"bid_depth" : 1500.0 ,
"ask_depth" : 1400.0 ,
}
hz.smart_route_pipeline
Smart order routing each cycle.
router = hz.smart_route_pipeline(
venue_feeds = { ... },
default_size = 100.0 ,
)
hz.cross_venue_mm
Market-make using the aggregated book. Quotes around the merged mid price.
mm = hz.cross_venue_mm(
venue_feeds = {
"polymarket" : { "bid_feed" : "poly_book" },
"kalshi" : { "bid_feed" : "kalshi_book" },
},
spread_markup = 0.01 ,
size = 50.0 ,
)
Parameter Type Default Description venue_feedsdict[str, dict]required Venue feed configuration spread_markupfloat0.01Spread around mid sizefloat50.0Quote size per side
Examples
Cross-Venue Arbitrage
import horizon as hz
def arb_strategy ( ctx ):
best_bid = ctx.params.get( "best_bid" , 0 )
best_ask = ctx.params.get( "best_ask" , 0 )
spread = best_ask - best_bid
if spread < 0 : # crossed book = arb
return [
hz.order( side = "buy" , price = best_ask, size = 50 ),
hz.order( side = "sell" , price = best_bid, size = 50 ),
]
return []
hz.run(
name = "cross_venue_arb" ,
exchange = [hz.Polymarket(), hz.Kalshi()],
feeds = {
"poly_book" : hz.PolymarketBook( "will-btc-hit-100k" ),
"kalshi_book" : hz.KalshiBook( "KXBTC-25DEC31" ),
},
pipeline = [
hz.aggregate_book( venue_feeds = {
"polymarket" : { "bid_feed" : "poly_book" },
"kalshi" : { "bid_feed" : "kalshi_book" },
}),
arb_strategy,
],
)