create account

Huobi API And Automated Selling Of STEEM/USDT by steempytutorials

View this thread on: hive.blogpeakd.comecency.com
· @steempytutorials · (edited)
$55.97
Huobi API And Automated Selling Of STEEM/USDT
<center>![banner.png](https://cdn-images-1.medium.com/max/1600/1*W3Lh17Km2be2NPIZAu5jbg.png)</center>

This tutorial is part of a series where different aspects of programming with Python are explained, using Python and public libraries to make useful tools.

---

#### Repository
https://github.com/huobiapi/API_Docs
https://github.com/huobiapi/REST-Python3-demo

#### What will I learn

- Setting up your Huobi API keys
- Retrieving account balances
- Retrieving current market price
- Creating market-sell orders
- Automated selling

#### Requirements

- Python3.6
- [Huobi account](https://www.huobi.com/en-us/)

#### Difficulty

- intermediate

---

### Tutorial

#### Preface

[Huobi](https://www.huobi.com/en-us/) is a Chinese exchange that offers trading in `STEEM/USDT`. While their website offers decent translation to English, the API documentation is lacking a bit. `STEEM/USDT` is a useful pair that offers great stability. For example setting up a bot that automatically sells any incoming `STEEM` for `USDT`. The value of the `USDT` will not change.

#### Setup
Download the files from [Github](https://github.com/Juless89/python-tutorials/tree/master/huobi). There are 3 files. The code is contained in `auto_sell.py` the other 2 files are files from Huobi that contain their API code. `huobiservices.py` contains all the functions that allow for interaction with the exchange functionality. `utils.py` contains the required code to perform the API requests. In this file the API `ACCESS_KEY` and `SECRET_KEY` are set.

Run scripts as following:
`> python auto_sell.py`

#### Setting up your Huobi API keys
In order to perform API requests with the Huobi exchange you need to set up an `API key` within your account. Go to `API Management` from the drop down menu which is found in the top right corner.

<center>![Screenshot 2018-07-20 13.00.58.png](https://cdn.steemitimages.com/DQmYjApLMuKnMy7wfrYPM4mpgb2XKd6kUaxh6b4p1maNqRC/Screenshot%202018-07-20%2013.00.58.png)</center>

Here you can create an `API key`. It is recommended to bind an IP address for added security. Keep in mind that an `API key` allows for full access to your exchange account. Including withdrawals.

<center>![Screenshot 2018-07-20 13.01.13.png](https://cdn.steemitimages.com/DQmTXbz9o4jtVuHgowbVeJDdiBNmDgGi6Gg4V72rsuPD7pY/Screenshot%202018-07-20%2013.01.13.png)</center>

#### Retrieving account balances
The `get_balance()` function can be used to retrieve a full list of all the account's balances. Every response contains a `status` message and the `data` relevant to to the request.  Inside data there is a list that contains all the currencies and their balances. Each currency has two entries, the difference is their type: `trade` or `frozen`.


```
{
	'status': 'ok',
	'data': {
		'id': 9999999,
		'type': 'spot',
		'state': 'working',
		'list': [{
			'currency': 'hb10',
			'type': 'trade',
			'balance': '0'
    }, {
			'currency': 'hb10',
			'type': 'frozen',
			'balance': '0'
		}
```
In order to retrieve the trade able balances for `STEEM` and `USDT` a list is set with their tickers. The `get_balance()` request is performed and checked to be `ok`. The currency list is then extracted from the returned data and checked against the tickers. For each ticker that matches and is of the type `trade`, their balance is taken and added to the `balances` dict.

```
self.tickers = ['usdt', 'steem']
self.balances = {}


# retrieve all balances and filter. Filter for funds that are tradeable
def update_balances(self):
    # get balances
    data = hs.get_balance()

    # verify status message
    if data['status'] == 'ok':
        currencies = data['data']['list']

        # go over all currencies
        for currency in currencies:
            type = currency['type']
            if currency['currency'] in self.tickers and type == 'trade':
                balance = currency['balance']
                self.balances[currency['currency']] = float(balance)
```

#### Retrieving current market price
Huobi does not offer a direct API for the current market price for a given trade pair. Instead the last trade can be requested which contains the last trading price. This is done with the `get_trade(symbol)` function. Inside the response the `price` can be found.

```
{
	'status': 'ok',
	'ch': 'market.steemusdt.trade.detail',
	'ts': 1532039331826,
	'tick': {
		'id': 12997659863,
		'ts': 1532039302973,
		'data': [{
			'amount': 0.0358,
			'ts': 1532039302973,
			'id': 129976598638136614141,
			'price': 1.5018,
			'direction': 'sell'
		}]
	}
}
```

The price is retrieved by first checking if the request went `ok`. If so the `price` can be extracted.

```
# retrieve last order and extract the sell/buy price
def update_price(self):
    data = hs.get_trade('steemusdt')
    if data['status'] == 'ok':
        self.last_price = data['tick']['data'][0]['price']
```



#### Creating market-sell orders
There are different kind of orders: `buy-market`, `sell-market`, `buy_limit` and `sell-limit`. Market orders execute on the current market price while limit order allow for a buy/sell price to be set. When wanting to sell everything a `sell-market` order is the simplest. Doing so with a `sell-limit` would require additional research into the depth of the market to decide what price to sell at. This example will be using the `sell-market` type. The `send_order()` functions takes 5 arguments: the `amount` to buy/sell, `source` which is only required for margin orders, `symbol` which trade pair, `_type` of order and the `price` which is set to 0 by default for market orders. An order that is accepted returns the the status `ok` and the `order_id`.


```
{
	'status': 'ok',
	'data': '8133838204'
}
```

The `order_id` can than be used to retrieve the full status about the order with the `order_info()` function.

```
{
	'status': 'ok',
	'data': {
		'id': 8133838204,
		'symbol': 'steemusdt',
		'account-id': 9999999,
		'amount': '1.000000000000000000',
		'price': '0.0',
		'created-at': 1532036264999,
		'type': 'sell-market',
		'field-amount': '1.000000000000000000',
		'field-cash-amount': '1.501600000000000000',
		'field-fees': '0.003003200000000000',
		'finished-at': 1532036265361,
		'source': 'spot-api',
		'state': 'filled',
		'canceled-at': 0
	}
}
```

In this example the order is send and verified to be `ok`. If so the `order_id` is taken and the additional information about the order is requested. There is a 1 second delay to account for the exchange processing the order. The status of the returned data is checked and then the preferred information is taken, converted and printed to the terminal.

```
# check if sell order went ok
            if data['status'] == 'ok':
                order_id = data['data']

                # wait for exchange to process the order
                time.sleep(1)

                # retrieve and print information about the order
                data = hs.order_info(order_id)
                if data['status'] == 'ok':
                    cash_amount = float(data['data']['field-cash-amount'])
                    fees = float(data['data']['field-fees'])
                    print(f'id: {order_id}\nAmount: {balance:.3f} STEEM' +
                          f'\nSell value: {cash_amount:.5f} USDT\nFees: ' +
                          f'{fees:.5f} US
```

#### Automated selling

All of this can be combined to create an automated selling script. Every second the script updates the balances and the current price of `STEEM/USDT`. These are printed to the terminal with the additional argument `end='\r'` so the same line is updated instead of a new line being created. Then the balance of `STEEM` is checked, if greater than 1.000 the whole balance is sold in a `sell-market` order.


```
# Update balances and the STEEM price every second. When the STEEM
# balane is greater than 1.000, sell total balance on the market.
def run(self):
    while True:
        try:
            self.update_balances()
            self.update_price()
            print(f'STEEM/USDT: {self.last_price} STEEM: ' +
                  f'{self.balances["steem"]:.3f} USDT: ' +
                  f'{self.balances["usdt"]:.2f}', end='\r')
            self.sell_steem()
            time.sleep(1)
        except Exception as e:
            time.sleep(1)
```


#### Running the code

Running the script allows for easy automated selling of `STEEM` for `USDT`. Whenever new `STEEM` gets deposited into the account it will be automatically sold after it reached a certain threshold.

<center>![Screenshot 2018-07-20 13.51.26.png](https://cdn.steemitimages.com/DQmYe2X1D9YqZ1PKn1ipbz9wYd9mVp8AJgnrxUqmfGxJYH7/Screenshot%202018-07-20%2013.51.26.png)</center>


```
python auto_sell.py
STEEM/USDT: 1.4598 STEEM: 1.150 USDT: 74.17

Created sell-market order
id: 8177315461
Amount: 1.150 STEEM
Sell value: 1.67762 USDT
Fees: 0.00336 USDT

STEEM/USDT: 1.4588 STEEM: 0.000 USDT: 75.84
```

---

The code for this tutorial can be found on [Github](https://github.com/Juless89/python-tutorials/tree/master/huobi)!

This tutorial was written by @juliank.
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorsteempytutorials
permlinkhuobi-api-and-automated-selling-of-steem-usdt
categoryutopian-io
json_metadata{"tags":["utopian-io","tutorials","programming","python","huobi"],"users":["juliank"],"image":["https://cdn-images-1.medium.com/max/1600/1*W3Lh17Km2be2NPIZAu5jbg.png","https://cdn.steemitimages.com/DQmYjApLMuKnMy7wfrYPM4mpgb2XKd6kUaxh6b4p1maNqRC/Screenshot%202018-07-20%2013.00.58.png","https://cdn.steemitimages.com/DQmTXbz9o4jtVuHgowbVeJDdiBNmDgGi6Gg4V72rsuPD7pY/Screenshot%202018-07-20%2013.01.13.png","https://cdn.steemitimages.com/DQmYe2X1D9YqZ1PKn1ipbz9wYd9mVp8AJgnrxUqmfGxJYH7/Screenshot%202018-07-20%2013.51.26.png"],"links":["https://github.com/huobiapi/API_Docs","https://github.com/huobiapi/REST-Python3-demo","https://www.huobi.com/en-us/","https://github.com/Juless89/python-tutorials/tree/master/huobi"],"app":"steemit/0.1","format":"markdown"}
created2018-07-20 12:02:30
last_update2018-07-20 20:03:30
depth0
children2
last_payout2018-07-27 12:02:30
cashout_time1969-12-31 23:59:59
total_payout_value42.506 HBD
curator_payout_value13.459 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9,085
author_reputation31,094,047,689,691
root_title"Huobi API And Automated Selling Of STEEM/USDT"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id65,357,085
net_rshares26,476,963,673,302
author_curate_reward""
vote details (65)
@portugalcoin ·
$0.03
Thank you for your contribution.

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/21313213).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , ,
properties (23)
authorportugalcoin
permlinkre-steempytutorials-huobi-api-and-automated-selling-of-steem-usdt-20180721t104645801z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/21313213","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-07-21 10:46:45
last_update2018-07-21 10:46:45
depth1
children0
last_payout2018-07-28 10:46:45
cashout_time1969-12-31 23:59:59
total_payout_value0.028 HBD
curator_payout_value0.005 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length524
author_reputation598,828,312,571,988
root_title"Huobi API And Automated Selling Of STEEM/USDT"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id65,464,154
net_rshares16,473,721,391
author_curate_reward""
vote details (3)
@utopian-io ·
Hey @steempytutorials
**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-huobi-api-and-automated-selling-of-steem-usdt-20180723t190509z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-07-23 19:05:09
last_update2018-07-23 19:05:09
depth1
children0
last_payout2018-07-30 19:05:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length308
author_reputation152,955,367,999,756
root_title"Huobi API And Automated Selling Of STEEM/USDT"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id65,730,881
net_rshares0