forked from KillerInk/binance-java-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAggTradesCacheExample.java
More file actions
executable file
·80 lines (68 loc) · 3.04 KB
/
AggTradesCacheExample.java
File metadata and controls
executable file
·80 lines (68 loc) · 3.04 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
package com.binance.api.examples;
import com.binance.api.client.api.sync.BinanceApiSpotRestClient;
import com.binance.api.client.api.BinanceApiWebSocketClient;
import com.binance.api.client.domain.market.AggTrade;
import com.binance.api.client.factory.BinanceSpotApiClientFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Illustrates how to use the aggTrades event stream to create a local cache of trades for a symbol.
*/
public class AggTradesCacheExample {
/**
* Key is the aggregate trade id, and the value contains the aggregated trade data, which is
* automatically updated whenever a new agg data stream event arrives.
*/
private Map<Long, AggTrade> aggTradesCache;
public AggTradesCacheExample(String symbol) {
initializeAggTradesCache(symbol);
startAggTradesEventStreaming(symbol);
}
/**
* Initializes the aggTrades cache by using the REST API.
*/
private void initializeAggTradesCache(String symbol) {
BinanceSpotApiClientFactory factory = BinanceSpotApiClientFactory.newInstance();
BinanceApiSpotRestClient client = factory.newRestClient();
List<AggTrade> aggTrades = client.getAggTrades(symbol.toUpperCase());
this.aggTradesCache = new HashMap<>();
for (AggTrade aggTrade : aggTrades) {
aggTradesCache.put(aggTrade.getAggregatedTradeId(), aggTrade);
}
}
/**
* Begins streaming of agg trades events.
*/
private void startAggTradesEventStreaming(String symbol) {
BinanceSpotApiClientFactory factory = BinanceSpotApiClientFactory.newInstance();
BinanceApiWebSocketClient client = factory.newWebSocketClient();
client.onAggTradeEvent(symbol.toLowerCase(), response -> {
Long aggregatedTradeId = response.getAggregatedTradeId();
AggTrade updateAggTrade = aggTradesCache.get(aggregatedTradeId);
if (updateAggTrade == null) {
// new agg trade
updateAggTrade = new AggTrade();
}
updateAggTrade.setAggregatedTradeId(aggregatedTradeId);
updateAggTrade.setPrice(response.getPrice());
updateAggTrade.setQuantity(response.getQuantity());
updateAggTrade.setFirstBreakdownTradeId(response.getFirstBreakdownTradeId());
updateAggTrade.setLastBreakdownTradeId(response.getLastBreakdownTradeId());
updateAggTrade.setBuyerMaker(response.isBuyerMaker());
// Store the updated agg trade in the cache
aggTradesCache.put(aggregatedTradeId, updateAggTrade);
System.out.println(updateAggTrade);
});
}
/**
* @return an aggTrades cache, containing the aggregated trade id as the key,
* and the agg trade data as the value.
*/
public Map<Long, AggTrade> getAggTradesCache() {
return aggTradesCache;
}
public static void main(String[] args) {
new AggTradesCacheExample("ETHBTC");
}
}