Coding level
: Beginner
Duration
: 5 minutes
Project type
: Automation with a
time-driven trigger
Objectives
- Understand what the solution does.
- Understand what the Apps Script services do within the
solution.
- Set up the script.
- Run the script.
About this solution
If you purchase a stock and the value drops, you can sell that stock, purchase
another, and claim a tax deduction. Doing so is known as a tax loss harvest.
List your stocks in a Google Sheets spreadsheet and get email alerts if
a stock price drops below its purchase price.
![Screenshot of Google Sheet with stock prices and a Gmail email alert.](/static/apps-script/samples/images/stock-alerts.png)
How it works
The spreadsheet uses the
Google Finance built-in function
in Sheets to get the current prices of stocks. The script
compares the purchase
price of each listed stock with its current price. Then, it emails you a list of
stocks that have fallen below their purchase price. You can set the script to
run as often as you want.
Apps Script services
This solution uses the following services:
- Spreadsheet service
?Loops through each
listed stock and compares the stock price to the purchase price.
- Gmail service
?Creates and
sends an email of
the stocks that have fallen below their purchase price.
Prerequisites
To use this sample, you need the following prerequisites:
- A Google Account (Google Workspace accounts might
require administrator approval).
- A web browser with access to the internet.
Set up the script
- Click the following button to make a copy of the
Tax loss harvest alerts
sample spreadsheet. The Apps Script project for this
solution is attached to the spreadsheet.
Make a copy
- In your copied spreadsheet, update the sheet with your own stock information,
or use the provided test data.
Run the script
- In your copied spreadsheet, click
Extensions
>
Apps Script
.
- In the function dropdown, select
checkLosses
.
- Click
Run
.
When prompted, authorize the script.
If the OAuth consent screen displays the warning,
This app isn't verified
,
continue by selecting
Advanced
>
Go to {Project Name} (unsafe)
.
Check your email for a list of stocks that fell below their purchase price.
If you didn't receive an email, check to see if any of the stock prices in
your list are lower than their purchase price.
Create a time-driven trigger
- Return to the script project.
- At the left, click
Triggers
alarm
.
- At the bottom-right, click
Add trigger
.
- For
Choose which function to run
, make sure
checkLosses
is selected.
- For
Select event source
, select
Time-driven
.
- Configure how often you want the script to run and click
Save
.
Review the code
To review the Apps Script code for this solution, click
View source code
below:
Contributors
This sample was created by Jeremy Glassenberg, Product Management and Platform
Strategy Consultant. Find Jeremy on Twitter
@jglassenberg
.
This sample is maintained by Google with the help of Google Developer Experts.
Next steps