接收股价下跌提醒

编码级别:新手
时长:5 分钟
项目类型:具有时间驱动型触发器的自动化

目标

  • 了解解决方案的用途。
  • 了解 Apps 脚本服务在此解决方案中执行的操作。
  • 设置脚本。
  • 运行脚本。

关于此解决方案

如果您买入了一只股票,但股值下降了,您可以卖出该股票,然后再买入一只股票,然后申请减税。这样做称为税务损失。 在 Google 表格的电子表格中列出您的股票,并在股价降至购买价格以下时收到电子邮件提醒。

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

运作方式

该电子表格使用 Google 表格中的 Google 财经内置函数获取股票的当前价格。该脚本会将每只上市股票的购买价格与其当前价格进行比较。然后,它会通过电子邮件向您发送跌至购买价格以下的股票的列表。您可以将脚本设置为希望的运行频率随时可用。

Apps 脚本服务

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

  • 电子表格服务 - 循环遍历每只上市的股票,并将股价与购买价格进行比较。
  • Gmail 服务 - 针对跌至购买价格以下的股票创建和发送电子邮件。

前提条件

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

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

设置脚本

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

运行脚本

  1. 在复制的电子表格中,依次点击扩展程序 > Apps 脚本
  2. 在函数下拉菜单中,选择 checkLosses
  3. 点击运行
  4. 出现提示时,授权脚本。如果 OAuth 同意屏幕显示 This app is not verified(此应用未经验证)警告,请依次选择 Advanced > Go to {Project Name} (unsafe),继续操作。

  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 @jglassenberg 上查找 Jeremy。

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

后续步骤