This should be pretty simple but I can't think of the best way to do this. Lets say I have the history of some buys and sells for a given stock:
What I want to do is keep a running number to what my accumulated cost per share is. This would be the price to which if I sold my currently owned stocks, I would break even at.
So for the example above, When I get to the third buy in the list, I would have $9$ shares so when I get to the first sell, I would be left with $8$ but those $8$ didn't all get purchased at the same price so how would I keep track of this?
What I have tried
I started with keeping a running total of quantity owned, adding during buys and removing during sells. With this, I keep a running total cost column to which I add on the ($price \ per \ share * number \ of \ shares$) on all buys. If a sell then I don't add anything to the total costs and if the number of shares $= 0$ ever then the running costs become $0$ as well. Then the average cost per share is simply the running total cost column divided by the running total quantity one.
Does this make sense or is that inaccurate to go at it this way?

