|
1 |
| -# python-huobi-client |
| 1 | +# Unofficial asynchronous python client for Huobi |
| 2 | + |
| 3 | +[](https://github.com/sometastycake/asynchuobi/actions/workflows/ci.yml) |
| 4 | +[]() |
| 6 | + |
| 7 | +## Official documentation |
| 8 | +[Documentation](https://huobiapi.github.io/docs/spot/v1/en/#change-log) |
| 9 | + |
| 10 | +## Install |
| 11 | + |
| 12 | +```bash |
| 13 | +pip install asynchuobi |
| 14 | +``` |
| 15 | + |
| 16 | +## Generic API |
| 17 | + |
| 18 | +```python |
| 19 | +from asynchuobi.api.clients.generic import GenericHuobiClient |
| 20 | + |
| 21 | + |
| 22 | +async def main(): |
| 23 | + async with GenericHuobiClient() as client: |
| 24 | + current_timestamp = await client.get_current_timestamp() |
| 25 | + trading_symbols = await client.get_all_supported_trading_symbols() |
| 26 | + system_status = await client.get_system_status() |
| 27 | +``` |
| 28 | + |
| 29 | +## Market API |
| 30 | + |
| 31 | +```python |
| 32 | +from asynchuobi.api.clients.market import MarketHuobiClient |
| 33 | +from asynchuobi.enums import CandleInterval |
| 34 | + |
| 35 | + |
| 36 | +async def main(): |
| 37 | + async with MarketHuobiClient() as client: |
| 38 | + candles = await client.get_candles('btcusdt', CandleInterval.min_1) |
| 39 | + orderbook = await client.get_market_depth('btcusdt') |
| 40 | + recent_trades = await client.get_most_recent_trades('btcusdt') |
| 41 | +``` |
| 42 | + |
| 43 | +## Subuser API |
| 44 | + |
| 45 | +```python |
| 46 | +from asynchuobi.api.clients.subuser import SubUserHuobiClient |
| 47 | +from asynchuobi.enums import ApiKeyPermission |
| 48 | + |
| 49 | + |
| 50 | +async def main(): |
| 51 | + async with SubUserHuobiClient( |
| 52 | + access_key='access_key', |
| 53 | + secret_key='secret_key', |
| 54 | + ) as client: |
| 55 | + subusers = await client.get_sub_users_list() |
| 56 | + for subuser in subusers['data']: |
| 57 | + keys = await client.api_key_query(subuser['uid']) |
| 58 | + status = await client.get_sub_user_status(subuser['uid']) |
| 59 | + balance = await client.get_account_balance_of_sub_user(subuser['uid']) |
| 60 | + api_key = await client.sub_user_api_key_creation( |
| 61 | + sub_uid=subuser['uid'], |
| 62 | + note='Test api', |
| 63 | + permissions=[ApiKeyPermission.readOnly, ApiKeyPermission.trade], |
| 64 | + ) |
| 65 | +``` |
| 66 | + |
| 67 | +## Wallet API |
| 68 | + |
| 69 | +```python |
| 70 | +from asynchuobi.api.clients.wallet import WalletHuobiClient |
| 71 | + |
| 72 | + |
| 73 | +async def main(): |
| 74 | + async with WalletHuobiClient( |
| 75 | + access_key='access_key', |
| 76 | + secret_key='secret_key', |
| 77 | + ) as client: |
| 78 | + deposit_address = await client.query_deposit_address('usdt') |
| 79 | + withdraw_address = await client.query_withdraw_address('usdt') |
| 80 | + withdraw_response = await client.create_withdraw_request( |
| 81 | + address='address', |
| 82 | + currency='usdt', |
| 83 | + amount=10 |
| 84 | + ) |
| 85 | +``` |
| 86 | + |
| 87 | +## Account API |
| 88 | + |
| 89 | +```python |
| 90 | +from asynchuobi.api.clients.account import AccountHuobiClient |
| 91 | +from asynchuobi.enums import AccountTypeCode, Sort |
| 92 | + |
| 93 | + |
| 94 | +async def main(): |
| 95 | + async with AccountHuobiClient( |
| 96 | + access_key='access_key', |
| 97 | + secret_key='secret_key', |
| 98 | + ) as client: |
| 99 | + accounts = await client.accounts() |
| 100 | + for account in accounts['data']: |
| 101 | + balances = await client.account_balance(account_id=account['id']) |
| 102 | + history = await client.get_account_history( |
| 103 | + account_id=account['id'], |
| 104 | + currency='usdt', |
| 105 | + transact_types=['deposit', 'withdraw'], |
| 106 | + sorting=Sort.desc, |
| 107 | + ) |
| 108 | + |
| 109 | + for account_type in (AccountTypeCode.spot, AccountTypeCode.flat): |
| 110 | + total_valuation = await client.get_total_valuation_of_platform_assets( |
| 111 | + account_type_code=account_type, |
| 112 | + valuation_currency='BTC', |
| 113 | + ) |
| 114 | +``` |
| 115 | + |
| 116 | +## Orders API |
| 117 | + |
| 118 | +```python |
| 119 | +from asynchuobi.api.clients.order import OrderHuobiClient |
| 120 | +from asynchuobi.enums import OrderType |
| 121 | + |
| 122 | +async def main(): |
| 123 | + async with OrderHuobiClient( |
| 124 | + access_key='access_key', |
| 125 | + secret_key='secret_key' |
| 126 | + ) as client: |
| 127 | + response = await client.new_order( |
| 128 | + account_id=account_id, |
| 129 | + symbol='dogeusdt', |
| 130 | + order_type=OrderType.buy_limit, |
| 131 | + amount=20, |
| 132 | + price=0.0660, |
| 133 | + ) |
| 134 | + if response['status'] == 'ok': |
| 135 | + order_id = response['data'] |
| 136 | + cancelling = await client.cancel_order( |
| 137 | + order_id=order_id, |
| 138 | + ) |
| 139 | + |
| 140 | + active_orders = await client.get_all_open_orders() |
| 141 | + order_detail = await client.get_order_detail_by_client_order_id( |
| 142 | + client_order_id=client_order_id, |
| 143 | + ) |
| 144 | +``` |
| 145 | + |
| 146 | +## Margin API |
| 147 | + |
| 148 | +```python |
| 149 | +from asynchuobi.api.clients.margin import MarginHuobiClient |
| 150 | + |
| 151 | + |
| 152 | +async def main(): |
| 153 | + async with MarginHuobiClient( |
| 154 | + access_key='access_key', |
| 155 | + secret_key='secret_key', |
| 156 | + ) as client: |
| 157 | + cross_margin_balance = await client.get_balance_of_cross_margin_account() |
| 158 | + isolated_margin_balance = await client.get_balance_of_isolated_margin_account( |
| 159 | + symbol='btcusdt', |
| 160 | + ) |
| 161 | +``` |
| 162 | + |
| 163 | + |
| 164 | +## WebSocket |
| 165 | + |
| 166 | +Client supports retrieving information about market data, such as candles, orderbook, trade details. |
| 167 | + |
| 168 | +### Usage |
| 169 | + |
| 170 | +```python |
| 171 | +from asynchuobi.enums import CandleInterval |
| 172 | +from asynchuobi.ws.ws_client import WSHuobiMarket |
| 173 | + |
| 174 | + |
| 175 | +async def main(): |
| 176 | + async with WSHuobiMarket() as ws: |
| 177 | + await ws.candlestick('btcusdt', CandleInterval.min_1).sub() |
| 178 | + await ws.market_stats('btcusdt').sub() |
| 179 | + await ws.market_ticker_info('btcusdt').sub() |
| 180 | + await ws.orderbook('btcusdt').sub() |
| 181 | + async for message in ws: |
| 182 | + ... |
| 183 | +``` |
| 184 | + |
| 185 | +You can define callbacks which will called when message was received from websocket |
| 186 | + |
| 187 | +```python |
| 188 | +from typing import Dict |
| 189 | + |
| 190 | +from asynchuobi.exceptions import WSHuobiError |
| 191 | +from asynchuobi.ws.ws_client import WSHuobiMarket |
| 192 | + |
| 193 | + |
| 194 | +def callback(msg: Dict): |
| 195 | + print(msg) |
| 196 | + |
| 197 | + |
| 198 | +def error(e: WSHuobiError): |
| 199 | + print(e) |
| 200 | + |
| 201 | + |
| 202 | +async def orderbook(): |
| 203 | + async with WSHuobiMarket() as ws: |
| 204 | + await ws.orderbook('btcusdt').sub(callback=callback) |
| 205 | + await ws.run_with_callbacks(error_callback=error) |
| 206 | +``` |
| 207 | + |
| 208 | +You can also define async callback |
| 209 | + |
| 210 | +### Retrieving information about account balance changing and about orders |
| 211 | + |
| 212 | +Authentication is required |
| 213 | + |
| 214 | +```python |
| 215 | +from asynchuobi.ws.ws_client import WSHuobiAccount |
| 216 | + |
| 217 | + |
| 218 | +async def main(): |
| 219 | + async with WSHuobiAccount('access_key', 'secret_key') as ws: |
| 220 | + await ws.subscribe_account_change() |
| 221 | + await ws.subscribe_order_updates('btcusdt') |
| 222 | + async for message in ws: |
| 223 | + ... |
| 224 | +``` |
| 225 | + |
| 226 | +With callbacks |
| 227 | + |
| 228 | +```python |
| 229 | +from asynchuobi.ws.ws_client import WSHuobiAccount |
| 230 | +from asynchuobi.exceptions import WSHuobiError |
| 231 | + |
| 232 | + |
| 233 | +def callback(message: dict): |
| 234 | + print(message) |
| 235 | + |
| 236 | + |
| 237 | +def error(e: WSHuobiError): |
| 238 | + print(e) |
| 239 | + |
| 240 | + |
| 241 | +async def main(): |
| 242 | + async with WSHuobiAccount('access_key', 'secret_key') as ws: |
| 243 | + await ws.subscribe_account_change( |
| 244 | + callback=callback, |
| 245 | + ) |
| 246 | + await ws.run_with_callbacks(error_callback=error) |
| 247 | +``` |
0 commit comments