주가 인하 알림 받기

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

목표

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

이 솔루션 정보

주식을 구매했는데 가격이 인하되면 주식을 팔아서 다른 주식을 구매하여 세금 공제를 신청할 수 있습니다. 이를 소득 손실 추산이라고 합니다. Google Sheets 스프레드시트에 주식을 등록하고 주가가 구매 가격 아래로 떨어지면 이메일 알림을 받습니다.

주가 및 Gmail 이메일 알림이 표시된 Google 시트의 스크린샷

사용 방법

이 스프레드시트는 Sheets의 Google Finance 내장 함수를 사용하여 현재 주식 가격을 가져옵니다. 스크립트는 등록된 각 주식의 구매 가격과 현재 가격을 비교합니다. 그런 다음 구매 가격 미만으로 떨어진 주식 목록을 이메일로 보냅니다. 원하는 만큼 스크립트가 실행되도록 설정할 수 있습니다.

Apps Script 서비스

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

  • 스프레드시트 서비스 - 나열된 각 주식을 순환하고 주가를 구매 가격과 비교합니다.
  • Gmail 서비스: 구매 가격 이하로 하락한 주식에 대한 이메일을 만들어 전송합니다.

기본 요건

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

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

스크립트 설정

  1. 다음 버튼을 클릭하여 세금 손실 추수 알림 샘플 스프레드시트의 사본을 만듭니다. 이 솔루션의 Apps Script 프로젝트는 스프레드시트에 첨부되어 있습니다.
    사본 만들기
  2. 복사한 스프레드시트에서 자체 주식 정보로 시트를 업데이트하거나 제공된 테스트 데이터를 사용합니다.

스크립트 실행

  1. 복사한 스프레드시트에서 확장 프로그램 > Apps Script를 클릭합니다.
  2. 함수 드롭다운에서 checkLosses를 선택합니다.
  3. 실행을 클릭합니다.
  4. 메시지가 표시되면 스크립트를 승인합니다. OAuth 동의 화면에 이 앱이 확인되지 않았습니다라는 경고가 표시되면 고급 > {프로젝트 이름}으로 이동(안전하지 않음)을 선택하여 계속 진행합니다.

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

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

다음 단계