How to calculate average buy price when you buy, sell and rebuy I wanted to get my avaerage buy price for some stocks I bought and see if I made money.

So far for just buying and then later selling I have been calculating the avaege buy price using this formula

((stockamount * price) + (stockamount * price)) / (stockamount + stockamount)


so If I buy 10 shares at 2.00 and 5 shares at 3 I would get average buy price of

((10 * 2.00) + (5 * 3.00)) / (10 + 5) = 2.33


This is how I have calculated the average sell price as well.



Recently I have bought some shares, sold them at a higher cost, and then re bought some when they slightly went under what I sold them at.

I have no idea how to figure out my average buy price in this situation because I re bought some.
here is sample data
bought 10 @ 2.00
sold 7 @ 3.00
bought 2 @ 2.50
I thought maybe doing something like this

((10 * 2.00) - (7 * 3.00) + (2 * 2.50)) / (10 - 7 + 2) = 0.8


It does not make any sense that my average buy was 0.8

I have searched but cant find the answer to a buy, sell and rebuy. if someone knows what the correct formula is to calculate the average buy price when you buy, sell and rebuy please let me know.

Thanks!
I am a Canadian, and under Canadian tax laws, I think there are at least two ways of determining your cost basis for shares. I am not going to get into the details of tax accounting; please see a qualified tax professional for advice.

However, if you just want to track your profit and loss for analysis purposes, the standard way of determining the book value of your positions is to track the average cost. You track the number of shares you own, and their total cost. 

When you buy more shares,


the total cost goes up by the total you paid in the transaction (=(# shares in the transaction) * (transaction price)),
the number of shares increases by the amount in the transaction


You get the average cost by dividing the total cost by the number of shares.

When you sell, the price you sell at does not matter for the determination of your average cost. You reduce the number of shares by the number of shares in the transaction, and you reduce the total cost by the (average price)*(number of shares in the transaction). This means that selling does not change the average price, just the number of shares.

Your profit on selling is based on comparing the selling price to the average cost. This would be the “cost of goods sold” in inventory accounting. (If you want more details on this subject, you could look for primers on inventory accounting on the internet.)

What you appear to want to do is to roll the profits or losses from a sale into the total cost. That is, if you made a profit, use that to lower the “total cost” of the shares so that you can compare that average of that “total cost” to the market price, and quickly tell if you are profitable.

Although that might be convenient, it probably would not be acceptable under most accounting conventions (so no textbook will tell you to do it that way). It also would give some strange-looking results. If you made \$1 million profit on some shares, and then sold all but one of those shares, you would have a “cost” of about -\$1 million for that one share. Most people would look at you funny if you showed them that number on a spreadsheet.