In this post, I want to introduce Notion and how can be used to track our stocks and portfolio. Notion is a productivity tool offering all in one workspace in order to organise multiple aspects of your life. It is similar to OneNote but with many more options for customisation.
It can be used to track tasks, as a planner, collaborate with others, add notes, etc. Since we are in a blog about Python for Finance, we are going to focus on how can we use Notion for financial purposes.
In this post, we are going to create a Notion Portfolio Tracker with Python to automatically track our portfolio daily price changes with Python.
What is Notion?
Notion is a cross-platform app and can be used for note taking, data management, project management, etc. It offers databases, boards, wikis, calendars and reminders. All available within your user workspace. The workspace is organised in sections (or pages) that can be used individually or in collaboration with your friends, colleagues or family. Below are some of the cool things about this productivity tool:
- It offers automation capabilities as we will see in this post. Perfect for Python. Although the official API is still to be realeased.
- It is free for personal use
- It works across different platforms
- Fun to work with
- Fully customisable
Portfolio Tracker in Notion
In the next section, we will go step by step in order to build a Notion Portfolio tracker that will update stock prices automatically thanks to a Python script and Yahoo Finance. But before starting with the next section, we will need to open a free account.
We can do that in the Notion site. Once we have our free account created, we can move to the next section to start the fun part.
Notion Portfolio Automation with Python
In order to automate the data collection for our Notion Portfolio tracker, we are going to use below Python packages:
- Notion-Py – Unofficial Notion API for Python. Run in terminal pip install notion in order to have the package installed. Here you can find the documentation of this package.
- yfinance – Python package to get stock prices from Yahoo Finance. Run in terminal pip install yfinance to have the package installed
With these two packages, we will be able to retrieve stock prices for all stocks in our portfolio and add them to our Notion Portfolio Tracker template.
Below is how our Notion Portfolio tracker will look like after running the Python script. The best thing is that to maintain the portfolio daily prices, no manual effort will be required from our side.
Preparing Notion API
Let’s start with the code. First thing is to have a look at the Notion-Py documentation. As you can observe, Notion-Py API requires you to pass your secrete cookie token in order to be able to write data into your Notion workspace. That will ensure that only you can update your personal workspace.
To get your token cookie, follow the following instruction in Google Chrome. Basically, we just need to open the developer tools in Google Chrome, go to Application and take the value of the token_v2 cookie stored under notion.so as shown below
I have hidden the value of my token_v2 since it needs to be kept secret.
Once we have the token_v2 value, we will need to pass it into our Python Script in order to open a connection to our Notion account:
import yfinance as yf from notion.client import NotionClient from datetime import datetime client = NotionClient(token_v2="your V2 token value paste here")
To see if the connection works, we can quickly print the title of one of our pages:
page = client.get_block("https://www.notion.so/Reading-List-755454c8a18946879149944be39a22f2") print("The title is:", page.title)
Note that within get_block you need to pass the url of one of your existing Notion pages. For example, in below example, I use the Stock Page:
Then, if I run the script, I get below output as it was expected.
The title is: Stock
Good, now we can continue with our script to automatically fill in our stock table. It is important that before running the script, we create a new page including a Database Table. This can be done easily in our Notion workspace as display below:
Setting-up our Portfolio Columns
Once we have the new page created, we will need to create manually as many columns as we need. Simply click on the + button to add a new column:
For our portfolio tracker, we need the following columns Date, Close, Open, Volume, High, DayReturn and stock. Also, ensure that we have the proper column type set-up as per below:
- Stock – text,
- Open – number,
- Close – number,
- Volume – number,
- High – number,
- Day-Return – number
Retrieving Stock Price Data
Once we have all required columns ready, copy the URL from the Stock table page. For that, it is important that you open the table as page (see below screenshot). Then, copy the URL from the opening page since that is the url that give us access to our portfolio table (collection).
Next, we pass the url as an argument of the get_collection_view object.
cv = client.get_collection_view("https://www.notion.so/fadfdsf32321514?v=1223r234jhkhkh12")
Then, as shown in the last part of the code below, we will use cv.collection.add_row() to add a row. Each row will contain the stock price information for a particular stock and date.
To add the stock price data into each of our portfolio tracker rows, we need to retrieve the stock prices from Yahoo Finance. We can do that with the yfinance package:
First, we create a list of tickers with the names of the companies that we want to track. Then, we loop through each of the items in the list. Each iteration of the loop relieves the stock price for one stock.
We use yf.Ticker and history methods to get the stock information. Within history, we pass 1d in order to get only the latest available day price information.
Finally, we write the stock information to our Notion Portfolio tracker by simply using row. and the name of the column:
tickers = ['AAPL','MSFT','VWRL.AS','CHF=X','AMZN','INTC'] for ticker in tickers: price = yf.Ticker(ticker) row = cv.collection.add_row() hist = price.history(period="1d") #information: row.Stock = ticker row.Date = datetime.today().strftime('%Y-%m-%d') row.Close = float(round((hist.Close),1)) row.Open = float(round((hist.Open),1)) row.Open = float(round((hist.Open),1)) row.Volume = float(round((hist.Volume),1)) row.High = float(round((hist.High),1)) row.DayReturn = float(round(((hist.Close) - (hist.Open) ) / (hist.Open)*100,2))
And that is all. Once we open our Notion collection, we should see a few new rows containing the latest portfolio stock information.
You can explore what Notion has to offer with the different types of columns and formatting.
We have created a Python script to update our Portfolio tracker in Notion. The script needs to be run daily in order to add the prices for the day.
New rows will be added each time the script is run. That will let us keep track of historical prices as well. Then, we can use the filter capabilities within Notion to query our data.
In addition, we can also add new columns to for example calculate the daily change in $ by using the formula column type. That I will leave it for you. If you have any good ideas to improve the portfolio tracker, feel free to comment below.
I hope that you have enjoyed the post. If you did not know Notion before, have a look at it since I find it very useful for organising purposes.
If you like my blog on Python for Finance, I would be more than happy if you can support and share the post in social media.
Below the full code for your reference. The content of this post on creating a Notion Portfolio tracker is also available in video tutorial format:
import yfinance as yf from notion.client import NotionClient from datetime import datetime client = NotionClient(token_v2="your token key") cv = client.get_collection_view("https://www.notion.so/fadfdsf32321514?v=1223r234jhkhkh12") tickers = ['AAPL','MSFT','VWRL.AS','CHF=X','AMZN','INTC'] for ticker in tickers: price = yf.Ticker(ticker) row = cv.collection.add_row() hist = price.history(period="1d") row.Stock = ticker row.Date = datetime.today().strftime('%Y-%m-%d') row.Close = float(round((hist.Close),1)) row.Open = float(round((hist.Open),1)) row.Open = float(round((hist.Open),1)) row.Volume = float(round((hist.Volume),1)) row.High = float(round((hist.High),1)) row.DayReturn = float(round(((hist.Close) - (hist.Open) ) / (hist.Open)*100,2))