Stock option price variations by users orders (buy/sell)

天涯浪子 提交于 2019-12-06 04:13:26

问题


Following this question Generate a fictitious stock option price variation

I wish to simulate that the price change, while users gives an order of buy or sell, like the real stock exchange. (I make a user case to help you to understand.)

Initial state "Stock option example" :

Company X, price of the stock option $20,000

A CRON task makes the price variation each 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 made by random news which makes $max_up > $max_down or $max_up < max_down for a random time. Between, $max_up = $max_down.

Result in picture (1hour by minutes)

User case "Buy example" :

  • A user send an order to buy 1000 of this option at $18,000
  • The system store the order in database
  • A CRON task verify each minute, if the price was <= to a buy order, the last minute
  • When the price of this option <= to this order, the user gets this stock option.

User case "Sell example" :

  • A user send an order to sell 1000 of this option at $22,000
  • The system store the order in database
  • A CRON task verify each minute, if the price was >= to a sell order, the last minute
  • When the price of this option >= to this order, the user sells this stock option.

My problem

It works fine, but it is not a real variation of a stock exchange market.

My question

How to make the price variation by the prices and quantities of the orders ?

Like the "law of supply and demand".

For example (edit regarding Peter 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       
}

Perhaps I'm a little bit crazy to think that it could be possible to automatize that, but I believe in it, any help will be greatly appreciated.


回答1:


Just wanted to expand on my comment a bit more. Here is a basic scenario; assume we start from zero unfulfilled orders in the database/system.

  1. User A submits a limit sell order for 20 units of Stock X @ $10.
  2. User B submits a limit buy order for 10 units of Stock X @ $12.

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

After step 2, the order User B submitted can be fulfilled by an open order in the system. (For simplicity, let's assume User A's order can be broken up, i.e. not an all-or-none order)

The reason User B's buy order can be fulfilled is for these two reasons:

  1. There is an open sell sell order for a quantity greater than or equal to the buy order quantity.
  2. The limit buy price is greater than or equal to the limit sell price, so a transaction price can be agreed upon.

User A doesn't want to sell for less than $10, and User B doesn't want to buy for more than $12. So in this case, there is a range of suitable transaction prices, i.e. any price between $10-12 is suitable.

The problem is finding the suitable transaction price. How to determine it? Pick the middle of the range? This is only one solution. (In a market with a lot of liquidity, you may not have this same sort of problem since there will be a lot of open orders at different prices and some at market price.)

For the sake of the example, let's say you picked a transaction price of $11, i.e. the middle of the suitable range. User B's order would now be fulfilled and since User A's order was only partially fulfilled, there would be one open order left in the system: User A to sell their remaining 10 units at $10. The last trade price would be updated to $11.



来源:https://stackoverflow.com/questions/9379261/stock-option-price-variations-by-users-orders-buy-sell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!