Advertisement
RemcoE33

Fastqoute (stock)

Sep 4th, 2021
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const ss = SpreadsheetApp.getActiveSpreadsheet();
  2.  
  3. function onOpen(e){
  4.   SpreadsheetApp.getUi().createMenu('Fastqoute')
  5.     .addItem('Update Latest Info','getTickerData')
  6.     .addItem('Update Chart', 'getChartData')
  7.     .addItem('Update Quote', 'getQuote')
  8.     .addToUi();
  9. }
  10.  
  11. function getTickerData() {
  12.   const sheet = ss.getSheetByName('LatestInfo');
  13.   const tickers = sheet.getRange(2,1,sheet.getLastRow()-1).getValues().flat();
  14.   const output = [];
  15.  
  16.   tickers.forEach((tic, i) => {
  17.   const url = `https://fastquote.fidelity.com/service/quote/nondisplay/json?productid=research&symbols=${tic}&quotetype=D&callback=jQuery1111012350412486796825_1630778346041&_=1630778346042`
  18.  
  19.   const response = UrlFetchApp.fetch(url);
  20.   const data = JSON.parse(response.getContentText()
  21.     .replace('jQuery1111012350412486796825_1630778346041(','')
  22.     .replace(')','')
  23.     .trim())
  24.     .QUOTE.SYMBOL_RESPONSE.QUOTE_DATA;
  25.   if(i == 0){
  26.     const headers = ['SECTOR', 'INDUSTRY', ...Object.keys(data)]
  27.     output.push(headers);
  28.   }
  29.   const sectorInfo = getSector(tic);
  30.   output.push([...sectorInfo, ...Object.values(data)]);
  31.   });
  32.  
  33.   sheet.getRange(2,2,sheet.getLastRow(),sheet.getLastColumn()).clearContent();
  34.   sheet.getRange(1,2,output.length, output[0].length).setValues(output);
  35. }
  36.  
  37. function getChartData(){
  38.   const ticker = ss.getSheetByName('Chart').getRange(1,2).getValue();
  39.   const today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy/MM/dd');
  40.   const twoYearsAgo = Utilities.formatDate(new Date(new Date().getTime() - 63113852000), Session.getScriptTimeZone(), 'yyyy/MM/dd');
  41.   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`
  42.  
  43.   const response = UrlFetchApp.fetch(url);
  44.   const data = JSON.parse(response.getContentText()
  45.   .replace('jQuery1111012350412486796825_1630778346043(','')
  46.   .replace(')','')
  47.   .trim());
  48.   const output = [];
  49.  
  50.   data.SYMBOL[0].BARS.CB.forEach(object => {
  51.     output.push(Object.values(object));
  52.   })
  53.  
  54.   const sheet = ss.getSheetByName('ChartData');
  55.   sheet.getRange(2,1,sheet.getLastRow(), sheet.getLastColumn()).clearContent();
  56.   sheet.getRange(2,1,output.length, output[0].length).setValues(output);
  57. }
  58.  
  59. function getQuote(){
  60.   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'
  61.  
  62.   const output = [];
  63.   const response = UrlFetchApp.fetch(url);
  64.   const qoutes = JSON.parse(response.getContentText()
  65.     .replace('dojo.io.script.jsonp_dojoIoScript2._jsonpCallback(','')
  66.     .replace(')','')
  67.     .trim())
  68.     .QUOTES;
  69.   const keys = Object.keys(qoutes);
  70.  
  71.   keys.forEach((key, i) => {
  72.     const q = qoutes[key];
  73.     if(i == 0){
  74.       const headers = Object.keys(q)
  75.       output.push(['QOUTE', ...headers]);
  76.     }
  77.     output.push([key,...Object.values(q)]);
  78.   })
  79.  
  80.   ss.getSheetByName('Quotes').getRange(1,1,output.length,output[0].length).setValues(output);
  81. }
  82.  
  83. function getSector(ticker){
  84.   const url = `https://eresearch.fidelity.com/eresearch/evaluate/snapshot.jhtml?symbols=${ticker}`;
  85.   const html = UrlFetchApp.fetch(url).getContentText();
  86.   let sector;
  87.   let industries;
  88.   html.match(/markets_sectors(.*?)>(.*?)<\/a>/gmi).forEach((element, index) => {
  89.     switch(index){
  90.       case 2:
  91.         sector = />(.*?)</gmi.exec(element)[1]
  92.       break;
  93.       case 3:
  94.         industries = />(.*?)</gmi.exec(element)[1]
  95.       break;
  96.     }
  97. });
  98. return [sector, industries];
  99. }
  100.  
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement