接收股价下跌提醒

编码级别:初级
时长:5 分钟(
项目类型:使用时间驱动型触发器的自动化

目标

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

关于此解决方案

如果您购买了某支股票,但股价下跌了,您可以卖出这支股票, 另选一个,并申请减税。这样做就称为“免税获取”。 在 Google 表格的电子表格中列出您的股票,并在出现以下情况时收到电子邮件提醒: 股票价格跌至购买价格以下。

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

工作原理

该电子表格使用 Google 财经内置函数 获取股票的当前价格。脚本 比较购买 每只上市股票的价格及其当前价格。然后,它会通过电子邮件 股价跌幅低于购买价格的股票。您可以将脚本设为 任意频率运行。

Apps 脚本服务

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

  • 电子表格服务 - 循环遍历每个电子表格 并将股票价格与购买价格进行比较。
  • Gmail 服务 - 创建和 发送电子邮件 股价跌幅到购买价格以下的股票。

前提条件

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

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

设置脚本

  1. 点击下面的按钮,复制纳税申报提醒 电子表格示例。此 API 的 Apps 脚本项目 解决方案。
    复制
  2. 在您复制的电子表格中,使用您自己的库存信息更新该工作表, 或使用提供的测试数据。

运行脚本

  1. 在您复制的电子表格中,点击附加信息 > Apps 脚本
  2. 在函数下拉菜单中,选择 checkLosses
  3. 点击运行
  4. 出现提示时,为脚本授权。 如果 OAuth 同意屏幕显示以下警告:“此应用未经验证”, 选择高级 > 以继续 前往“{Project Name}”(不安全)

  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 开发者专家的帮助下进行维护。

后续步骤