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.
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:
Even as a simplistic model, this is woefully inadequate.
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.