-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
binance.ts
109 lines (92 loc) · 2.67 KB
/
binance.ts
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
import {
getContextFromWsKey,
isWsPartialBookDepthEventFormatted,
MainClient,
OrderBookResponse,
WebsocketClient,
} from 'binance';
// import { OrderBookLevel, OrderBooksStore } from '../src';
import { OrderBookLevel, OrderBooksStore } from 'orderbooks';
const binanceWs = new WebsocketClient({
beautify: true,
});
const binanceRest = new MainClient();
const OrderBooks = new OrderBooksStore({
traceLog: true,
checkTimestamps: false,
maxDepth: 40,
});
// This example just dumps full snapshots into the orderbook store
function handleOrderbookSnapshot(symbol: string, snapshot: OrderBookResponse) {
// combine bids and asks
const { bids, asks } = snapshot;
const bidsArray = bids.map(([price, amount]) => {
return OrderBookLevel(symbol, +price, 'Buy', +amount);
});
const asksArray = asks.map(([price, amount]) => {
return OrderBookLevel(symbol, +price, 'Sell', +amount);
});
// store inititial snapshot
const storedOrderbook = OrderBooks.handleSnapshot(
symbol,
[...bidsArray, ...asksArray],
snapshot.lastUpdateId,
);
// log book state to screen
storedOrderbook.print();
}
// connect to a websocket and relay orderbook events to handlers
const symbol = 'BTCUSDT';
binanceWs.on('error', (msg) => {
console.error(new Date(), `Binance WS Error`, msg);
});
binanceWs.on('open', (data) => {
console.log(
new Date(),
'Binance WS connection opened open:',
data.wsKey,
data.ws.target.url,
);
});
binanceWs.on('reply', (data) => {
console.log(
new Date(),
'Binance WS log reply: ',
JSON.stringify(data, null, 2),
);
});
binanceWs.on('reconnecting', (data) => {
console.log(
new Date(),
'Binance WS ws automatically reconnecting.... ',
data?.wsKey,
);
});
binanceWs.on('reconnected', (data) => {
console.log(new Date(), 'Binance WS ws has reconnected ', data?.wsKey);
});
binanceWs.on('formattedMessage', (data) => {
// https://github.com/tiagosiebler/binance/blob/master/examples/ws-public-spot-orderbook.ts#L34
if (isWsPartialBookDepthEventFormatted(data)) {
const context = getContextFromWsKey(data.wsKey);
if (!context?.symbol) {
throw new Error(`Failed to extract context from event?`);
}
console.clear();
handleOrderbookSnapshot(context.symbol.toUpperCase(), data);
return;
}
});
async function startOrderbookMonitoring() {
try {
const snapshot = await binanceRest.getOrderBook({
symbol,
});
handleOrderbookSnapshot(symbol.toUpperCase(), snapshot);
} catch (e) {
console.error(`Failed to fetch orderbook snapshot via REST API`, e);
throw e;
}
binanceWs.subscribePartialBookDepths(symbol, 20, 100, 'spot');
}
startOrderbookMonitoring();