Skip to content

Commit

Permalink
Update to v1.0.13
Browse files Browse the repository at this point in the history
1. Add performance test
2. Add orders update event (v2)
3. Refactor: rename market full MBP event
4. Delete CHANGE_LOG section in READE as it is duplicate with CHANGE_LOG and Github releases module
  • Loading branch information
eynzhang committed Apr 22, 2020
1 parent 6266e2b commit 8a23f95
Show file tree
Hide file tree
Showing 16 changed files with 514 additions and 95 deletions.
37 changes: 5 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,15 @@ This is Huobi Java SDK, This is a lightweight Java library, you can import to yo
The SDK supports both synchronous and asynchronous RESTful API invoking, and subscribe the market data from the Websocket connection.


## Huobi Java SDK Releases

## Huobi Java SDK (change log)
[Java SDK Change Log](https://github.com/HuobiRDCenter/huobi_Java/blob/master/CHANGE_LOG.md)


## Huobi Java SDK Download

- [Huobi Global API Java SDK version 1.0.11](https://github.com/HuobiRDCenter/huobi_Java/releases/tag/1.0.11)

- [Huobi Global API Java SDK version 1.0.10](https://github.com/HuobiRDCenter/huobi_Java/releases/tag/1.0.10)

- [Huobi Global API Java SDK version 1.0.9](https://github.com/HuobiRDCenter/huobi_Java/releases/tag/1.0.9)

- [Huobi Global API Java SDK version 1.0.8](https://github.com/HuobiRDCenter/huobi_Java/releases/tag/1.0.8)

- [Huobi Global API Java SDK version 1.0.7](https://github.com/HuobiRDCenter/huobi_Java/releases/tag/1.0.7)

- [Huobi Global API Java SDK version 1.0.6](https://github.com/HuobiRDCenter/huobi_Java/releases/tag/1.0.6)

- [Huobi Global API Java SDK version 1.0.5](https://github.com/HuobiRDCenter/huobi_Java/releases/tag/1.0.5)

- [Huobi Global API Java SDK version 1.0.4](https://github.com/HuobiRDCenter/huobi_Java/releases/tag/1.0.4)

- [Huobi Global API Java SDK version 1.0.3](https://github.com/HuobiRDCenter/huobi_Java/releases/tag/1.0.3)

- [Huobi Global API Java SDK version 1.0.2](https://github.com/HuobiRDCenter/huobi_Java/releases/tag/1.0.2)

- [Huobi Global API Java SDK version 1.0.1](https://github.com/HuobiRDCenter/huobi_Java/releases/tag/1.0.1)
Go to [Releases](https://github.com/HuobiRDCenter/huobi_Java/releases) page to view and download each release.


## Table of Contents

- [Beginning](#Beginning)

- [Installation](#Installation)
- [Quick Start](#Quick-Start)
- [Request vs. Subscription](#request-vs.-subscription)
Expand Down Expand Up @@ -800,11 +776,8 @@ subscriptionClient.subscribeOrderUpdateEvent("btcusdt", (orderEvent) -> {

```java
// Subscribe account change.
subscriptionClient.subscribeAccountEvent(BalanceMode.AVAILABLE, (accountEvent) -> {
for (AccountChange change : accountEvent.getData()) {
System.out.println(change.getAccountType());
System.out.println("Balance: " + change.getBalance());
}
subscriptionClient.subscribeAccountChangeV2Event(AccountChangeModeEnum.TOTAL, (event) -> {
System.out.println("===>"+ JSON.toJSONString(event));
});
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.huobi.sdk</groupId>
<artifactId>huobi-client</artifactId>
<version>1.0.12-SNAPSHOT</version>
<version>1.0.13-SNAPSHOT</version>

<properties>
<java.version>1.8</java.version>
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/huobi/client/SubscriptionClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.huobi.client.model.event.AccountListEvent;
import com.huobi.client.model.event.CandlestickEvent;
import com.huobi.client.model.event.CandlestickReqEvent;
import com.huobi.client.model.event.FullMarketDepthMBPEvent;
import com.huobi.client.model.event.MarketDepthFullMBPEvent;
import com.huobi.client.model.event.MarketBBOEvent;
import com.huobi.client.model.event.MarketDepthMBPEvent;
import com.huobi.client.model.event.OrderListEvent;
Expand All @@ -21,6 +21,7 @@
import com.huobi.client.model.event.TradeClearingEvent;
import com.huobi.client.model.event.TradeEvent;
import com.huobi.client.model.event.TradeStatisticsEvent;
import com.huobi.client.model.event.OrdersUpdateEvent;
import com.huobi.client.model.request.OrdersRequest;

/***
Expand Down Expand Up @@ -467,7 +468,16 @@ void request24HTradeStatisticsEvent(String symbols, boolean autoClose,
void requestAccountListEvent(boolean autoClose, SubscriptionListener<AccountListEvent> callback,
SubscriptionErrorHandler errorHandler);

void subscribeFullMarketDepthMBP(String symbol, MBPLevelEnums level, SubscriptionListener<FullMarketDepthMBPEvent> callback);
/**
* when the order status is changed, the order change event will be triggered
* @param symbol
* @param listener
* @param errorHandler
*/
void subscribeOrderChangeEvent(String symbol, SubscriptionListener<OrdersUpdateEvent> listener,
SubscriptionErrorHandler errorHandler);

void subscribeMarketDepthFullMBP(String symbol, MBPLevelEnums level, SubscriptionListener<MarketDepthFullMBPEvent> callback);

void subscribeMarketDepthMBP(String symbol, MBPLevelEnums level, SubscriptionListener<MarketDepthMBPEvent> callback);

Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/huobi/client/impl/WebSocketStreamClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.huobi.client.model.event.AccountListEvent;
import com.huobi.client.model.event.CandlestickEvent;
import com.huobi.client.model.event.CandlestickReqEvent;
import com.huobi.client.model.event.FullMarketDepthMBPEvent;
import com.huobi.client.model.event.MarketDepthFullMBPEvent;
import com.huobi.client.model.event.MarketBBOEvent;
import com.huobi.client.model.event.MarketDepthMBPEvent;
import com.huobi.client.model.event.OrderListEvent;
Expand All @@ -29,6 +29,7 @@
import com.huobi.client.model.event.TradeClearingEvent;
import com.huobi.client.model.event.TradeEvent;
import com.huobi.client.model.event.TradeStatisticsEvent;
import com.huobi.client.model.event.OrdersUpdateEvent;
import com.huobi.client.model.request.OrdersRequest;

public class WebSocketStreamClientImpl implements SubscriptionClient {
Expand Down Expand Up @@ -394,8 +395,16 @@ public void requestMarketDepthMBP(String symbol, MBPLevelEnums level, Subscripti
}

@Override
public void subscribeFullMarketDepthMBP(String symbol, MBPLevelEnums level,
SubscriptionListener<FullMarketDepthMBPEvent> callback) {
createConnection(requestImpl.subscribeFullMarketDepthMBPEvent(symbol, level, callback, null));
public void subscribeMarketDepthFullMBP(String symbol, MBPLevelEnums level,
SubscriptionListener<MarketDepthFullMBPEvent> callback) {
createConnection(requestImpl.subscribeMarketDepthFullMBPEvent(symbol, level, callback, null));
}

@Override
public void subscribeOrderChangeEvent(String symbol, SubscriptionListener<OrdersUpdateEvent> listener,
SubscriptionErrorHandler errorHandler) {
createConnection(requestImpl.subscribeOrderChangeEvent(symbol, listener, errorHandler));
}


}
57 changes: 51 additions & 6 deletions src/main/java/com/huobi/client/impl/WebsocketRequestImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import org.apache.commons.lang.time.DateFormatUtils;

import com.huobi.client.SubscriptionErrorHandler;
Expand Down Expand Up @@ -46,7 +50,7 @@
import com.huobi.client.model.event.AccountListEvent;
import com.huobi.client.model.event.CandlestickEvent;
import com.huobi.client.model.event.CandlestickReqEvent;
import com.huobi.client.model.event.FullMarketDepthMBPEvent;
import com.huobi.client.model.event.MarketDepthFullMBPEvent;
import com.huobi.client.model.event.MarketBBOEvent;
import com.huobi.client.model.event.MarketDepthMBPEvent;
import com.huobi.client.model.event.OrderListEvent;
Expand All @@ -56,6 +60,10 @@
import com.huobi.client.model.event.TradeClearingEvent;
import com.huobi.client.model.event.TradeEvent;
import com.huobi.client.model.event.TradeStatisticsEvent;
import com.huobi.client.model.event.OrdersUpdateEvent;
import com.huobi.client.model.event.OrdersCancellationEvent;
import com.huobi.client.model.event.OrdersCreationEvent;
import com.huobi.client.model.event.OrdersTradeEvent;
import com.huobi.client.model.request.OrdersRequest;

import static com.huobi.client.impl.utils.InternalUtils.await;
Expand Down Expand Up @@ -847,18 +855,18 @@ WebsocketRequest<OrderListEvent> requestOrderDetailEvent(
return request;
}

WebsocketRequest<FullMarketDepthMBPEvent> subscribeFullMarketDepthMBPEvent(String symbol, MBPLevelEnums level,
SubscriptionListener<FullMarketDepthMBPEvent> subscriptionListener, SubscriptionErrorHandler errorHandler) {
WebsocketRequest<FullMarketDepthMBPEvent> request = new WebsocketRequest<FullMarketDepthMBPEvent>(
WebsocketRequest<MarketDepthFullMBPEvent> subscribeMarketDepthFullMBPEvent(String symbol, MBPLevelEnums level,
SubscriptionListener<MarketDepthFullMBPEvent> subscriptionListener, SubscriptionErrorHandler errorHandler) {
WebsocketRequest<MarketDepthFullMBPEvent> request = new WebsocketRequest<MarketDepthFullMBPEvent>(
subscriptionListener, errorHandler);
request.connectionHandler = (connection) -> {
connection.send(Channels.fullMarketDepthMBPChannel(symbol, level));
connection.send(Channels.marketDepthFullMBPChannel(symbol, level));
};

request.jsonParser = (jsonWrapper) -> {
JsonWrapper data = jsonWrapper.getJsonObject("tick");

FullMarketDepthMBPEvent event = new FullMarketDepthMBPEvent();
MarketDepthFullMBPEvent event = new MarketDepthFullMBPEvent();
event.setSeqNum(data.getLong("seqNum"));

List<DepthEntry> bidList = new LinkedList<>();
Expand Down Expand Up @@ -965,5 +973,42 @@ WebsocketRequest<MarketDepthMBPEvent> requestMarketDepthMBPEvent(String symbol,

return request;
}

WebsocketRequest<OrdersUpdateEvent> subscribeOrderChangeEvent(String symbol, SubscriptionListener<OrdersUpdateEvent> listener,
SubscriptionErrorHandler errorHandler) {
WebsocketRequest<OrdersUpdateEvent> request = new WebsocketRequest<>(listener, errorHandler);

request.name = "Order Change Event";
// 新版本需设置版本号
request.signatureVersion = ApiSignatureV2.SIGNATURE_VERSION_VALUE;
request.authHandler = (connection) -> {
connection.send(Channels.orderChangeChannel(symbol));
};
request.jsonParser = (jsonWrapper) -> {
JsonWrapper data = jsonWrapper.getJsonObject("data");
return Optional.ofNullable(data).map(a -> {
String eventType = data.getString("eventType");
OrdersUpdateEvent result = null;
switch (eventType) {
case "creation":
result = JSON.parseObject(data.getJson().toString(), new TypeReference<OrdersCreationEvent>(){});
break;
case "trade":
result = JSON.parseObject(data.getJson().toString(), new TypeReference<OrdersTradeEvent>(){});
break;
case "cancellation":
result = JSON.parseObject(data.getJson().toString(), new TypeReference<OrdersCancellationEvent>(){});
break;

default:
break;
}

return result;
}).orElse(null);
};

return request;
}

}
25 changes: 16 additions & 9 deletions src/main/java/com/huobi/client/impl/utils/Channels.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,31 @@ public static String requestAccountListChannel() {
public static String marketDepthMBPChannel(String symbol, MBPLevelEnums level) {
return marketDepthMBPChannel(OP_SUB, symbol, level);
}

public static String fullMarketDepthMBPChannel(String symbol, MBPLevelEnums level) {
JSONObject json = new JSONObject();
String topic = "market." + symbol + ".mbp.refresh." + level.getLevels();
json.put(OP_SUB, topic);
json.put("id", System.currentTimeMillis() + "");
return json.toJSONString();
}

public static String requestMarketDepthMBPChannel(String symbol, MBPLevelEnums level) {
return marketDepthMBPChannel(OP_REQ, symbol, level);
}

public static String marketDepthMBPChannel(String op, String symbol, MBPLevelEnums level) {
private static String marketDepthMBPChannel(String op, String symbol, MBPLevelEnums level) {
JSONObject json = new JSONObject();
String topic = "market." + symbol + ".mbp." + level.getLevels();
json.put(op, topic);
json.put("cid", System.currentTimeMillis() + "");
return json.toJSONString();
}

public static String marketDepthFullMBPChannel(String symbol, MBPLevelEnums level) {
JSONObject json = new JSONObject();
String topic = "market." + symbol + ".mbp.refresh." + level.getLevels();
json.put(OP_SUB, topic);
json.put("id", System.currentTimeMillis() + "");
return json.toJSONString();
}

public static String orderChangeChannel(String symbol) {
JSONObject json = new JSONObject();
json.put("action", OP_SUB);
json.put("ch", "orders#".concat(symbol));
return json.toJSONString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import com.huobi.client.model.DepthEntry;

public class FullMarketDepthMBPEvent {
public class MarketDepthFullMBPEvent {

private Long seqNum;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.huobi.client.model.event;

@SuppressWarnings("serial")
public class OrdersCancellationEvent extends OrdersUpdateEvent {

private String remainAmt;

private Long lastActTime;

public String getRemainAmt() {
return remainAmt;
}

public void setRemainAmt(String remainAmt) {
this.remainAmt = remainAmt;
}

public Long getLastActTime() {
return lastActTime;
}

public void setLastActTime(Long lastActTime) {
this.lastActTime = lastActTime;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.huobi.client.model.event;

@SuppressWarnings("serial")
public class OrdersCreationEvent extends OrdersUpdateEvent {

private String orderPrice;

private String orderSize;

private String type;

private Long orderCreateTime;

public String getOrderPrice() {
return orderPrice;
}

public void setOrderPrice(String orderPrice) {
this.orderPrice = orderPrice;
}

public String getOrderSize() {
return orderSize;
}

public void setOrderSize(String orderSize) {
this.orderSize = orderSize;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Long getOrderCreateTime() {
return orderCreateTime;
}

public void setOrderCreateTime(Long orderCreateTime) {
this.orderCreateTime = orderCreateTime;
}

}
Loading

0 comments on commit 8a23f95

Please sign in to comment.