-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStock.gs
48 lines (39 loc) · 1.26 KB
/
Stock.gs
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
/*
STOCK(string :ticker, char :type)
This function requires an account on FinnHub (https://finnhub.io/). It's free, and easy to set up.
How it works:
:ticker: The symbol of the stock to retrieve information;
:type: The type of information to be retrieved:
- o: Open price of the day
- h: High price of the day
- l: Low price of the day
- c: Current price
- pc: Previous close price
@return: Number, the price requested on the call
*/
function STOCK(ticker, type) {
//API Token
var token = 'FINNHUB-TOKEN';
//API Endpoint
var url = "https://finnhub.io/api/v1/quote?symbol=" + ticker.toUpperCase() + ".SA&token=" + token;
//Avoids HTTP 429 (Too many requests)
Utilities.sleep(parseInt(Math.random() * 10000));
//Fetch the remote resource
var json = UrlFetchApp.fetch(url);
//Converts to a valid JSON object
json = JSON.parse(json);
//If the type of price wasn't used, then it is the current price of the stock
if (typeof type == 'undefined') {
type = 'c';
} else {
type = type.toLowerCase();
}
//If the information retrieved is invalid, then just give a null return
if (typeof json[type] == 'undefined') {
return null;
}
//Converts to a floating point
var r = parseFloat(json[type]);
//The End
return r;
}