Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OKX} orderbook orders timestamp #4809

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading