Frage

I have been working on a virtual stock market game using PHP. The formula that I have been using for deciding the price of a stock is

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

where

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

$intensity is a number between -5 to 5 depending upon whether the news is good or bad for the company and

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

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

The problem with this formula is that, even if the intensity is negative (even -5) the ratio term is always added to the price which makes the overall term increase. The prices are refreshed every 10 seconds and with the above formula they keep on increasing and never come down. So, can anyone help me out with this formula so that it varies more closely to the actual stock market?

War es hilfreich?

Lösung

If I understand correctly, you're trying to define an algorithm to determine a logical next price based on the current price, some market activity, and a random input. This is called a Random Walk, and the linked page is quite informative.

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

It's difficult for us to provide an exact function for you, since the exact behavior you expect of a function like this is inherently application specific. However it is possible to test the behavior and improve on it, by pulling it out into its own method and tweaking it until you see the behavior you want.

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

Here's some boilerplate to get 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 $ran definition is definitely flawed, if $intensity is -5 you're executing $ran = rand(5, -10); which generates a warning and doesn't return the value you want. This is likely the root of your issue, as any negative $intensity will essentially set $ran to zero.
  • Furthermore your $ran definition is biased towards positive numbers, meaning the price is - rather quickly - going to rise even if there's bad news. I'd suggest ensuring your random value is equally likely to lower the stock as raise it, and if you intend for the stock to rise in value over time regardless (which seems like a bad idea to me) set a separate $longTermGrowthFactor that always increases the stock by that factor, separately from the randomness.
  • Turn on warning reporting in PHP - since you presumably hadn't seen the warnings related to your rand() call, you likely have warnings and other error types turned off, which quite likely means there are other errors hidden in your code you aren't aware of, and without the reporting they're going to be hard to spot.
  • Use mt_rand() instead of rand(), the latter is deprecated, and mt_rand() is a drop-in replacement providing better randomness.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top