Advertisement
RemcoE33

YCHART (tickers/stock)

Sep 3rd, 2021
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Returns YCHART information
  3. *
  4. * @param {"AAPL"} tickers - Input range, can be an array {"AAPL","TSLA"} or A1:B20.
  5. * @return {array} range with security, date and value.
  6. * @customfunction
  7. */
  8. function YCHARTS(tickers){
  9.   if(!Array.isArray(tickers)){
  10.     tickers = [tickers]
  11.   }
  12.  
  13.   tickers = tickers.flat();
  14.  
  15.   const output = [];
  16.  
  17.    tickers.forEach(tic => {
  18.      const url = `https://ycharts.com/charts/fund_data.json?securities=id%3A${tic}%2Cinclude%3Atrue%2C%2C&calcs=id%3Ape_ratio%2Cinclude%3Atrue%2C%2C&correlations=&format=real&recessions=true&zoom=5&startDate=&endDate=&chartView=&splitType=single&scaleType=linear&note=&title=&source=true&units=true&quoteLegend=true&partner=&quotes=&legendOnChart=true&securitylistSecurityId=&displayTicker=true&ychartsLogo=&useEstimates=true&maxPoints=862`
  19.  
  20.     console.log(url);
  21.  
  22.      try{
  23.        const response = UrlFetchApp.fetch(url);
  24.        const object = JSON.parse(response.getContentText());
  25.        const data = object.chart_data[0][0].raw_data;
  26.  
  27.        data.forEach(row => {
  28.          output.push([tic, new Date(row[0]).toLocaleDateString(), row[1]]);
  29.        })
  30.  
  31.      } catch (err){
  32.        console.error(err);
  33.      }
  34.    })
  35.  
  36.    return output
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement