View Source Hyperliquid.Utils.Format (hyperliquid v0.2.2)

Formatting utilities for Hyperliquid prices and sizes.

Implements Hyperliquid's tick and lot size rules:

  • Prices: Maximum 5 significant figures, max 6 (perp) or 8 (spot) - szDecimals decimals
  • Sizes: Truncated to szDecimals decimal places

See: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/tick-and-lot-size

Summary

Functions

Format price according to Hyperliquid rules.

Format size according to Hyperliquid rules.

Get the maximum allowed price decimals for an asset.

Truncate to a certain number of decimal places.

Truncate to a certain number of significant figures.

Functions

Link to this function

format_price(price, sz_decimals, opts \\ [])

View Source

Format price according to Hyperliquid rules.

Rules:

  • Maximum 5 significant figures
  • Maximum 6 (for perp) or 8 (for spot) - szDecimals decimal places
  • Integer prices are always allowed regardless of significant figures

Parameters

  • price: Price as string, integer, or float
  • sz_decimals: Size decimals for the asset
  • opts: Options
    • :perp - true for perpetual (default), false for spot

Examples

iex> Format.format_price("50000.123456", 5)
"50000"

iex> Format.format_price("0.0000123456789", 0, perp: false)
"0.00001234"

iex> Format.format_price(50000, 5)
"50000"
Link to this function

format_size(size, sz_decimals)

View Source

Format size according to Hyperliquid rules.

Rules:

  • Truncate decimal places to szDecimals

Parameters

  • size: Size as string, integer, or float
  • sz_decimals: Size decimals for the asset

Examples

iex> Format.format_size("1.23456789", 5)
"1.23456"

iex> Format.format_size(0.001, 3)
"0.001"
Link to this function

max_price_decimals(asset_id, sz_decimals)

View Source

Get the maximum allowed price decimals for an asset.

Parameters

  • asset_id: Asset index
  • sz_decimals: Size decimals for the asset

Returns

Maximum decimal places allowed for price

Link to this function

to_fixed_truncate(value, decimals)

View Source

Truncate to a certain number of decimal places.

Examples

iex> Format.to_fixed_truncate("1.23456789", 5)
"1.23456"

iex> Format.to_fixed_truncate("100.999", 2)
"100.99"
Link to this function

to_precision_truncate(value, precision)

View Source

Truncate to a certain number of significant figures.

Examples

iex> Format.to_precision_truncate("123456", 5)
"123450"

iex> Format.to_precision_truncate("0.00012345", 3)
"0.000123"