How to determine how much cash a user has in C\$50 Finance's portfolio.php

I've read portfolio's specifications. However, I have a doubt regarding how to update a given users's balance. Suppose a user with an id '1' buys a stock whose symbol is 'A' for a price '\$B'. Now suppose that A's price increments by \$5. Now, when I list how much shares a user has and how much they're worth, which price should I print and how should I display the balance?

I'm thinking regarding the cash balance, I would have to use the previous value of price with which the user made the transaction. Is that right?

However, in portfolio, when I'm displaying how much the user's share are worth currently, should I use the updated price? This would create a discrepancy between how much the user is billed for the stock 'A' and how much they're currently worth. Is that discrepancy acceptable?

Yes your logic is correct, and this is exactly the way the world works.

• Say I have \$20.
• I buy a diamond for \$10.
• So now I have \$10 left.
• In a week from now, the same diamond, costs \$5.
• But I still have \$10 left, not \$15. My current cash balance is not determined by the value of the things I bought at past. It stays the same.
• Now say I also buy this diamond.
• I now have a cash balance of \$5.
• In a week from now, the same diamond, costs \$20.
• In cash, I have \$5, but if I sell both my diamonds now, I will get an extra \$40 (2x\$20)
• So if I sell them I have \$45, more than I started with.

And that's how economy works. Your money in cash doesn't change, but the value of your possessions might change.

So you should have the user's cash updated by the current price of a share, each time a transaction is made, and in a later time, if you want to show the value of the shares a user has, you should use the value of the shares at that moment, not the moment he/she bought them.

If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

• Thanks! That was helpful. Sep 2 '15 at 4:40