- Notifications
You must be signed in to change notification settings - Fork 762
Expand file tree
/
Copy pathDepthCacheExample.java
More file actions
Latest commit
146 lines (124 loc) · 4.84 KB
/
Copy pathDepthCacheExample.java
File metadata and controls
146 lines (124 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
packagecom.binance.api.examples;
importcom.binance.api.client.BinanceApiClientFactory;
importcom.binance.api.client.BinanceApiRestClient;
importcom.binance.api.client.BinanceApiWebSocketClient;
importcom.binance.api.client.domain.market.OrderBook;
importcom.binance.api.client.domain.market.OrderBookEntry;
importjava.math.BigDecimal;
importjava.util.Comparator;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importjava.util.NavigableMap;
importjava.util.TreeMap;
/**
* Illustrates how to use the depth event stream to create a local cache of bids/asks for a symbol.
*/
publicclassDepthCacheExample {
privatestaticfinalStringBIDS = "BIDS";
privatestaticfinalStringASKS = "ASKS";
privatelonglastUpdateId;
privateMap<String, NavigableMap<BigDecimal, BigDecimal>> depthCache;
publicDepthCacheExample(Stringsymbol) {
initializeDepthCache(symbol);
startDepthEventStreaming(symbol);
}
/**
* Initializes the depth cache by using the REST API.
*/
privatevoidinitializeDepthCache(Stringsymbol) {
BinanceApiClientFactoryfactory = BinanceApiClientFactory.newInstance();
BinanceApiRestClientclient = factory.newRestClient();
OrderBookorderBook = client.getOrderBook(symbol.toUpperCase(), 10);
this.depthCache = newHashMap<>();
this.lastUpdateId = orderBook.getLastUpdateId();
NavigableMap<BigDecimal, BigDecimal> asks = newTreeMap<>(Comparator.reverseOrder());
for (OrderBookEntryask : orderBook.getAsks()) {
asks.put(newBigDecimal(ask.getPrice()), newBigDecimal(ask.getQty()));
}
depthCache.put(ASKS, asks);
NavigableMap<BigDecimal, BigDecimal> bids = newTreeMap<>(Comparator.reverseOrder());
for (OrderBookEntrybid : orderBook.getBids()) {
bids.put(newBigDecimal(bid.getPrice()), newBigDecimal(bid.getQty()));
}
depthCache.put(BIDS, bids);
}
/**
* Begins streaming of depth events.
*/
privatevoidstartDepthEventStreaming(Stringsymbol) {
BinanceApiClientFactoryfactory = BinanceApiClientFactory.newInstance();
BinanceApiWebSocketClientclient = factory.newWebSocketClient();
client.onDepthEvent(symbol.toLowerCase(), response -> {
if (response.getUpdateId() > lastUpdateId) {
System.out.println(response);
lastUpdateId = response.getUpdateId();
updateOrderBook(getAsks(), response.getAsks());
updateOrderBook(getBids(), response.getBids());
printDepthCache();
}
});
}
/**
* Updates an order book (bids or asks) with a delta received from the server.
*
* Whenever the qty specified is ZERO, it means the price should was removed from the order book.
*/
privatevoidupdateOrderBook(NavigableMap<BigDecimal, BigDecimal> lastOrderBookEntries, List<OrderBookEntry> orderBookDeltas) {
for (OrderBookEntryorderBookDelta : orderBookDeltas) {
BigDecimalprice = newBigDecimal(orderBookDelta.getPrice());
BigDecimalqty = newBigDecimal(orderBookDelta.getQty());
if (qty.compareTo(BigDecimal.ZERO) == 0) {
// qty=0 means remove this level
lastOrderBookEntries.remove(price);
} else {
lastOrderBookEntries.put(price, qty);
}
}
}
publicNavigableMap<BigDecimal, BigDecimal> getAsks() {
returndepthCache.get(ASKS);
}
publicNavigableMap<BigDecimal, BigDecimal> getBids() {
returndepthCache.get(BIDS);
}
/**
* @return the best ask in the order book
*/
privateMap.Entry<BigDecimal, BigDecimal> getBestAsk() {
returngetAsks().lastEntry();
}
/**
* @return the best bid in the order book
*/
privateMap.Entry<BigDecimal, BigDecimal> getBestBid() {
returngetBids().firstEntry();
}
/**
* @return a depth cache, containing two keys (ASKs and BIDs), and for each, an ordered list of book entries.
*/
publicMap<String, NavigableMap<BigDecimal, BigDecimal>> getDepthCache() {
returndepthCache;
}
/**
* Prints the cached order book / depth of a symbol as well as the best ask and bid price in the book.
*/
privatevoidprintDepthCache() {
System.out.println(depthCache);
System.out.println("ASKS:");
getAsks().entrySet().forEach(entry -> System.out.println(toDepthCacheEntryString(entry)));
System.out.println("BIDS:");
getBids().entrySet().forEach(entry -> System.out.println(toDepthCacheEntryString(entry)));
System.out.println("BEST ASK: " + toDepthCacheEntryString(getBestAsk()));
System.out.println("BEST BID: " + toDepthCacheEntryString(getBestBid()));
}
/**
* Pretty prints an order book entry in the format "price / quantity".
*/
privatestaticStringtoDepthCacheEntryString(Map.Entry<BigDecimal, BigDecimal> depthCacheEntry) {
returndepthCacheEntry.getKey().toPlainString() + " / " + depthCacheEntry.getValue();
}
publicstaticvoidmain(String[] args) {
newDepthCacheExample("ETHBTC");
}
}