hbdmapi / huobi_sdk_CSharp

CSharp SDK for Huobi API
Apache License 2.0
9 stars 4 forks source link
api csharp exchange futures huobi restful sdk spot websocket

Huobi C# SDK

This is Huobi C# SDK, This is a lightweight .NET library. You can import it to your project or nuget in: https://www.nuget.org/packages/Huobi_SDK_Core/

The SDK API supports both RESTful and websocket to get/sub the market, account and order infomation.

It supports Spot trading, Coin-M Futures, Coin-M Swaps and USDT-M.

Table of Contents

Quick Start

The SDK is compiled by .NET Standard 2.1, you can import the source code directly or build as dll lib.

The project Huobi.SDK.Core is a dll project as SDK API. The project Huobi.SDK.Core is a unit test project and you can find usage of each API interface.

If you want to create your own application, you can follow below steps:

// RESTful api
AccountClient client = new AccountClient("AccessKey", "SecretKey");
var result = client.GetAccountAssetsAsync(contractCode).Result;

// websocker api
WSMarketClient client = new WSMarketClient();
client.SubKLine("BTC-USDT", "1min", delegate (SubKLineResponse data)
{
    Console.WriteLine(JsonConvert.SerializeObject(data));
});

Usage

Configuration

The client constructor must set AccessKey/SecretKey two value when you access private data. And it not need to set AccessKey/SecretKey value when you access public data such as market data.

You can create appsettings.json in your project for config AccessKey/SecretKey and other input data.

{
  "Host": "api.hbdm.com",
  "AccessKey": "x-x-x-x",
  "SecretKey": "x-x-x-x",
  "AccountId": 10000000,
  "SubUid": 10000000
}

And use it as follow:

IConfigurationRoot config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
string access_key = config["AccessKey"];
string secret_key = config["SecretKey"];

Folder Structure

This is the folder and namespace structure of SDK source code and the description

You can find all demo in LinearSwapTest.cs to get/sub linear swap private/public data

Client

In this SDK, the client is the object to access the Huobi API.All the client are listed in below table. Each client is very small and simple.

Access Type Client Privacy Data Category
RESTful AccountClient Private account info
MarketClient Public market info
OrderClient Private about order
TransferClient Private transfer assets
TriggerOrderClient Private about trigger order
Websocket WSIndexClinet Public index infor
WSMarketClinet Public market info
WSNotifyClinet Public/Private market info/ account info

Public vs. Private

There are two types of privacy that is correspondent with privacy of API:

Public client: It invokes public API to get public data (Common data and Market data), therefore you can create a new instance without applying an API Key.

MarketClient client = new MarketClient();
var result = client.GetContractInfoAsync(“BTC-USDT”).Result;

Private client: It invokes private API to access private data, you need to follow the API document to apply an API Key first, and pass the API Key to the constructor.

AccountClient client = new AccountClient("AccessKey", "SecretKey");
var result = client.GetAccountAssetsAsync(“BTC-USDT”).Result;

The API key is used for authentication. If the authentication cannot pass, the invoking of private interface will fail.

Rest vs. WebSocket

Rest Client: It invokes Rest API and get once-off response.

AccountClient client = new AccountClient("AccessKey", "SecretKey");
var result = client.GetAccountAssetsAsync(“BTC-USDT”).Result;

You can refer to C# programming guide Asynchronous programming with async and await to know more details.

WebSocket Client: It establishes WebSocket connection with server and data will be pushed from server actively. There are two types of method for WebSocket client:

WSMarketClient client = new WSMarketClient();

// Reqxxx
client.ReqKLine("BTC-USDT", "1min", delegate (ReqKLineResponse data)
{
    Console.WriteLine(JsonConvert.SerializeObject(data));
}, 1604395758, 1604396758);

// Subxxx
client.SubKLine("BTC-USDT", "1min", delegate (SubKLineResponse data)
{
    Console.WriteLine(JsonConvert.SerializeObject(data));
});

Custom host

Each client constructor support an optional host parameter, by default it is "api.btcgateway.pro". If you need to use different host, you can specify the custom host.

AccountClient client = new AccountClient("AccessKey", "SecretKey", "Host");
var result = client.GetAccountAssetsAsync(“BTC-USDT”).Result;

WSMarketClient client = new WSMarketClient("Host");

Response

In this SDK, the response is the object that define the data returned from API, which is deserialized from JSON string. It is the return type from each client method, you can declare the object use keyword var and don't need to specify the concrete type.

AccountClient client = new AccountClient("AccessKey", "SecretKey");

// Use 'var' to declare a variable to hold the response
var result = client.GetAccountAssetsAsync(“BTC-USDT”).Result;

Request Examples

Market data

MarketClient client = new MarketClient();
var result = client.GetContractInfoAsync(“BTC-USDT”).Result;

Subscription Examples

Subscribe trade update

WSNotifyClient client = new WSNotifyClient("AccessKey", "SecretKey");
client.SubOrders(“BTC-USDT”, delegate (SubOrdersResponse data)
{
    Console.WriteLine(JsonConvert.SerializeObject(data));
});
System.Threading.Thread.Sleep(1000 * 30);
client.UnsubOrders(“BTC-USDT”);
System.Threading.Thread.Sleep(1000 * 5);