-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (102 loc) · 3.87 KB
/
index.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
var FileManagement = require("./FileManagement")
var ds = require("./DataStructures")
var logging = require("./Logging")
/*
TODO List:
- Fix rounding issue with diviends - DONE (No Issue)
- Filter out duplicate events. In case there is an overlap in CSV data - DONE
- Seperate into multiple JS files e.g. FileManagement - DONE
- Output transaction history with summary of money in/out
- Make dates work when / or - are used
- Clean code
*/
let orders = new Map();
let dividends = new Map();
let transactions = new Map();
var portfolio = new Map();
var dividendHistory = new Map();
function processCSV(events) {
//Seperate the events into different maps. Maps are used to remove duplicate events
for (let index = 0; index < events.length; index++) {
const event = events[index];
if (event["Action"] === "Market sell" || event["Action"] === "Market buy") {
if (!orders.has(event["ID"])) {
orders.set(event["ID"], new ds.Order(event))
}
}
if (event["Action"] === "Dividend (Ordinary)" || event["Action"] === "Dividend (Property income)" || event["Action"] === "Dividend (Bonus)") {
//UID is created for dividends as they dont have a unique ID
let uid = event["Action"] + ":" + event["Ticker"] + ":" + event["Time"] + ":" + event["No. of shares"]
if (!dividends.has(uid)) {
dividends.set(uid, new ds.Dividend(event))
}
}
if (event["Action"] === "Deposit" || event["Action"] === "Withdrawal") {
if (!transactions.has(event["ID"])) {
transactions.set(event["ID"], new ds.Transaction(event))
}
}
}
logging.log("Finished Processing", "DEBUG");
}
function buildPortfolio() {
orders.forEach((value, key) => {
const Ticker = value.Ticker;
if (portfolio.has(Ticker)) {
if (value.Action === "Market buy") {
portfolio.get(Ticker).buy(value);
} else if (value.Action === "Market sell") {
portfolio.get(Ticker).sell(value)
}
} else {
portfolio.set(Ticker, new ds.Portfolio(value))
}
})
//Removes any shares that have less than 7dp (lowest trading212 shows)
portfolio.forEach((value, key) => {
if (value.Shares <= 0.00000001) {
portfolio.delete(key);
}
})
logging.log("Finished Building Portfolio", "DEBUG")
}
function buildDividendList() {
dividends.forEach((value, key) => {
var date = new Date(value.Time);
var keyDate = new Date(0)
keyDate.setFullYear(date.getFullYear())
keyDate.setMonth(date.getMonth())
keyDate = keyDate.toISOString()
//Adds up the total dividends for each month
if (dividendHistory.has(keyDate)) {
const currentValue = dividendHistory.get(keyDate)
let sum = currentValue + value.Total
dividendHistory.set(keyDate, +sum.toFixed(2))
} else {
dividendHistory.set(keyDate, value.Total)
}
})
logging.log("Finished Building Dividend History", "DEBUG")
}
function app() {
FileManagement.checkFolders()
var file_list = FileManagement.listFiles();
//Adds Promises to an array
const dataPromises = []
for (let i = 0; i < file_list.length; i++) {
const file = file_list[i];
dataPromises.push(FileManagement.readCSV(file))
}
//Waits for all Promises to complete before processing CSV's
Promise.all(dataPromises).then(result => {
for (const events of result) {
processCSV(events);
}
buildPortfolio()
buildDividendList()
FileManagement.writeCSVPortfolio(portfolio)
FileManagement.writeYahooFinancePortfolio(portfolio)
FileManagement.writeCSVDividend(dividendHistory)
})
}
app();