3

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.

2

1 Answer 1

2

Generally if you are using FIFO (first in, first out) accounting, you will need to match the transactions based on the number of shares.

In your example, at the beginning of day 6, you had two lots of shares, 100 @ 50 and 10 @ 52. On that day you sold 50 shares, and using FIFO, you sold 50 shares of the first lot. This leaves you with 50 @ 50 and 10 @ 52, and a taxable capital gain on the 50 shares you sold.

Note that commissions incurred buying the shares increase your basis, and commissions incurred selling the shares decrease your proceeds. So if you spent $10 per trade, your basis on the 100 @ 50 lot was $5010, and the proceeds on your 50 @ 60 sale were $2990. In this example you sold half of the lot, so your basis for the sale was half of $5010 or $2505, so your capital gain is $2990 - 2505 = $485.

The sales you describe are also "wash sales", in that you sold stock and bought back an equivalent stock within 30 days. Generally this is only relevant if one of the sales was at a loss but you will need to account for this in your code. You can look up the definition of wash sale, it starts to get complex.

If you are writing code to handle this in any generic situation you will also have to handle stock splits, spin-offs, mergers, etc. which change the number of shares you own and their cost basis. I have implemented this myself and I have written about 25-30 custom routines, one for each kind of transaction that I've encountered. The structure of these deals is limited only by the imagination of investment bankers so I think it is impossible to write a single generic algorithm that handles them all, instead I have a framework that I update each quarter as new transactions occur.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .