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.
Multi-Leg Execution
Spread trades, hedged positions, and arbitrage strategies require multiple orders that must succeed or fail together. Horizon’s multi-leg module provides atomic execution with three fill policies, aggregate risk checks, and cross-exchange leg support. Type validation is in Rust; execution orchestration is in Python.
Overview
Fill Policies All-or-none, best-effort, and delta-neutral execution modes.
Aggregate Risk hz.aggregate_leg_risk() sums notional across all legs before submission.
Validation hz.validate_legs() checks legs for completeness before execution.
Cross-Exchange Each leg can target a different exchange.
Core Types
FillPolicy
Policy Behavior AllOrNoneIf any leg fails, cancel all previously submitted legs BestEffortSubmit all legs, continue on individual failures DeltaNeutralIf one side fails, cancel the paired leg
Leg
import horizon as hz
leg = hz.Leg(
market_id = "will-btc-hit-100k" ,
side = "buy" ,
size = 100.0 ,
price = 0.55 ,
exchange = "polymarket" ,
)
Field Type Description market_idstrTarget market sidestr"buy" or "sell"sizefloatOrder size pricefloatLimit price exchangestrTarget exchange
Core Functions
hz.validate_legs
Check legs for completeness (non-empty market_id, positive size).
error = hz.validate_legs(legs)
if error:
print ( f "Invalid: { error } " )
hz.aggregate_leg_risk
Sum risk metrics across all legs.
total_notional, max_single_leg, num_exchanges = hz.aggregate_leg_risk(legs)
print ( f "Total notional: $ { total_notional :.2f} " )
print ( f "Max single leg: $ { max_single_leg :.2f} " )
print ( f "Exchanges: { num_exchanges } " )
hz.build_mock_result
Build a mock execution result for backtesting.
result = hz.build_mock_result(legs, hz.FillPolicy.AllOrNone, True )
print ( f "Success: { result.success } " )
print ( f "Filled: { result.total_filled } / { result.total_legs } " )
Pipeline Function
hz.multi_leg
Execute multiple legs atomically each cycle.
ml = hz.multi_leg(
legs_config = [
{ "market_id" : "btc_100k" , "side" : "buy" , "size" : 50 , "price" : 0.55 , "exchange" : "polymarket" },
{ "market_id" : "TLT" , "side" : "sell" , "size" : 10 , "price" : 95.0 , "exchange" : "alpaca" },
],
policy = "all_or_none" ,
)
Parameter Type Default Description legs_configlist[dict]NoneLeg configurations (or read from ctx.params["legs"]) policystr"best_effort"all_or_none, best_effort, or delta_neutral
Returns each cycle:
{
"success" : True ,
"legs" : [
{ "index" : 0 , "market" : "btc_100k" , "status" : "submitted" , "order_id" : "p1" },
{ "index" : 1 , "market" : "TLT" , "status" : "submitted" , "order_id" : "p2" },
],
"total_notional" : 977.5 ,
"policy" : "all_or_none" ,
}
Examples
Hedged Entry
import horizon as hz
hz.run(
name = "hedged_entry" ,
exchange = [hz.Polymarket(), hz.Alpaca( paper = True )],
feeds = {
"btc" : hz.PolymarketBook( "will-btc-hit-100k" ),
"tlt" : hz.AlpacaFeed( symbols = [ "TLT" ]),
},
pipeline = [
hz.multi_leg(
legs_config = [
{ "market_id" : "will-btc-hit-100k" , "side" : "buy" , "size" : 100 , "price" : 0.55 , "exchange" : "polymarket" },
{ "market_id" : "TLT" , "side" : "sell" , "size" : 5 , "price" : 95.0 , "exchange" : "alpaca" },
],
policy = "all_or_none" ,
),
],
)
Dynamic Legs from Pipeline
import horizon as hz
def compute_legs ( ctx ):
if ctx.feed is None :
return { "legs" : []}
return {
"legs" : [
{ "market_id" : "btc_100k" , "side" : "buy" , "size" : 50 , "price" : ctx.feed.price},
{ "market_id" : "btc_150k" , "side" : "sell" , "size" : 50 , "price" : ctx.feed.price * 0.5 },
],
}
hz.run(
name = "dynamic_legs" ,
pipeline = [compute_legs, hz.multi_leg( policy = "delta_neutral" )],
)