Module parse

Module parse 

Source
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:

  1. Order Type: Stop orders → SL, If Touched orders → TP
  2. 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_TYPES
  • crates/adapters/bybit/src/common/enums.rs - BybitStopOrderType, BybitTriggerType
  • crates/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.