-
Notifications
You must be signed in to change notification settings - Fork 765
/
Copy pathfuturesChart.js
78 lines (66 loc) · 2.46 KB
/
futuresChart.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
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
const Binance = require( '../node-binance-api.js' )
const parseFuturesCandles = ( data ) => {
const [ time, open, high, low, close, volume, closeTime, quoteVolume, trades, takerBuyBaseVolume, takerBuyQuoteVolume ] = data
return {
time,
open,
high,
low,
close,
volume,
closeTime,
quoteVolume,
trades,
takerBuyBaseVolume,
takerBuyQuoteVolume
}
}
const sleep = async ( ms ) => {
return new Promise( resolve => {
setTimeout( resolve, ms )
} )
}
const main = async () => {
const binance = new Binance()
const symbol = 'ETHUSDT'
const interval = '1m'
binance.futuresChart( symbol, interval, async ( symbol, interval, wsChart ) => {
const candles = Object.values( wsChart ).reverse()
const latestCandle = candles[0]
if ( latestCandle.isFinal ) {
console.log( 'Candle closed for', new Date( latestCandle.time ).toLocaleDateString() )
// check with rest api if the candle values are identical
// sleep a bit to give Binance servers some time to update their REST apis
await sleep( 1000 )
let restChart = await binance.futuresCandles( symbol, interval )
restChart = restChart.map( parseFuturesCandles )
let hasMismatch = false
for ( const restKline of restChart ) {
const wsKline = wsChart[restKline.time]
if ( !wsKline ) {
console.log( restKline )
console.error( `WS Cache does not have kline for timestamp: ${ new Date( restKline.time ).toLocaleDateString() }` )
continue
}
if ( wsKline.isFinal ) {
// compare both now
for ( const key of Object.keys( restKline ) ) {
if ( key === 'isFinal' ) {
continue
}
const restValue = restKline[key]
const wsValue = wsKline[key]
if ( restValue !== wsValue ) {
console.log( `"${ key }" mismatch: ${ restValue } and ${ wsValue }` )
hasMismatch = true
}
}
}
}
if ( !hasMismatch ) {
console.log( '-- REST and WS in sync' )
}
}
} )
}
main()