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?
This method of predicting future price of a stock is based on a basic formula. The formula is shown above (P/E x EPS = Price). According to this formula, if we can accurately predict a stock's future P/E and EPS, we will know its accurate future price.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With