# Simplified math formula to calculate stock price based on supply/demand

I am making an overly simplified version of a stock simulator game, because I:

1. am fascinated with how the stock market works
2. learn better & faster from a project like this, rather than text-books

Before writing any actual code, I started making an Excel spreadsheet to simulate the fluctuations of 1 company's stock based on how many people buy/sell the stock on any given day, so that when I have the code, I have something to compare it against to know that it works.

• a) Total amount of stock issued - 10000
• b) Starting stock price when the company goes public - $10.00 • c) Stock available on any given day • d) Stock bought/sold on that day - for example, 150 on day 1 • e) Stock bought/sold the previous day - when first starting the simulation, this number is 0 • f) Company profits on any given day - For simulation purposes, profits are calculated daily, not quarterly or yearly • g) Stock price - This is what I need calculated I tried using (d-e)/a which gives me a difference, like 0.015 which I use to get the stock price for the next day. But it just seems too simple, since buying 100% of the stock on day 1 and then selling 100% of it on day 2, brings down the price to$0.00. Perhaps this would work given enough random participants who all buy/sell the stock, but in this case, I need the simulation to work with just 1 person. And the simple function I wrote just gives too much power to that 1 person.

PS: For simulation purposes, I assume there is always a buyer and always a seller for the stocks.

PPS - Edited to provide the test numbers I'm starting with

• Maybe it would be better to give it here: economics.stackexchange.com Oct 30 '11 at 19:44
• @zacharmarz I've asked this question on 2 SA Q&A sites that deal with finance and got redirected here. And now I'm being directed to the Economics one... And I'm pretty sure, they are going to redirect me to another Q&A site. And since this is a simulation game I'm making, I don't really need real-world scenarios and functions, which is what the other boards might provide. Oct 30 '11 at 19:47
• "I need the simulation to work with just 1 person." Who is the other side of the trade then?
– Den
Oct 30 '11 at 20:26
• @Den As I mentioned, for simulation purposes, I assume that there is always a buyer and a seller, so there wouldn't be anyone else on the other side of the trade. The player would be able to buy or sell as much as he likes/can afford. Oct 30 '11 at 20:29
• Logically there is always someone on the other side of the trade. In your case it is simplified to a virtual "person"/market that is always willing to buy/sell any amount of stock. Maybe you can add a randomiser to change the maximum amount of stock that virtual person is willing to trade on any given day (like based on company profit)?
– Den
Oct 30 '11 at 20:37

Maybe you can have an entity 'Market' that represents a collection of players 'Trader'. Each of those players can be either a real person or a "virtual" one - bot. Each player can be willing to buy or sell certain amount of stock at certain price. Some bots can be optimistic, some pessimistic.

To simplify things all trades will be carried out at certain time(s) within a day. You can open a position to trade specific amount at specific price.

The 'Market' than sums up all the requests at "trade time" and if your position qualifies (within available amount and price) your trade will be carried out.

• I would just like to clarify that even though I did not get a mathematical function that would solve my problem, by using the answer @Den provided, I could stick to the math I have in place and use virtual bots, to simulate supply/demand. Oct 30 '11 at 21:14
• Sorry to res an old topic, @leo.vingi do you have a working example? Mar 22 '17 at 12:49

In finance the simplest model of prices is the lognormal distribution https://en.wikipedia.org/wiki/Log-normal_distribution

To simulate this you create a random normal variate each "tick" (using Box-Muller https://stackoverflow.com/questions/2325472/generate-random-numbers-following-a-normal-distribution-in-c-c ) and multiply that by the price and a constant to get the change in value of the stock.

Pick a different constant for each stock, and they will need to be small (0.01 is reasonable for daily updates, smaller for shorter periods).

Actually trying to trade these will be a boring game--since the price moves are random there will be no skill involved. To make it more pointed, multiply each stock price (not the stock price move) by a different factor as well as adding the random noise. Factors will be very close to 1 (0.999-1.001) but if the player can work out which ones are going up, he's in with a chance of making a profit.