ridulfo/order-matching-engine

sorting problem in orderbook (?)

marmooli opened this issue · 2 comments

I'm not sure, but it seems to me something could be wrong with the sorting of orders in the orderbook or with your trade processOrder function. I tried the following exactly in this sequence:

1- limit order (side=sell, size=5, price=105)
2- limit order (side=sell, size=5, price=106)
and then
3- limit order (side=buy, size=1, price=105)
... but no trade took place!

The orderbook just added the lasd buy order to its content!
A trade would takes place, if I buy (for example size=1 and) price=106 and matches with the second order, but the first limit sell order with lower ask price (105) should have priority in this case.

I think it could be fixed in Order.py like this:

def __lt__(self, other):
    if self.price != other.price:
        if self.side == Side.BUY:
            return self.price > other.price
        else:
            return self.price < other.price

You are completely right, it fixed it! Would you like to make a pull request or should I make the change and give you credit?