Creating a stock market simulation

By Oscar Kelk • 15 minutes to read

Created


Supply and demand are the two factors which influence share prices within a stock market. When people buy, it rises. When people sell, it falls.

Some weeks ago I began a stock market simulation on a website run by myself and a friend. We already had a money system so I could implement it simply, the prospect being that players would deposit and withdraw cash, having it multiply daily.

And so this is how it continued for 3 days. Player accounts had 2 data fields: cash, and money (in shares). Every day you loaded the page - and if your money (in shares) hadn't yet been updated, a randomly generated multiplier seeded by the day's unix timestamp (12 am) was applied.

This seemed fine, but this system ended up going under heavy revision.

SM Stocks: Phase 2

Having the value of your shares wasn't realistic, and didn't allow for thorough manipulation, so first I changed the way the data was handled - each account now had a shares field in place of the money (in shares) field. In this new handling you would be told both how many shares you had, and what they worth - based on the share price. And the share price was what was changed.

Now every day the share price would be randomly multiplied; it could be anywhere from halved to doubled. But this didn't last.

SM Stocks: Phase 3

Allow me to introduce someone: I doubt he'd want to be named, so we'll give him a pseudonym: "Walter". Walter is a very strong fan of the website, and as you'll see he made quite a contribution to the system.

Walter was concerned with the lack of that principle I outlined earlier; whether someone bought at a low price or sold at a low price, they were equally as likely to make or lose money.

I first trialled a system in which the share price would be randomly added to or subtracted from. After running a simulation I saw that this would have created wild peaks and troughs in the share price (see below) which would sometimes run into a complete crash. Furthermore, it didn't really solve Walter's problem, so this too was only shortly in place.

Share price graph extremely peaking at the beginning before slowly falling into a crash

30 day simulation of the system

SM Stocks: Phase 4 - The formulas

Warning: maths

Walter and I spent more than an hour just figuring out a viable formula for the share price. Criteria was as follows:

But this is easier said than done! Imagine the new share price is simply calculated as the product of the old share price and the shares bought.

pricenew=priceold×sharesbought
pricenew=priceoldsharessold

Take this hypothetical player: Gobi. Gobi has $200,000 in his account, and at the moment the share price is $1, so he decides to buy.

Now Gobi has 200,000 shares, and the price is $200,000. See the problem?

Cash Shares Share price
$200,000 0 $1
$0 200,000 $200,000
$40,000,000,000 0 $1

Great. Now Gobi has made almost 40 billion dollars by doing absolutely nothing. And if he likes he may do it again, and again, and again.

How might this be solved? We tried modelling it after the real world. What if there was a certain amount of money owned by the 'company' which was split out among the total amount of shares?

pricenew=moneytotalsharestotal

Then we simulated, where Gobi (who still has $200,000) invests in the company, which begins with $20,000 and 5 shares.

Gobi's cash Gobi's shares Company funds Shares left Share price
$200,000 0 $20,000 5 $4,000
$196,000 1 $24,000 4 $6,000
$190,000 2 $30,000 3 $10,000
$180,000 3 $40,000 2 $20,000
$160,000 4 $60,000 1 $60,000

Once again. Gobi has 4 shares, each worth $60,000. From here he sells them for a total of $240,000, doubling his initial balance. So where is all this money coming from? The problem lies in the fact that each share is being bought at a lower price than what they are being sold for. In our simulation the first share was bought for $56,000 less than it was sold for. How can that be right?

Here's what we did: the shares, instead of being bought for the share price, are bought for what the share price will be after the shares are bought.

So when buying this is used:

pricenew=priceold+multiplier×shares×priceold

And for selling, its inverse:

pricenew=priceoldmultiplier×shares+1

Note the multiplier. This is in place to tone down the price a little bit - keep it at a reasonable level (if someone bought 200,000 shares, we wouldn't want the price at $200,000!).

Here I thought finally we had it. I wrote the code and was ready to push it to the website. Then, Walter came to me with a glaring concern. People could still make money by buying shares individually (as opposed to the whole lot at once). He came to me with his solution, newly written formulas which ensured each share was considered individually no matter how many are purchased.

When buying shares

pricenew=priceold×(multiplier+1)shares
investment=priceold×((multiplier+1)shares1)×(multiplier+1)multiplier

The investment formula is how much one would need to pay, based on how many shares they want to buy. It's a geometric sequence :)

Another feature of the simulation is the "money's worth" option. Instead of inputting how many shares they wish to buy, players may input how much money they wish to invest. For the calculation of shares from money, we use the following (rearranged from above):

shares=ln(multiplier×investment+priceold×(multiplier+1)priceold×(multiplier+1))ln(multiplier+1)

When selling shares

pricenew=priceold(multiplier+1)shares
investmentreturn=priceold×(multiplier+1)×(1(1multiplier+1)shares)multiplier

And of course rearranged:

shares=ln((multiplier×investmentreturnpriceold×(multiplier+1))priceold×(multiplier+1))ln(1multiplier+1)

Conclusion

So now I'm just letting it run. I'll see how it ends up in a few weeks and whether there are any tweaks that need to be made. I have identified some issues with graph rendering and the like, and I know how to fix them, but they're so minor that I'd rather just let them be.

If you are thinking of making a stock market game and you don't know how to calculate share price - it really should be based off purchases and sales of shares. Don't bother with any randomness unless you can find a better way to make it work!

Formulas

Here are the currently in-use formulas, in a more readable form.

Where:

When buying:

pnew=pold(c+1)n
a=pold((c+1)n1)(c+1)c
n=ln(ca+pold(c+1)pold(c+1))ln(c+1)

When selling:

pnew=pold(c+1)n
areturn=pold(c+1)(1(1c+1)n)c
n=ln((careturnpold(c+1))pold(c+1))ln(1c+1)

Viewed 2044 times

arrow_upward

Comments

New Comment