View Source Hyperliquid.Orders (hyperliquid v0.1.6)

Provides helper methods for order creation and management.

This module offers a set of functions to facilitate various order operations, including:

  • Retrieving and calculating mid-prices
  • Creating market and limit orders
  • Handling order types and triggers
  • Managing position closures

Key features:

  • Mid-price retrieval with caching mechanism
  • Slippage price calculation for market orders
  • Support for various order types (GTC, IOC, ALO)
  • Market buy and sell order creation
  • Limit order creation with customizable time-in-force
  • Position closing functionality

The module interacts with the Hyperliquid API and cache to ensure efficient and accurate order processing. It handles both perpetual and spot markets, and provides flexibility in order parameters such as size, price, and slippage.

Usage examples:

# Retrieve mid-price for a coin mid_price = Hyperliquid.Orders.get_midprice("SOL") 135.545

# Place a market buy order Hyperliquid.Orders.market_buy("ETH", 1.0, "0x123...") {:ok,

%{
  "response" => %{
    "data" => %{
      "statuses" => [
        %{"filled" => %{"avgPx" => "128.03", "oid" => 17114311614, "totalSz" => "1.0"}}
      ]
    },
    "type" => "order"
  },
  "status" => "ok"
}}

# Place a limit sell order Hyperliquid.Orders.limit_order("BTC", 0.5, false, 50000, "gtc", false, "0x123...") {:ok,

%{
  "response" => %{
    "data" => %{"statuses" => [%{"resting" => %{"oid" => 10030901240}}]},
    "type" => "order"
  },
  "status" => "ok"
}}

# Close list of positions {:ok, %{"assetPositions" => positions}} = Info.clearinghouse_state("0x123") Hyperliquid.Orders.market_close(positions)

# Close single position positions |> Enum.at(0) |> Hyperliquid.Orders.market_close()

# Close all positions for an address Hyperliquid.Orders.market_close("0x123...") [

ok: %{
  "response" => %{
    "data" => %{
      "statuses" => [
        %{"filled" => %{"avgPx" => "148.07", "oid" => 10934427319, "totalSz" => "1.0"}}
      ]
    },
    "type" => "order"
  },
  "status" => "ok"
}

]

Summary

Functions

Link to this function

limit_order(coin, sz, buy?, px, tif \\ "gtc", reduce? \\ false, vault_address \\ nil)

View Source
Link to this function

market_buy(coin, sz, vault_address \\ nil)

View Source
Link to this function

market_close(position, slippage \\ 0.05, vault_address \\ nil)

View Source
Link to this function

market_order(coin, sz, buy?, reduce?, vault_address \\ nil, px \\ nil, slippage \\ 0.05)

View Source
Link to this function

market_sell(coin, sz, vault_address \\ nil)

View Source
Link to this function

slippage_price(coin, buy?, slippage \\ 0.05, px \\ nil)

View Source
Link to this function

trigger_from_order_type(type)

View Source