API Huobi

Huobi (HTX)

Huobi (now rebranded as HTX) is an international multi-language cryptocurrency exchange.

Configuration

If you want to subscribe to the private account updates, you need to create an API Key in your Huobi Account. Once created, set the API Key and Secret in the Huobi API Client

Public and Private Data with a Single Component

HTX serves the market data and the account data on two different endpoints, with two different schemas: wss://api.htx.com/ws carries the market data as v1 subscription frames, and wss://api.htx.com/ws/v2 carries the account and order channels as v2 authenticated frames. Neither endpoint answers the frames of the other one.

Previous behaviour. The component opened a single connection and chose the endpoint from the credentials: with an empty ApiKey it connected to the public endpoint and only the public methods worked, and with an ApiKey configured it connected to the private endpoint and only the private methods worked. An application that needed the market data and the account data at the same time had to create two component instances, one with credentials and one without.

Current behaviour. The connection of the component is always the public one, so the market data is available whether credentials are configured or not. When an ApiKey is configured, the component also opens a second connection to the v2 endpoint by itself, and one single instance then receives the public market data and the private account data at the same time. That second connection is internal: it is opened and closed by the component together with the public one, it has no Active property and no events of its own, and everything that arrives on it is surfaced through the events of the component, exactly like the market data.

There is nothing to change in existing applications. No new property and no new event has been added, the public API of the component is the same as before. An application that already sets the ApiKey and subscribes only to private channels keeps working, and it can now subscribe to the public channels too on the same instance. An application that used two instances still works, though the second instance is no longer necessary.

The authentication of the private connection runs on its own, so the private connection may still be connecting or authenticating when the application subscribes. That is not an error and nothing has to be waited for: the channel is recorded and it is requested as soon as the authentication is acknowledged. The same applies after a reconnection when Resubscribe is enabled, the private channels are rebuilt and requested again once the new session has been authenticated.

Calling a private subscribe method with no ApiKey configured now raises the OnHuobiError event, reporting the channel that was requested and the endpoint it needed. Previously the request was written on a connection that could not serve it and it was lost without a trace.

The futures client (TsgcWS_API_Huobi_Fut) connects to a different endpoint, which has no v2 counterpart here, so it opens no second connection and its private frames ride its own connection, exactly as they always have.

Public Methods

You can subscribe to the following public channels (api key is not required)

Method Description
SubscribeKLine This topic sends a new candlestick whenever it is available. Supported periods: 1min, 5min, 15min, 30min, 60min, 4hour, 1day, 1mon, 1week, 1year.
SubscribeMarketDepth This topic sends the latest market by price order book in snapshot mode at 1-second interval. Supported depth aggregation levels: step0 through step15.
SubscribeTradeDetail This topic sends the latest completed trades. It updates in tick by tick mode.
SubscribeMarketDetail

This topic sends the latest market stats with 24h summary. It updates in snapshot mode, at a frequency of no more than 10 times per second

SubscribeBBO User can receive BBO (Best Bid/Offer) update in tick by tick mode.
SubscribeMarketTicker

Retrieve the market ticker. Data is pushed every 100ms.

SubscribeMarketByPrice User could subscribe to this channel to receive refresh update of Market By Price order book. The update interval is around 100ms. Supported levels: 5, 10, 20, 150, 400.

Futures Public Methods

The following methods are available for the Futures API client (TsgcWS_API_Huobi_Fut) in addition to all the public methods above.

Method Description
SubscribeIncrementalMarketDepth Subscribe to incremental market depth updates with configurable size and data type (snapshot or incremental).
SubscribePremiumIndexKLine Subscribe to premium index kline/candlestick data for futures contracts.
SubscribeEstimatedRateKLine Subscribe to estimated funding rate kline/candlestick data for futures contracts.
SubscribeBasisData Subscribe to basis data (spot-futures price spread). Supports different basis price types: open, close, high, low.
SubscribeMarkPriceKLine Subscribe to mark price kline/candlestick data for futures contracts.
SubscribeLiquidationOrders Subscribe to public liquidation order feed for a given contract. No authentication required.
SubscribeFundingRate Subscribe to public funding rate updates for a given contract. No authentication required.
SubscribeContractInfo Subscribe to contract parameter changes (e.g. contract listings, delistings, parameter adjustments).

Private Methods

You can subscribe to the following private channels (an api key is required). They are served by the second connection the component opens automatically when the ApiKey is configured, so they can be combined with the public channels on the same component instance. If the credentials are not correct, the connection will be closed automatically, and if there is no ApiKey at all the request is reported in the OnHuobiError event.

Method Description
SubscribeOrderUpdates

An order update can be triggered by any of following:


- Conditional order triggering failure (eventType=trigger)
- Conditional order cancellation before trigger (eventType=deletion)
- Order creation (eventType=creation)
- Order matching (eventType=trade)
- Order cancellation (eventType=cancellation)

SubscribeTradeClearing Only update when order is in transaction or cancellation. Order transaction update is in tick by tick mode, which means, if a taker's order matches with multiple maker's orders, the simultaneous multiple trades will be disseminated one by one. But the update sequence of the multiple trades, may not be exactly the same as the sequence of the transactions made. Also, if an order is auto cancelled immediately just after its partial fills, for example a typical IOC order, this channel would possibly disseminate the cancellation update first prior to the trade.
SubscribeAccountChange

Upon subscription field value specified, the update can be triggered by either of following events. The aMode parameter controls the update behavior:

Mode 0: Only update when account balance is changed.

Mode 1: Update when either account balance or available balance is changed (separate updates).

Mode 2: Update when account balance or available balance is changed (combined update).

Events

OnHuobiSubscribed: event called after a successful subscription.

OnHuobiUnSubscribed: event called after a successful unsubscription.

OnHuobiUpdate: every time there is an update in data (kline, market depth...) this event is called. It reports the updates of the public channels and of the private ones, whichever of the two connections they arrived on.

OnHuobiError: if there is an error in Huobi API, this event will provide information about error. It is also called when a private channel is requested and the component has no ApiKey to open the private connection, with the Code parameter set to "private-credentials" and the Msg parameter naming the channel that was requested.