I am creating a market, and I want to create an appropriate mechanism for orders of market participants.
For example, I receive these orders:
A buys 50 B buys 100 C sells 50 D sells 20
which can be represented as List<Orders> , where Order is a class with Participant , BuySell and Amount
I want to create a Match function that outputs 2 things:
- A set of unsurpassed orders (
List<Order> ) - Set of Agreed Orders (
List<MatchedOrder> where MatchOrder has Buyer , Seller , Amount
The limitation is to minimize the number of orders (unsurpassed and comparable), without leaving a possible coincidence canceled (i.e. at the end there can only be purchase or sale orders that are unparalleled)
So, in the above example, the result will look like this:
A buys 50 from C B buys 20 from D B buys 80 (outstanding)
This seems like a pretty complicated writing algorithm, but very common in practice. Any pointers to where to look?
d - b source share