Skip to content

Commit

Permalink
Issue bitrich-info#561: [HitBTC] OrderBook is missing support of time…
Browse files Browse the repository at this point in the history
…stamp
  • Loading branch information
David Peisakhov committed Mar 23, 2020
1 parent 62f6d73 commit d173251
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package info.bitrich.xchangestream.hitbtc.dto;

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,16 +3,25 @@
import static java.util.Collections.reverseOrder;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
import org.knowm.xchange.hitbtc.v2.dto.HitbtcOrderBook;
import org.knowm.xchange.hitbtc.v2.dto.HitbtcOrderLimit;

/** Created by Pavel Chertalev on 15.03.2018. */
public class HitbtcWebSocketOrderBook {
// To parse dates in format of '2018-11-19T05:00:28.700Z'
private static DateTimeFormatter DATE_FORMAT =
new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").toFormatter();
private Map<BigDecimal, HitbtcOrderLimit> asks;
private Map<BigDecimal, HitbtcOrderLimit> bids;
private long sequence = 0;
private long timestamp = 0;

public HitbtcWebSocketOrderBook(HitbtcWebSocketOrderBookTransaction orderbookTransaction) {
createFromLevels(orderbookTransaction);
Expand All @@ -35,16 +44,22 @@ private void createFromLevels(HitbtcWebSocketOrderBookTransaction orderbookTrans
}

sequence = orderbookTransaction.getParams().getSequence();
timestamp = parseISOTimestampInUTC(orderbookTransaction.getParams().getTimestamp());
}

public HitbtcOrderBook toHitbtcOrderBook() {
HitbtcOrderLimit[] askLimits =
asks.entrySet().stream().map(Map.Entry::getValue).toArray(HitbtcOrderLimit[]::new);
private long parseISOTimestampInUTC(String timestamp) {
if (timestamp != null) {
LocalDateTime time = LocalDateTime.parse(timestamp, DATE_FORMAT);
return time.toInstant(ZoneOffset.UTC).toEpochMilli();
}
return 0L;
}

HitbtcOrderLimit[] bidLimits =
bids.entrySet().stream().map(Map.Entry::getValue).toArray(HitbtcOrderLimit[]::new);
public HitbtcOrderBook toHitbtcOrderBook() {
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 +69,7 @@ public void updateOrderBook(HitbtcWebSocketOrderBookTransaction orderBookTransac
updateOrderBookItems(orderBookTransaction.getParams().getAsk(), asks);
updateOrderBookItems(orderBookTransaction.getParams().getBid(), bids);
sequence = orderBookTransaction.getParams().getSequence();
timestamp = parseISOTimestampInUTC(orderBookTransaction.getParams().getTimestamp());
}

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;
}
}

0 comments on commit d173251

Please sign in to comment.