Change in the price of stock options by order of users (purchase / sale)

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) Stock option price variation

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.

+4
source share
1 answer

Just wanted to expand my comment a little more. Here is a basic scenario; Suppose we start from zero outstanding orders in the database / system.

  • User A submits a limit on the sale of 20 pieces X @ $ 10.
  • User B submits an application for a limit of 10 units of shares X @ $ 12.

After step 1, you will have one unfulfilled order in the system, since there are no open orders for matching.

After step 2, the submitted User B order can be executed in open order in the system. (For simplicity, we assume that the user order can be broken, i.e., not all-or-no order)

The reason that an order for user B can be executed can be fulfilled for two reasons:

  • There is a sell order with an open sale for an amount greater than or equal to the quantity of a purchase order.
  • The purchase price of the limit is greater than or equal to the maximum selling price, so you can agree on the transaction price.

User A does not want to sell less than $ 10, and User B does not want to buy more than $ 12. Thus, in this case, there are a number of suitable transaction prices, that is, any price between 10-12 US dollars is suitable.

The problem is finding the right deal price. How to define it? Choose the middle of the range? This is only one solution. (In a market with high liquidity, you may not have the same problem, as there will be many open orders at different prices, and some at the market price.)

For example, suppose you choose a transaction price of $ 11, i.e. in the middle of a suitable range. The order of User B will now be executed, and since the order of the User has been partially completed, the system will have one open order: User A will sell the remaining 10 units at a price of 10 US dollars. The latest trading price will be updated to $ 11.

+2
source

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


All Articles