def testBuyMatchMarketOrdersTimePrecedence(self): new_order = Order(OrderType.Sell, -1, 1, OrderConditions.Market, 1) existing_order_1 = Order(OrderType.Buy, -1, 1, OrderConditions.Market, 2) existing_order_2 = Order(OrderType.Buy, -1, 1, OrderConditions.Market, 3) order_matcher = OrderMatcher(Queue()) order_matcher.buy_orders.append(existing_order_1) order_matcher.buy_orders.append(existing_order_2) expected_match = existing_order_1 actual_match = order_matcher.match(new_order) self.assertEqual(actual_match, expected_match)
def testLimitBuyAtMarket(self): new_order = Order(OrderType.Buy, 1.5, 1, OrderConditions.Limit, 1) existing_order_1 = Order(OrderType.Sell, 1.5, 1, OrderConditions.Limit, 2) existing_order_2 = Order(OrderType.Sell, 1.6, 1, OrderConditions.Limit, 3) order_matcher = OrderMatcher(Queue()) order_matcher.sell_orders.append(existing_order_1) order_matcher.sell_orders.append(existing_order_2) actual_match = order_matcher.match(new_order) expected_match = existing_order_1 self.assertEqual(expected_match, actual_match)
def testLimitSellAboveMarket(self): new_order = Order(OrderType.Sell, 1.7, 1, OrderConditions.Limit, 1) existing_order_1 = Order(OrderType.Buy, 1.5, 1, OrderConditions.Limit, 2) existing_order_2 = Order(OrderType.Buy, 1.6, 1, OrderConditions.Limit, 3) order_matcher = OrderMatcher(Queue()) order_matcher.buy_orders.append(existing_order_1) order_matcher.buy_orders.append(existing_order_2) expected_match = None actual_match = order_matcher.match(new_order) self.assertEqual(actual_match, expected_match)
def testLimitBuyAboveMarketWithPricePrecedence( self): # marketable limit order new_order = Order(OrderType.Buy, 1.5, 1, OrderConditions.Limit, 1) existing_order_1 = Order(OrderType.Sell, 1.6, 1, OrderConditions.Limit, 2) existing_order_2 = Order(OrderType.Sell, 1.5, 1, OrderConditions.Limit, 3) order_matcher = OrderMatcher(Queue()) order_matcher.sell_orders.append(existing_order_1) order_matcher.sell_orders.append(existing_order_2) actual_match = order_matcher.match(new_order) self.assertEqual(existing_order_2, actual_match)
def testMatchBuyLimitSellMarketOverLimitEvenWithTimePrecedence(self): new_order = Order(OrderType.Buy, 1.5454, 1, OrderConditions.Limit, 1) existing_order_1 = Order(OrderType.Sell, 1.5353, 1, OrderConditions.Limit, 2) existing_order_2 = Order(OrderType.Sell, -1, 1, OrderConditions.Market, 3) order_matcher = OrderMatcher(Queue()) order_matcher.sell_orders.append(existing_order_1) order_matcher.sell_orders.append(existing_order_2) expected_match = existing_order_2 actual_match = order_matcher.match(new_order) self.assertEqual(actual_match, expected_match)
def testMatchBuyMarketSellLimitPricePrecedence(self): new_order = Order(OrderType.Buy, -1, 1, OrderConditions.Market, 1) existing_order_1 = Order(OrderType.Sell, 1.6, 1, OrderConditions.Limit, 2) existing_order_2 = Order(OrderType.Sell, 1.5, 1, OrderConditions.Limit, 3) order_matcher = OrderMatcher(Queue()) order_matcher.sell_orders.append(existing_order_1) order_matcher.sell_orders.append(existing_order_2) expected_match = existing_order_2 actual_match = order_matcher.match(new_order) self.assertEqual(actual_match, expected_match)
def generateOrder(self, orderid): order_size = self.size_gen.generateOrderSize() order_type = self.type_gen.generateOrderType() order_conditions = self.cond_gen.generateOrderConditions() if order_conditions == OrderConditions.Market: order_price = -1 else: #TODO AHHHH if order_type is OrderType.Buy: order_price = self.price_gen.generateBidPrice() else: order_price = self.price_gen.generateAskPrice() order = Order(order_type, order_price, order_size, order_conditions, orderid) return order