-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManagement.js
195 lines (160 loc) · 6.04 KB
/
FileManagement.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
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
const csv = require('csv-parser');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const fs = require('fs');
var logging = require("./Logging")
const input_folder_path = "Data/"
const output_folder_path = "Output/"
module.exports = {
checkFolders: function () {
//Checks if folders exist if they dont it creates them
if (!fs.existsSync(output_folder_path)) {
fs.mkdirSync(output_folder_path);
}
if (!fs.existsSync(input_folder_path)) {
fs.mkdirSync(input_folder_path);
}
},
listFiles: function () {
//Returns a list of CSV files in a folder
var csvFiles = []
//Gets a list of all CSV Files
var files = fs.readdirSync(input_folder_path);
files.forEach(file => {
if (file.endsWith("csv")) {
csvFiles.push(input_folder_path + file)
}
});
if (csvFiles <= 0) {
logging.log("No Files", "INFO")
process.exit()
}
return csvFiles;
},
readCSV: function (path) {
//Reads the CSV file
return new Promise((resolve) => {
var events = [];
fs.createReadStream(path)
.pipe(csv())
.on('data', (row) => {
events.push(row);
}).on('end', () => {
logging.log('CSV file successfully processed. Length: ' + events.length, "DEBUG");
resolve(events);
});
})
},
writeCSVPortfolio: function (data) {
const csvWriterPortfolio = createCsvWriter({
path: 'Output/PortfolioOutput.csv',
header: [
{ id: 'ISIN', title: 'ISIN' },
{ id: 'Ticker', title: 'Ticker' },
{ id: 'Name', title: 'NAME' },
{ id: 'Shares', title: 'Shares' },
{ id: 'Price', title: 'Price' },
{ id: 'Currency', title: 'Currency' }
]
});
var stocks = []
data.forEach((stock, key) => {
stocks.push(stock)
})
//Sort alphabetically by ticker
stocks.sort(function (a, b) {
if (a.Ticker < b.Ticker) { return -1; }
if (a.Ticker > b.Ticker) { return 1; }
return 0;
})
csvWriterPortfolio.writeRecords(stocks).then(() => {
logging.log('Portfolio File created', "DEBUG");
});
},
writeYahooFinancePortfolio: function (data) {
const csvWriterYahooPortfolio = createCsvWriter({
path: 'Output/YahooPortfolioOutput.csv',
header: [
{ id: 'Ticker', title: 'Symbol' },
{ id: 'no_value', title: 'Current' },
{ id: 'no_value', title: 'Price' },
{ id: 'no_value', title: 'Date' },
{ id: 'no_value', title: 'Time' },
{ id: 'no_value', title: 'Change' },
{ id: 'no_value', title: 'Open' },
{ id: 'no_value', title: 'High' },
{ id: 'no_value', title: 'Low' },
{ id: 'no_value', title: 'Volume' },
{ id: 'no_value', title: 'Trade Date' },
{ id: 'no_value', title: 'Purchase Price' },
{ id: 'Shares', title: 'Quantity' },
{ id: 'no_value', title: 'Commission' },
{ id: 'no_value', title: 'High Limit' },
{ id: 'no_value', title: 'Low Limit' },
{ id: 'no_value', title: 'Comment' }
]
});
var stocks = []
data.forEach((stock, key) => {
let ISIN_Country_Code = stock["ISIN"].substring(0, 2)
//Adds .L to the end of UK stocks
if (ISIN_Country_Code == "GB" || ISIN_Country_Code == "IE") {
stock["Ticker"] = stock["Ticker"] + ".L"
}
//Fills fields with a space
stock['no_value'] = " "
stocks.push(stock)
})
csvWriterYahooPortfolio.writeRecords(stocks).then(() => {
logging.log('Yahoo Portfolio File created', "DEBUG");
});
},
writeCSVDividend: function (data) {
const csvWriterDividends = createCsvWriter({
path: 'Output/DividendOutput.csv',
header: [
{ id: 'year', title: 'Year' },
{ id: '1', title: 'January' },
{ id: '2', title: 'Febuary' },
{ id: '3', title: 'March' },
{ id: '4', title: 'April' },
{ id: '5', title: 'May' },
{ id: '6', title: 'June' },
{ id: '7', title: 'July' },
{ id: '8', title: 'August' },
{ id: '9', title: 'September' },
{ id: '10', title: 'October' },
{ id: '11', title: 'November' },
{ id: '12', title: 'December' },
]
})
var years = new Map()
//Adds the dividends to the month & year
data.forEach((value, key) => {
const date = new Date(key)
if (!years.has(date.getFullYear())) {
years.set(date.getFullYear(), new Array(13))
years.get(date.getFullYear())[date.getMonth() + 1] = value
} else {
years.get(date.getFullYear())[date.getMonth() + 1] = value
}
})
var arrayList = []
//Converts into a format that can be used by csv-writer
years.forEach((value, key) => {
var emptyObject = {}
emptyObject.year = key;
for (let index = 1; index < value.length; index++) {
const month = value[index];
if (month == undefined) {
emptyObject[index] = 0
} else {
emptyObject[index] = month;
}
}
arrayList.push(emptyObject)
})
csvWriterDividends.writeRecords(arrayList).then(() => {
logging.log('Dividend File created', "DEBUG");
});
}
}