-
Notifications
You must be signed in to change notification settings - Fork 794
/
DepthCacheExample.java
146 lines (124 loc) · 4.84 KB
/
DepthCacheExample.java
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
package com.binance.api.examples;
import com.binance.api.client.BinanceApiClientFactory;
import com.binance.api.client.BinanceApiRestClient;
import com.binance.api.client.BinanceApiWebSocketClient;
import com.binance.api.client.domain.market.OrderBook;
import com.binance.api.client.domain.market.OrderBookEntry;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
/**
* Illustrates how to use the depth event stream to create a local cache of bids/asks for a symbol.
*/
public class DepthCacheExample {
private static final String BIDS = "BIDS";
private static final String ASKS = "ASKS";
private long lastUpdateId;
private Map<String, NavigableMap<BigDecimal, BigDecimal>> depthCache;
public DepthCacheExample(String symbol) {
initializeDepthCache(symbol);
startDepthEventStreaming(symbol);
}
/**
* Initializes the depth cache by using the REST API.
*/
private void initializeDepthCache(String symbol) {
BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance();
BinanceApiRestClient client = factory.newRestClient();
OrderBook orderBook = client.getOrderBook(symbol.toUpperCase(), 10);
this.depthCache = new HashMap<>();
this.lastUpdateId = orderBook.getLastUpdateId();
NavigableMap<BigDecimal, BigDecimal> asks = new TreeMap<>(Comparator.reverseOrder());
for (OrderBookEntry ask : orderBook.getAsks()) {
asks.put(new BigDecimal(ask.getPrice()), new BigDecimal(ask.getQty()));
}
depthCache.put(ASKS, asks);
NavigableMap<BigDecimal, BigDecimal> bids = new TreeMap<>(Comparator.reverseOrder());
for (OrderBookEntry bid : orderBook.getBids()) {
bids.put(new BigDecimal(bid.getPrice()), new BigDecimal(bid.getQty()));
}
depthCache.put(BIDS, bids);
}
/**
* Begins streaming of depth events.
*/
private void startDepthEventStreaming(String symbol) {
BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance();
BinanceApiWebSocketClient client = 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.
*/
private void updateOrderBook(NavigableMap<BigDecimal, BigDecimal> lastOrderBookEntries, List<OrderBookEntry> orderBookDeltas) {
for (OrderBookEntry orderBookDelta : orderBookDeltas) {
BigDecimal price = new BigDecimal(orderBookDelta.getPrice());
BigDecimal qty = new BigDecimal(orderBookDelta.getQty());
if (qty.compareTo(BigDecimal.ZERO) == 0) {
// qty=0 means remove this level
lastOrderBookEntries.remove(price);
} else {
lastOrderBookEntries.put(price, qty);
}
}
}
public NavigableMap<BigDecimal, BigDecimal> getAsks() {
return depthCache.get(ASKS);
}
public NavigableMap<BigDecimal, BigDecimal> getBids() {
return depthCache.get(BIDS);
}
/**
* @return the best ask in the order book
*/
private Map.Entry<BigDecimal, BigDecimal> getBestAsk() {
return getAsks().lastEntry();
}
/**
* @return the best bid in the order book
*/
private Map.Entry<BigDecimal, BigDecimal> getBestBid() {
return getBids().firstEntry();
}
/**
* @return a depth cache, containing two keys (ASKs and BIDs), and for each, an ordered list of book entries.
*/
public Map<String, NavigableMap<BigDecimal, BigDecimal>> getDepthCache() {
return depthCache;
}
/**
* Prints the cached order book / depth of a symbol as well as the best ask and bid price in the book.
*/
private void printDepthCache() {
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".
*/
private static String toDepthCacheEntryString(Map.Entry<BigDecimal, BigDecimal> depthCacheEntry) {
return depthCacheEntry.getKey().toPlainString() + " / " + depthCacheEntry.getValue();
}
public static void main(String[] args) {
new DepthCacheExample("ETHBTC");
}
}