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[0]: Buy 100 shares of stock A @ $50 -- Long Position opened
Day[3]: Buy 10 shares of stock A @ $52
Day[6]: Sell 50 shares of stock A @ $60
Day[7]: Sell 60 shares of stock A @ $58 -- Position closed w/ zero shares
Day[10]: Buy 100 shares of stock A @ $55 -- Long Position opened
Day[16]: Sell 50 shares of stock A @ $65
Day[20]: Sell 50 shares of stock A @ $58 -- Position closed w/ zero shares
Return: ?
Buy and Hold:
Day[0]: Buy $6000 worth of stock A @ $50 (120 shares)
Day[20]: 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.
EDIT:
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.