-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
203 lines (178 loc) · 5.85 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Infront code-test</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<script type="text/javascript">
var bollingerData = [], RSIinterm = [], RSIData = [];
document.addEventListener("DOMContentLoaded", function(event) {
getDataSet("data/Oslo_STL.json", function(dataSet) {
if (dataSet) {
calculateBolingerData('STL', JSON.parse(dataSet));
calculateRSIData('STL', JSON.parse(dataSet));
renderChart("STL", JSON.parse(dataSet));
}
});
getDataSet("data/Stockholm_ABB.json", function(dataSet) {
if (dataSet) {
calculateBolingerData('ABB', JSON.parse(dataSet));
calculateRSIData('ABB', JSON.parse(dataSet));
renderChart("ABB", JSON.parse(dataSet));
}
});
});
function standardDeviation(values) {
var avg = values.reduce(function(total, value) {
return total + value.last;
}, 0) / values.length;
var squareDiffs = values.map(function(value) {
var diff = value.last - avg;
var sqrDiff = diff * diff;
return sqrDiff;
});
var avgSquareDiff = squareDiffs.reduce(function(total, value) {
return total + value;
}, 0) / squareDiffs.length;
var stdDev = Math.sqrt(avgSquareDiff);
return {SMA: avg, STDEV: stdDev, UPPER: (avg + 2 * stdDev), LOWER: (avg - 2 * stdDev)};
}
function calculateRSIData(id, dataSet) {
RSIinterm = dataSet.reduce(function(rsiData, value, index) {
if (index > 0) {
var change = value.last - dataSet[index - 1].last;
var gain = change > 0 ? change : 0;
var loss = change < 0 ? -change : 0;
rsiData.push({date: value.date, change: change, gain: gain, loss: loss});
}
return rsiData;
}, []);
RSIData[id] = RSIinterm.reduce(function(rsiData, value, index) {
if (index >= 14) {
var avgGain = RSIinterm.slice(index - 14, index).reduce(function(total, value) {
return total + value.gain;
}, 0) / 14;
var avgLoss = RSIinterm.slice(index - 14, index).reduce(function(total, value) {
return total + value.loss;
}, 0) / 14;
var RS = avgGain / avgLoss;
var RSI = avgLoss === 0 ? 100 : 100 - (100 / (1 + RS));
rsiData.push(Object.assign(value, {avgGain: avgGain, avgLoss: avgLoss, RS: RS, RSI: RSI}));
}
return rsiData;
}, []);
}
function calculateBolingerData(id, dataSet) {
bollingerData[id] = dataSet.reduce(function(bollingerData, value, index) {
if (index > 19) {
var result = standardDeviation(dataSet.slice(index - 20, index));
bollingerData.push(Object.assign({date: value.date}, result));
}
return bollingerData;
}, []);
}
function renderChart(id, dataSet) {
var data = dataSet.map(function(item, index, array) {
return {
x: (new Date(item.date)).toISOString(),
y: item.last
};
});
var context = document.querySelector("#" + id).getContext('2d');
var stockChart = new Chart(context, {
type: 'line',
data: {
datasets: [
{
label: id,
fill: false,
data: data,
pointRadius: 0,
borderColor: "#1a5ecc"
},
{
label: `Upper Band ${id}`,
fill: '+2',
data: bollingerData[id].map(function(item, index, array) {
return {
x: (new Date(item.date)).toISOString(),
y: item.UPPER
};
}),
pointRadius: 0,
borderColor: "blue",
borderWidth: 0.3,
},
{
label: `20-day SMA ${id}`,
fill: false,
data: bollingerData[id].map(function(item, index, array) {
return {
x: (new Date(item.date)).toISOString(),
y: item.SMA
};
}),
pointRadius: 0,
borderColor: "red",
borderWidth: 0.8,
borderDash: [4, 4]
},
{
label: `Lower Band ${id}`,
fill: false,
data: bollingerData[id].map(function(item, index, array) {
return {
x: (new Date(item.date)).toISOString(),
y: item.LOWER
};
}),
pointRadius: 0.3,
borderColor: "blue",
borderWidth: 0.3,
},
{
label: `RSI ${id}`,
fill: false,
data: RSIData[id].map(function(item, index, array) {
return {
x: (new Date(item.date)).toISOString(),
y: item.RSI
};
}),
pointRadius: 0.3,
borderColor: "magenta",
borderWidth: 0.3,
}
]
},
options: {
responsive: false,
scales: {
xAxes: [{
type: "time",
display: true
}]
}
}
});
}
function getDataSet(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.status == 200) {
callback(xhr.responseText);
} else {
callback(null);
}
};
xhr.send();
}
</script>
</head>
<body>
<canvas id="STL" width="900" height="450"></canvas>
<canvas id="ABB" width="900" height="450"></canvas>
</body>
</html>