I am creating a mock stock exchange and have a question about implementation of orders that dont execute right on creation, but need to be kicked off if certain criteria becomes true. ie, a stop order (sell once stock hits a certain price).
For normal orders, the code is pretty straightforward. If a buy order comes in, the orders table is immediately queried for sell orders that match. If the whole buy order cannot be filled, the remainder sits in the order table and waits for a new sell order to come in, at which point buy orders are queried and matched to the new sell order. So, orders sitting in the table have already been checked against all other orders in the table, and never need to be checked again for matches with other orders in the table. Only checks happen with a new order is added.
Question is, with a stop order, once the stock price hits a certain point, that order sitting in the table may now be compatible with another order in the table, so that would have to be checked. What is the best way for having PHP do this check automatically once certain criteria are met? When an order like that comes in, do i feed its order_id and criteria information into a constantly running script that knows to check that order when the time is right? And how do i do that? Any ideas would be greatly appreciated. Thanks.