-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathwebsocket_v2_orderbooks_snapshot.js
52 lines (49 loc) · 1.59 KB
/
websocket_v2_orderbooks_snapshot.js
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
var ba = require('bitcoinaverage');
var pub = 'your public key';
var secret = 'your secret key';
function reorder(orderbook, symbols){
var bprices = Object.values(orderbook[symbols[0]].bids);
sorted_bids = bprices.sort(function(a, b){
return a.price >= b.price ? -1 : 1;
});
var aprices = Object.values(orderbook[symbols[0]].asks);
sorted_asks = aprices.sort(function(a, b){
return a.price < b.price ? -1 : 1;
});
}
function connect_orderbook(exchange, symbols){
var ws = ba.websocketClient(pub, secret);
var BOOK = {};
symbols.forEach(function(symbol){
BOOK[symbol] = {
'asks': [],
'bids': []
};
});
var symbol = '';
ws.connectToOrderbookWebsocket(exchange, symbols, function(response){
symbol = response.data.symbol;
if(response.data.type === "snapshot"){
console.log("Got snapshot for " + symbol + response.data.asks.length);
console.log("Best ASK: ");
console.log(response.data.asks[0]);
console.log("Best BID: ");
console.log(response.data.bids[0]);
BOOK[symbol].asks = response.data.asks;
BOOK[symbol].bids = response.data.bids;
}else{
console.log("Now updates " + response.data.updates.length + response.data.symbol);
for (var i = 0; i < response.data.updates.length; i++){
var item = response.data.updates[i];
if(item.amount === 0){
delete(BOOK[symbol][item.side][item.price]);
}else{
BOOK[symbol][item.side][item.price] = item;
}
}
}
}, function(err){
console.log(err);
});
setInterval(function(){reorder(BOOK, symbols)}, 10000)
}