-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-FearAndGreedIndex.js
80 lines (62 loc) · 2.73 KB
/
MMM-FearAndGreedIndex.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
Module.register("MMM-FearAndGreedIndex", {
defaults: {
updateInterval: 3600000, // Update every hour
filename: "data.json" // Add a default filename here
},
start: function() {
this.fearAndGreedIndex = {};
this.cryptoFearAndGreedIndex = {};
this.sendSocketNotification("CONFIG", this.config);
},
getDom: function() {
var wrapper = document.createElement("div");
// Create and append the header
var header = document.createElement("header");
header.innerHTML = "Fear and Greed Index";
wrapper.appendChild(header);
var createIndexDisplay = (index, title) => {
var indexWrapper = document.createElement("div");
indexWrapper.innerHTML = "<strong>" + title + "</strong>";
if (index.value) {
var valueElement = document.createElement("div");
var indexValue = parseFloat(index.value).toFixed(2);
// Check for 'description' or 'value_classification' depending on index type
var indexCategory = index.description || index.value_classification || "Loading...";
valueElement.innerHTML = indexValue + " (" + indexCategory + ")";
valueElement.style.color = this.getColorForIndex(parseFloat(indexValue));
indexWrapper.appendChild(valueElement);
} else {
indexWrapper.innerHTML += ": Loading...";
}
return indexWrapper;
};
// Append traditional index
wrapper.appendChild(createIndexDisplay(this.fearAndGreedIndex, "Traditional"));
// Append crypto index
wrapper.appendChild(createIndexDisplay(this.cryptoFearAndGreedIndex, "Crypto"));
return wrapper;
},
// Helper function to interpolate color based on index value
getColorForIndex: function(value) {
if (value <= 45) {
return "rgb(0, 255, 0)"; // Green
} else if (value <= 55) {
return "rgb(255, 255, 0)"; // Yellow
} else if (value <= 75) {
return "rgb(255, 165, 0)"; // Orange
} else {
return "rgb(255, 0, 0)"; // Red
}
},
socketNotificationReceived: function(notification, payload) {
if (notification === "INDEX_DATA") {
console.log("Received INDEX_DATA:", payload); // Confirm that this data is received
this.fearAndGreedIndex = payload;
this.updateDom();
} else if (notification === "CRYPTO_INDEX_DATA") {
console.log("Received CRYPTO_INDEX_DATA:", payload); // Confirm that this data is received
this.cryptoFearAndGreedIndex = payload;
this.updateDom();
}
},
});