In this video, I demonstrate how to analyze stock using the GOOGLEFINANACE Function on Google Sheets with Google Apps Script.
How to Video:
Video Notes:
- Google Finance Function Documentation
Code in Video:
function start() {
var ss= SpreadsheetApp.getActiveSpreadsheet();
var setupSheet = ss.getSheetByName("SETUP");
var lastRow = setupSheet.getLastRow();
for(var y = 6; y <= lastRow; y++)
{
var checkbox = setupSheet.getRange(y,8).getValue();
if(checkbox == true)
{
setupSheet.getRange("D"+y+":G"+y).clear();
var symbol = setupSheet.getRange(y,3).getValue();
var market = setupSheet.getRange(y,2).getValue();
getData(symbol, market, y);
setLink(symbol, market, y);
setupSheet.getRange(y,8).setValue(false);
}
}
}
function checkAll() {
var ss= SpreadsheetApp.getActiveSpreadsheet();
var setupSheet = ss.getSheetByName("SETUP");
var lastRow = setupSheet.getLastRow();
for(var y = 6; y <= lastRow; y++)
{
setupSheet.getRange(y,8).setValue(true);
}
}
function uncheckAll() {
var ss= SpreadsheetApp.getActiveSpreadsheet();
var setupSheet = ss.getSheetByName("SETUP");
var lastRow = setupSheet.getLastRow();
for(var y = 6; y <= lastRow; y++)
{
setupSheet.getRange(y,8).setValue(false);
}
}
function setLink(symbol, market, row)
{
var ss= SpreadsheetApp.getActiveSpreadsheet();
var setupSheet = ss.getSheetByName("SETUP");
var richValue = SpreadsheetApp.newRichTextValue()
.setText(symbol)
.setLinkUrl("https://www.google.com/finance/quote/"+symbol+":"+market)
.build();
setupSheet.getRange(row, 9).setRichTextValue(richValue);
}
function getData(symbol, market, row) {
var ss= SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheetByName("DATA");
var setupSheet = ss.getSheetByName("SETUP");
var percentGain = setupSheet.getRange(2,2).getValue();
var percentDown = setupSheet.getRange(3,2).getValue();
var daysBack = setupSheet.getRange(1,2).getValue();
dataSheet.getRange("C1:D1000").clear();
var cell = dataSheet.getRange("A1");
cell.setFormula("=GOOGLEFINANCE(\""+market+":"+symbol+"\",\"price\",TODAY()-"+daysBack+",TODAY())");
dataSheet.getRange("C1").setValue(symbol);
var datalastRow = dataSheet.getLastRow();
for(var i = 3; i <= datalastRow; i++)
{
var pastRow = i - 1;
var cell = dataSheet.getRange(i,3);
cell.setFormula("=B"+i+"-B"+pastRow+"");
}
for(var i = 3; i <= datalastRow; i++)
{
var pastRow = i - 1;
var cell = dataSheet.getRange(i,4);
cell.setFormula("=(C"+i+"/B"+pastRow+")*100");
}
var percentGainTotal = 0;
var percentDownTotal = 0;
var consecutiveDaysGain = 0;
for(var i = 3; i <= datalastRow; i++)
{
var currentValue = dataSheet.getRange(i,4).getValue();
if(currentValue > percentGain)
{
percentGainTotal++;
}
if(currentValue < percentDown * -1)
{
percentDownTotal++;
}
}
for(var i = datalastRow; i >= 3; i--)
{
var currentValue = dataSheet.getRange(i,4).getValue();
if(currentValue > 0)
{
consecutiveDaysGain++;
}
else
{
break;
}
}
setupSheet.getRange(row,5).setValue(percentGainTotal);
setupSheet.getRange(row,4).setValue(percentDownTotal);
setupSheet.getRange(row,6).setValue(consecutiveDaysGain);
setupSheet.getRange(row,7).setValue(datalastRow-1);
}
Related Posts
How to Back Up Google Drive Files using Apps Script - In this post, I demonstrate how to back up Google Drive Files using Google Apps Script with Google Sheets.
How to Embed Charts on Google Sheets using Apps Script - In this post, I demonstrate How to Create Charts on Google Sheets using Google Apps Script.
How to Search Google Drive on Google Sheets using Apps Script - In this post, I demonstrate how to search Google Drive by File Name on Google Sheets using Apps Script
How to Create a Menu on Google Sheets using Apps Script - In this post, I demonstrate how to Create a Customer Menu on Google Sheets using Google Apps Script.
How to Get and Set Values on Google Sheets using Apps Script - In this post, I demonstrate how to Get and Set Values using Google Apps Script on Google Sheets.
How to Create Data Validation on Drop Down using Google Apps Script - In this post, I demonstrate How to Create Data Validation Drop-down List Using Google Apps Script on Google Sheets.
Scrape Amazon Website to Google Sheets - In this video, I demonstrate how to Scrape Amazon Website to Google Sheets using software called ScraperAPI.
Create Alert Popups on Google Sheets - In this post, I demonstrate how to create 3 different types of alert popups using Google Apps Script on Google Sheets.
Combine Multiple Spreadsheets into One Google Sheet - In this post, I demonstrate how to Combine Multiple Spreadsheets into one Sheet using Google Apps Script.
How to Get Next Open Row and Column using Google Apps Script - In this post, I demonstrate how to get the next open row and column using Google Apps Script on Google Sheets.
How to Get Map Time and Distance using Google Apps Script - In this post, I demonstrate how to get the Map Time and Distance between locations using Google Apps Script using the Map Class.
How to Create Dependent Dropdown on Google Sheets - In this post, I demonstrate how to Create a Dependent Dropdown list on Google Sheets using Google Apps Script.
How to Call Rest API on Google Sheets - In this post, I demonstrate how to call a Rest API using Google Apps Script and Displaying the data on a Google Sheet.
How to Create Google Doc from Apps Script - In this post, I demonstrate how to create a Google Doc using Google Apps Script and save it in Google Drive. In this example, I use the data from a Google Sheet.
How to Clear, Delete, Insert, and Replace Row using Google Apps Script - In this post, I demonstrate how to clear, delete, insert, and replace rows using Google Apps Script on Google Sheets.
Email Last Update on Google Sheet - In this post, I demonstrate how to email the last update made on a google sheet. This is a way to monitor any changes made on Google Sheets
How to Call JSON Web Service to Populate Google Sheet - In this post, I demonstrate how to Call a JSON Web Service and Populate a Google Sheet with a table of raw data.
Scrape Website Data to Google Sheets - In this post, I demonstrate how to Scrape data from a Website and import it to a Google Sheet. I used JavaScript and Reg Expressions to accomplish the task.
Create Report Builder on Google Sheets using QUERY Function - In this post, I demonstrate how to use the QUERY function and Google Apps Script and build multiple report views from a list.
Copy Row from Sheet to Sheet using Apps Script on Google Sheets - In this post, I show how to copy a row of data from one sheet to another using Google Apps Script. This video also shows how to search a column on Google Sheets.