-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetCurrentStocks.js
72 lines (60 loc) · 1.63 KB
/
getCurrentStocks.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
const yahooFinance = require("yahoo-finance");
const _ = require("lodash");
const cTable = require("console.table");
const symbols = require("./my-stocks.json").symbols;
yahooFinance.quote(
{
symbols: symbols,
modules: ["price", "summaryDetail", "financialData"]
},
function(err, quotes) {
if (err) {
console.log(err);
}
let shares = [];
symbols.forEach(symbol => {
let pe, earningsYield, returnOnAssets;
let share = quotes[symbol];
if (share.summaryDetail) {
pe = share.summaryDetail.trailingPE;
earningsYield = (100 / pe).toFixed(2);
}
if (share.financialData) {
returnOnAssets = (share.financialData.returnOnAssets * 100).toFixed(2);
}
shares.push({
symbol: symbol.substring(0, symbol.length - 3),
name: share.price.longName,
earningsYield,
returnOnAssets,
price: share.financialData.currentPrice
});
});
shares = _.chain(shares)
.sortBy(share => parseFloat(share.earningsYield))
.value()
.reverse();
shares.map((share, index) => {
share.eRank = index + 1;
});
shares = _.chain(shares)
.sortBy(share => parseFloat(share.returnOnAssets))
.value()
.reverse();
shares.map((share, index) => {
share.rRank = index + 1;
if (share.eRank) {
share.rank = share.eRank + share.rRank;
}
});
shares = _.chain(shares)
.sortBy("rank")
.map(share => {
share.RoA = share.returnOnAssets;
delete share.returnOnAssets;
return share;
})
.value();
console.table(shares);
}
);