Artificial stock market

Hello! Today’s article will be practical. My game needs a problem solved. It is related to market making, stocks and a whole loads of fun a player can have around that. I will try to extract the most generic bit of it and while solving it I will share the solution with the public.

The problem

I need a realistic artificial simulation of a stock market in real time. It means the solution must generate a current stock price in real time in a way, so the historical aggregation of the price on a chart looks more or less like a real stock market. Of all the ways to draw historical market data I prefer the candlesticks. They look like the image below and are really good at visualizing OHLC data.

Let’s go!

The analysis

The problem might seem a bit complicated in the beginning. But we will step back a bit and do some analysis first.

The desired chart displays data point the can be expressed as OHLC = f(t), and since OHLC is just an aggregation of price within a definite time span, then what we really need is to create a model, that will follow a function of time.

P=f(t)

The function f(t) has the following properties observed:

  1. it has a value for every point of time, does not have gaps, to put it simple.
  2. every new value is reasonably close to previous values, so despite being discrete, it looks like it is a differentiable function.
  3. it is unpredictable to practical extent, you never know when the trend changes.

We will stick to these three intuitive features of the function and will try to keep it simple.

The solution design

In order to define a function that satisfies our criteria we will use the Fourier series. Tl;dr some functions, that satisfy a certain criteria, can be decomposed into a sum of harmonic components. Let’s say harmonic is just a function of sin(t) or cos(t). Having said all of that and doing unforgivable simplifications based on many-many questionable assumptions we define that our function will be the following for the sake of simplicity:

f(t)= C+∑A(k)*cos(k*t/T), where

t – the point in time we generate the price for

k – the index of the harmonic component, we can have as many as we need

T – is the period of the function, will get back to it later, treat it as a parameter for now

C – just a constant

A(k) – another parameter that is bound to k

Good enough for now and later we will tinker with it a bit more to better fit our needs. Now as the time flows we can do a calculation of the price.

Solution implementation

Let’s have it as technology agnostic as possible. We will build the thing in Excel to illustrate the implementation flow. This way anybody will be able to implement it in any language, be it JavaScript, Go, C# or, my godness, PHP.

Create a new spreadsheet and prefill it like on the image below.

Automarket1

I have highlighted the areas of our interest. You can already guess, where the ‘t’ goes and all other parts of the equation.

Next step is to come up with parameters. To cut a corner I suggest you to start with with the same parameter set as on the image below, then go try change them, experiment and see which parameters work for your case.

Automarket2

You see the changes and, probably, you’ve noticed that the ‘Open’ value and ‘HighLow’ value remain zero. No worries, their time is yet to come. Now we will put our function to a good use. Go ahead and put this Excel formula to the cell G6

=G$4*COS(G$3*$A6/$J$1)

If you use my parameter set, you will see the value 99.97530966 in the cell. Now go ahead and expand the formula to the G:P range like on the image below. Let it go to till it meets the last value of the ‘t’ down there.

Automarket3

I have 30 ‘t’ on the sheet, you can have as much as you want, obviously. Let’s take a short explanation break. We’ve defined the parameters of the equation and have got some results on the sheet that might not be quite obvious. I am talking about you ‘k’ and ‘Ak’! If you take a quick look at our formula, you notice that each price consists of a sum of components. The formula in the cell defines a single component, where ‘k’ is the index of the component, and ‘Ak’ is its amplitude. The sense behind them is the following: the bigger the ‘k’ – the faster the component goes between its max and min value, and the bigger ‘Ak’ is – the more its impact on the overall price at a time. So, we have the components, now lets get them all together in a happy sum. We need to build four values (Open, High, Low, Close) per ‘t’, but a function can build only one, we’ll pick one of the values we want to build and build the other values from that main one. Let’s pick Close as our main value of the four. Go ahead and put the following formula into E6 and expand it to the bottom of your ‘t’:

=$H$1+SUM(G6:Q6)+(RAND()-0.5)*$L$1

Now notice that the formula makes use of a random value that depends on the ‘Open’ parameter. This is needed to make our series a bit more unpredictable and realistic. This is a ‘magic number’ for you now, but when we’re done, you will be able to experiment and see how this parameter impacts the overall series. The image below features what your sheet resembles at this stage.Automarket4

Now let’s go and finish our OHLC. Our Open price for every ‘t+1’ will be equal to the Close price for ‘t’, and our first Open price will be equal our constant component ‘C’. The formulas for High and Low are up to you, but you can peek the ones I suggest in the image below. Now the time has come to set Open and High low to some values, I suggest 50 if you’re still using my parameters.Automarket5

The most boring part is over! Now you can use your Open, High, Low, Close table as a source for an Excel Stock chart and enjoy the result. Automarket6

As we can see, our data series satisfies the criteria I’ve defined during the analysis phase pretty well. Or does it really? Now it is time to get a little back-stab from her majesty Maths. You remember that we’ve defined a finite set of harmonics. Speaking mathematically, if you have a finite set of harmonics you can only build a periodical function. That means if you have a series long enough you will notice that the chart starts to repeat itself. It might be not that obvious since we use random under the hood, but the resemblance becomes uncanny as the time passes. That is not something we want our stock market to be. In order to survive that you have a couple options:

  • Make sure the parameters you choose make up a long period, so the function does not live long enough to start repeating. The bigger ‘T’ and ‘k’s will help.
  • If you cannot make the first option happen, you will have to change the parameters before the function goes beyond its full cycle.

Conclusion

In order to generate artificial stock market data we’ve picked a simple function and discussed its potential and applicability. This principle can be implemented in any language, tweaked for a specific purpose or case while still leaving you in control of the data generation to some extent. I really hope my article helped to solve the challenges and make somebody’s life a tiny bit easier.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s