I'm making a corporation simulator, and it contains stocks. In the game you can buy and sell stocks, but I can't implement a realistic change in the price for them. How do I make them change up/down in a realistic way?


Rather unspecific to the programming language or the platform mentioned, but I would probably go for a greater change as a primary depiction of important stock change, and then generate a small fast change upon that to depict the hooky part of the stock graphs.

So basically:

  1. You start at time T1. You're going towards time T2. Price at time T1 is P1. P2 is end price.
  2. You generate a random number (probably based on some events, maybe?) for P2. This is your larger number target price.
  3. You iterate towards it with a timestep. If you update a couple of times (denoted as Tt) between those timesteps, you do

    (T2 - T1)/Tt = Your time step

  4. To get the price at this time, you do

    P1 + (P2 - P1)/Tt * AmountOfTimeStepsPassed

  5. This will get you a pretty straight line, so you could look into something like Bezier or something to make a curve. But to get those hooks, you need to add a very small totally random deviation upon it. This needs to go from a small negative number to a small positive number!

    P1 + (P2 - P1)/Tt * AmountsOfTimeStepsPassed + SmallRandomNumber

The smoother line would represent the great change, and the less smoother line is the actual stock.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.