在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:HuobiRDCenter/huobi_Cpp开源软件地址:https://github.com/HuobiRDCenter/huobi_Cpp开源编程语言:C++ 96.4%开源软件介绍:Huobi C++ SDK v2This is Huobi C++ SDK v2, you can import to your C++ project and use this SDK to query all market data, trading and manage your account. The SDK supports RESTful API invoking, and 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 Table of ContentsQuick StartAfter you install this SDK properly, you can follow below steps in your C++ project
// Create ReferenceClient instance and get the timestamp
ReferenceClient client;
cout << client.getTimestamp() << endl;
// Create MarketClient instance and get btcusdt latest 1-min candlestick
MarketClient client;
char *symbol = "btcusdt";
CandlestickRequest candlestickRequest;
candlestickRequest.symbol = symbol;
candlestickRequest.period = "1min";
vector<Candlestick> klines = client.getCandlestick(candlestickRequest);
for (Candlestick candlestick:klines) {
cout << "open " << candlestick.open << endl;
cout << "close " << candlestick.close << endl;
} For how to install the SDK, please read the Usage section below. UsageFolder structureThis is the folder structure of SDK source code and the description
InstallationThe SDK is compiled by C++ 11. The compiler can be Currently, The SDK has the compatibility on linux system (centos 7 and ubuntu 18.04) only. Later, macOS and windows. Install CMakePlease make sure the CMake is installed on your OS. If not, you can follow https://cmake.org/install/ to install it. The minimum required version of CMake is 2.8, but we suggest to use the lastest CMake version. Install 3rd party librariesPlease make sure the 3rd party libraries have been installed in your system. If not, please install them.
Build SDKRefer to below steps to build the SDK library
if you see the following error when executing "make" command
It indicates your openssl library was not installed expectedly, you can try below command to fix:
Run examplesAfter installing the SDK, you are able to run the examples under /demo folder. if you want to access private data, you need below additional two steps:
#define APIKEY "hrf5gdfghe-e74bebd8-2f4a33bc-e7963"
#define SECRETKEY "fecbaab2-35befe7e-2ea695e8-67e56" If you don't need to access private data, you can ignore the API key. Regarding the difference between public data and private data you can find details in Client section below. There are different types of demo, refer below steps to run the 'market' demo
ClientIn this SDK, the client is the struct to access the Huobi API. In order to isolate the private data with public data, and isolated different kind of data, the client category is designated to match the API category. All the client is listed in below table. Each client is very small and simple, it is only responsible to operate its related data, you can pick up multiple clients to create your own application based on your business.
Public and PrivateThere are two types of privacy that is correspondent with privacy of API: Public client: It invokes public API to get public data (Reference data and Market data), therefore you can create a new instance without applying an API Key. // Create a ReferenceClient instance
ReferenceClient client;
// Create a MarketClient instance
MarketClient client; 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 init function // Create an AccountClient instance with APIKey
AccountClient accountClient{APIKEY, SECRETKEY};
// Create a TradeClient instance with API Key
TradeClient tradeClient{APIKEY, SECRETKEY}; The API key is used for authentication. If the authentication cannot pass, the invoking of private interface will fail. Rest and WebSocketThere are two protocols of API, Rest and WebSocket Rest: It invokes Rest API and get once-off response, it has two basic types of method: GET and POST WebSocket: It establishes WebSocket connection with server and data will be pushed from server actively. There are two types of method for WebSocket client:
Migrate from v1Why v2The 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. How to migrateYou 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. Request exampleReference dataExchange timestampReferenceClient client;
cout << client.getTimestamp() << endl; SymbolsReferenceClient client;
vector<Symbol> symbols = client.getSymbols(); CurrenciesReferenceClient client;
vector<std::string> currencies = client.getCurrencies(); Currency & ChainsReferenceClient client;
ReferenceCurrenciesRequest referenceCurrenciesRequest;
vector<ReferenceCurrencies> vec = client.getReferenceCurrencies(referenceCurrenciesRequest); Market dataCandlestickMarketClient client;
char *symbol = "btcusdt";
CandlestickRequest candlestickRequest;
candlestickRequest.symbol = symbol;
candlestickRequest.period = "1min";
vector<Candlestick> klines = client.getCandlestick(candlestickRequest); DepthMarketClient client;
DepthRequest depthRequest;
depthRequest.symbol = symbol;
Depth depth = client.getDepth(depthRequest);
cout << "ask price: " << depth.asks[0].price << endl;
cout << "bid price: " << depth.bids[0].price << endl; Latest tradeMarketClient client;
TradeRequest tradeRequest{symbol};
vector<Trade> trade = client.getTrade(tradeRequest); Historical TradeMarketClient client;
HistoryTradeRequest historyTradeRequest;
historyTradeRequest.symbol = symbol;
vector<Trade> tradeHistory = client.getHistoryTrade(historyTradeRequest);
cout << "trade price: " << tradeHistory[0].price << endl; AccountAuthentication is required. Account balanceAccountClient accountClient{APIKEY, SECRETKEY};
vector<Balance> balanceVec = accountClient.getBalance(12345); Get Account HistoryAccountClient accountClient{APIKEY, SECRETKEY};
AccountHistoryRequest accountHistoryRequest{12345, "usdt"};
accountHistoryRequest.transactTypes = "trade";
accountHistoryRequest.size = 10;
vector<AccountHistory> accountHistory = accountClient.getHistory(accountHistoryRequest); WalletAuthentication is required. WithdrawWalletClient walletClient{APIKEY, SECRETKEY};
WithdrawCreateRequest withdrawCreateRequest;
withdrawCreateRequest.amount = "5";
withdrawCreateRequest.currency = "usdt";
withdrawCreateRequest.address = "xxxxx";
withdrawCreateRequest.chain = "trc20usdt";
withdrawCreateRequest.fee = "0.0";
long withdrawId = walletClient.withdrawCreate(withdrawCreateRequest); Cancel withdrawWalletClient walletClient{APIKEY, SECRETKEY};
long cancelwithdraw = walletClient.withdrawCancel(withdrawId); Withdraw and deposit historyWalletClient walletClient{APIKEY, SECRETKEY};
QueryDepositWithdrawRequest queryDepositWithdrawRequest;
queryDepositWithdrawRequest.type = "withdraw";
vector<DepositWithdraw> record = walletClient.queryDepositWithdraw(queryDepositWithdrawRequest);
queryDepositWithdrawRequest.type = "deposit";
vector<DepositWithdraw> record = walletClient.queryDepositWithdraw(queryDepositWithdrawRequest); TradingAuthentication is required. Create orderTradeClient tradeClient{APIKEY, SECRETKEY};
PlaceOrderRequest placeOrderRequest;
placeOrderRequest.accountId = 12345;
placeOrderRequest.symbol = "htusdt";
placeOrderRequest.type = "buy-market";
placeOrderRequest.amount = "5.0";
placeOrderRequest.clientOrderId = "client_order-id";
long orderId = tradeClient.placeOrder(placeOrderRequest); Cancel orderTradeClient tradeClient{APIKEY, SECRETKEY};
tradeClient.submitCancelOrder("order-id"); Cancel open ordersTradeClient tradeClient{APIKEY, SECRETKEY};
BatchCancelOpenOrdersRequest batchCancelOpenOrdersRequest;
batchCancelOpenOrdersRequest.accountId = accountId;
BatchCancelOpenOrders batchCancelOpenOrders = tradeClient.batchCancelOpenOrders(batchCancelOpenOrdersRequest); Get order infoTradeClient tradeClient{APIKEY, SECRETKEY};
Order order = tradeClient.getOrder(orderId); Historical ordersTradeClient tradeClient{APIKEY, SECRETKEY};
OrdersHistoryRequest ordersHistoryRequest;
std::vector<Order> historicalOrders = tradeClient.getOrdersHistory(ordersHistoryRequest); Margin LoanAuthentication is required. These are examples for cross margin ####Apply loan CrossMarginClient crossMarginClient{APIKEY, SECRETKEY};
CrossMarginTransferOrApplyRequest crossMarginTransferOrApplyRequest{currency, amount};
long marginId = crossMarginClient.marginOrders(crossMarginTransferOrApplyRequest); Repay loanCrossMarginClient crossMarginClient{APIKEY, SECRETKEY};
std::string amount = "100.0";
crossMarginClient.repay(marginId, amount.c_str()); ####Loan history CrossMarginClient crossMarginClient{APIKEY, SECRETKEY};
CrossMarginLoanOrdersRequest crossMarginLoanOrdersRequest;
vector<CrossMarginLoanOrder> crossMarginLoanOrders = crossMarginClient.getLoanOrders(crossMarginLoanOrdersRequest); Subscription exampleSubscribe trade updatewebsocketMarketClient client;
client.subTrade("htusdt", [](Trade trade) {
cout << trade.price << endl;
cout << trade.tradeId << endl;
}); ###Subscribe candlestick update websocketMarketClient client;
client.subKline("htusdt", "1min", [](Candlestick candlestick) {
cout << candlestick.amount << endl;
}); Subscribe order updateAuthentication is required. websocketOrdersClient client{APIKEY, SECRETKEY};
client.subOrders("htusdt", [](OrdersUpdate ordersUpdate) {
cout << ordersUpdate.symbol << endl;
}); Subscribe account changeAuthentication is required. websocketAssetClient client{APIKEY, SECRETKEY};
client.subAccounts(1, [](AccountsUpdate accountsUpdate) {
cout << accountsUpdate.changeType << endl;
}); |
2022-08-15
2022-08-17
2022-09-23
2023-10-27
2022-08-18
请发表评论