ccxt / ccxt

A JavaScript / TypeScript / Python / C# / PHP cryptocurrency trading API with support for more than 100 bitcoin/altcoin exchanges
https://docs.ccxt.com
MIT License
33.02k stars 7.54k forks source link

Huobi - Can't use stop-limit order type properly #9402

Closed KevinBaudin closed 2 years ago

KevinBaudin commented 3 years ago

I managed to create a limit order on ccxt with exchange huobipro, but can't manage to perform a stop-limit. What am I doing wrong here? I tried different prices, str and float prices...

exchange_id = 'huobipro'
exchange_params = {
    'enableRateLimit': True,
    'apiKey': keys['apiKey'],  # public huobi key
    'secret': keys['secret'],  # private huobi key
    'options': {
        'defaultType': 'spot',
    },
}
exchange = getattr(ccxt, exchange_id)(exchange_params)

exchange.create_order('ADA/USDT', 'stop-limit', 'sell', amount=10, price="1.59", params={'stop-price':"1.59", 'operator':'gte'})
XXXXX is my public apiKey

Request: POST https://api.huobi.pro/v1/order/orders/place?AccessKeyId=XXXXX&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2021-06-17T09%3A06%3A04&Signature=0xNVjrHXcGABictt0%2FrQNgBPD7WyQNHy8%2BkbQ0ZAQas%3D {'Content-Type': 'application/json', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36', 'Accept-Encoding': 'gzip, deflate'} {"account-id":"17485XX","symbol":"adausdt","type":"sell-stop-limit","amount":"10","stop-price":"1.59","operator":"gte"}

Response: POST https://api.huobi.pro/v1/order/orders/place?AccessKeyId=XXXXX&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2021-06-17T09%3A06%3A04&Signature=0xNVjrHXcGABictt0%2FrQNgBPD7WyQNHy8%2BkbQ0ZAQas%3D 200 {'Date': 'Thu, 17 Jun 2021 09:06:04 GMT', 'Content-Type': 'application/json; charset=UTF-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Vary': 'Accept-Encoding, Origin, Access-Control-Request-Method, Access-Control-Request-Headers', 'X-HB-RateLimit-Requests-Remain': '99', 'X-HB-RateLimit-Requests-Expire': '1623920765966', 'Request-Id': '750eaa0c869e94cd388cbb647d3a9df6', 'Content-Encoding': 'gzip', 'X-Request-ID': '03346a9ae79c8a7e5b2e73d3741fa850', 'CF-Cache-Status': 'DYNAMIC', 'cf-request-id': '0abad17b6400000863de9d7000000001', 'Expect-CT': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"', 'Server': 'cloudflare', 'CF-RAY': '660b1ea56cf70863-CDG'} {"status":"error","err-code":"order-invalid-price","err-msg":"invalid price","data":null}

---------------------------------------------------------------------------
ExchangeError                             Traceback (most recent call last)
<ipython-input-10-d525b1e84d26> in <module>
     11 exchange = getattr(ccxt, exchange_id)(exchange_params)
     12 
---> 13 exchange.create_order('ADA/USDT', 'stop-limit', 'sell', 10, "1.59", params={'stop-price':"1.59", 'operator':'gte'})

~/anaconda3/envs/quant/lib/python3.7/site-packages/ccxt/huobipro.py in create_order(self, symbol, type, side, amount, price, params)
   1097             request['price'] = self.price_to_precision(symbol, price)
   1098         method = self.options['createOrderMethod']
-> 1099         response = getattr(self, method)(self.extend(request, params))
   1100         timestamp = self.milliseconds()
   1101         id = self.safe_string(response, 'data')

~/anaconda3/envs/quant/lib/python3.7/site-packages/ccxt/base/exchange.py in inner(_self, params)
    459                             if params is not None:
    460                                 inner_kwargs['params'] = params
--> 461                             return entry(_self, **inner_kwargs)
    462                         return inner
    463                     to_bind = partialer()

~/anaconda3/envs/quant/lib/python3.7/site-packages/ccxt/base/exchange.py in request(self, path, api, method, params, headers, body)
    484     def request(self, path, api='public', method='GET', params={}, headers=None, body=None):
    485         """Exchange.request is the entry point for all generated methods"""
--> 486         return self.fetch2(path, api, method, params, headers, body)
    487 
    488     @staticmethod

~/anaconda3/envs/quant/lib/python3.7/site-packages/ccxt/base/exchange.py in fetch2(self, path, api, method, params, headers, body)
    480         self.lastRestRequestTimestamp = self.milliseconds()
    481         request = self.sign(path, api, method, params, headers, body)
--> 482         return self.fetch(request['url'], request['method'], request['headers'], request['body'])
    483 
    484     def request(self, path, api='public', method='GET', params={}, headers=None, body=None):

~/anaconda3/envs/quant/lib/python3.7/site-packages/ccxt/base/exchange.py in fetch(self, url, method, headers, body)
    632                 raise ExchangeError(details) from e
    633 
--> 634         self.handle_errors(http_status_code, http_status_text, url, method, headers, http_response, json_response, request_headers, request_body)
    635         if json_response is not None:
    636             return json_response

~/anaconda3/envs/quant/lib/python3.7/site-packages/ccxt/huobipro.py in handle_errors(self, httpCode, reason, url, method, headers, body, response, requestHeaders, requestBody)
   1458                 message = self.safe_string(response, 'err-msg')
   1459                 self.throw_exactly_matched_exception(self.exceptions['exact'], message, feedback)
-> 1460                 raise ExchangeError(feedback)

ExchangeError: huobipro {"status":"error","err-code":"order-invalid-price","err-msg":"invalid price","data":null}

Additionnal question: I can't find stop-limit open orders from huobipro I did (from huobi interface) a stop-limit sell on ADA/USDT but exchange.fetch_open_orders('ADA/USDT', params={"type":"sell-limit"})returns me []

Thanks all for the great job on ccxt !

carlosmiei commented 2 years ago

Hello @KevinBaudin , Some of the parameters such as the type were not correct for opening a stop-limit order in Huobi. Besides that, the operator parameter was missing and it's a mandatory piece of information for this exchange.

Please checkout these examples where stop-limit orders are being created: https://github.com/ccxt/ccxt/blob/master/examples/py/huobi-spot.py

Thanks