Skip to content

Commit

Permalink
Merge pull request #4809 from rizer1980/okx_orderbook-timestamp_to-PR
Browse files Browse the repository at this point in the history
[OKX} orderbook orders timestamp
  • Loading branch information
timmolter authored Apr 8, 2024
2 parents 9d9b0b4 + fe18777 commit 7f53dca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<OkexOrderbook> okexOrderbooks, Instrument instrument) {
List<LimitOrder> asks = new ArrayList<>();
List<LimitOrder> 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(
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OkexPublicOrder> asks;
Expand All @@ -22,14 +24,6 @@ public OkexOrderbook(
this.ts = ts;
}

public List<OkexPublicOrder> getAsks() {
return asks;
}

public List<OkexPublicOrder> getBids() {
return bids;
}

@Override
public String toString() {
return "OkexOrderbookResponse{" + "asks=" + asks + ", bids=" + bids + '}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ public Observable<OrderBook> getOrderBook(Instrument instrument, Object... args)
// "books5" channel pushes 5 depth levels every time.
String action =
channelName.equals(ORDERBOOK5) ? "snapshot" : jsonNode.get("action").asText();
List<OkexOrderbook> okexOrderbooks =
mapper.treeToValue(
jsonNode.get("data"),
mapper
.getTypeFactory()
.constructCollectionType(List.class, OkexOrderbook.class));
if ("snapshot".equalsIgnoreCase(action)) {
List<OkexOrderbook> 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);
Expand All @@ -121,36 +121,24 @@ public Observable<OrderBook> getOrderBook(Instrument instrument, Object... args)
LOG.error(String.format("Failed to get orderBook, instId=%s.", instId));
return Observable.fromIterable(new LinkedList<>());
}
List<OkexPublicOrder> 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<OkexPublicOrder> 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);

Expand Down

0 comments on commit 7f53dca

Please sign in to comment.