How can I calculate the return on a series of stock positions with multiple uneven transactions that I can compare to the equivalent buy and hold return over the same period?
Random Example: Starting cash = $6000
Day: Buy 100 shares of stock A @ $50 -- Long Position opened
Day: Buy 10 shares of stock A @ $52
Day: Sell 50 shares of stock A @ $60
Day: Sell 60 shares of stock A @ $58 -- Position closed w/ zero shares
Day: Buy 100 shares of stock A @ $55 -- Long Position opened
Day: Sell 50 shares of stock A @ $65
Day: Sell 50 shares of stock A @ $58 -- Position closed w/ zero shares
Buy and Hold:
Day: Buy $6000 worth of stock A @ $50 (120 shares)
Day: Sell 120 shares @ $58 ($6960)
Return = ($6960 - $6000) / $6000 * 100 = 16%
I'm trying to implement this in code and need to figure out a generic formula/algorithm that would cover any case.
I may have been overthinking this before...
Just tracking the value of the account through each transaction and calculating the final amount at the end can get me to the answer. Per the example: the final value after all the stock trades is $7610. Therefore:
Return = (7610-6000)/6000 * 100 = 26.8%
Maybe there is another advanced return metric that I'm not aware of, but this seems to be the obvious solution to me.