Dictionary for storing orders - DataStructure
I have some orders coming down the wire quite frequently, I need to store
them and build an aggregation out of them. An order will have an ID and
there will be an instrument type associated with it. The orders can also
have some events attached to it, like say add, update or remove. If it's
an update event, then there will not be an instrument type attached with
the order, but the order id will be same. For ex: if I have an order for
instrument "xyz" with order id 100, later on I can get an event to update
the order which has an id 100 by $20, and there will not be an instrument
type present with that event (order).
Once I receive an order, I need to build an order book for unique
instruments, for example instrument "xyz" should contain all recieved
orders for it inside the orderbook.
My question is how efficiently can I store this and what kind of data
structure should I use for it?
An Order looks something like this:
public class Order
{
public Order(Action add, int id, string instrument, int price)
}
An Orderbook:
public class OrderBook
{
public string Instrument;
public List<Order> AllOrders;
}
Option 1:
Update a Dictionary<int,OrderBook> when I receive an order, with key as
order id, and create an order book for the instrument.
Issue: This will take care of the update events, I can check whether the
order already exists, and then update the order book. However an
instrument type should only have one order book, and this condition is
violated here, as for instrument "xyz" there could be multiple Add orders
coming through, and also makes the manipulation difficult.
Option 2:
Update a dictionary of Dictionary<OrderBook, List<int>>, with values as
the order id's.
Issue: This will take care of the above issue, however when I get an
update event, I'll have to check through every list of values (i.e list of
order id's) to see whether the order exists already, since the instrument
type is going to be empty and I cannot look by the OrderBook key.
Orders are coming down at real time, and the operation for storing and
retrieving has has to be bit more efficient(if not O(1) then O(logn)), is
there a better way to structure this please?
NOTE: An OrderBook is an aggregation of all orders for an instrument and
will be unique for the instrument. An order will be for an instrument for
a particular price, and there will be many orders for the same instrument.
I get the orders along with the event from someone else(a third party
lib), and I'm responsible for building the orderbook.
No comments:
Post a Comment