接收股价下跌提醒

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

目标

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

关于此解决方案

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

Google 表格的屏幕截图,其中显示了股票价格和 Gmail 电子邮件提醒。

运作方式

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

Apps 脚本服务

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

  • 电子表格服务 - 循环遍历每个列出的股票,并将股票价格与购买价格进行比较。
  • Gmail 服务 - 创建并发送电子邮件,其中包含跌破买入价的股票。

前提条件

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

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

设置脚本

  1. 点击以下按钮,制作税收损失收割提醒示例电子表格的副本。此解决方案的 Apps 脚本项目已附加到电子表格。
    制作副本
  2. 在复制的电子表格中,使用您自己的库存信息更新工作表,或使用提供的测试数据。

运行脚本

  1. 在复制的电子表格中,依次点击扩展 > Apps 脚本
  2. 在函数下拉菜单中,选择 checkLosses
  3. 点击运行
  4. 根据提示为脚本授权。 如果 OAuth 权限请求页面显示警告此应用未经过验证,请继续操作,依次选择高级 > 前往“{项目名称}”(不安全)

  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
  let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(
    "Calculations"
  );
  let source = sheet.getRange("A:G");
  let 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 (let i in data) {
    //Skips the first row
    if (n++ == 0) continue;

    //Loads the current row
    let 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] +
        ": " +
        (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 开发者专家的帮助下维护。

后续步骤