-
Notifications
You must be signed in to change notification settings - Fork 1
/
Fastqoute.js
106 lines (90 loc) · 3.83 KB
/
Fastqoute.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
/*
Created by: RemcoE33
https://github.com/RemcoE33/apps-script-codebase
Change sheetname (codeline 19), this function asumes the tickers are in Col A from row 2.
*/
const ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen(e){
SpreadsheetApp.getUi().createMenu('Fastqoute')
.addItem('Update Latest Info','getTickerData')
.addItem('Update Chart', 'getChartData')
.addItem('Update Quote', 'getQuote')
.addToUi();
}
function getTickerData() {
const sheet = ss.getSheetByName('LatestInfo');
const tickers = sheet.getRange(2,1,sheet.getLastRow()-1).getValues().flat();
const output = [];
tickers.forEach((tic, i) => {
const url = `https://fastquote.fidelity.com/service/quote/nondisplay/json?productid=research&symbols=${tic}"etype=D&callback=jQuery1111012350412486796825_1630778346041&_=1630778346042`
const response = UrlFetchApp.fetch(url);
const data = JSON.parse(response.getContentText()
.replace('jQuery1111012350412486796825_1630778346041(','')
.replace(')','')
.trim())
.QUOTE.SYMBOL_RESPONSE.QUOTE_DATA;
if(i == 0){
const headers = ['SECTOR', 'INDUSTRY', ...Object.keys(data)]
output.push(headers);
}
const sectorInfo = getSector(tic);
output.push([...sectorInfo, ...Object.values(data)]);
});
sheet.getRange(2,2,sheet.getLastRow(),sheet.getLastColumn()).clearContent();
sheet.getRange(1,2,output.length, output[0].length).setValues(output);
}
function getChartData(){
const ticker = ss.getSheetByName('Chart').getRange(1,2).getValue();
const today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy/MM/dd');
const twoYearsAgo = Utilities.formatDate(new Date(new Date().getTime() - 63113852000), Session.getScriptTimeZone(), 'yyyy/MM/dd');
const url = `https://fastquote.fidelity.com/service/historical/chart/lite/json?productid=research&symbols=${ticker}&dateMin=${twoYearsAgo}:00:00:00&dateMax=${today}:00:00:00&intraday=n&granularity=1&incextendedhours=n&dd=y&callback=jQuery1111012350412486796825_1630778346043&_=1630778346044`
const response = UrlFetchApp.fetch(url);
const data = JSON.parse(response.getContentText()
.replace('jQuery1111012350412486796825_1630778346043(','')
.replace(')','')
.trim());
const output = [];
data.SYMBOL[0].BARS.CB.forEach(object => {
output.push(Object.values(object));
})
const sheet = ss.getSheetByName('ChartData');
sheet.getRange(2,1,sheet.getLastRow(), sheet.getLastColumn()).clearContent();
sheet.getRange(2,1,output.length, output[0].length).setValues(output);
}
function getQuote(){
const url = 'https://fastquote.fidelity.com/service/quote/json?productid=embeddedquotes&subproductid=eresearch&market_close=1&symbols=.DJI%2C.IXIC%2C.SPX&dojo.preventCache=1630779480852&callback=dojo.io.script.jsonp_dojoIoScript2._jsonpCallback'
const output = [];
const response = UrlFetchApp.fetch(url);
const qoutes = JSON.parse(response.getContentText()
.replace('dojo.io.script.jsonp_dojoIoScript2._jsonpCallback(','')
.replace(')','')
.trim())
.QUOTES;
const keys = Object.keys(qoutes);
keys.forEach((key, i) => {
const q = qoutes[key];
if(i == 0){
const headers = Object.keys(q)
output.push(['QOUTE', ...headers]);
}
output.push([key,...Object.values(q)]);
})
ss.getSheetByName('Quotes').getRange(1,1,output.length,output[0].length).setValues(output);
}
function getSector(ticker){
const url = `https://eresearch.fidelity.com/eresearch/evaluate/snapshot.jhtml?symbols=${ticker}`;
const html = UrlFetchApp.fetch(url).getContentText();
let sector;
let industries;
html.match(/markets_sectors(.*?)>(.*?)<\/a>/gmi).forEach((element, index) => {
switch(index){
case 2:
sector = />(.*?)</gmi.exec(element)[1]
break;
case 3:
industries = />(.*?)</gmi.exec(element)[1]
break;
}
});
return [sector, industries];
}