|
| 1 | +# hyperliquid-python |
| 2 | +Python SDK (sync and async) for Hyperliquid cryptocurrency exchange with Rest and WS capabilities. |
| 3 | + |
| 4 | +- You can check the SDK docs here: [SDK](https://docs.ccxt.com/#/exchanges/hyperliquid) |
| 5 | +- You can check Hyperliquid's docs here: [Docs](https://www.google.com/search?q=google+hyperliquid+cryptocurrency+exchange+api+docs) |
| 6 | +- Github repo: https://github.com/ccxt/hyperliquid-python |
| 7 | +- Pypi package: https://pypi.org/project/hyperliquid |
| 8 | + |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +``` |
| 13 | +pip install hyperliquid |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +### Sync |
| 19 | + |
| 20 | +```Python |
| 21 | +from hyperliquid import HyperliquidSync |
| 22 | + |
| 23 | +def main(): |
| 24 | + instance = HyperliquidSync({}) |
| 25 | + ob = instance.fetch_order_book("BTC/USDC:USDC") |
| 26 | + print(ob) |
| 27 | + # |
| 28 | + # balance = instance.fetch_balance() |
| 29 | + # order = instance.create_order("BTC/USDC:USDC", "limit", "buy", 1, 100000) |
| 30 | + |
| 31 | +main() |
| 32 | +``` |
| 33 | + |
| 34 | +### Async |
| 35 | + |
| 36 | +```Python |
| 37 | +import sys |
| 38 | +import asyncio |
| 39 | +from hyperliquid import HyperliquidAsync |
| 40 | + |
| 41 | +### on Windows, uncomment below: |
| 42 | +# if sys.platform == 'win32': |
| 43 | +# asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) |
| 44 | + |
| 45 | +async def main(): |
| 46 | + instance = HyperliquidAsync({}) |
| 47 | + ob = await instance.fetch_order_book("BTC/USDC:USDC") |
| 48 | + print(ob) |
| 49 | + # |
| 50 | + # balance = await instance.fetch_balance() |
| 51 | + # order = await instance.create_order("BTC/USDC:USDC", "limit", "buy", 1, 100000) |
| 52 | + |
| 53 | + # once you are done with the exchange |
| 54 | + await instance.close() |
| 55 | + |
| 56 | +asyncio.run(main()) |
| 57 | +``` |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +### Websockets |
| 62 | + |
| 63 | +```Python |
| 64 | +import sys |
| 65 | +from hyperliquid import HyperliquidWs |
| 66 | + |
| 67 | +### on Windows, uncomment below: |
| 68 | +# if sys.platform == 'win32': |
| 69 | +# asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) |
| 70 | + |
| 71 | +async def main(): |
| 72 | + instance = HyperliquidWs({}) |
| 73 | + while True: |
| 74 | + ob = await instance.watch_order_book("BTC/USDC:USDC") |
| 75 | + print(ob) |
| 76 | + # orders = await instance.watch_orders("BTC/USDC:USDC") |
| 77 | + |
| 78 | + # once you are done with the exchange |
| 79 | + await instance.close() |
| 80 | + |
| 81 | +asyncio.run(main()) |
| 82 | +``` |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +#### Raw call |
| 89 | + |
| 90 | +You can also construct custom requests to available "implicit" endpoints |
| 91 | + |
| 92 | +```Python |
| 93 | + request = { |
| 94 | + 'type': 'candleSnapshot', |
| 95 | + 'req': { |
| 96 | + 'coin': coin, |
| 97 | + 'interval': tf, |
| 98 | + 'startTime': since, |
| 99 | + 'endTime': until, |
| 100 | + }, |
| 101 | + } |
| 102 | + response = await instance.public_post_info(request) |
| 103 | +``` |
| 104 | + |
| 105 | + |
| 106 | +## Available methods |
| 107 | + |
| 108 | +### REST Unified |
| 109 | + |
| 110 | +- `create_order_request(self, symbol: Str, type: Str, side: Str, amount: str, price: Str = None, params: dict = {})` |
| 111 | +- `create_order(self, symbol: str, type: OrderType, side: OrderSide, amount: float, price: Num = None, params: dict = {})` |
| 112 | +- `create_orders_request(self, orders: list, params: dict = {})` |
| 113 | +- `create_orders(self, orders: list[OrderRequest], params: dict = {})` |
| 114 | +- `create_sub_account(self, name: str, params: dict = {})` |
| 115 | +- `create_twap_order(self, symbol: str, side: OrderSide, amount: float, duration: float, params: dict = {})` |
| 116 | +- `create_vault(self, name: str, description: str, initialUsd: int, params: dict = {})` |
| 117 | +- `fetch_balance(self, params: dict = {})` |
| 118 | +- `fetch_canceled_and_closed_orders(self, symbol: Str = None, since: Int = None, limit: Int = None, params: dict = {})` |
| 119 | +- `fetch_canceled_orders(self, symbol: Str = None, since: Int = None, limit: Int = None, params: dict = {})` |
| 120 | +- `fetch_closed_orders(self, symbol: Str = None, since: Int = None, limit: Int = None, params: dict = {})` |
| 121 | +- `fetch_currencies(self, params: dict = {})` |
| 122 | +- `fetch_deposits(self, code: Str = None, since: Int = None, limit: Int = None, params: dict = {})` |
| 123 | +- `fetch_funding_history(self, symbol: Str = None, since: Int = None, limit: Int = None, params: dict = {})` |
| 124 | +- `fetch_funding_rate_history(self, symbol: Str = None, since: Int = None, limit: Int = None, params: dict = {})` |
| 125 | +- `fetch_funding_rate(self, symbol: str, params: dict = {})` |
| 126 | +- `fetch_funding_rates(self, symbols: Strings = None, params: dict = {})` |
| 127 | +- `fetch_hip3_markets(self, params: dict = {})` |
| 128 | +- `fetch_ledger(self, code: Str = None, since: Int = None, limit: Int = None, params: dict = {})` |
| 129 | +- `fetch_markets(self, params: dict = {})` |
| 130 | +- `fetch_my_liquidations(self, symbol: Str = None, since: Int = None, limit: Int = None, params: dict = {})` |
| 131 | +- `fetch_my_trades(self, symbol: Str = None, since: Int = None, limit: Int = None, params: dict = {})` |
| 132 | +- `fetch_ohlcv(self, symbol: str, timeframe: str = '1m', since: Int = None, limit: Int = None, params: dict = {})` |
| 133 | +- `fetch_open_interest(self, symbol: str, params: dict = {})` |
| 134 | +- `fetch_open_interests(self, symbols: Strings = None, params: dict = {})` |
| 135 | +- `fetch_open_orders(self, symbol: Str = None, since: Int = None, limit: Int = None, params: dict = {})` |
| 136 | +- `fetch_order_book(self, symbol: str, limit: Int = None, params: dict = {})` |
| 137 | +- `fetch_order(self, id: str, symbol: Str = None, params: dict = {})` |
| 138 | +- `fetch_orders(self, symbol: Str = None, since: Int = None, limit: Int = None, params: dict = {})` |
| 139 | +- `fetch_position(self, symbol: str, params: dict = {})` |
| 140 | +- `fetch_positions(self, symbols: Strings = None, params: dict = {})` |
| 141 | +- `fetch_spot_markets(self, params: dict = {})` |
| 142 | +- `fetch_status(self, params: dict = {})` |
| 143 | +- `fetch_swap_markets(self, params: dict = {})` |
| 144 | +- `fetch_tickers(self, symbols: Strings = None, params: dict = {})` |
| 145 | +- `fetch_time(self, params: dict = {})` |
| 146 | +- `fetch_trades(self, symbol: str, since: Int = None, limit: Int = None, params: dict = {})` |
| 147 | +- `fetch_trading_fee(self, symbol: str, params: dict = {})` |
| 148 | +- `fetch_withdrawals(self, code: Str = None, since: Int = None, limit: Int = None, params: dict = {})` |
| 149 | +- `action_hash(self, action: object, vaultAddress: Str, nonce: object, expiresAfter: Int = None)` |
| 150 | +- `add_margin(self, symbol: str, amount: float, params: dict = {})` |
| 151 | +- `amount_to_precision(self, symbol: Str, amount: object)` |
| 152 | +- `approve_builder_fee(self, builder: str, maxFeeRate: str)` |
| 153 | +- `build_approve_builder_fee_sig(self, message: dict)` |
| 154 | +- `build_send_asset_sig(self, message: dict)` |
| 155 | +- `build_usd_class_send_sig(self, message: dict)` |
| 156 | +- `build_usd_send_sig(self, message: dict)` |
| 157 | +- `build_user_abstraction_sig(self, message: dict)` |
| 158 | +- `build_user_dex_abstraction_sig(self, message: dict)` |
| 159 | +- `build_withdraw_sig(self, message: dict)` |
| 160 | +- `calculate_price_precision(self, price: float, amountPrecision: int, maxDecimals: int)` |
| 161 | +- `calculate_rate_limiter_cost(self, api: list[str], method: str, path: str, params: object, config: dict = {})` |
| 162 | +- `cancel_all_orders_after(self, timeout: Int, params: dict = {})` |
| 163 | +- `cancel_order(self, id: str, symbol: Str = None, params: dict = {})` |
| 164 | +- `cancel_orders_for_symbols(self, orders: list[CancellationRequest], params: dict = {})` |
| 165 | +- `cancel_orders_request(self, ids: list[str], symbol: Str = None, params: dict = {})` |
| 166 | +- `cancel_orders(self, ids: list[str], symbol: Str = None, params: dict = {})` |
| 167 | +- `cancel_twap_order(self, id: str, symbol: Str = None, params: dict = {})` |
| 168 | +- `coin_to_market_id(self, coin: Str)` |
| 169 | +- `construct_phantom_agent(self, hash: object, isTestnet: bool = True)` |
| 170 | +- `describe(self)` |
| 171 | +- `edit_order(self, id: str, symbol: str, type: OrderType, side: OrderSide, amount: Num = None, price: Num = None, params: dict = {})` |
| 172 | +- `edit_orders_request(self, orders: list, params: dict = {})` |
| 173 | +- `edit_orders(self, orders: list[OrderRequest], params: dict = {})` |
| 174 | +- `enable_user_dex_abstraction(self, enabled: bool, params: dict = {})` |
| 175 | +- `extract_type_from_delta(self, data: list[dict] = [])` |
| 176 | +- `format_vault_address(self, address: Str = None)` |
| 177 | +- `get_dex_from_hip3_symbol(self, market: Market)` |
| 178 | +- `get_dex_from_symbols(self, methodName: str, symbols: Strings = None)` |
| 179 | +- `hash_message(self, message: object)` |
| 180 | +- `initialize_client(self)` |
| 181 | +- `is_unified_enabled(self, method: str, address: Str = None, shouldRefresh: bool = False, params: dict = {})` |
| 182 | +- `market(self, symbol: Str)` |
| 183 | +- `modify_margin_helper(self, symbol: str, amount: Num, type: str, params: dict = {})` |
| 184 | +- `nonce(self)` |
| 185 | +- `price_to_precision(self, symbol: Str, price: object)` |
| 186 | +- `reduce_margin(self, symbol: str, amount: float, params: dict = {})` |
| 187 | +- `reserve_request_weight(self, weight: Num, params: dict = {})` |
| 188 | +- `set_agent_abstraction(self, abstraction: str, params: dict = {})` |
| 189 | +- `set_leverage(self, leverage: int, symbol: Str = None, params: dict = {})` |
| 190 | +- `set_margin_mode(self, marginMode: str, symbol: Str = None, params: dict = {})` |
| 191 | +- `set_ref(self)` |
| 192 | +- `set_sandbox_mode(self, enabled: bool)` |
| 193 | +- `set_user_abstraction(self, abstraction: str, params: dict = {})` |
| 194 | +- `transfer(self, code: str, amount: float, fromAccount: str, toAccount: str, params: dict = {})` |
| 195 | +- `update_spot_currency_code(self, code: Str)` |
| 196 | +- `withdraw(self, code: str, amount: float, address: str, params: dict = {})` |
| 197 | + |
| 198 | +### REST Raw |
| 199 | + |
| 200 | +- `public_post_info(request)` |
| 201 | +- `private_post_exchange(request)` |
| 202 | + |
| 203 | +### WS Unified |
| 204 | + |
| 205 | +- `describe(self)` |
| 206 | +- `create_orders_ws(self, orders: list[OrderRequest], params: dict = {})` |
| 207 | +- `create_order_ws(self, symbol: str, type: OrderType, side: OrderSide, amount: float, price: Num = None, params: dict = {})` |
| 208 | +- `edit_order_ws(self, id: str, symbol: str, type: str, side: str, amount: Num = None, price: Num = None, params: dict = {})` |
| 209 | +- `cancel_orders_ws(self, ids: list[str], symbol: Str = None, params: dict = {})` |
| 210 | +- `cancel_order_ws(self, id: str, symbol: Str = None, params: dict = {})` |
| 211 | +- `watch_order_book(self, symbol: str, limit: Int = None, params: dict = {})` |
| 212 | +- `un_watch_order_book(self, symbol: str, params: dict = {})` |
| 213 | +- `watch_ticker(self, symbol: str, params: dict = {})` |
| 214 | +- `un_watch_ticker(self, symbol: str, params: dict = {})` |
| 215 | +- `watch_tickers(self, symbols: Strings = None, params: dict = {})` |
| 216 | +- `un_watch_tickers(self, symbols: Strings = None, params: dict = {})` |
| 217 | +- `watch_my_trades(self, symbol: Str = None, since: Int = None, limit: Int = None, params: dict = {})` |
| 218 | +- `watch_my_liquidations(self, symbol: str, since: Int = None, limit: Int = None, params: dict = {})` |
| 219 | +- `watch_my_liquidations_for_symbols(self, symbols: list[str], since: Int = None, limit: Int = None, params: dict = {})` |
| 220 | +- `un_watch_my_trades(self, symbol: Str = None, params: dict = {})` |
| 221 | +- `watch_trades(self, symbol: str, since: Int = None, limit: Int = None, params: dict = {})` |
| 222 | +- `un_watch_trades(self, symbol: str, params: dict = {})` |
| 223 | +- `watch_ohlcv(self, symbol: str, timeframe: str = '1m', since: Int = None, limit: Int = None, params: dict = {})` |
| 224 | +- `un_watch_ohlcv(self, symbol: str, timeframe: str = '1m', params: dict = {})` |
| 225 | +- `watch_balance(self, params: dict = {})` |
| 226 | +- `un_watch_balance(self, params: dict = {})` |
| 227 | +- `watch_positions(self, symbols: Strings = None, since: Int = None, limit: Int = None, params: dict = {})` |
| 228 | +- `set_positions_cache(self, client: Client, symbols: Strings = None)` |
| 229 | +- `un_watch_positions(self, symbols: Strings = None, params: dict = {})` |
| 230 | +- `watch_orders(self, symbol: Str = None, since: Int = None, limit: Int = None, params: dict = {})` |
| 231 | +- `un_watch_orders(self, symbol: Str = None, params: dict = {})` |
| 232 | +- `wait_for_pending_unsubscribe(self, url: str, subHash: str)` |
| 233 | +- `expire_pending_unsubscribe(self, client: Client, subHash: str, unsubHash: str)` |
| 234 | +- `wrap_as_post_action(self, request: dict)` |
| 235 | + |
| 236 | +## Contribution |
| 237 | +- Give us a star :star: |
| 238 | +- Fork and Clone! Awesome |
| 239 | +- Select existing issues or create a new issue. |
0 commit comments