I recently wrote some Javascript code to generate random fake stock data as I wanted to show a chart that at first glanced looked like real stock data - but all I came up with was pretty noddy. I was just wondering if there are some resources that explain how this might be done "properly" i.e. so you get realistic looking data that has the same patterns that you see in real stock data?
A simple algorithm is to use a simple volatility number that restricts how much the stock can change within a given period (say, a single day). The higher the number, the more volatile. So each day you can compute the new price by:
rnd = Random_Float(); // generate number, 0 <= x < 1.0
change_percent = 2 * volatility * rnd;
if (change_percent > volatility)
change_percent -= (2 * volatility);
change_amount = old_price * change_percent;
new_price = old_price + change_amount;
A stable stock would have a volatility number of perhaps 2%. A volatility of 10% would show some pretty large swings.
Not perfect, but it could look pretty realistic.
Samples