Hello Grabber, I don't think you have defined the problem precisely enough. You don't need to balance shares, but to balance dollars, and to calculate the difference in shares. From your example
1) bgt 500 shares @ 5.75 - bgt $2875/500/5.75
2) sld 185 shares @ 6.40 - sld $1184/185. $1184=206@5.75; add to PC (206-185)*5.75; unbalanced: bgt $1691/294/5.75 note: the number of shares left unbalanced reflects the $$$ sold, not the number of shares sold!
8) sld 771 shares @ 8.00 - Funny boy! sld 6168/771, but there are only 609 shares left unbalanced, for $3394 total. $3394 is 424 shares @ 8, so you add to PC (609-424)*[((12*4.75)+(385*5.50)+(212*5.75))/(609)]; unbalanced: sld $2776/347/8
In step 8, the amount sold straddles three lots bought, with some left over to be balanced against future buys! Do yourself a favor and go for a percentage approximation! Please!! I cry for you when I see this kind of thing.
Register for free to join our community of investors and share your ideas. You will also get access to streaming quotes, interactive charts, trades, portfolio, live options flow and more tools.