|
Question : Stock Market Tracking Database Design : Multiple Buys and Sells
|
|
Good Morning Experts!
I have a theory / practicality question.
I have been planning for the past month or so the proper layout for a stock market tracking database. It'll be for my personal usage, so probably not to much data will be stored. Because of that I've chosen Access as my database of choice. (As an added bonus I'm pretty familiar with Access)
Any ways. The problem that I've encountered and can't seem to work through is this:
Let's say a stock has dividends (both cash or stock). Also, Lets say I purchase a certain amount of shares of stock A and then two weeks later I buy some more shares of stock A. 3 weeks after that I sell some shares of that. During all of this time I am receiving cash or stock dividends on the current amount of shares that I own.
I've written databases before that take an original unit amount and multiple that by a certain dividend amount for the life of a program. The difference between that and that though is that those unit amounts stayed static, but with this stock database the unit amount fluctuate (for the same program).
Any ideas on table configurations for this? i.e. how to set up tables and relations for stock names, units owned, cash dividends, etc.
If you already have prefabbed exmples that would work as well. Like I said the planning of this thing has been killing me, I just can't seem to think of a way to handle all this.
Thank you for all the support/suggestions!
|
|
Answer : Stock Market Tracking Database Design : Multiple Buys and Sells
|
|
Doesn't something like this do it? If you had Shares purchased:
ShareBuys ======== ShBuyID - pk ShID - text Qty - Double PurDate PricePerShare BrokerID etc.
And those sold
ShareSells ======== ShSellID ShBuyID - fk - from ShareBuy ShSellDate Qty PricePerShare BrokerID etc.
And a list of Dividends:
Dividends ======= DivID DivDate - date ShareID - fk from ShareBuys DivPerShare = Currency
SELECT a.ShareID, a.DivDate, (Sum(b.Qty) - Sum(c.Qty)) * a.DivPerShare AS ShareDividends FROM Dividends a INNER JOIN ShareBuys ON a.ShareID=b.ShareID INNER JOIN ShareSells c. ON a.ShareID=c.ShareID WHERE b.ShBuyDate < a.DivDate AND c.ShSellDate > a.DivDate GROUP BY a.ShareID, a.DivDate;
|
|
|
|