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"
}
]