Get stock price drop alerts
Stay organized with collections
Save and categorize content based on your preferences.
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.

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
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-28 UTC.
[null,null,["Last updated 2025-08-28 UTC."],[[["\u003cp\u003eThis Google Apps Script solution automatically alerts you via email when your stocks drop below their purchase price, aiding in tax loss harvesting.\u003c/p\u003e\n"],["\u003cp\u003eThe script utilizes Google Sheets to track stock data and Google Finance functions to fetch current prices, comparing them to your purchase prices.\u003c/p\u003e\n"],["\u003cp\u003eYou can easily set up this solution by copying the provided spreadsheet, inputting your stock information, and running the script to receive email alerts.\u003c/p\u003e\n"],["\u003cp\u003eThe script can be automated with a time-driven trigger, enabling periodic checks and notifications without manual intervention.\u003c/p\u003e\n"],["\u003cp\u003eThe solution is beginner-friendly and requires minimal setup, providing a simple yet effective way to monitor your stock portfolio for potential tax loss harvesting opportunities.\u003c/p\u003e\n"]]],[],null,["# Get stock price drop alerts\n\n**Coding level** : Beginner \n\n**Duration** : 5 minutes \n\n**Project type** : Automation with a [time-driven trigger](/apps-script/guides/triggers/installable#time-driven_triggers)\n\nObjectives\n----------\n\n- Understand what the solution does.\n- Understand what the Apps Script services do within the solution.\n- Set up the script.\n- Run the script.\n\nAbout this solution\n-------------------\n\nIf you purchase a stock and the value drops, you can sell that stock, purchase\nanother, and claim a tax deduction. Doing so is known as a tax loss harvest.\nList your stocks in a Google Sheets spreadsheet and get email alerts if\na stock price drops below its purchase price.\n\n### How it works\n\nThe spreadsheet uses the\n[Google Finance built-in function](https://support.google.com/docs/answer/3093281)\nin Sheets to get the current prices of stocks. The script\ncompares the purchase\nprice of each listed stock with its current price. Then, it emails you a list of\nstocks that have fallen below their purchase price. You can set the script to\nrun as often as you want.\n\n### Apps Script services\n\nThis solution uses the following services:\n\n- [Spreadsheet service](/apps-script/reference/spreadsheet)--Loops through each listed stock and compares the stock price to the purchase price.\n- [Gmail service](/apps-script/reference/gmail)--Creates and sends an email of the stocks that have fallen below their purchase price.\n\nPrerequisites\n-------------\n\nTo use this sample, you need the following prerequisites:\n\n- A Google Account (Google Workspace accounts might require administrator approval).\n- A web browser with access to the internet.\n\nSet up the script\n-----------------\n\n1. 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. \n [Make a copy](https://docs.google.com/spreadsheets/d/1hYxWe9zku7XcY0JO3KCOABqkk03K9L0xDytnSm4v71Y/copy)\n2. In your copied spreadsheet, update the sheet with your own stock information, or use the provided test data.\n\nRun the script\n--------------\n\n1. In your copied spreadsheet, click **Extensions** \\\u003e **Apps Script**.\n2. In the function dropdown, select **checkLosses**.\n3. Click **Run**.\n4. When prompted, authorize the script.\n If the OAuth consent screen displays the warning, **This app isn't verified** ,\n continue by selecting **Advanced** \\\u003e\n **Go to {Project Name} (unsafe)**.\n\n5. Check your email for a list of stocks that fell below their purchase price.\n If you didn't receive an email, check to see if any of the stock prices in\n your list are lower than their purchase price.\n\n### Create a time-driven trigger\n\n1. Return to the script project.\n2. At the left, click **Triggers** alarm.\n3. At the bottom-right, click **Add trigger**.\n4. For **Choose which function to run** , make sure **checkLosses** is selected.\n5. For **Select event source** , select **Time-driven**.\n6. Configure how often you want the script to run and click **Save**.\n\nReview the code\n---------------\n\nTo review the Apps Script code for this solution, click\n**View source code** below: \n\n#### View source code\n\n### Code.gs\n\n\u003cbr /\u003e\n\nsolutions/automations/tax-loss-harvest-alerts/Code.js \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/solutions/automations/tax-loss-harvest-alerts/Code.js) \n\n```javascript\n// To learn how to use this script, refer to the documentation:\n// https://developers.google.com/apps-script/samples/automations/tax-loss-harvest-alerts\n\n/*\nCopyright 2022 Google LLC\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n https://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\n/** \n* Checks for losses in the sheet.\n*/\nfunction checkLosses() {\n // Pulls data from the spreadsheet\n let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(\n \"Calculations\"\n );\n let source = sheet.getRange(\"A:G\");\n let data = source.getValues();\n\n //Prepares the email alert content\n let message = \"Stocks: \u003cbr\u003e\u003cbr\u003e\";\n\n let send_message = false;\n\n console.log(\"starting loop\");\n\n //Loops through the cells in the spreadsheet to find cells where the stock fell below purchase price\n let n = 0;\n for (let i in data) {\n //Skips the first row\n if (n++ == 0) continue;\n\n //Loads the current row\n let row = data[i];\n\n console.log(row[1]);\n console.log(row[6]);\n\n //Once at the end of the list, exits the loop\n if (row[1] == \"\") break;\n\n //If value is below purchase price, adds stock ticker and difference to list of tax loss opportunities\n if (row[6] \u003c 0) {\n message +=\n row[1] +\n \": \" +\n (parseFloat(row[6].toString()) * 100).toFixed(2).toString() +\n \"%\u003cbr\u003e\";\n send_message = true;\n }\n }\n if (!send_message) return;\n\n MailApp.sendEmail({\n to: SpreadsheetApp.getActiveSpreadsheet().getOwner().getEmail(),\n subject: \"Tax-loss harvest\",\n htmlBody: message,\n\n });\n}\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nContributors\n------------\n\nThis sample was created by Jeremy Glassenberg, Product Management and Platform\nStrategy Consultant. Find Jeremy on Twitter\n[@jglassenberg](https://twitter.com/jglassenberg).\n\nThis sample is maintained by Google with the help of Google Developer Experts.\n\nNext steps\n----------\n\n- [Time-driven triggers](/apps-script/guides/triggers/installable#time-driven_triggers)\n- [Extend Google Sheets](/apps-script/guides/sheets)"]]