接收股价下跌提醒

编码水平:初级
时长:5 分钟
项目类型:使用时间驱动型触发器实现自动化

目标

  • 了解此解决方案的功能。
  • 了解 Apps 脚本服务在此解决方案中的功能。
  • 设置脚本。
  • 运行脚本。

关于此解决方案

如果您购买了股票,但其价值下跌,您可以出售该股票,购买另一只股票,并申请税收减免。这种做法称为“税收损失收割”。 在 Google 表格电子表格中列出您的股票,并在股票价格跌破购买价格时收到电子邮件提醒。

包含股票价格和 Gmail 电子邮件提醒的 Google 表格。

运作方式

该电子表格使用 Google 财经内置函数在 Google 表格中获取股票的当前价格。该脚本会将每只列出的股票的购买价格与其当前价格进行比较。然后,它会通过电子邮件向您发送一份股票列表,其中列出了价格跌破购买价格的股票。您可以根据需要设置脚本的运行频率。

Apps 脚本服务

此解决方案使用以下服务:

  • Spreadsheet 服务:遍历每只 列出的股票,并将股票价格与购买价格进行比较。
  • Gmail 服务:创建并发送一封 电子邮件,其中列出了价格跌破购买价格的股票。

前提条件

如需使用此示例,您需要满足以下前提条件:

  • 一个 Google 账号(Google Workspace 账号可能需要管理员批准)。
  • 一个可以访问互联网的网络浏览器。

设置脚本

  1. 点击以下按钮,制作税收损失收割提醒 示例电子表格的副本。此解决方案的 Apps 脚本项目已附加到该电子表格。

    制作副本

  2. 在复制的电子表格中,使用您自己的股票信息更新工作表,或使用提供的测试数据。

运行脚本

  1. 在复制的电子表格中,依次选择扩展程序 > Apps 脚本
  2. 在函数下拉列表中,选择 checkLosses
  3. 点击运行
  4. 根据提示为脚本授权。 <<../_snippets/oauth.md>>
  5. 查看您的电子邮件,了解价格跌破购买价格的股票列表。 如果您没有收到电子邮件,请检查列表中的任何股票价格是否低于其购买价格。

创建时间驱动型触发器

  1. 返回到脚本项目。
  2. 点击左侧的触发器
  3. 点击右下方的添加触发器
  4. 对于选择要运行的功能,请确保已选择checkLosses
  5. 对于选择事件来源,请选择时间驱动
  6. 配置您希望脚本运行的频率,然后点击保存

查看代码

如需查看此解决方案的 Apps 脚本代码,请点击 查看源代码

查看源代码

Code.gs

solutions/automations/tax-loss-harvest-alerts/Code.js
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/automations/tax-loss-harvest-alerts

/*
Copyright 2022 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/**
 * Checks for losses in the sheet.
 */
function checkLosses() {
  // Pulls data from the spreadsheet
  const sheet =
    SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Calculations");
  const source = sheet.getRange("A:G");
  const data = source.getValues();

  //Prepares the email alert content
  let message = "Stocks: <br><br>";

  let send_message = false;

  console.log("starting loop");

  //Loops through the cells in the spreadsheet to find cells where the stock fell below purchase price
  let n = 0;
  for (const i in data) {
    //Skips the first row
    if (n++ === 0) continue;

    //Loads the current row
    const row = data[i];

    console.log(row[1]);
    console.log(row[6]);

    //Once at the end of the list, exits the loop
    if (row[1] === "") break;

    //If value is below purchase price, adds stock ticker and difference to list of tax loss opportunities
    if (row[6] < 0) {
      message += `${row[1]}: ${(Number.parseFloat(row[6].toString()) * 100).toFixed(2).toString()}%<br>`;
      send_message = true;
    }
  }
  if (!send_message) return;

  MailApp.sendEmail({
    to: SpreadsheetApp.getActiveSpreadsheet().getOwner().getEmail(),
    subject: "Tax-loss harvest",
    htmlBody: message,
  });
}

贡献者

此示例由产品管理和平台策略顾问 Jeremy Glassenberg 创建。您可以在 Twitter 上找到 Jeremy ,他的账号是@jglassenberg

此示例由 Google 在 Google 开发者专家的帮助下维护。

后续步骤