I am creating a stock market simulator for an event in my college. Supposing I have 20 companies in it all starting with a common base price, I need a formula to calculate the changes in the fortunes of a stock based on how many shares are bought or sold.



/** A simplistic model for calculating share price.
 *  @param company_value The total value of the company.
 *  @param number_of_shares The total number of shares in circulation.
calc_share_price (company_value, number_of_shares) {
  return company_value / number_of_shares;

Answer proper:

If you had that, my friend, you would rule the world.

Unfortunately, stock values don't change as a function of buying and selling; they change as a function of their perceived value, which then causes people to sell and buy.

To elaborate; any model in which buying and selling directly affects the price is fundamentally broken and will cause a positive feedback loop.

If the cost of each share increases with people buying it, and the cost decreases with people selling it; then the only sensible thing to do would be to:

  1. Buy as many shares as you can.

  2. Wait until all other shares have been sold.

  3. Sell all your shares.

  4. Repeat.

Even as a simplistic model, this is woefully inadequate.

To wit:

To simulate a stock market; buyers and sellers must be able to determine the price themselves: A stock exchange just tracks the approximate value of a single share and makes trading easier.

There is a more in-depth explanation of how it works over at Economics; but the gist of it is: The value of a share of stock is roughly dependent on the value of the company and the number of shares, so those are the things you need to track.

Your problem is that buying and selling of shares in and of itself doesn't really change the value of the company; so you need some kind of external impulse to cause value changes.

  • \$\begingroup\$ Can You suggest a very simple and very basic format of stock simulation? \$\endgroup\$ – Kartik Krishnan Jan 13 '12 at 8:45
  • 1
    \$\begingroup\$ +1000 for the "Proper answer". You might get a more accurate simulation by using Supply/Demand competitively. \$\endgroup\$ – Jonathan Dickinson Jan 13 '12 at 8:59
  • \$\begingroup\$ @JonathanDickinson: That reads "Answer Proper", and references the writ of TL;DR. I like to split the longest of my answers into two, a short version and a full version. \$\endgroup\$ – Williham Totland Jan 13 '12 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.