forked from KibaeKim/SectorTradingAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
117 lines (105 loc) · 3.24 KB
/
main.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
const csv = require('csv-parser');
const fs = require('fs');
const etfDirectory = 'VanguardSectorETFs';
var res = {};
fs.readdir(etfDirectory, function (err, files) {
if (err) {
console.log('Could not list the directory.', err);
process.exit(1);
}
// Reading each csv file in folder VanguardSectorETFs
files.forEach((file, index) => {
fs.createReadStream(`${etfDirectory}/${file}`)
.pipe(csv())
.on('data', (data) => {
var etf = file.substring(0, 3);
var date = data.Date;
var open = data.Open;
var close = data.Close;
// Making a new array if this is the first piece of data on this date
if (!res[date]) {
res[date] = [];
}
res[date].push({ name: etf, open: open, close: close });
})
.on('end', () => {
// Run mainFunction() after reading the last file
if (index == files.length - 1) {
mainFunction();
}
});
});
});
function mainFunction() {
bestPerformers = [];
Object.keys(res)
.sort()
.forEach((date, index) => {
var bestPerformance = Number.NEGATIVE_INFINITY;
var bestEtf = '';
res[date].forEach((etf) => {
etfPerformance = (etf.close - etf.open) / etf.open;
if (etfPerformance > bestPerformance) {
bestEtf = etf.name;
bestPerformance = etfPerformance;
}
});
bestPerformers.push(bestEtf);
});
performance = [];
cash = 1000.0;
Object.keys(res)
.sort()
.forEach((date, index) => {
if (index != 0) {
res[date].forEach((etf) => {
if (etf.name == bestPerformers[index - 1]) {
cash = cash * (1 + (etf.open - etf.close) / etf.close);
performance.push((etf.open - etf.close) / etf.close);
}
});
}
});
var total = 0;
for (var i = 0; i < performance.length; i++) {
total += performance[i];
}
var avg = total / performance.length;
var tradingDays = performance.length;
var annualizedReturn = (1 + avg) ** 253 - 1;
/*
Average daily rate = 0.08774690684042068%
Number of trading days = 4366
Annualized return = 24.84494387511409%
1000 on January 30, 2005 would equal 31829.94540714155 on June 6, 2021
Standard deviation = 0.013087711662769837
Performance on the worst trading day -9.825416558076196%
*/
console.log(`Average daily rate = ${avg * 100}%`);
console.log(`Number of trading days = ${tradingDays}`);
console.log(`Annualized return = ${annualizedReturn * 100}%`);
console.log(
`${1000} on January 30, 2005 would equal ${cash} on June 6, 2021`
);
console.log(
`Standard deviation = ${calculateStandardDeviation(performance)}`
);
console.log(
`Performance on the worst trading day ${
worstDayPerformance(performance) * 100
}%`
);
}
function calculateStandardDeviation(performance) {
var total = 0;
for (var key in performance) total += performance[key];
var meanVal = total / performance.length;
var sdPrep = 0;
for (var key in performance)
sdPrep += Math.pow(parseFloat(performance[key]) - meanVal, 2);
var sdResult = Math.sqrt(sdPrep / performance.length);
return sdResult;
}
function worstDayPerformance(performance) {
return Math.min(...performance);
}