Forum Discussion

ashko20's avatar
ashko20
Copper Contributor
Aug 08, 2023

Need help with formulas calculating cost basis on shares

Hi All

 

Was hoping to get some help in creating a trading log to track my stock purchases over time. I am stuck in not being able to get the formula for calculating my weighted average cost basis for each stock given multiple purchases over time. I have tried using the sum product formula with a couple of conditions so it can be presented in a report however the formula is returning an incorrect value. 

 

The formula I am using to calculate cost basis is: =IF($CL2="HUT",SUMPRODUCT($AI$2:$AI2,$AJ$2:$AJ2)/CN2,IF($CL2="PLTR",SUMPRODUCT($AI$2:$AI2,$AJ$2:$AJ2)/CP2,""))

 

Looking at the data however the cost basis is incorrect for PLTR but correct for HUT. 

 

Is there an en easier way to get this information?


Have pasted the sample data below:

SymbolQuantityTradePriceBuy/SellNet ProceedsIdentifier (helper)HUT Share BalancePLTR Share BalanceCOST BASIS
HUT1007BUY-$650HUT$100$0$7
HUT5006BUY-$2,750HUT$600$0$6
HUT2003BUY-$600HUT$800$0$5
PLTR10020BUY-$2,000PLTR$800$100$60
PLTR20020BUY-$4,000PLTR$800$300$33
PLTR158BUY-$115PLTR$800$315$32
PLTR10012BUY-$1,200PLTR$800$415$27
  • mathetes's avatar
    mathetes
    Silver Contributor

    ashko20 

     

    It's not clear to me (as somebody who also tracks stock and options purchases and sales) what your other formulas are doing here, before we even get to that final column. And, while we're at it, headings like "Net Proceeds" are misleading in a minor way since that tends to imply proceeds after a sale; I'd go with plain ol' "Net" to cover both outflow and inflow of funds.

    Continuing, though, with points where clarification is needed:  in your first row, why are you showing $650 for a transaction where the quantity is 100 and the price 7? Why not $700? Is there a $50 fee or commission hidden in there?

     

    Now, to get to the calculation you asked about: it would be helpful if you could post a copy of this actual spreadsheet on OneDrive or GoogleDrive with a link pasted here. Your posted data here leaves off the Column and Row headers so we can't really determine which set of cells is referenced by (for example) $CL2, which is central to the IF condition in your formula. Is that the first "SYMBOL" column, or the "Identifier (helper)" column?

     

    All of that having been said, if I were doing this, I'd keep the transaction records as one database, and place the calculation of weighted average cost in a separate sheet (call it a "Dashboard" sheet), where it wouldn't vary row by row but would simply aggregate all of the data pertinent to HUT or PLTR into a display with one row per stock. In the attached I've put that summary data below the data table, but that's not where I'd keep it in the real workbook. Good design generally separates a transactional database from whatever summaries you're wanting to do.

    The formula that calculates weighted cost is this:

    =ABS(SUM(FILTER($E$5:$E$11,$A$5:$A$11=A17)))/SUM(FILTER($B$5:$B$11,$A$5:$A$11=A17))

    Essentially all it's doing is adding up the numbers in column E ("Net Proceeds") for the stock in question and dividing that by the sum of the shares purchased (column B) and showing the absolute value of that result.

    Assuming your larger real database is more complex, showing sales as well as purchases, multiple stocks, the formula would need to be tweaked a bit to account for the possibility of sales, etc., but the basic idea would still work.

    The formulas here are very dependent on the FILTER function, which requires Excel 2021 or newer (a Microsoft 365 subscription)

     

    • bmissell's avatar
      bmissell
      Copper Contributor
      Mathetes - been a while since you posted this and I hope you are still out there... I really like your answer / explanation to this one - great job. Thank you it is very helpful.
      I'm on to the more complicated version - I have some sells and need to incorporate that into my weighted cost...
      Bill
      • mathetes's avatar
        mathetes
        Silver Contributor
        Without seeing how you’re approaching this, it’s rather difficult to give any advice. For that matter, you haven’t actually asked 😏

        But if you are seeking help, you’d help us (me or one of the other frequent contributors) help you by posting a copy of your workbook. Use OneDrive or equivalent to post, and paste a link here that grants access.
  • macandal's avatar
    macandal
    Copper Contributor
    Following this. Looking for help in this very topic. I have a taxable account. I have buys, sales, dividends paid, dividends reinvested. Basically, how do I figure my cost basis? Thank you.
    • mathetes's avatar
      mathetes
      Silver Contributor

      macandal 

       

      Your question is, I would suggest, less of an Excel question than it is an accounting question. So let me refer you to this answer I got via Google.

       

      If you need help with implementing that in Excel, feel free to come back, but come back with a sample (attached to your inquiry) of the spreadsheet you've created to track your " buys, sales, dividends paid, dividends reinvested" in a particular stock.

      • macandal's avatar
        macandal
        Copper Contributor
        mathetes, thanks for the link. It's both. I also need help implementing this in Excel.

        Thank you.

Resources