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. Using built-in date time formatter with UTC timezone.
  • Loading branch information
dp770 committed May 3, 2020
1 parent d173251 commit 63dc267
Showing 1 changed file with 13 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import static java.util.Collections.reverseOrder;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
Expand All @@ -15,44 +14,35 @@

/** 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 static DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(ZoneId.of("UTC"));
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();
timestamp = parseISOTimestampInUTC(orderbookTransaction.getParams().getTimestamp());
}

private long parseISOTimestampInUTC(String timestamp) {
if (timestamp != null) {
LocalDateTime time = LocalDateTime.parse(timestamp, DATE_FORMAT);
return time.toInstant(ZoneOffset.UTC).toEpochMilli();
}
return 0L;
sequence = orderBookTransaction.getParams().getSequence();
timestamp = ZonedDateTime.parse(orderBookTransaction.getParams().getTimestamp(), DATE_FORMAT)
.toInstant().toEpochMilli();
}

public HitbtcOrderBook toHitbtcOrderBook() {
Expand All @@ -69,7 +59,8 @@ public void updateOrderBook(HitbtcWebSocketOrderBookTransaction orderBookTransac
updateOrderBookItems(orderBookTransaction.getParams().getAsk(), asks);
updateOrderBookItems(orderBookTransaction.getParams().getBid(), bids);
sequence = orderBookTransaction.getParams().getSequence();
timestamp = parseISOTimestampInUTC(orderBookTransaction.getParams().getTimestamp());
timestamp = ZonedDateTime.parse(orderBookTransaction.getParams().getTimestamp(), DATE_FORMAT)
.toInstant().toEpochMilli();
}

private void updateOrderBookItems(
Expand Down

0 comments on commit 63dc267

Please sign in to comment.