Expand description
Parsing utilities that convert Hyperliquid payloads into Nautilus domain models.
§Conditional Order Support
This module implements comprehensive conditional order support for Hyperliquid, following patterns established in the OKX, Bybit, and BitMEX adapters.
§Supported Order Types
§Standard Orders
- Market: Implemented as IOC (Immediate-or-Cancel) limit orders
- Limit: Standard limit orders with GTC/IOC/ALO time-in-force
§Conditional/Trigger Orders
- StopMarket: Protective stop that triggers at specified price and executes at market
- StopLimit: Protective stop that triggers at specified price and executes at limit
- MarketIfTouched: Profit-taking/entry order that triggers and executes at market
- LimitIfTouched: Profit-taking/entry order that triggers and executes at limit
§Order Semantics
§Stop Orders (StopMarket/StopLimit)
- Used for protective stops and risk management
- Mapped to Hyperliquid’s trigger orders with
tpsl: Sl
- Trigger when price reaches the stop level
- Execute immediately (market) or at limit price
§If Touched Orders (MarketIfTouched/LimitIfTouched)
- Used for profit-taking or entry orders
- Mapped to Hyperliquid’s trigger orders with
tpsl: Tp
- Trigger when price reaches the target level
- Execute immediately (market) or at limit price
§Trigger Price Logic
The tpsl
field (Take Profit / Stop Loss) is determined by:
- Order Type: Stop orders → SL, If Touched orders → TP
- Price Relationship (if available):
- For BUY orders: trigger above market → SL, below → TP
- For SELL orders: trigger below market → SL, above → TP
§Trigger Type Support
Currently, Hyperliquid uses last traded price for all trigger evaluations.
Future enhancement: Add support for mark/index price triggers if Hyperliquid API adds this feature.
See OKX’s OKXTriggerType
and Bybit’s BybitTriggerType
for reference implementations.
§Examples
§Stop Loss Order
ⓘ
// Long position at $100, stop loss at $95
let order = StopMarket {
side: Sell,
trigger_price: $95,
// ... other fields
};
// Maps to: Trigger { is_market: true, trigger_px: $95, tpsl: Sl }
§Take Profit Order
ⓘ
// Long position at $100, take profit at $110
let order = MarketIfTouched {
side: Sell,
trigger_price: $110,
// ... other fields
};
// Maps to: Trigger { is_market: true, trigger_px: $110, tpsl: Tp }
§Integration with Other Adapters
This implementation reuses patterns from:
- OKX: Conditional order types and algo order API structure
- Bybit: TP/SL mode detection and trigger direction logic
- BitMEX: Stop order handling and trigger price validation
See:
crates/adapters/okx/src/common/consts.rs
- OKX_CONDITIONAL_ORDER_TYPEScrates/adapters/bybit/src/common/enums.rs
- BybitStopOrderType, BybitTriggerTypecrates/adapters/bitmex/src/execution/mod.rs
- trigger_price handling
Functions§
- cancel_
requests_ to_ hyperliquid_ action_ value - Creates a JSON value representing cancel requests for the Hyperliquid exchange action.
- client_
order_ id_ to_ cancel_ request - Converts a client order ID to a Hyperliquid cancel request.
- deserialize_
decimal_ from_ str - Deserializes decimal from string only (reject numbers to avoid precision loss).
- deserialize_
optional_ decimal_ from_ str - Deserialize optional decimal from string
- deserialize_
vec_ decimal_ from_ str - Deserialize vector of decimals from strings
- ensure_
min_ notional - Ensure the notional value meets minimum requirements
- extract_
asset_ id_ from_ symbol - Extracts asset ID from instrument symbol.
- extract_
error_ message - Extracts error message from a Hyperliquid exchange response.
- format_
trailing_ stop_ info - Converts WebSocket trailing stop data to description string.
- is_
conditional_ order_ data - Determines if an order is a conditional/trigger order based on order data.
- is_
response_ successful - Checks if a Hyperliquid exchange response indicates success.
- normalize_
order - Complete normalization for an order including price, quantity, and notional validation
- normalize_
price - Normalize price to the specified number of decimal places
- normalize_
quantity - Normalize quantity to the specified number of decimal places
- order_
any_ to_ hyperliquid_ request - Converts an OrderAny into a Hyperliquid order request.
- order_
to_ hyperliquid_ request - Converts a Nautilus order into a Hyperliquid order request.
- orders_
to_ hyperliquid_ action_ value - Creates a JSON value representing multiple orders for the Hyperliquid exchange action.
- orders_
to_ hyperliquid_ requests - Converts a list of Nautilus orders into Hyperliquid order requests.
- parse_
account_ balances_ and_ margins - Parses Hyperliquid clearinghouse state into Nautilus account balances and margins.
- parse_
order_ status_ with_ trigger - Extracts order status from WebSocket order data.
- parse_
trigger_ order_ type - Parses trigger order type from Hyperliquid order data.
- parse_
trigger_ price - Parses trigger price from string to Decimal.
- round_
down_ to_ step - Round quantity down to the nearest valid step size
- round_
down_ to_ tick - Round price down to the nearest valid tick size
- serialize_
decimal_ as_ str - Serializes decimal as string (lossless, no scientific notation).
- serialize_
optional_ decimal_ as_ str - Serialize optional decimal as string
- serialize_
vec_ decimal_ as_ str - Serialize vector of decimals as strings
- time_
in_ force_ to_ hyperliquid_ tif - Converts a Nautilus
TimeInForce
to Hyperliquid TIF. - validate_
conditional_ order_ params - Validates conditional order parameters from WebSocket data.