주가 인하 알림 받기

코딩 수준: 초급
시간: 5분
프로젝트 유형: 시간 기반 트리거를 사용한 자동화

목표

  • 솔루션의 기능을 이해합니다.
  • 솔루션 내에서 Apps Script 서비스의 기능을 이해합니다.
  • 스크립트를 설정합니다.
  • 스크립트를 실행합니다.

이 솔루션 정보

주식을 구매한 후 가치가 하락하면 해당 주식을 판매하고 다른 주식을 구매하여 세금 공제를 받을 수 있습니다. 이를 세금 손실 수확이라고 합니다. Google Sheets 스프레드시트에 주식을 나열하고 주가가 구매 가격 아래로 떨어지면 이메일 알림을 받습니다.

주식 가격과 Gmail 이메일 알림이 포함된 Google 시트

작동 방식

스프레드시트는 Google Finance 기본 제공 함수를 사용하여 Sheets에서 주식의 현재 가격을 가져옵니다. 스크립트는 나열된 각 주식의 구매 가격을 현재 가격과 비교합니다. 그런 다음 구매 가격 아래로 떨어진 주식 목록을 이메일로 보냅니다. 스크립트를 원하는 만큼 자주 실행하도록 설정할 수 있습니다.

Apps Script 서비스

이 솔루션은 다음 서비스를 사용합니다.

기본 요건

이 샘플을 사용하려면 다음 기본 요건이 필요합니다.

  • Google 계정 (Google Workspace 계정의 경우 관리자 승인이 필요할 수 있음)
  • 인터넷에 액세스할 수 있는 웹브라우저

스크립트 설정

  1. 다음 버튼을 클릭하여 세금 손실 수확 알림 샘플 스프레드시트의 사본을 만듭니다. 이 솔루션의 Apps Script 프로젝트가 스프레드시트에 연결되어 있습니다.

    사본 만들기

  2. 복사된 스프레드시트에서 자체 주식 정보로 시트를 업데이트하거나 제공된 테스트 데이터를 사용합니다.

스크립트 실행

  1. 복사된 스프레드시트에서 확장 프로그램 > Apps Script를 선택합니다.
  2. 함수 드롭다운에서 checkLosses 를 선택합니다.
  3. 실행 을 클릭합니다.
  4. 메시지가 표시되면 스크립트를 승인합니다. <<../_snippets/oauth.md>>
  5. 구매 가격 아래로 떨어진 주식 목록이 포함된 이메일을 확인합니다. 이메일을 받지 못한 경우 목록에 있는 주식 가격이 구매 가격보다 낮은지 확인합니다.

시간 기반 트리거 만들기

  1. 스크립트 프로젝트로 돌아갑니다.
  2. 왼쪽에서 트리거 를 클릭합니다.
  3. 오른쪽 하단에서 트리거 추가 를 클릭합니다.
  4. 실행할 함수 선택에서 checkLosses가 선택되어 있는지 확인합니다.
  5. 이벤트 소스 선택에서 시간 기반을 선택합니다.
  6. 스크립트를 실행할 빈도를 구성하고 저장 을 클릭합니다.

코드 검토

이 솔루션의 Apps Script 코드를 검토하려면 소스 코드 보기를 클릭합니다.

소스 코드 보기

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가 만들었습니다. 트위터에서 Jeremy (@jglassenberg)를 찾아보세요.

이 샘플은 Google Developer Experts의 도움을 받아 Google에서 유지관리합니다.

다음 단계