Following this question Generate a change in the price of fictitious stocks.
I want to pretend that a price change, while users give an order to buy or sell, for example, on a real exchange. (I am making a user case to help you understand.)
Initial state "Stock option example" :
Company X, stock option price $ 20,000
The CRON task does a price change every second, with this PHP script:
function stockVariation($price,$max_up,$max_down) { // Variation calculate, with volatility max (10 to 100) $ratio=(mt_rand(0,$max_up)-mt_rand(0,$max_down))/10000; // New price $price+=$ratio; return round($price,5); }
Volatility is generated by random news that makes $ max_up> $ max_down or $ max_up <max_down for random time. Between, $ max_up = $ max_down.
The result in the picture (1 hour per minute) 
User Case "Purchase Example" :
- The user sends a purchase order 1000 of this option at a price of $ 18,000.
- The system stores the order in the database
- The CRON task is checked every minute if the price was <= before the purchase order, at the last minute
- When the price of this option is <= in this order, the user receives this stock option.
Case of the user "Selling example" :
- The user submits an order to sell 1,000 of these options at a price of $ 22,000.
- The system stores the order in the database
- The CRON task is checked every minute, if the price was = = for sale, last minute
- When the price of this parameter a> = to this order, the user sells this stock option.
My problem
It works great, but it's not a real stock market variation.
My question
How to make a price change for prices and the number of orders?
Like the "law of supply and demand."
For example ( edit Peter's answer ):
function stockOrder($orderPrice,$orderQuantity,$type)//$type= buy or sell { // Record the order in database (ok) // Compare with other orders (ok) // $orderPrice<=$dbSellPrice or $orderPrice>=$dbBuyPrice if checks // Buy and sell at the best prices // for quantities available holded by users (ok) // Record/update the holding of the stock (ok) // Update the price of the stock end if }
I may be a little crazy to think that you can automate this, but I believe in that, any help would be greatly appreciated.