Jump to content

Recommended Posts

How would you go about creating a Simulation Stock Exchange? I basicly just want, a few companies(made up), and they all have a starting rate of $2.00 , and some how code it so it increases or decreases at a maximum of $2 difference, and is updated say every 10 minutes. and would you need a database for this to work?

Link to comment
https://forums.phpfreaks.com/topic/52201-solved-creating-the-stock-exchange/
Share on other sites

ok, you would have for example 3 companies, and probably have them in an array.

 

for this example lets say the 3 company names are, bobs steel. Rocket. and FI(farming Industry)(all made up).

 

ok, so you got them on a single page, displayed like so.

 

Companies  -  Rates - Date

 

Bob's Steel -  $2.00    20/5

Rocket      -  $2.00    20/5

FI            -  $2.00    20/5

 

Have them in a table format like that.

Now you have a script, that is run by cron every 10 minutes or so.

The script, first choose's 1 of the 4 prime mathematical operators.( - + * / ) and then

have 2 randomly generated numbers, within 2 of the initial value of the companies rate.

and then place the first random number, followed by the randomly chosen mathematical operator, which is followed by the second chosen number.

 

$math = ("$randnum1 $randop $randnum2");

 

something like that. it does that, and then displays the new number in the table for each company. so then the table may look like this for example.

 

 

Companies  -  Rates - Date

 

Bob's Steel -  $2.23    20/5

Rocket      -  $1.78    20/5

FI            -  $2.04    20/5

 

and it keeps running the cron job every 10 minutes or whatever.

 

and what I mean by within $2 difference, I mean whatever the math equation above does, it can't go higher or lower then $2 each way of the current rate for that company.

 

I hope you have a better understanding now. Thanks in advance  :)

 

Regards MasterACE14

This is actually quite a simple job.  Since you don't want it to shift more than $2, you should first of all only use + or - in your mathematical operations.  You will also want a database for this, seeing as it would be easier.  But this is pretty much all you need to have in your cronjob file:

 

<?php
// Connect to your database
$math = array("+", "-");
$result = mysql_query("SELECT * FROM stocks ORDER BY RAND()");
while ($row = mysql_fetch_assoc($result))
{
     $randOp = $math[array_rand($math)];
     $amount = rand(1, 200) / 100;
     $newAmount = eval("return {$row['amount']} $randOp $amount");
     while ($newAmount <= 0)
     {
          $randOp = $math[array_rand($math)];
          $amount = rand(1, 200) / 100;
          $newAmount = eval("return {$row['amount']} $randOp $amount");
     }
     mysql_query("UPDATE stocks SET amount='{$newAmount}' WHERE id='{$row['stock_id']}");
}
?>

From that you should be able to figure out what your table structure should be like.  Please note that's an absolutely rough script and shouldn't be copied and pasted...it should be used as a reference.  The way above is very inefficient.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.