-
Notifications
You must be signed in to change notification settings - Fork 2
/
MMM-MktIndex.js
210 lines (188 loc) · 5.88 KB
/
MMM-MktIndex.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
String.prototype.hashCode = function() {
let hash = 0;
if (this.length == 0) {
return hash;
}
for (let i = 0; i < this.length; i++) {
let char = this.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
const header = ["symbol", "price", "close", "change", "changeP"]
const headerTitle = ["Symbol", "Cur.Price", "Prev.Close", "CHG", "CHG%"]
let marketIsOpen = false;
let initDone = false;
Module.register("MMM-MktIndex", {
defaults: {
timeFormat: "DD-MM HH:mm",
symbols: ["^DJI", "^IXIC", "^GSPC", "^TNX", "CL=F", "EURUSD=X"],
alias: ["DOW 30", "Nasdaq", "S&P 500", "10yr Bond", "Crude Oil", "EUR/USD"],
updateInterval: 3 * 60,
debug: false,
},
getScripts: function() {
return ["moment.js"];
},
getStyles: function() {
return ["MMM-MktIndex.css"]
},
start: function() {
if (!initDone) {
this.sendSocketNotification("INIT", this.config);
initDone = true;
}
},
updateMarket: function() {
if (!initDone) this.start();
this.sendSocketNotification("UPDATE", this.config);
},
checkMarketOpen: function(firstCheck = false) {
// API is limited to 500 requests/month.
// After first cycle, check for market open (M..F between 09:30..16:00 Eastern Time)
const now = new Date();
const dayOfWeek = now.getDay();
const clockMins = now.getMinutes() + 60 * now.getHours();
if ((dayOfWeek > 0 && dayOfWeek < 6) &&
(clockMins >= ((9 * 60) + 30) && clockMins <= (16 * 60))) {
if (!marketIsOpen) {
// Get opening quotes
marketIsOpen = true;
this.updateMarket();
} else {
this.updateMarket();
}
} else {
// Get closing quotes
if (marketIsOpen || firstCheck) {
marketIsOpen = false;
this.updateMarket();
}
}
},
getDom: function() {
let wrapper = document.createElement("div");
wrapper.id = "MKTINDEX";
return wrapper;
},
getStockName: function(symbol) {
let stockAlias = symbol;
let i = this.config.symbols.indexOf(symbol);
if (this.config.symbols.length == this.config.alias.length) {
stockAlias = (this.config.alias[i]) ? this.config.alias[i] : stockAlias;
}
return stockAlias;
},
prepareTable: function() {
let wrapper = document.getElementById("MKTINDEX");
wrapper.innerHTML = "";
let tbl = document.createElement("table");
tbl.id = "MKTINDEX_TABLE";
let thead = document.createElement("thead");
let tr = document.createElement("tr");
for (let i in header) {
let td = document.createElement("td");
td.innerHTML = headerTitle[i];
td.className = header[i];
tr.appendChild(td)
}
thead.appendChild(tr);
tbl.appendChild(thead);
for (let i in this.config.symbols) {
const stock = this.config.symbols[i];
const hashId = stock.hashCode();
let tr = document.createElement("tr");
tr.className = "stock";
tr.id = "STOCK_" + hashId;
for (let j in header) {
let td = document.createElement("td");
const stockAlias = this.getStockName(stock);
td.innerHTML = (j != 0) ? "---" : stockAlias;
td.className = header[j];
td.id = header[j] + "_" + hashId;
tr.appendChild(td);
}
tbl.appendChild(tr);
}
wrapper.appendChild(tbl);
let tl = document.createElement("div");
tl.className = "tagline";
tl.id = "MKTINDEX_TAGLINE";
tl.innerHTML = "Last updated: ";
wrapper.appendChild(tl);
},
notificationReceived: function(notifyID, payload) {
if (notifyID == "DOM_OBJECTS_CREATED") {
this.prepareTable();
// First callback when market opens
marketIsOpen = false;
let _this = this;
// Start 3min timer check
_this.checkMarketOpen(true);
setInterval(function() {
_this.checkMarketOpen();
}, this.config.updateInterval * 1000);
}
},
socketNotificationReceived: function(notifyID, payload) {
if (notifyID == "UPDATE") {
const numItems = payload.length;
for (let i= 0; i < numItems; i++) {
let item = payload[i];
if (item.hasOwnProperty('symbol')) {
if (this.config.symbols.indexOf(item.symbol) >= 0) {
this.update(item);
}
}
}
}
},
dpyFmt: function(value) {
return value.toLocaleString('en-US',
{ minimumFractionDigits: 2, maximumFractionDigits: 2 });
},
update: function(item) {
const stock = {
"symbol": item.symbol,
"price": this.dpyFmt(item.regularMarketPrice),
"close": this.dpyFmt(item.regularMarketPreviousClose),
"change": item.regularMarketChange, // Format after sign test
"changeP": this.dpyFmt(item.regularMarketChangePercent) + '%',
"requestTime": moment().format(this.config.timeFormat),
"hash": item.symbol.hashCode()
}
this.drawTable(stock);
},
drawTable: function(stock) {
const hash = stock.hash;
let tr = document.getElementById("STOCK_" + hash);
let ud = "";
for (let j = 1 ; j <= 4 ; j++) {
const tdId = header[j] + "_" + hash;
let td = document.getElementById(tdId);
td.className = header[j];
if (header[j] == "change") {
if (stock[header[j]] > 0) {
ud = "up";
} else if (stock[header[j]] < 0) {
ud = "down";
}
td.innerHTML = this.dpyFmt(stock[header[j]]);
} else {
td.innerHTML = stock[header[j]];
}
}
tr.className = "animated stock " + ud;
let tl = document.getElementById("MKTINDEX_TAGLINE");
tl.innerHTML = "Last updated: " + stock.requestTime;
setTimeout(()=>{
tr.className = "stock " + ud;
}, 1500);
},
log: function (msg) {
if (this.config && this.config.debug) {
console.log(this.name + ": ", (msg));
}
},
});