-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetDividend.js
66 lines (53 loc) · 1.59 KB
/
getDividend.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
function DIVIDEND(stock) {
if (stock === '' || stock === null) {
return 'ERROR: Unknown symbol';
}
var DIVIDEND_ENDPOINT = 'https://api.iextrading.com/1.0/stock/{stock}/stats';
var url = DIVIDEND_ENDPOINT.replace('{stock}', stock);
var response = null;
try {
var r = UrlFetchApp.fetch(url);
response = r.getContentText();
} catch (err) {
return 0.0;
}
response = JSON.parse(response);
var dividend = 0.0;
if (response === null || response.length === 0) {
return dividend;
}
if (response.hasOwnProperty('dividendYield') && response.dividendYield) {
var yield = parseFloat(response.dividendYield);
var movingAverage = parseFloat(response.day50MovingAvg);
dividend = yield/100*movingAverage/4;
} else {
return 0.0;
}
return dividend.toFixed(2);
}
function YIELD(stock) {
if (stock === '' || stock === null) {
return 'ERROR: Unknown symbol';
}
var DIVIDEND_ENDPOINT = 'https://api.iextrading.com/1.0/stock/{stock}/stats';
var url = DIVIDEND_ENDPOINT.replace('{stock}', stock);
var response = null;
try {
var r = UrlFetchApp.fetch(url);
response = r.getContentText();
} catch (err) {
return 0.0;
}
response = JSON.parse(response);
if (response === null || response.length === 0) {
return 0.0;
}
if (response.hasOwnProperty('dividendYield') && response.dividendYield) {
return response.dividendYield;
} else {
return 0.0;
}
}
module.exports = {
DIVIDEND: DIVIDEND
};