Skip to content

Instantly share code, notes, and snippets.

@kannaiah
Last active February 2, 2024 21:31
Show Gist options
  • Save kannaiah/53881607c9eba63099689591ad6e949e to your computer and use it in GitHub Desktop.
Save kannaiah/53881607c9eba63099689591ad6e949e to your computer and use it in GitHub Desktop.
function getYahooPrice(symbol, datetime) {
symbol = symbol || "NSE:PANACEABIO";
// symbol = symbol.replace("NSE:", "") + ".NS";
Utilities.sleep(Math.floor(Math.random() * 5000))
var url = 'https://query1.finance.yahoo.com/v7/finance/download/'+ symbol + '?interval=1d&events=history'; // last one day history
Logger.log(url);
var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
if (response.getResponseCode()) {
var textFile = response.getContentText();
if (textFile.indexOf("Date") != -1) {
var csv = Utilities.parseCsv(textFile);
} else {
Logger.log("Some Error");
return "No Data";
}
}
Logger.log(csv);
Logger.log(csv[1][4]);
return parseFloat(csv[1][4]);
}
@fourwallz
Copy link

Based on this, I have created the following:

function getYPrice3(symbol) {
  symbol = symbol || "MSFT";
  symbol = encodeURI(symbol);
  Utilities.sleep(Math.floor(Math.random() * 2000))
  var url = 'https://query1.finance.yahoo.com/v10/finance/quoteSummary/'+symbol+'?modules=price'; // last one day history
  Logger.log(url);  
  var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
  var responseCode = response.getResponseCode();
  if (responseCode === 200) {
    var quote = JSON.parse(response.getContentText());
    Logger.log(parseFloat(quote.quoteSummary.result[0].price.regularMarketPrice.raw));
    return parseFloat(quote.quoteSummary.result[0].price.regularMarketPrice.raw);
  }else{
     return -0.1; 
  }
}
function getYChange(symbol) {
  symbol = symbol || "MSFT";
  symbol = encodeURI(symbol);
  Utilities.sleep(Math.floor(Math.random() * 1000))
  var url = 'https://query1.finance.yahoo.com/v10/finance/quoteSummary/'+symbol+'?modules=price'; // change from last day trading * 100
  Logger.log(url);  
  var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
  var responseCode = response.getResponseCode();
  if (responseCode === 200) {
    var quote = JSON.parse(response.getContentText());
    Logger.log(parseFloat(quote.quoteSummary.result[0].price.regularMarketChangePercent.raw) * 100);
    return parseFloat(quote.quoteSummary.result[0].price.regularMarketChangePercent.raw) * 100;
  }else{
     return -0.1; 
  }
}
function getYDiv(symbol) {
  symbol = symbol || "MSFT";
  symbol = encodeURI(symbol);
  Utilities.sleep(Math.floor(Math.random() * 5000))
  var url = 'https://query2.finance.yahoo.com/v7/finance/options/'+symbol; // dividend return
  Logger.log(url);  
  var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
  var responseCode = response.getResponseCode();
  if (responseCode === 200) {
    var quote = JSON.parse(response.getContentText());
    Logger.log(parseFloat(quote.optionChain.result[0].quote.trailingAnnualDividendYield));
    return parseFloat(quote.optionChain.result[0].quote.trailingAnnualDividendYield);
  }else{
     return -0.1; 
  }
}
function getY52High(symbol) {
  symbol = symbol || "MSFT";
  symbol = encodeURI(symbol);
  Utilities.sleep(Math.floor(Math.random() * 5000))
  var url = 'https://query2.finance.yahoo.com/v7/finance/options/'+symbol; // 52 week high
  Logger.log(url);  
  var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
  var responseCode = response.getResponseCode();
  if (responseCode === 200) {
    var quote = JSON.parse(response.getContentText());
    Logger.log(parseFloat(quote.optionChain.result[0].quote.fiftyTwoWeekHigh));
    return parseFloat(quote.optionChain.result[0].quote.fiftyTwoWeekHigh);
  }else{
     return -0.1; 
  }
}
function getY52Low(symbol) {
  symbol = symbol || "MSFT";
  symbol = encodeURI(symbol);
  Utilities.sleep(Math.floor(Math.random() * 5000))
  var url = 'https://query2.finance.yahoo.com/v7/finance/options/'+symbol; // 52 week low
  Logger.log(url);  
  var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
  var responseCode = response.getResponseCode();
  if (responseCode === 200) {
    var quote = JSON.parse(response.getContentText());
    Logger.log(parseFloat(quote.optionChain.result[0].quote.fiftyTwoWeekLow));
    return parseFloat(quote.optionChain.result[0].quote.fiftyTwoWeekLow);
  }else{
     return -0.1; 
  }
}
function getY52Change(symbol) {
  symbol = symbol || "MSFT";
  symbol = encodeURI(symbol);
  Utilities.sleep(Math.floor(Math.random() * 5000))
  var url = 'https://query2.finance.yahoo.com/v7/finance/options/'+symbol; // percent change 52 weeks * 100
  Logger.log(url);  
  var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
  var responseCode = response.getResponseCode();
  // var percentChange = parseFloat(quote.optionChain.result[0].quote.regularMarketChangePercent)
  if (responseCode === 200) {
    var quote = JSON.parse(response.getContentText());
    Logger.log(parseFloat(quote.optionChain.result[0].quote.regularMarketChangePercent) * 100);
    return parseFloat(quote.optionChain.result[0].quote.regularMarketChangePercent);
    // return percentChange * 1000;
  }else{
     return -0.1; 
  }
}
function getYLongName(symbol) {
  symbol = symbol || "MA";
  symbol = encodeURI(symbol);
  Utilities.sleep(Math.floor(Math.random() * 5000))
  var url = 'https://query2.finance.yahoo.com/v7/finance/options/'+symbol; // Long Name of Stock
  Logger.log(url);  
  var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
  var responseCode = response.getResponseCode();
  if (responseCode === 200) {
    var quote = JSON.parse(response.getContentText());
    Logger.log((quote.optionChain.result[0].quote.longName));
    return (quote.optionChain.result[0].quote.longName);
  }else{
     return -1; 
  }
}
function getYProfile(symbol) {
  symbol = symbol || "DVY";
  symbol = encodeURI(symbol);
  Utilities.sleep(Math.floor(Math.random() * 4000))
  var url = 'https://query2.finance.yahoo.com/v10/finance/quoteSummary/'+symbol+'?modules=summaryProfile'; // profile
  Logger.log(url);  
  var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
  var responseCode = response.getResponseCode();
  if (responseCode === 200) {
    var quote = JSON.parse(response.getContentText());
    Logger.log((quote.quoteSummary.result[0].summaryProfile.sector));
    return (quote.quoteSummary.result[0].summaryProfile.sector);
  }else{
     return "N/A"; 
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment