Simple stock market behavior modeling algorithm

I am working on a virtual game in the stock market using PHP. The formula I used to determine the stock price

$price += $ran*0.001*$price + $ratio*0.005*$price 

Where

 $ran = rand(-1*$intensity, 2*$intensity) 
Intensity

$ is a number from -5 to 5, depending on whether it’s good or bad for news for the company and

 $ratio = (1.0*($buy-$sell))/($buy + $sell) 

$ buy and $ sell represent the number of shares bought and sold by the company, respectively.

The problem with this formula is that even if the intensity is negative (even -5), the ratio is always added to the price, which increases the overall period. Prices are updated every 10 seconds, and with the above formula, they continue to rise and never go down. So, can someone help me with this formula so that it changes more closely with the actual stock market?

+6
source share
1 answer

If I understand correctly, you are trying to determine an algorithm for determining the logical next price based on the current price, some market activity and random input. This is called Random Walk , and the linked page is pretty informative.

In economics, the “random walk hypothesis” is used to model stock prices and other factors. Empirical studies have found some deviations from this theoretical model, especially in the short and long term correlations.

It's hard for us to provide you with an exact function, because the exact behavior that you expect from such a function is inherently application specific. However, you can test the behavior and improve it by pulling it into your own method and tweaking it until you see the desired behavior.

I would suggest pulling this behavior that you defined in SSCCE (or unit test, but assuming you don't already have the PHP unit test framework, the example will succeed) and creating some test cases, then you can set up your algorithm in a vacuum and find the behavior you like.

Here are some templates to get you started:

 <?php function nextPrice($price, $intensity, $buy, $sell, $rand) { // TODO } // Can tweak these values between runs, or put them in a loop if you want $testPrice = 10.0; $testBuy = 10000; $testSell = 10000; for ($i = -5; $i <= 5; $i++) { // random float, from http://stackoverflow.com/a/14155720/113632 // set to a constant if you want to isolate the randomness and test other variables $testRand = mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(); echo "<p>Intensity: $i - Rand: $testRand = ". nextPrice($testPrice, $i, $testBuy, $testSell, $testRand)."</p>"; } ?> 

Some additional thoughts:

  • Your definition of $ran is not defined correctly if $intensity is -5 runs $ran = rand(5, -10); which generates a warning and does not return the required value. This is probably the root of your problem , since any negative $intensity essentially sets $ran to zero.
  • Also, your definition of $ran biased towards positive numbers, that is, the price - pretty quickly - will rise even if there is bad news. I would suggest that your random value equally contribute to lower stock prices, and if you intend to increase your stock in time (which seems like a bad idea), install a separate $longTermGrowthFactor , which always increases the stock by this ratio separately from randomness.
  • Enable a warning report in PHP - since you probably haven’t seen the warnings associated with your rand() call, you probably have warnings and other types of errors are turned off, which most likely means that in your code you you don’t know, there are other errors, and without a report they will be difficult to detect.
  • Use mt_rand() instead of rand() , the latter is deprecated, and mt_rand() is a replacement replacement, providing better chance.
+4
source

Source: https://habr.com/ru/post/955693/


All Articles