From fe18777494f1eda9603de2046593c1bbf8a70876 Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Fri, 22 Dec 2023 08:48:13 +0300 Subject: [PATCH] okx_orderbook_timestamp --- .../org/knowm/xchange/okex/OkexAdapters.java | 15 +++---- .../okex/dto/marketdata/OkexOrderbook.java | 10 +---- .../okex/OkexStreamingMarketDataService.java | 42 +++++++------------ 3 files changed, 25 insertions(+), 42 deletions(-) diff --git a/xchange-okex/src/main/java/org/knowm/xchange/okex/OkexAdapters.java b/xchange-okex/src/main/java/org/knowm/xchange/okex/OkexAdapters.java index d7e9fe31d6e..afbfdf973da 100644 --- a/xchange-okex/src/main/java/org/knowm/xchange/okex/OkexAdapters.java +++ b/xchange-okex/src/main/java/org/knowm/xchange/okex/OkexAdapters.java @@ -190,27 +190,28 @@ public static OkexOrderRequest adaptOrder( } public static LimitOrder adaptLimitOrder( - OkexPublicOrder okexPublicOrder, Instrument instrument, OrderType orderType) { + OkexPublicOrder okexPublicOrder, Instrument instrument, OrderType orderType, Date timestamp) { return adaptOrderbookOrder( - okexPublicOrder.getVolume(), okexPublicOrder.getPrice(), instrument, orderType); + okexPublicOrder.getVolume(), okexPublicOrder.getPrice(), instrument, orderType,timestamp); } public static OrderBook adaptOrderBook( List okexOrderbooks, Instrument instrument) { List asks = new ArrayList<>(); List bids = new ArrayList<>(); + Date timeStamp = new Date(Long.parseLong(okexOrderbooks.get(0).getTs())); okexOrderbooks .get(0) .getAsks() - .forEach(okexAsk -> asks.add(adaptLimitOrder(okexAsk, instrument, OrderType.ASK))); + .forEach(okexAsk -> asks.add(adaptLimitOrder(okexAsk, instrument, OrderType.ASK, timeStamp))); okexOrderbooks .get(0) .getBids() - .forEach(okexBid -> bids.add(adaptLimitOrder(okexBid, instrument, OrderType.BID))); + .forEach(okexBid -> bids.add(adaptLimitOrder(okexBid, instrument, OrderType.BID, timeStamp))); - return new OrderBook(Date.from(Instant.now()), asks, bids); + return new OrderBook(timeStamp, asks, bids); } public static OrderBook adaptOrderBook( @@ -219,9 +220,9 @@ public static OrderBook adaptOrderBook( } public static LimitOrder adaptOrderbookOrder( - BigDecimal amount, BigDecimal price, Instrument instrument, Order.OrderType orderType) { + BigDecimal amount, BigDecimal price, Instrument instrument, Order.OrderType orderType, Date timestamp) { - return new LimitOrder(orderType, amount, instrument, "", null, price); + return new LimitOrder(orderType, amount, instrument, "", timestamp, price); } public static Ticker adaptTicker(OkexTicker okexTicker) { diff --git a/xchange-okex/src/main/java/org/knowm/xchange/okex/dto/marketdata/OkexOrderbook.java b/xchange-okex/src/main/java/org/knowm/xchange/okex/dto/marketdata/OkexOrderbook.java index e91b34e6c8c..951525a0cc2 100644 --- a/xchange-okex/src/main/java/org/knowm/xchange/okex/dto/marketdata/OkexOrderbook.java +++ b/xchange-okex/src/main/java/org/knowm/xchange/okex/dto/marketdata/OkexOrderbook.java @@ -3,7 +3,9 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import java.util.List; +import lombok.Getter; +@Getter public class OkexOrderbook { private final List asks; @@ -22,14 +24,6 @@ public OkexOrderbook( this.ts = ts; } - public List getAsks() { - return asks; - } - - public List getBids() { - return bids; - } - @Override public String toString() { return "OkexOrderbookResponse{" + "asks=" + asks + ", bids=" + bids + '}'; diff --git a/xchange-stream-okex/src/main/java/info/bitrich/xchangestream/okex/OkexStreamingMarketDataService.java b/xchange-stream-okex/src/main/java/info/bitrich/xchangestream/okex/OkexStreamingMarketDataService.java index 089d019aa61..aa22c24900f 100644 --- a/xchange-stream-okex/src/main/java/info/bitrich/xchangestream/okex/OkexStreamingMarketDataService.java +++ b/xchange-stream-okex/src/main/java/info/bitrich/xchangestream/okex/OkexStreamingMarketDataService.java @@ -105,13 +105,13 @@ public Observable getOrderBook(Instrument instrument, Object... args) // "books5" channel pushes 5 depth levels every time. String action = channelName.equals(ORDERBOOK5) ? "snapshot" : jsonNode.get("action").asText(); + List okexOrderbooks = + mapper.treeToValue( + jsonNode.get("data"), + mapper + .getTypeFactory() + .constructCollectionType(List.class, OkexOrderbook.class)); if ("snapshot".equalsIgnoreCase(action)) { - List okexOrderbooks = - mapper.treeToValue( - jsonNode.get("data"), - mapper - .getTypeFactory() - .constructCollectionType(List.class, OkexOrderbook.class)); OrderBook orderBook = OkexAdapters.adaptOrderBook(okexOrderbooks, instrument); orderBookMap.put(instId, orderBook); return Observable.just(orderBook); @@ -121,36 +121,24 @@ public Observable getOrderBook(Instrument instrument, Object... args) LOG.error(String.format("Failed to get orderBook, instId=%s.", instId)); return Observable.fromIterable(new LinkedList<>()); } - List asks = - mapper.treeToValue( - jsonNode.get("data").get(0).get("asks"), - mapper - .getTypeFactory() - .constructCollectionType(List.class, OkexPublicOrder.class)); - asks.forEach( + Date timestamp = new Timestamp( + Long.parseLong(okexOrderbooks.get(0).getTs())); + okexOrderbooks.get(0).getAsks().forEach( okexPublicOrder -> orderBook.update( OkexAdapters.adaptLimitOrder( - okexPublicOrder, instrument, Order.OrderType.ASK))); - - List bids = - mapper.treeToValue( - jsonNode.get("data").get(0).get("bids"), - mapper - .getTypeFactory() - .constructCollectionType(List.class, OkexPublicOrder.class)); - bids.forEach( + okexPublicOrder, instrument, Order.OrderType.ASK, timestamp))); + okexOrderbooks.get(0).getBids().forEach( okexPublicOrder -> orderBook.update( OkexAdapters.adaptLimitOrder( - okexPublicOrder, instrument, Order.OrderType.BID))); + okexPublicOrder, instrument, Order.OrderType.BID, timestamp))); if (orderBookUpdatesSubscriptions.get(instrument) != null) { orderBookUpdatesSubscriptions( instrument, - asks, - bids, - new Timestamp( - Long.parseLong(jsonNode.get("data").get(0).get("ts").asText()))); + okexOrderbooks.get(0).getAsks(), + okexOrderbooks.get(0).getBids(), + timestamp); } return Observable.just(orderBook);