-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporttemplate.html
313 lines (295 loc) · 13.6 KB
/
reporttemplate.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="chart.js"></script>
<script src="statistics.min.js"></script>
<title>{{indexid}} {{indexname}} ({{indexcurrency}}/{{indexvariant}}) Visualization. USE AT YOUR OWN RISK! {{indexstart}} to {{indexend}}</title>
<style>
.winner {
color: green;
}
.loser {
color: red;
}
</style>
<script>
function fetchJSON(res, func) {
fetch(res, {cache: 'no-cache'}).then(function(response) {return response.json()}).then(func);
}
function movingAverage(windowSize, data, datakey) {
const result = [];
const currentwindow = []
average = arr => arr.reduce( ( p, c ) => p + c, 0 ) / arr.length;
for (var i = 0; i < data.length; i++) {
const datapoint = data[i];
if (currentwindow.length < windowSize) {
currentwindow.push(datapoint[datakey]);
const sma = {
calc_date: datapoint.calc_date
};
sma[datakey] = null;
result.push(sma);
} else {
currentwindow.shift();
currentwindow.push(datapoint[datakey]);
const sma = {
calc_date: datapoint.calc_date
};
sma[datakey] = average(currentwindow);
result.push(sma);
}
}
return result;
}
</script>
</head>
<body>
<canvas id="chart_visualization"></canvas>
<canvas id="risk_return_changes"></canvas>
<table id="correlations">
<tr>
<td>ID</td>
<th>Name</th>
<th>Pearson's correlation</th>
<th>Stats. 7 Days</th>
<th>Stats. 30 Days</th>
<th>Stats. 3 Months</th>
<th>Stats. 6 Months</th>
<th>Stats. 1 Year</th>
<th>Stats. 2 Years</th>
<th>Stats. 3 Years</th>
</tr>
</table>
<script>
fetchJSON('stockdata.json', function(stock) {
var stockvalues = stock.levels;
var bodyVars = {
'{{indexid}}': 'metric',
};
var keys = Object.keys(stock.stocks);
var correlations = [];
for (var i = 0; i < keys.length; i++) {
var id = keys[i];
if ('level_eod' !== id) {
bodyVars[id] = 'metric';
var stats = new Statistics(stockvalues, bodyVars);
var r = stats.correlationCoefficient('{{indexid}}', id);
if (r) {
correlations.push({
id: id,
stock: stock.stocks[id],
correlationCoefficient: r.correlationCoefficient
});
}
}
}
correlations.sort(function(a, b) {
if (a.correlationCoefficient < b.correlationCoefficient) {
return 1;
}
if (a.correlationCoefficient > b.correlationCoefficient) {
return -1;
}
return 0;
});
for (var i = 0; i < correlations.length; i++) {
var correlation = correlations[i];
var row = document.getElementById('correlations').insertRow(-1);
var cell1 = row.insertCell(0);
cell1.innerHTML = '<a href="' + correlation.id + '.html">' + correlation.id + '</a>';
var cell2 = row.insertCell(1);
cell2.innerHTML = correlation.stock.name;
var cell3 = row.insertCell(2);
cell3.innerHTML = correlation.correlationCoefficient.toFixed(4);
if (correlation.stock.stats_7days) {
var cell = row.insertCell(3);
cell.innerHTML = 'Perf: ' + correlation.stock.stats_7days.performance + " %";
if (correlation.stock.stats_7days.winner) {
cell.className ='winner';
} else {
cell.className ='loser';
}
}
if (correlation.stock.stats_30days) {
var cell = row.insertCell(4);
cell.innerHTML = 'Perf: ' + correlation.stock.stats_30days.performance + " %";
if (correlation.stock.stats_30days.winner) {
cell.className ='winner';
} else {
cell.className ='loser';
}
}
if (correlation.stock.stats_90days) {
var cell = row.insertCell(5);
cell.innerHTML = 'Perf: ' + correlation.stock.stats_90days.performance + " %";
if (correlation.stock.stats_90days.winner) {
cell.className ='winner';
} else {
cell.className ='loser';
}
}
if (correlation.stock.stats_180days) {
var cell = row.insertCell(6);
cell.innerHTML = 'Perf: ' + correlation.stock.stats_180days.performance + " %";
if (correlation.stock.stats_180days.winner) {
cell.className ='winner';
} else {
cell.className ='loser';
}
}
if (correlation.stock.stats_365days) {
var cell = row.insertCell(7);
cell.innerHTML = 'Perf: ' + correlation.stock.stats_365days.performance + " %";
if (correlation.stock.stats_365days.winner) {
cell.className ='winner';
} else {
cell.className ='loser';
}
}
if (correlation.stock.stats_730days) {
var cell = row.insertCell(8);
cell.innerHTML = 'Perf: ' + correlation.stock.stats_730days.performance + " %";
if (correlation.stock.stats_730days.winner) {
cell.className ='winner';
} else {
cell.className ='loser';
}
}
if (correlation.stock.stats_1095days) {
var cell = row.insertCell(9);
cell.innerHTML = 'Perf: ' + correlation.stock.stats_1095days.performance + " %";
if (correlation.stock.stats_1095days.winner) {
cell.className ='winner';
} else {
cell.className ='loser';
}
}
}
const data = {
labels: stockvalues.map(a => a.calc_date),
datasets: [{
label: '{{indexid}} {{indexname}} ({{indexcurrency}}/{{indexvariant}}) (USE AT YOUR OWN RISK!) {{indexstart}} to {{indexend}}',
backgroundColor: '#2c3e50',
borderColor: '#2c3e50',
data: stockvalues,
parsing: {
xAxisKey: 'calc_date',
yAxisKey: '{{indexid}}'
},
pointRadius: 0
}, {
label: 'Moving Average 200 days',
backgroundColor: '#16a085',
borderColor: '#16a085',
data: movingAverage(200, stockvalues, '{{indexid}}'),
parsing: {
xAxisKey: 'calc_date',
yAxisKey: '{{indexid}}'
},
pointRadius: 0
}, {
label: 'Moving Average 37 days',
backgroundColor: '#d35400',
borderColor: '#d35400',
data: movingAverage(37, stockvalues, '{{indexid}}'),
parsing: {
xAxisKey: 'calc_date',
yAxisKey: '{{indexid}}'
},
pointRadius: 0
}]
};
const config = {
type: 'line',
data: data,
options: {
animation: false
},
plugins: []
};
const c = new Chart(document.getElementById('chart_visualization'), config);
const riskreturn_data = {
datasets: [
{
label: 'Risk-Return changes over time ({{indexid}})',
labels: ["7 days", "30 days", "90 days", "180 days", "1 year", "2 years", "3 years"],
data: [
{
x: stock.stocks['{{indexid}}'].stats_7days.volatility,
y: stock.stocks['{{indexid}}'].stats_7days.performance / 7 * 365,
},
{
x: stock.stocks['{{indexid}}'].stats_30days.volatility,
y: stock.stocks['{{indexid}}'].stats_30days.performance / 30 * 365,
},
{
x: stock.stocks['{{indexid}}'].stats_90days.volatility,
y: stock.stocks['{{indexid}}'].stats_90days.performance / 90 * 365,
},
{
x: stock.stocks['{{indexid}}'].stats_180days.volatility,
y: stock.stocks['{{indexid}}'].stats_180days.performance / 180 * 365,
},
{
x: stock.stocks['{{indexid}}'].stats_365days.volatility,
y: stock.stocks['{{indexid}}'].stats_365days.performance / 365 * 365,
},
{
x: stock.stocks['{{indexid}}'].stats_730days.volatility,
y: stock.stocks['{{indexid}}'].stats_730days.performance / 730 * 365,
},
{
x: stock.stocks['{{indexid}}'].stats_1095days.volatility,
y: stock.stocks['{{indexid}}'].stats_1095days.performance / 1095 * 365,
}
],
backgroundColor: 'rgb(255, 99, 132)',
showLine: true
}
]
};
const riskreturn_config = {
type: 'scatter',
data: riskreturn_data,
options: {
scales: {
x: {
title: {
display: true,
text: 'Risk / Volatility'
}
},
y: {
title: {
display: true,
text: 'Return / Performance'
}
}
},
plugins: {
legend: {
display: false,
},
title: {
display: true,
text: 'Rist / Return changes over time'
},
tooltip: {
callbacks: {
label: function(ctx) {
let label = ctx.dataset.labels[ctx.dataIndex];
label += " (" + ctx.parsed.x + ", " + ctx.parsed.y + ")";
return label;
}
}
}
},
}
};
const c1 = new Chart(document.getElementById('risk_return_changes'), riskreturn_config);
});
</script>
</body>
</html>