-
Notifications
You must be signed in to change notification settings - Fork 15
/
realtime.js
82 lines (75 loc) · 2.47 KB
/
realtime.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
78
79
80
81
82
"use strict"
const Client = require("./index").RealtimeClient;
//const Client = require("./index").ReplayClient;
const accessKey = "API_KEY_HERE";
const config = {
provider: 'REALTIME', //REALTIME or DELAYED_SIP or NASDAQ_BASIC or MANUAL
ipAddress: undefined,
tradesOnly: false,
isPublicKey: false
};
// const config = { //replay config
// provider: 'REALTIME', //REALTIME or DELAYED_SIP or NASDAQ_BASIC or MANUAL
// ipAddress: undefined,
// tradesOnly: false,
// isPublicKey: false,
// replayDate: '2023-10-06',
// replayAsIfLive: false,
// replayDeleteFileWhenDone: true
// };
let trades = new Map();
let quotes = new Map();
let maxTradeCount = 0;
let maxCountTrade = null;
let maxQuoteCount = 0;
let maxCountQuote = null;
function onTrade(trade) {
let key = trade.Symbol;
if (trades.has(key)) {
let value = trades.get(key);
if (value + 1 > maxTradeCount) {
trades.set(key, value + 1);
maxTradeCount = value + 1;
maxCountTrade = trade;
}
}
else trades.set(key, 1);
}
function onQuote(quote) {
let key = quote.Symbol + ":" + quote.Type;
if (quotes.has(key)) {
let value = quotes.get(key);
if (value + 1 > maxQuoteCount) {
quotes.set(key, value + 1);
maxQuoteCount = value + 1;
maxCountQuote = quote;
}
}
else quotes.set(key, 1);
}
let client = new Client(accessKey, onTrade, onQuote, config);
client.join("AAPL", false); //use $lobby for firehose.
//require("./index").replayToCsv("outputFilePath.csv", config, ["$lobby"], false, accessKey)
setInterval(() => {
if (maxTradeCount > 0) {
console.log("Most active security (by trade frequency): %s (%i updates)", JSON.stringify(maxCountTrade, (key, value) =>
typeof value === 'bigint'
? value.toString()
: value // return everything else unchanged
), maxTradeCount);
}
if (maxQuoteCount > 0) {
console.log("Most active security (by quote frequency): %s (%i updates)", JSON.stringify(maxCountQuote, (key, value) =>
typeof value === 'bigint'
? value.toString()
: value // return everything else unchanged
), maxQuoteCount);
}
let totalMsgCount = client.getTotalMsgCount();
if (totalMsgCount > 0) {
console.log("Total updates received = %i", totalMsgCount);
}
else {
console.log("No updates");
}
}, 10000);