-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
204 lines (169 loc) · 7.4 KB
/
app.js
File metadata and controls
204 lines (169 loc) · 7.4 KB
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
import * as fs from 'fs';
import {parse} from 'csv-parse/sync';
import {got} from "got";
import * as Handlebars from 'handlebars';
function formatDate(date) {
let month = '' + (date.getMonth() + 1);
let day = '' + date.getDate();
const year = date.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('');
}
const today = new Date();
const todayminus4years = new Date();
todayminus4years.setDate(todayminus4years.getDate() - 365 * 4);
const indexstart = formatDate(todayminus4years);
const indexend = formatDate(today);
(async () => {
const stockdata = {stocks: {}, levels: []};
const reporttemplatedata = fs.readFileSync('reporttemplate.html', {encoding: 'utf8', flag: 'r'});
const reporttemplate = Handlebars.default.compile(reporttemplatedata);
const csvdata = fs.readFileSync('indexlist.csv', {encoding: 'utf8', flag: 'r'})
const records = parse(csvdata, {delimiter: ',', from_line: 2});
for (let i = 0; i < records.length; i++) {
const record = records[i];
const indexdata = {
indexid: record[0],
indexname: record[1],
indexcurrency: record[2],
indexvariant: record[3],
indexstart: indexstart,
indexend: indexend
};
stockdata.stocks[indexdata.indexid] = {
id: indexdata.indexid,
name: indexdata.indexname,
currency: indexdata.indexcurrency,
indexvariant: indexdata.indexvariant,
};
console.log("Downloading indexdata " + indexdata.indexid + " " + indexdata.indexname);
const url = "https://app2.msci.com/products/service/index/indexmaster/getLevelDataForGraph?currency_symbol=" + indexdata.indexcurrency + "&index_variant=" + indexdata.indexvariant + "&start_date=" + indexdata.indexstart + "&end_date=" + indexdata.indexend + "&data_frequency=DAILY&index_codes=" + indexdata.indexid;
const json = await got(url).json();
const indexvalues = json.indexes.INDEX_LEVELS;
for (let j = 0; j < indexvalues.length; j++) {
const index = indexvalues[j];
let foundsomething = false;
for (let k = 0; k < stockdata.levels.length; k++) {
const stock = stockdata.levels[k];
if (stock.calc_date === index.calc_date) {
stock["" + indexdata.indexid + ""] = index.level_eod;
foundsomething = true;
}
}
if (!foundsomething) {
const newentry = {};
newentry["calc_date"] = index.calc_date;
newentry["" + indexdata.indexid + ""] = index.level_eod;
stockdata.levels.push(newentry);
}
}
fs.writeFileSync('web/' + indexdata.indexid + '.json', JSON.stringify(json));
const report = reporttemplate(indexdata);
fs.writeFileSync('web/' + indexdata.indexid + '.html', report);
}
function stockDataFrom(stockid, refdate) {
const levels = [];
const refAsInt = parseInt(formatDate(refdate))
for (let i = stockdata.levels.length -1; i >=0 ; i--) {
let level = stockdata.levels[i];
if (level.calc_date >= refAsInt) {
if (level[stockid]) {
levels.push(level);
}
}
}
levels.sort(function(a, b) {
if (a.calc_date > b.calc_date) {
return 1;
}
if (a.calc_date < b.calc_date) {
return -1;
}
return 0;
});
return levels;
}
function performanceInPercent(oldvalue, newvalue) {
const ratio = newvalue / oldvalue;
if (ratio >= 1) {
return ((ratio - 1) * 100).toFixed(2);
}
return (-((1 - ratio) * 100)).toFixed(2) ;
}
// We have to calculate the returns and rists per stock
var stockids = Object.keys(stockdata.stocks);
var calcperiods = [7, 30, 90, 180, 365, 730, 1095];
stockiter: for (var i = 0; i < stockids.length; i++) {
const stockid = stockids[i];
const stock = stockdata.stocks[stockid];
console.log("Calculating performance metrics for " + stockid + " " + stock.name);
for (var j = 0; j < calcperiods.length; j++) {
const period = calcperiods[j];
console.log(" Today minus " + period + " days");
const todayminus = new Date();
todayminus.setDate(today.getDate() - period);
const levels = stockDataFrom(stockid, todayminus);
console.log(" -> " + levels.length + " datapoints");
if (levels.length > 2) {
const currentlevel = levels[levels.length - 1][stockid];
const earliestlevel = levels[0][stockid];
let average = 0;
let last = currentlevel;
for (var k = 0; k < levels.length; k++) {
const p = performanceInPercent(last, levels[k][stockid]);
average = average + parseFloat(p);
last = levels[k][stockid];
}
average = average / levels.length;
let volatility = 0;
last = currentlevel;
for (var k = 0; k < levels.length; k++) {
const p = performanceInPercent(last, levels[k][stockid]);
const delta = parseFloat(p) - average;
volatility = volatility + (delta * delta);
last = levels[k][stockid];
}
volatility = volatility / levels.length;
volatility = Math.sqrt(volatility) * Math.sqrt(252);
const perf = performanceInPercent(earliestlevel, currentlevel);
stock["stats_" + period + "days"] = {
performance: perf,
winner: perf >= 0 ? true : false,
volatility: volatility.toFixed(2),
perfvolratio: (perf / volatility).toFixed(2),
startdate: levels[0].calc_date,
startleveleod: earliestlevel,
enddate: levels[levels.length - 1].calc_date,
endleveleod: currentlevel,
};
} else {
continue stockiter;
}
}
}
console.log("Writing index.html");
const indextemplatedata = fs.readFileSync('indextemplate.html', {encoding: 'utf8', flag: 'r'});
const indextemplate = Handlebars.default.compile(indextemplatedata);
const stockssorted = [];
for (var i = 0; i < stockids.length; i++) {
const stockid = stockids[i];
stockssorted.push(stockdata.stocks[stockid]);
}
stockssorted.sort(function(a, b) {
if (a.stats_365days && b.stats_365days) {
if (parseFloat(a.stats_365days.perfvolratio) < parseFloat(b.stats_365days.perfvolratio)) {
return 1;
}
if (parseFloat(a.stats_365days.perfvolratio) > parseFloat(b.stats_365days.perfvolratio)) {
return -1;
}
}
return 0;
});
const index = indextemplate({stockdata: stockdata, indexstart : indexstart, indexend: indexend, sorted: stockssorted});
fs.writeFileSync('web/index.html', index);
fs.writeFileSync('web/stockdata.json', JSON.stringify(stockdata));
})();