在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:HuobiRDCenter/huobi_Java开源软件地址:https://github.com/HuobiRDCenter/huobi_Java开源编程语言:Java 100.0%开源软件介绍:Huobi Java SDK v2This is Huobi Java SDK v2, you can import to your 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 startThe SDK is compiled by Java8, you can import the source code in java IDE (idea or eclipse) The example code are in folder /java/src/test/java/com/huobi/examples that you can run directly If you want to create your own application, you can follow below steps:
// Create GenericClient instance and get the timestamp
GenericClient genericService = GenericClient.create(HuobiOptions.builder().build());
Long serverTime = genericService.getTimestamp();
System.out.println("server time:" + serverTime);
// Create MarketClient instance and get btcusdt latest 1-min candlestick
MarketClient marketClient = MarketClient.create(new HuobiOptions());
List<Candlestick> list = marketClient.getCandlestick(CandlestickRequest.builder()
.symbol("btcusdt")
.interval(CandlestickIntervalEnum.MIN1)
.size(10)
.build());
list.forEach(candlestick -> {
System.out.println(candlestick.toString());
}); UsageFolder StructureThis is the folder and package structure of SDK source code and the description
Run ExamplesThis SDK provides examples that under src/test/java/com/huobi/example folder, if you want to run the examples to access private data, you need below additional steps:
public static final String API_KEY = "hrf5gdfghe-e74bebd8-2f4a33bc-e7963"
public static final String SECRET_KEY = "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. ClientIn this SDK, the client is the class 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 (Generic data and Market data), therefore you can create a new instance without applying an API Key. // Create a GenericClient instance
GenericClient genericService = GenericClient.create(new HuobiOptions());
// Create a MarketClient instance
MarketClient marketClient = MarketClient.create(new HuobiOptions()); 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 accountService = AccountClient.create(HuobiOptions.builder()
.apiKey(Constants.API_KEY)
.secretKey(Constants.SECRET_KEY)
.build());
// Create a TradeClient instance with API Key
TradeClient tradeService = TradeClient.create(HuobiOptions.builder()
.apiKey(Constants.API_KEY)
.secretKey(Constants.SECRET_KEY)
.build()); 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 timestampGenericClient genericService = GenericClient.create(new HuobiOptions());
Long serverTime = genericService.getTimestamp(); Symbol and currenciesGenericClient genericService = GenericClient.create(new HuobiOptions());
List<Symbol> symbolList = genericService.getSymbols();
List<String> currencyList = genericService.getCurrencys(); Market dataCandlestickMarketClient marketClient = MarketClient.create(new HuobiOptions());
List<Candlestick> list = marketClient.getCandlestick(CandlestickRequest.builder()
.symbol(symbol)
.interval(CandlestickIntervalEnum.MIN15)
.size(10)
.build()); DepthMarketClient marketClient = MarketClient.create(new HuobiOptions());
MarketDepth marketDepth = marketClient.getMarketDepth(MarketDepthRequest.builder()
.symbol(symbol)
.depth(DepthSizeEnum.SIZE_5)
.step(DepthStepEnum.STEP0)
.build()); Latest tradeMarketClient marketClient = MarketClient.create(new HuobiOptions());
List<MarketTrade> marketTradeList = marketClient.getMarketTrade(MarketTradeRequest.builder().symbol(symbol).build()); HistoricalMarketClient marketClient = MarketClient.create(new HuobiOptions());
List<MarketTrade> marketHistoryTradeList = marketClient.getMarketHistoryTrade(MarketHistoryTradeRequest.builder().symbol(symbol).build()); AccountAuthentication is required. Get account balanceAccountClient accountService = AccountClient.create(HuobiOptions.builder()
.apiKey(Constants.API_KEY)
.secretKey(Constants.SECRET_KEY)
.build());
AccountBalance accountBalance = accountService.getAccountBalance(AccountBalanceRequest.builder()
.accountId(accountId)
.build()); WalletAuthentication is required. WithdrawHuobiWalletService walletService = new HuobiWalletService(HuobiOptions.builder()
.apiKey(Constants.API_KEY)
.secretKey(Constants.SECRET_KEY)
.build());
long withdrawId = walletService.createWithdraw(CreateWithdrawRequest.builder()
.address(withdrawAddress)
.addrTag(withdrawAddressTag)
.currency("eos")
.amount(new BigDecimal("1"))
.fee(new BigDecimal("0.1"))
.build()); Cancel withdrawHuobiWalletService walletService = new HuobiWalletService(HuobiOptions.builder()
.apiKey(Constants.API_KEY)
.secretKey(Constants.SECRET_KEY)
.build());
long res = walletService.cancelWithdraw(withdrawId); Withdraw and deposit historyList<DepositWithdraw> depositWithdrawList = walletService.getDepositWithdraw(DepositWithdrawRequest.builder()
.type(DepositWithdrawTypeEnum.WITHDRAW)
.build()); TradingAuthentication is required. Create orderTradeClient tradeService = TradeClient.create(HuobiOptions.builder()
.apiKey(Constants.API_KEY)
.secretKey(Constants.SECRET_KEY)
.build());
CreateOrderRequest buyLimitRequest = CreateOrderRequest.spotBuyLimit(spotAccountId, clientOrderId, symbol, bidPrice, new BigDecimal("2"));
Long buyLimitId = tradeService.createOrder(buyLimitRequest); Cancel orderTradeClient tradeService = TradeClient.create(HuobiOptions.builder()
.apiKey(Constants.API_KEY)
.secretKey(Constants.SECRET_KEY)
.build());
int cancelResult = tradeService.cancelOrder(clientOrderId); Cancel open ordersTradeClient tradeService = TradeClient.create(HuobiOptions.builder()
.apiKey(Constants.API_KEY)
.secretKey(Constants.SECRET_KEY)
.build());
BatchCancelOpenOrdersResult result = tradeService.batchCancelOpenOrders(BatchCancelOpenOrdersRequest.builder()
.accountId(spotAccountId)
.symbol(symbol)
.build()); Get order infoTradeClient tradeService = TradeClient.create(HuobiOptions.builder()
.apiKey(Constants.API_KEY)
.secretKey(Constants.SECRET_KEY)
.build());
Order getOrder = tradeService.getOrder(51210074624L); Historical ordersTradeClient tradeService = TradeClient.create(HuobiOptions.builder()
.apiKey(Constants.API_KEY)
.secretKey(Constants.SECRET_KEY)
.build());
List<Order> historyOrderList = tradeService.getOrdersHistory(OrderHistoryRequest.builder()
.symbol(symbol)
.startTime(1565107200000L)
.direction(QueryDirectionEnum.PREV)
.build()); Margin LoanAuthentication is required. These are examples for cross margin Apply loanCrossMarginClient marginService = CrossMarginClient.create(HuobiOptions.builder()
.apiKey(Constants.API_KEY)
.secretKey(Constants.SECRET_KEY)
.build());
Long applyId = marginService.applyLoan(CrossMarginApplyLoanRequest.builder()
.currency("usdt")
.amount(new BigDecimal("100"))
.build()); Repay loanCrossMarginClient marginService = CrossMarginClient.create(HuobiOptions.builder()
.apiKey(Constants.API_KEY)
.secretKey(Constants.SECRET_KEY)
.build());
marginService.repayLoan(CrossMarginRepayLoanRequest.builder()
.orderId(applyId)
.amount(loanAmount)
.build()); Loan historyCrossMarginClient marginService = CrossMarginClient.create(HuobiOptions.builder()
.apiKey(Constants.API_KEY)
.secretKey(Constants.SECRET_KEY)
.build());
List<Balance> balanceList = crossMarginAccount1.getBalanceList() Subscription exampleSubscribe trade updateMarketClient marketClient = MarketClient.create(new HuobiOptions());
marketClient.subMarketTrade(SubMarketTradeRequest.builder().symbol(symbol).build(), (marketTradeEvent) -> {
System.out.println("ch:" + marketTradeEvent.getCh());
System.out.println("ts:" + marketTradeEvent.getTs());
marketTradeEvent.getList().forEach(marketTrade -> {
System.out.println(marketTrade.toString());
});
}); ###Subscribe candlestick update MarketClient marketClient = MarketClient.create(new HuobiOptions());
marketClient.subCandlestick(SubCandlestickRequest.builder()
.symbol(symbol)
.interval(CandlestickIntervalEnum.MIN15)
.
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论