forked from Mohamed1756/Crypto-Liquidation-Feed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliqData.js
More file actions
65 lines (54 loc) · 1.65 KB
/
liqData.js
File metadata and controls
65 lines (54 loc) · 1.65 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
const WebSocket = require('ws');
const fs = require('fs');
const { DateTime } = require('luxon');
const websocketUri = 'wss://fstream.binance.com/ws/!forceOrder@arr';
const filename = 'binance.csv';
if (!fs.existsSync(filename)) {
fs.writeFileSync(
filename,
['Symbol', 'Side', 'Order Type', 'Original Quantity', 'Liq Price', 'Order Status', 'TimeStamp', 'Value'].join(',') + '\n'
);
}
async function binanceLiquidations(uri, filename) {
const ws = new WebSocket(uri);
ws.on('open', () => {
console.log('WebSocket connected');
});
ws.on('message', (message) => {
try {
const msg = JSON.parse(message)['o'];
const symbol = msg['s'];
const side = msg['S'];
const orderType = msg['o'];
const quantity = parseFloat(msg['q']);
const averagePrice = parseFloat(msg['ap']);
const orderStatus = msg['X'];
const timestamp = parseInt(msg['T']);
const value = quantity * averagePrice;
// Convert timestamp to UTC datetime
const tradeTime = DateTime.fromMillis(timestamp).toUTC().toFormat('HH:mm:ss');
const data = [
symbol,
side,
orderType,
quantity.toString(),
averagePrice.toString(),
orderStatus,
tradeTime,
value.toString(),
];
fs.appendFileSync(filename, data.join(',') + '\n');
} catch (error) {
console.error(error);
}
});
ws.on('close', () => {
console.log('WebSocket closed');
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
});
}
binanceLiquidations(websocketUri, filename).catch((error) => {
console.error('An error occurred:', error);
});