## Tracking a stock portfolio #2 in excel

*Article last updated on February 08, 2011*

This is follow up post to: Tracking a stock portfolio in excel (auto update)

In this post we are going to calculate cost basis and returns. The calculations are simplified, commissions, stock splits and dividends are removed from calculations.

In the first post we created dynamic ranges.

We also identified accumulated stocks and the number of shares. I have now added cost basis and returns.

### Excel formula in cell C2:

Copy cell C2 and paste down as far as needed.

### How this formula works in cell C2

*Step 1 - Calculate total cost you paid*

=IF(A2<>"", (**SUMPRODUCT((A2=Symbol)*(Type="Buy")*Shares*Price)**-SUMPRODUCT((A2=Symbol)*(Type="Sell")*Shares*Price))/(SUMPRODUCT((A2=Symbol)*(Type="Buy")*Shares)-SUMPRODUCT((A2=Symbol)*(Type="Sell")*Shares)), "")

SUMPRODUCT(array1, array2, )

Returns the sum of the products of the corresponding ranges or arrays

SUMPRODUCT((A2=Symbol)*(Type="Buy")*Shares*Price)

becomes

SUMPRODUCT((F={F, MSFT, MSFT, F, GOOG, MSFT, GOOG, GOOG, GOOG}))*({Buy, Buy, Buy, Buy, Sell, Sell, Buy, Buy, Sell}=Buy)*{100, 100, 50, 100, 25, 50, 10, 100, 200}*{12, 25, 28, 16, 550, 24, 500, 500, 550})

becomes

SUMPRODUCT(({TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE}))*({TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE})*{100, 100, 50, 100, 25, 50, 10, 100, 200}*{12, 25, 28, 16, 550, 24, 500, 500, 550})

becomes

SUMPRODUCT({1200, 0, 0, 1600, 0, 0, 0, 0, 0}) returns 2800.

*Step 2 - Calculate total amount you sold*

=IF(A2<>"", (SUMPRODUCT((A2=Symbol)*(Type="Buy")*Shares*Price)-**SUMPRODUCT((A2=Symbol)*(Type="Sell")*Shares*Price)**)/(SUMPRODUCT((A2=Symbol)*(Type="Buy")*Shares)-SUMPRODUCT((A2=Symbol)*(Type="Sell")*Shares)), "")

SUMPRODUCT((A2=Symbol)*(Type="Sell")*Shares*Price)

becomes

SUMPRODUCT({0, 0, 0, 0, 0, 0, 0, 0, 0}) returns 0.

*Step 3 - Calculate accumulated shares*

=IF(A2<>"", (SUMPRODUCT((A2=Symbol)*(Type="Buy")*Shares*Price)-SUMPRODUCT((A2=Symbol)*(Type="Sell")*Shares*Price))/**(SUMPRODUCT((A2=Symbol)*(Type="Buy")*Shares)-SUMPRODUCT((A2=Symbol)*(Type="Sell")*Shares))**, "")

(SUMPRODUCT((A2=Symbol)*(Type="Buy")*Shares)-SUMPRODUCT((A2=Symbol)*(Type="Sell")*Shares

becomes

SUMPRODUCT({100, 0, 0, 100, 0, 0, 0, 0, 0}) - SUMPRODUCT({0, 0, 0, 0, 0, 0, 0, 0, 0})

and returns 200.

*Step 4 - Calculate cost basis*

=IF(A2<>"", (SUMPRODUCT((A2=Symbol)*(Type="Buy")*Shares*Price)-SUMPRODUCT((A2=Symbol)*(Type="Sell")*Shares*Price))/(SUMPRODUCT((A2=Symbol)*(Type="Buy")*Shares)-SUMPRODUCT((A2=Symbol)*(Type="Sell")*Shares)), "")

becomes

=IF(A2<>"", (2800-0)/200, "") returns 14 in cell C2.

### Excel formula in cell D2

Copy cell D2 and paste down as far as needed.

### How this formula works in cell D2

*Step 1 - Calculate average price you paid per share*

=IF(A2<>"", **(IFERROR(SUMPRODUCT(--(A2=Symbol), --("Sell"=Type), Price, Shares)/SUMPRODUCT(--(A2=Symbol), --("Sell"=Type), Shares), 0)**-IFERROR(SUMPRODUCT(--(A2=Symbol), --("Buy"=Type), Price, Shares)/SUMPRODUCT(--(A2=Symbol), --("Buy"=Type), Shares), 0))*(MIN(SUMPRODUCT(--(A2=Symbol), --("Sell"=Type), Shares), SUMPRODUCT(--(A2=Symbol), --("Buy"=Type), Shares))), "")

(IFERROR(SUMPRODUCT(--(A2=Symbol), --("Sell"=Type), Price, Shares)/SUMPRODUCT(--(A2=Symbol), --("Sell"=Type), Shares), 0)

returns 0.

*Step 2 - Calculate average selling price*

=IF(A2<>"", (IFERROR(SUMPRODUCT(--(A2=Symbol), --("Sell"=Type), Price, Shares)/SUMPRODUCT(--(A2=Symbol), --("Sell"=Type), Shares), 0)-**IFERROR(SUMPRODUCT(--(A2=Symbol), --("Buy"=Type), Price, Shares)/SUMPRODUCT(--(A2=Symbol), --("Buy"=Type), Shares), 0))***(MIN(SUMPRODUCT(--(A2=Symbol), --("Sell"=Type), Shares), SUMPRODUCT(--(A2=Symbol), --("Buy"=Type), Shares))), "")

IFERROR(SUMPRODUCT(--(A2=Symbol), --("Buy"=Type), Price, Shares)/SUMPRODUCT(--(A2=Symbol), --("Buy"=Type), Shares)

returns 14.

*Step 3 - Multiply with bought or sold shares*

=IF(A2<>"", (IFERROR(SUMPRODUCT(--(A2=Symbol), --("Sell"=Type), Price, Shares)/SUMPRODUCT(--(A2=Symbol), --("Sell"=Type), Shares), 0)-IFERROR(SUMPRODUCT(--(A2=Symbol), --("Buy"=Type), Price, Shares)/SUMPRODUCT(--(A2=Symbol), --("Buy"=Type), Shares), 0))*(**MIN(SUMPRODUCT(--(A2=Symbol), --("Sell"=Type), Shares), SUMPRODUCT(--(A2=Symbol), --("Buy"=Type), Shares)))**, "")

MIN(SUMPRODUCT(--(A2=Symbol), --("Sell"=Type), Shares), SUMPRODUCT(--(A2=Symbol), --("Buy"=Type), Shares))

returns 0.

*Step 4 - Calculate returns*

=IF(A2<>"", (IFERROR(SUMPRODUCT(--(A2=Symbol), --("Sell"=Type), Price, Shares)/SUMPRODUCT(--(A2=Symbol), --("Sell"=Type), Shares), 0)-IFERROR(SUMPRODUCT(--(A2=Symbol), --("Buy"=Type), Price, Shares)/SUMPRODUCT(--(A2=Symbol), --("Buy"=Type), Shares), 0))*(MIN(SUMPRODUCT(--(A2=Symbol), --("Sell"=Type), Shares), SUMPRODUCT(--(A2=Symbol), --("Buy"=Type), Shares))), "")

becomes

=IF(A2<>"", (0-14)*0) returns 0

### Final notes

I am not telling you to sell or buy any stock, this is just an example.

I hope I got all calculations right.

**Download excel file**

** **

** **Tracking-a-stock-portfolio2.xlsx

(Excel 2007 -2010 Workbook *.xlsx)

The picture above shows how to merge two columns into one list using a formula. If you are looking […]

Combine data from multiple sheets

Question: Problem description (simplified of course): I have a list of employees (by ID number) and date (by yr & […]

Merge two columns with possible blank cells

Question: This article is terrific. Thanks so much for posting this solution! I do have one question: Let's say my […]

Merge three columns into one list in excel

Question: How do I merge three columns into one list? Answer: Excel 2007 array formula in D2: =IFERROR(INDEX(List1, ROWS(D1:$D$1)), IFERROR(INDEX(List2, […]

Question: I have multiple worksheets in a workbook. Each worksheets is project specific. Each worksheet contains almost identical format. The […]

Calculate your stock portfolio performance in excel

Track your stock investments in excel and use a web query to import current stock prices from yahoo. Setup excel […]

Automate net asset value (NAV) calculation on your stock portfolio

Introduction In this post I am creating a spreadsheet that will calculate stock portfolio performance. To do this I am […]

Excel udf: Import historical stock prices from yahoo – added features

This post describes how to import historical stock quotes from yahoo. This custom function is more advanced than the previous […]

Calculate your stock portfolio performance with Net Asset Value based on units in excel

In a previous related post we calculated the stock portfolio performance using the most current stock prices compared to buying […]

Brad asks: I'm trying to use your formulas to create my own bill reminder sheet. I envision a workbook where […]

### 3 Responses to “Tracking a stock portfolio #2 in excel”

### Leave a Reply

### How to comment

**How to add a formula to your comment**

<code>Insert your formula here.</code>

**Convert less than and larger than signs**

Use html character entities instead of less than and larger than signs.

< becomes < and > becomes >

**How to add VBA code to your comment**

[vb 1="vbnet" language=","]

Put your VBA code here.

[/vb]

**How to add a picture to your comment:**

Upload picture to postimage.org or imgur

Use the img tag, like this: <img src="Insert pic link here">

**Contact Oscar**

You can contact me through this contact form

We still need to copy cell and paste as far as needed right? Is there a way to avoid that step? That is, if I paste the formula in cell A2, it should automatically update A3 down to how much ever is required.

Ram,

yes, you are right. You need to copy and paste as far as needed.

Is there a way to avoid that step?

No, not without using vba. If you don´t like the errors that show up, use IFERROR function to remove errors.

Why do you use the minimum of the amount of bought and sold shares?

I don't understand why this key figure is relevant...

If it was the same formula without the 'MIN' part, it would tell you something. But how do you for instance read 'returns for stock MSFT are - 100 dollar'... this does not make sense to me.

Thanks for explaining!