Skip to main content

Compute daily evolution of a stock investment portfolio by using only built-in functions of Google Sheets

To effectively track a stock investment portfolio, it is necessary to know its evolution in the past. As I use Google Sheets to track my stock investment portfolio, I have researched and successfully implemented several solutions. In this post, I am happy to share in detail how to compute the daily evolution of a stock investment portfolio by simply using only the available built-in functions in Google Sheets.

The evolution chart of a stock investment portfolio in Google Sheets

Concept

A stock investment portfolio consists of several different stocks. The composition of a portfolio changes over time depending on the frequency of transactions. At any time, it is necessary to answer the three questions below to determine the market value of an equity investment portfolio:

  • What stocks were present in the investment portfolio?
  • How many shares did each stock present in the investment portfolio?
  • What was the price of each stock?

The three questions can be answered with Google Sheets because:

It means that computing the historical evolution of a stock investment portfolio in Google Sheets is not an impossible mission. However, there are still several technical challenges concerning its implementation:

  • The repetitive use of the GOOGLEFINANCE function to fetch prices of stocks might decrease the performance of a spreadsheet.
  • What is the most suitable design of the sheet for storing the daily evolution of a stock investment portfolio?
  • What are the available built-in functions that can be helpful?
  • How to automatically compute the market value of a new day?
  • etc.

In the following sections, I explain in detail the solution for computing the daily evolution of a stock investment portfolio by simply using the available built-in function in Google Sheets.

Implementation

Register transactions of a stock investment portfolio

To compute the historical evolution of a stock investment portfolio, its transactions must be carefully registered from the beginning. I create the Transactions sheet to keep track of my investment portfolio's transactions. In that sheet, a row represents essential information about a transaction: the date, the type (BUY, SELL, DEPOSIT, WITHDRAW, and DIVIDEND), the symbol of the involved stock, the amount of money, and the number of shares.

I explained the further details in the post how to manage stock transactions with Google Sheets

Manage transactions of a stock investment portfolio with Google Sheets

Optimally use the GOOGLEFINANCE function to fetch historical prices of stocks

To compute the historical evolution of a stock investment portfolio, I need to use the GOOGLEFINANCE function to fetch the historical prices of my stocks in Google Sheets. However, the repetitive use of the GOOGLEFINANCE function can cause performance issues for the spreadsheet, especially when it comes to many stocks and many dates. To avoid that, I create a dedicated sheet for each stock, then call the GOOGLFINANCE function only once to get its historical price and store the results in that sheet. After that, I will query that sheet to search for the price on the date that I need.

For example:

  • I started my stock investment portfolio on 27/12/2017.
  • I own the EPA:TTE stock and I create a sheet named EPA:TTE.
  • On the cell A1 of that sheet, I put the formula =GOOGLEFINANCE("EPA:TTE", "price", DATE(2017,12,27), TODAY(), "DAILY") to get the daily historical prices of the EPA:TTE stock since 27/12/2017 to today.
  • When I need the price of the EPA:TTE stock on any given date, I will use built-in functions of Google Sheets to lookup the corresponding price in the sheet EPA:TTE instead of using the GOOGLEFINANCE function again. By doing so, I optimize the performance of my spreadsheet.

For many other useful tips for working with the GOOGLEFINANCE function in Google Sheets, check out my post GOOGLEFINANCE Best Practices

Formula to determine the (close) price of a stock on a given day

Continuing with the example for the EPA:TTE stock, concretely, to know its close price on 01/11/2022, I will use the QUERY function as follow:

=QUERY('EPA:TTE'!A:B,"select B where datediff(A, date '2022-11-01')=0 limit 1",0)

There are some reasons why I need to use the QUERY function here instead of the VLOOKUP function. I explained those reasons at the section Use QUERY function instead of VLOOKUP function for looking up by date of the post GOOGLEFINANCE Best Practices.

Formula to determine the number of shares that a stock presents in the portfolio on a given day

To know the number of shares of a stock that I own on a given day, I need to do two steps:

  • Firstly, I need to identify all the BUY and SELL transactions of that stock before or on that day. For that, I will use once again the powerful QUERY function.
  • Secondly, I need to sum up all the shares of those identified BUY or SELL transactions. For that, I will the classic SUM function.

Continuing with the example for the EPA:TTE stock, concretely, to know how many shares of the the EPA:TTE stock that I own on 01/11/2022, I will use the QUERY function and the SUM function as follow:

=SUM(QUERY(Transactions!$A:$E,"select E where C='EPA:TTE' and B != 'DIVIDEND' and A <= date '2022-11-01'"))

Design the sheet for storing the daily evolution of a stock investment portfolio

So far, I have managed to determine the close price and the number of shares for the EPA:TTE stock on 01/11/2022. Therefore, it is an easy task to know how much value the EPA:TTE stock contributes to my investment portfolio on 01/11/2022. For that, I simply multiply the number of shares by the close price.

To know the total market value of my investment portfolio on 01/11/2022, I need to repeat the process for other stocks that I held on that day.

I then need to repeat again on other days to compute my investment portfolio market value on those days.

How should I design the Evolution sheet to ease the computation?

There are 2 main variables: the stocks and the dates. Therefore, I think of the design where:

  • The header row contains the stocks.
  • The header column contains the dates.
  • Just after the header column, I create a column Portfolio Market Value
how to compute the daily evolution of a stock investment portfolio by simply using the available built-in function in Google Sheets

Getting started

  • Create a Evolution sheet
  • Use the column A for Date and put the wanted dates to this column. The values must be of type Date
  • Use the column B for Portfolio Market Value
  • On the cell C1, put the formula =TRANSPOSE(UNIQUE(QUERY(Transactions!A:F,"select C where B='BUY' order by A desc",0))), so that all the available stocks in the investment portfolio will be populated on the first row, starting from the column C
  • On the cell C2, put the below formula then apply it by dragging from the cell C2 to other cells starting from the column C and the row 2

=IF(SUM(QUERY(Transactions!$A:$E,"select E where C='"&C$1&"' and B != 'DIVIDEND' and A <= date '"&TEXT($A2,"yyyy-MM-dd")&"'"))>0,SUM(QUERY(Transactions!$A:$E,"select E where C='"&C$1&"' and B != 'DIVIDEND' and A <= date '"&TEXT($A2,"yyyy-MM-dd")&"'"))*QUERY(INDIRECT(C$1&"!A:B"),"select B where datediff(A, date '"&TEXT($A2,"yyyy-MM-dd")&"')=0 limit 1",0),0)

  • On the cell B2, put the formula =SUM(C2:2) and then apply it to the whole column B

Demo

The demo spreadsheet is available here.

Another solution for computing the daily evolution of a stock investment portfolio is to write a scripting program with Google Apps Script. Although that solution requires programming, it can compute the evolution of a stock investment portfolio for a longer period. For more information, I have explained in detail in the post Compute daily evolutions of a stock portfolio with Google Sheets and Apps Script

Compute daily evolutions of a stock portfolio with Google Sheets and Apps Script

Conclusion

In this post, I have explained in detail how to compute the daily evolution of a stock investment portfolio by simply using the available built-in function in Google Sheets. The post covered many topics:

Disclaimer

The post is only for informational purposes and not for trading purposes or financial advice.

Feedback

If you have any feedback, question, or request please:

Support this blog

If you value my work, please support me with as little as a cup of coffee! I appreciate it. Thank you!

Share with your friends

If you read it this far, I hope you have enjoyed the content of this post. If you like it, share it with your friends!

Comments

Popular posts

How to convert column index into letters with Google Sheets

How to convert column index into letters with Google Sheets

In Google Sheets, rows are indexed numerically, starting from 1, but columns are indexed alphabetically, starting from A. Hence, it is pretty straightforward to work with rows and trickier to work with columns as we need to convert between column index and corresponding letters. For example, what are the letters of column 999th in Google Sheets? In this post, we will look at how to convert a column index into its corresponding letters by using the built-in functions of Google Sheets. What are letters of the column 999th in a spreadsheet? Unfortunately, Google Sheets does not provide a ready-to-use function that takes a column index as an input and returns corresponding letters as output. However, there is a little trick of combining other available built-in functions to do the task. In Google Sheets, there is the ADDRESS function that returns the cell reference, according to the specified row index (first parameter) and column index (second parameter) in the input. For example,
Create a dividend income tracker with Google Sheets by simply using pivot tables

Create a dividend income tracker with Google Sheets by simply using pivot tables

As my investment strategy is to buy stocks that pay regular and stable dividends during a long-term period, I need to monitor my dividends income by stocks, by months, and by years, so that I can answer quickly and exactly the following questions: How much dividend did I receive on a given month and a given year? How much dividend did I receive for a given stock in a given year? Have a given stock's annual dividend per share kept increasing gradually over years? Have a given stock's annual dividend yield been stable over years? In this post, I explain how to create a dividend tracker for a stock investment portfolio with Google Sheets by simply using pivot tables.
Use SPARKLINE to create 52-week range price indicator chart for stocks in Google Sheets

Use SPARKLINE to create 52-week range price indicator chart for stocks in Google Sheets

The 52-week range price indicator chart shows the relative position of the current price compared to the 52-week low and the 52-week high price. It visualizes whether the current price is closer to the 52-week low or the 52-week high price. In this post, I explain how to create a 52-week range price indicator chart for stocks by using the SPARKLINE function and the GOOGLEFINANCE function in Google Sheets.
How to convert column index into letters with Google Apps Script

How to convert column index into letters with Google Apps Script

Although Google Sheets does not provide a ready-to-use function that takes a column index as an input and returns corresponding letters as output, we can still do the task by leveraging other built-in functions ADDRESS , REGEXEXTRACT , INDEX , SPLIT as shown in the post . However, in form of a formula, that solution is not applicable for scripting with Google Apps Script. In this post, we look at how to write a utility function with Google Apps Script that converts column index into corresponding letters.
Slice array in Google Sheets

Slice array in Google Sheets

Many functions in Google Sheets return an array as the result. However, I find that there is a lack of built-in support functions in Google Sheets when working with an array. For example, the GOOGLEFINANCE function can return the historical prices of a stock as a table of two columns and the first-row being headers Date and Close. How can I ignore the headers or remove the headers from the results?
Create dividend income tracker with Google Data Studio

Create dividend income tracker with Google Data Studio

With transactions registered, it is easy to create a dividend income tracker with Google Sheets. However, a dividend income tracker in Google Sheets is not interactive. Instead of having different pivot tables and switching forth and back among them, I can create an interactive dividend income tracker with a single-page report on Google Data Studio. In this post, I explain how to create a dividend income tracker with Google Data Studio.
Compute cost basis of stocks with FIFO method in Google Sheets

Compute cost basis of stocks with FIFO method in Google Sheets

After selling a portion of my holdings in a stock, the cost basis for the remain shares of that stock in my portfolio is not simply the sum of all transactions. When selling, I need to decide which shares I want to sell. One of the most common accounting methods is FIFO (first in, first out), meaning that the shares I bought earliest will be the shares I sell first. As you might already know, I use Google Sheets extensively to manage my stock portfolio investment, but, at the moment of writing this post, I find that Google Sheets does not provide a built-in formula for FIFO. Luckily, with lots of effort, I succeeded in building my own FIFO solution in Google Sheets, and I want to share it on this blog. In this post, I explain how to implement FIFO method in Google Sheets to compute cost basis in stocks investing.
How to copy data in Google Sheets as HTML table

How to copy data in Google Sheets as HTML table

I often need to extract some sample data in Google Sheets and present it in my blog as an HTML table. However, when copying a selected range in Google Sheets and paste it outside the Google Sheets, I only get plain text. In this post, I explain how to copy data in Google Sheets as an HTML table by writing a small Apps Script program.