A dividend reinvestment plan (DRIP) is a program that allows investors to reinvest their cash dividends into additional shares or fractional shares of the underlying stock.
Since Wealthsimple doesn't provide this functionality automatically, the goal of this project is to automate this process.
- Authenticate with Wealthsimple using email, password, and OTP code
- Pull account info:
- Current buying power
- Open positions
- Dividends collected since last executed buy order for each position (or within the last 99 activites on the account)
- Place fractional buy orders for every open position
- Determine a base dollar amount to buy for all positions based on the current buying power minus accumulated dividends
- Securities that have not paid dividends place buy orders of the base amount
- Securities that have paid dividends place buy orders of the base amount plus dividends
- Sign up with Wealthsimple Trade
- Either a TFSA or RRSP trading account
- All open positions should support fractional orders
- Security settings must be configured to receive OTP codes by email
- A Google Workspace account
- Wealthsimple OTP code must be sent or forwarded to an email under your Google Workspace
- A GCP service account with domain-wide delegation
- Gmail API is enabled for the current project in GCP
- Your Wealthsimple account receives deposits on a regular schedule
- This script is run on a regular schedule
- You want all deposited funds to be distributed evenly among your open postions
- Buy orders are only executed by this script
- Any manual buy orders may obscure previous unnacounted dividend payouts
- Fork and clone the repo
- Create an API key for your service account. Download the JSON file to the project directory and name it
serviceAccount.json
- Rename
.env.example
to.env
and fill out the environment variables for Gmail and Wealthsimple
Install dependencies:
npm install
Start the cloud function:
npm start
> [email protected] start
> functions-framework --target=wsDrip
Serving function...
Function: wsDrip
Signature type: http
URL: http://localhost:8080/
Use a browser or API client to GET
or POST
to http://localhost:8080/
It will take at least 15 seconds to execute. There is a 10 second setTimeout during authentication to give Gmail enough time to receive the OTP code.
Sample response:
{
"status": 200,
"orderResults": [
{
"symbol": "BCE",
"buyAmount": 37.35,
"dividendAmount": 0.68,
"result": {
"response": "debug mode",
"error": null
}
},
{
"symbol": "BEPC",
"buyAmount": 37.06,
"dividendAmount": 0.39,
"result": {
"response": "debug mode",
"error": null
}
},
{
"symbol": "BIPC",
"buyAmount": 36.67,
"dividendAmount": 0,
"result": {
"response": "debug mode",
"error": null
}
},
...
...
...
{
"symbol": "TD",
"buyAmount": 36.67,
"dividendAmount": 0,
"result": {
"response": "debug mode",
"error": null
}
}
]
}
result.response
will return the API response from Wealthsimple
result.error
will contain any errors thrown by wstrade-api
Initialize Google Cloud SDK
See the Node.js Cloud Functions Quickstart Guide
See the Deploying from Your Local Machine
Authenticate with gcloud
gcloud auth login
Deploy the cloud function to your GCP project
gcloud functions deploy wsDrip --project={PROJECT_ID} --runtime nodejs16 --trigger-http --allow-unauthenticated --set-env-vars={ENV_VARS}
Configure Google Cloud Scheduler to schedule the function to execute as a cron.
Thanks to @ahmedsakr for publishing the wstrade-api wrapper package via NPM.
This project was archived as of May 2021. I've forked it here and extended it to support fractional buy/sell orders.