Skip to content
This repository has been archived by the owner on Jun 8, 2020. It is now read-only.

Issue #561: [HitBTC] OrderBook is missing support of timestamp #563

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package info.bitrich.xchangestream.hitbtc.dto;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see how this is being used. I'd expect to see a corresponding change in https://github.com/bitrich-info/xchange-stream/blob/develop/xchange-stream-hitbtc/src/main/java/info/bitrich/xchangestream/hitbtc/HitbtcStreamingMarketDataService.java to feed the timestamp to clients?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locally, I extended HitbtcAdapters.adaptOrderBook method from the org.knowm.xchange library to support timestamps. However, ideally, it should be updated at the source library, rather than "overridden" here. Having said that, I'm fine to commit it as well if it does make sense.


import org.knowm.xchange.hitbtc.v2.dto.HitbtcOrderBook;
import org.knowm.xchange.hitbtc.v2.dto.HitbtcOrderLimit;

import java.util.Date;

public class HitbtcOrderBookWithTimestamp extends HitbtcOrderBook {

private final Date timestamp;

HitbtcOrderBookWithTimestamp(Date timestamp, HitbtcOrderLimit[] asks, HitbtcOrderLimit[] bids) {
super(asks, bids);
this.timestamp = timestamp;
}

public Date getTimestamp() {
return timestamp;
}

@Override
public String toString() {
StringBuilder asks = new StringBuilder();
StringBuilder bids = new StringBuilder();

for (HitbtcOrderLimit ask : getAsks()) {
asks.append(ask).append(';');
}

for (HitbtcOrderLimit bid : getBids()) {
bids.append(bid).append(';');
}

return "HitbtcOrderBookWithTimestamp{" +
"asks=" + asks +
", bids=" + bids +
", timestamp=" + timestamp +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static java.util.Collections.reverseOrder;

import java.math.BigDecimal;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
import org.knowm.xchange.hitbtc.v2.dto.HitbtcOrderBook;
Expand All @@ -13,38 +15,37 @@ public class HitbtcWebSocketOrderBook {
private Map<BigDecimal, HitbtcOrderLimit> asks;
private Map<BigDecimal, HitbtcOrderLimit> bids;
private long sequence = 0;
private long timestamp = 0;

public HitbtcWebSocketOrderBook(HitbtcWebSocketOrderBookTransaction orderbookTransaction) {
createFromLevels(orderbookTransaction);
public HitbtcWebSocketOrderBook(HitbtcWebSocketOrderBookTransaction orderBookTransaction) {
createFromLevels(orderBookTransaction);
}

private void createFromLevels(HitbtcWebSocketOrderBookTransaction orderbookTransaction) {
private void createFromLevels(HitbtcWebSocketOrderBookTransaction orderBookTransaction) {
this.asks = new TreeMap<>(BigDecimal::compareTo);
this.bids = new TreeMap<>(reverseOrder(BigDecimal::compareTo));

for (HitbtcOrderLimit orderBookItem : orderbookTransaction.getParams().getAsk()) {
for (HitbtcOrderLimit orderBookItem : orderBookTransaction.getParams().getAsk()) {
if (orderBookItem.getSize().signum() != 0) {
asks.put(orderBookItem.getPrice(), orderBookItem);
}
}

for (HitbtcOrderLimit orderBookItem : orderbookTransaction.getParams().getBid()) {
for (HitbtcOrderLimit orderBookItem : orderBookTransaction.getParams().getBid()) {
if (orderBookItem.getSize().signum() != 0) {
bids.put(orderBookItem.getPrice(), orderBookItem);
}
}

sequence = orderbookTransaction.getParams().getSequence();
sequence = orderBookTransaction.getParams().getSequence();
timestamp = ZonedDateTime.parse(orderBookTransaction.getParams().getTimestamp()).toInstant().toEpochMilli();
}

public HitbtcOrderBook toHitbtcOrderBook() {
HitbtcOrderLimit[] askLimits =
asks.entrySet().stream().map(Map.Entry::getValue).toArray(HitbtcOrderLimit[]::new);

HitbtcOrderLimit[] bidLimits =
bids.entrySet().stream().map(Map.Entry::getValue).toArray(HitbtcOrderLimit[]::new);
HitbtcOrderLimit[] asks = this.asks.values().toArray(new HitbtcOrderLimit[0]);
HitbtcOrderLimit[] bids = this.bids.values().toArray(new HitbtcOrderLimit[0]);

return new HitbtcOrderBook(askLimits, bidLimits);
return new HitbtcOrderBookWithTimestamp(new Date(timestamp), asks, bids);
}

public void updateOrderBook(HitbtcWebSocketOrderBookTransaction orderBookTransaction) {
Expand All @@ -54,6 +55,7 @@ public void updateOrderBook(HitbtcWebSocketOrderBookTransaction orderBookTransac
updateOrderBookItems(orderBookTransaction.getParams().getAsk(), asks);
updateOrderBookItems(orderBookTransaction.getParams().getBid(), bids);
sequence = orderBookTransaction.getParams().getSequence();
timestamp = ZonedDateTime.parse(orderBookTransaction.getParams().getTimestamp()).toInstant().toEpochMilli();
}

private void updateOrderBookItems(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package info.bitrich.xchangestream.hitbtc.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import info.bitrich.xchangestream.hitbtc.dto.HitbtcWebSocketBaseParams;
import org.knowm.xchange.hitbtc.v2.dto.HitbtcOrderLimit;

/** Created by Pavel Chertalev on 15.03.2018. */
Expand All @@ -9,16 +10,19 @@ public class HitbtcWebSocketOrderBookParams extends HitbtcWebSocketBaseParams {
private final HitbtcOrderLimit[] ask;
private final HitbtcOrderLimit[] bid;
private final long sequence;
private final String timestamp;

public HitbtcWebSocketOrderBookParams(
@JsonProperty("property") String symbol,
@JsonProperty("symbol") String symbol,
@JsonProperty("sequence") long sequence,
@JsonProperty("ask") HitbtcOrderLimit[] ask,
@JsonProperty("bid") HitbtcOrderLimit[] bid) {
@JsonProperty("bid") HitbtcOrderLimit[] bid,
@JsonProperty("timestamp") String timestamp) {
super(symbol);
this.ask = ask;
this.bid = bid;
this.sequence = sequence;
this.timestamp = timestamp;
}

public HitbtcOrderLimit[] getAsk() {
Expand All @@ -32,4 +36,8 @@ public HitbtcOrderLimit[] getBid() {
public long getSequence() {
return sequence;
}

public String getTimestamp() {
return timestamp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
}
],
"symbol": "ETHBTC",
"sequence": 8073827
"sequence": 8073827,
"timestamp": "2020-05-03T18:26:28.714Z"
}
}