Skip to content

Commit

Permalink
python现货sdk接口修改更新
Browse files Browse the repository at this point in the history
  • Loading branch information
cray-cmd committed Mar 4, 2024
1 parent 6bd3131 commit d840a35
Show file tree
Hide file tree
Showing 102 changed files with 813 additions and 722 deletions.
41 changes: 23 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# Huobi Python SDK v2
# Huobi Python SDK For Spot v3

This is Huobi Python SDK v2, you can import to your python project and use this SDK to query all market data, trading and manage your account. The SDK supports RESTful API invoking, and concurrently subscribing the market, account and order update from the Websocket connection.
This is Huobi Python SDK v3, you can import to your python project and use this SDK to query all market data, trading and manage your account. The SDK supports RESTful API invoking, and concurrently subscribing the market, account and order update from the Websocket connection.

If you already use SDK v1, it is strongly suggested migrate to v2 as we refactor the implementation to make it simpler and easy to maintain. We will stop the maintenance of v1 in the near future. Please refer to the instruction on how to migrate v1 to v2 in section [Migrate from v1](#Migrate-from-v1)
If you already use SDK v1 or v2, it is strongly suggested migrate to v3 as we refactor the implementation to make it simpler and easy to maintain. The SDK v3 is completely consistent with the API documentation of the new HTX open platform. Compared to SDK versions v1 and v2, due to changes in parameters of many interfaces, in order to match the latest interface parameter situation, v3 version has made adjustments to parameters of more than 80 interfaces to ensure that requests can be correctly initiated and accurate response data can be obtained. Meanwhile, the v3 version has added over 130 new interfaces available for use, greatly expanding the number of available interfaces. We will stop the maintenance of v2 in the near future. Please refer to the instruction on how to migrate v1 or v2 to v3 in section [Migrate from v1 or v2](#Migrate-from-v1-or-v2)


## Table of Contents

- [Quick Start](#Quick-Start)
- [Usage](#Usage)
- [Folder structure](#Folder-structure)
- [Run Examples](#Run-examples)
- [Client](#client)
- [Migrate from v1](#Migrate-from-v1)
- [Folder structure](#Folder-structure)
- [Run Examples](#Run-examples)
- [Client](#client)
- [Migrate from v1 or v2](#Migrate-from-v1-or-v2)
- [Request example](#Request-example)
- [Reference data](#Reference-data)
- [Market data](#Market-data)
- [Account](#account)
- [Wallet](#wallet)
- [Trading](#trading)
- [Margin Loan](#margin-loan)
- [Reference data](#Reference-data)
- [Market data](#Market-data)
- [Account](#account)
- [Wallet](#wallet)
- [Trading](#trading)
- [Margin Loan](#margin-loan)
- [Subscription example](#Subscription-example)
- [Subscribe trade update](#Subscribe-trade-update)
- [Subscribe candlestick update](#subscribe-candlestick-update)
Expand Down Expand Up @@ -75,6 +75,7 @@ This SDK provides examples that under **/example** folder, if you want to run th
1. Create an **API Key** first from Huobi official website
2. Create **privateconfig.py** into your **huobi** folder. The purpose of this file is to prevent submitting SecretKey into repository by accident, so this file is already added in the *.gitignore* file.
3. Assign your API access key and secret key to as below:

```python
p_api_key = "hrf5gdfghe-e74bebd8-2f4a33bc-e7963"
p_secret_key = "fecbaab2-35befe7e-2ea695e8-67e56"
Expand All @@ -98,9 +99,12 @@ All the client is listed in below table. Each client is very small and simple, i
| Wallet | WalletClient | Private | Rest |
| Trade | TradeClient | Private | Rest, WebSocket v2 |
| Margin | MarginClient | Private | Rest |
| Sub User | SubuserClient | Private | Rest |
| Algo | AlgoClient | Private | Rest |
| ETF | ETFClient | Private | Rest |

#### Customized Host

The client class support customized host so that you can define your own host, refer to example in later section.

#### Public and Private
Expand Down Expand Up @@ -147,19 +151,21 @@ In this python SDK, some clients support both Rest and WebSocket protocols, the
- **req**: req_order_list
- **sub**: sub_order_update

### Migrate from v1
### Migrate from v1 or v2

#### Why v2
#### Why v3

The major difference between v1 and v2 is that the client category.

In SDK v1, the client is categorized as two protocol, request client and subscription client. For example, for Rest API, you can operate everything in request client. It is simple to choose which client you use, however, when you have a client instance, you will have dozens of method, and it is not easy to choose the proper method.

The thing is different in SDK v2, the client class is categorized as seven data categories, so that the responsibility for each client is clear. For example, if you only need to access market data, you can use MarketClient without applying API Key, and all the market data can be retrieved from MarketClient. If you want to operate your order, then you know you should use TradeClient and all the order related methods are there. Since the category is exactly same as the API document, so it is easy to find the relationship between API and SDK. In SDK v2, each client is smaller and simpler, which means it is easier to maintain and less bugs.

Compared to SDK versions v1 and v2, due to changes and updates in the out and in parameters of many interfaces, in order to match the latest interface in and out parameter situation, v3 version has made adjustments and updates to the out and in parameters of more than 80 interfaces to ensure that requests can be correctly initiated and accurate response data can be obtained. Meanwhile, the v3 version has added over 130 new interfaces available for use, greatly expanding the number of available interfaces.

#### How to migrate

You don't need to change your business logic, what you need is to find the v1 request client and subscription client, and replace with the proper v2 client. The additional cost is that you need to have additional initialization for each v2 client.
You don't need to change your business logic, what you need is to find the v1 or v2 request client and subscription client, and replace with the proper v3 client. The additional cost is that you need to have additional initialization for each v3 client.

## Request example

Expand Down Expand Up @@ -370,5 +376,4 @@ account_client = AccountClient(api_key=g_api_key,
secret_key=g_secret_key,
init_log=True)
account_client.sub_account_update(AccountBalanceMode.TOTAL, callback)
```

```
19 changes: 6 additions & 13 deletions example/account/get_account_balance_sufficient.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
from huobi.client.account import AccountClient
from huobi.constant import *


# get accounts
from huobi.utils import *

account_client = AccountClient(api_key=g_api_key,
secret_key=g_secret_key)
# list_obj = account_client.get_accounts()
# if list_obj and len(list_obj):
# for account_obj in list_obj:
# list_obj = account_client.get_balance(account_id=account_obj.id)
# LogInfo.output("===== {account_id} , {account_type} =====".format(account_id=account_obj.id, account_type=account_obj.type))
# if len(list_obj):
# for obj in list_obj:
# if float(obj.balance) > 0.1: # only show account with balance
# obj.print_object()
# print()
secret_key=g_secret_key)
# list_obj = account_client.get_accounts() if list_obj and len(list_obj): for account_obj in list_obj: list_obj =
# account_client.get_balance(account_id=account_obj.id) LogInfo.output("===== {account_id} , {account_type}
# =====".format(account_id=account_obj.id, account_type=account_obj.type)) if len(list_obj): for obj in list_obj: if
# float(obj.balance) > 0.1: # only show account with balance obj.print_object() print()

LogInfo.output("====== (SDK encapsulated api) not recommend for low performance and frequence limitation ======")
account_balance_list = account_client.get_account_balance()
Expand All @@ -31,4 +24,4 @@
if float(balance_obj.balance) > 0.1: # only show account with balance
balance_obj.print_object("\t")
print()
print()
print()
9 changes: 9 additions & 0 deletions example/account/get_account_transfer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from huobi.client.account import AccountClient
from huobi.constant import *

account_client = AccountClient(api_key=g_api_key,
secret_key=g_secret_key)

result = account_client.get_account_transfer(from_="spot", to="linear-swap", currency="usdt", amount=100,
margin_account="USDT")
result.print_object()
8 changes: 8 additions & 0 deletions example/account/get_account_valuation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from huobi.client.account import AccountClient
from huobi.constant import *

account_client = AccountClient(api_key=g_api_key,
secret_key=g_secret_key)

result = account_client.get_account_valuation(account_type="spot", valuation_currency="BTC")
result.print_object()
3 changes: 1 addition & 2 deletions example/account/get_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from huobi.utils import *

account_client = AccountClient(api_key=g_api_key,
secret_key=g_secret_key)
secret_key=g_secret_key)

list_obj = account_client.get_accounts()
LogInfo.output_list(list_obj)

11 changes: 0 additions & 11 deletions example/account/get_aggregated_subuser_balance.py

This file was deleted.

5 changes: 2 additions & 3 deletions example/account/get_balance.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from huobi.client.account import AccountClient
from huobi.constant import *


# get accounts
from huobi.utils import *

account_client = AccountClient(api_key=g_api_key,
secret_key=g_secret_key)
secret_key=g_secret_key)
list_obj = account_client.get_balance(account_id=g_account_id)
LogInfo.output_list(list_obj)
LogInfo.output_list(list_obj)
8 changes: 4 additions & 4 deletions example/account/post_account_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from huobi.constant import *

account_client = AccountClient(api_key=g_api_key, secret_key=g_secret_key)
# transfer_result = account_client.post_account_transfer(111859319, 'spot', 10354000, 147509118, 'spot', 13956126, 'trx', 3600)
# transfer_result.print_object()
# transfer_result = account_client.post_account_transfer(111859319, 'spot', 10354000, 147509118, 'spot', 13956126,
# 'trx', 3600) transfer_result.print_object()

# transfer_result = account_client.post_account_transfer(111859319, 'spot', 10354000, 147991959, 'spot', 14026125, 'usdt', 3600)
# transfer_result.print_object()
# transfer_result = account_client.post_account_transfer(111859319, 'spot', 10354000, 147991959, 'spot', 14026125,
# 'usdt', 3600) transfer_result.print_object()

transfer_result = account_client.post_account_transfer(111859319, 'spot', 10354000, 141403659, 'spot', 13082796, 'usdt', 32.21)
transfer_result.print_object()
3 changes: 2 additions & 1 deletion example/account/post_point_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
account_client = AccountClient(api_key=g_api_key,
secret_key=g_secret_key)

point_transfer_result = account_client.post_point_transfer(from_uid=111859319, to_uid=124409916, group_id=0, amount=10)
point_transfer_result = account_client.post_point_transfer(from_uid="111859319", to_uid="124409916", group_id="0",
amount="10")
point_transfer_result.print_object()
13 changes: 0 additions & 13 deletions example/account/post_sub_user_management.py

This file was deleted.

12 changes: 5 additions & 7 deletions example/account/post_transfer_between_futures_and_pro.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@

from huobi.client.account import AccountClient
from huobi.constant import *
from huobi.utils import *
import time

account_client = AccountClient(api_key=g_api_key,
secret_key=g_secret_key)
transfer_order_id = account_client.transfer_between_futures_and_pro(currency="trx", amount=200, transfer_type=TransferFuturesPro.TO_FUTURES)
secret_key=g_secret_key)
transfer_order_id = account_client.transfer_between_futures_and_pro(currency="trx", amount=200,
transfer_type=TransferFuturesPro.TO_FUTURES)
LogInfo.output("transfer from pro to future Order Id : {id}".format(id=transfer_order_id))

# need wait a minute
time.sleep(2)

transfer_order_id = account_client.transfer_between_futures_and_pro(currency="trx", amount=200, transfer_type=TransferFuturesPro.TO_PRO)
transfer_order_id = account_client.transfer_between_futures_and_pro(currency="trx", amount=200,
transfer_type=TransferFuturesPro.TO_PRO)
LogInfo.output("transfer from future to pro Order Id : {id}".format(id=transfer_order_id))



15 changes: 0 additions & 15 deletions example/account/post_transfer_between_parent_and_subuser.py

This file was deleted.

1 change: 1 addition & 0 deletions example/account/req_account_balance_list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from huobi.client.account import AccountClient
from huobi.constant import *
from huobi.model.account import AccountBalanceReq


def callback(account_balance_req: 'AccountBalanceReq'):
Expand Down
8 changes: 4 additions & 4 deletions example/account/sub_account_update.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from huobi.client.account import AccountClient
from huobi.constant import *
from huobi.model.account.updated import Updated


def callback(account_change_event: 'AccountChangeEvent'):
def callback(account_change_event: 'Updated'):
account_change_event.print_object()
print()


account_client = AccountClient(api_key=g_api_key,
secret_key=g_secret_key,
init_log=True)
secret_key=g_secret_key,
init_log=True)
# account_client.sub_account_update(AccountBalanceMode.TOTAL, callback)
account_client.sub_account_update(AccountBalanceMode.BALANCE, callback)

8 changes: 4 additions & 4 deletions example/algo/post_cancel_algo_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
result = algo_client.cancel_orders(orders_to_cancel)
result.print_object()

# order_id = algo_client.create_order(symbol=symbol_test, account_id=account_id, order_type=OrderType.BUY_MARKET, source=OrderSource.API, amount=5.0, price=1.292)
# LogInfo.output("created order id : {id}".format(id=order_id))
# order_id = algo_client.create_order(symbol=symbol_test, account_id=account_id, order_type=OrderType.BUY_MARKET,
# source=OrderSource.API, amount=5.0, price=1.292) LogInfo.output("created order id : {id}".format(id=order_id))
#
# order_id = algo_client.create_order(symbol=symbol_test, account_id=account_id, order_type=OrderType.SELL_MARKET, source=OrderSource.API, amount=1.77, price=None)
# LogInfo.output("created order id : {id}".format(id=order_id))
# order_id = algo_client.create_order(symbol=symbol_test, account_id=account_id, order_type=OrderType.SELL_MARKET,
# source=OrderSource.API, amount=1.77, price=None) LogInfo.output("created order id : {id}".format(id=order_id))
8 changes: 8 additions & 0 deletions example/algo/post_cancel_all_after.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from huobi.client.algo import AlgoClient
from huobi.constant import *

algo_client = AlgoClient(api_key=g_api_key,
secret_key=g_secret_key)

result = algo_client.post_cancel_all_after(timeout=10)
result.print_object()
4 changes: 2 additions & 2 deletions example/algo/post_create_algo_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

algo_client = AlgoClient(api_key=g_api_key, secret_key=g_secret_key)
order_id = algo_client.create_order(symbol=symbol_test, account_id=account_id, order_side=OrderSide.BUY,
order_type=AlgoOrderType.LIMIT, order_size=65, order_price=0.08, stop_price=0.085,
client_order_id="test004")
order_type=AlgoOrderType.LIMIT, order_size="65", order_price="0.08",
stop_price="0.085", client_order_id="test004")
LogInfo.output("created order id : {id}".format(id=order_id))
10 changes: 0 additions & 10 deletions example/etf/get_etf_swap_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,3 @@
etf_client = EtfClient()
etf_config = etf_client.get_etf_swap_config("hb10")
etf_config.print_object()










18 changes: 1 addition & 17 deletions example/etf/get_etf_swap_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,4 @@

etf_client = EtfClient(api_key=g_api_key, secret_key=g_secret_key)
etf_list = etf_client.get_etf_swap_list(etf_name="hb10", offset=0, size=20)
LogInfo.output_list(etf_list)
















LogInfo.output_list(etf_list)
3 changes: 1 addition & 2 deletions example/etf/post_etf_swap_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from huobi.constant import *

etf_client = EtfClient(api_key=g_api_key, secret_key=g_secret_key)
etf_swap_ret = etf_client.post_etf_swap_in("hb10", 1000)
etf_swap_ret.print_object()
etf_client.post_etf_swap_in("hb10", 1000)



Expand Down
3 changes: 1 addition & 2 deletions example/etf/post_etf_swap_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from huobi.constant import *

etf_client = EtfClient(api_key=g_api_key, secret_key=g_secret_key)
etf_swap_ret = etf_client.post_etf_swap_out("hb10", 1000)
etf_swap_ret.print_object()
etf_client.post_etf_swap_out("hb10", 1000)



Expand Down
9 changes: 9 additions & 0 deletions example/generic/get_common_chains.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from huobi.client.generic import GenericClient
from huobi.constant import *
from huobi.utils import *

generic_client = GenericClient(api_key=g_api_key,
secret_key=g_secret_key)

list_obj = generic_client.get_common_chains()
LogInfo.output_list(list_obj)
9 changes: 9 additions & 0 deletions example/generic/get_common_currencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from huobi.client.generic import GenericClient
from huobi.constant import *
from huobi.utils import *

generic_client = GenericClient(api_key=g_api_key,
secret_key=g_secret_key)

list_obj = generic_client.get_common_currencies()
LogInfo.output_list(list_obj)
Loading

0 comments on commit d840a35

Please sign in to comment.