def test_cancel_confirm_entire_price_level(): ob = build_base_order_book() # first add an order at a second price (34.53) a = NewOrderCommand(56, 1234002.123, 1009, "user_x", MARKET, ASK_SIDE, FAR, Price("34.53"), 20) ask_oec = OrderEventChain(a, LOGGER, SUBCHAIN_ID_GENERATOR) a_ack = AcknowledgementReport(57, 1234002.123, 1009, "user_z", MARKET, a, Price("34.53"), 20, 20) ask_oec.apply_acknowledgement_report(a_ack) ob.handle_acknowledgement_report(a_ack, ask_oec) # cancel the orderchains at best price id_generator = MonotonicIntID() for i, chain in enumerate(ob.order_chains_at_price(ASK_SIDE, ob.best_price(ASK_SIDE))): cancel_command = CancelCommand(id_generator.id(), 1234002.123, chain.chain_id(), chain.user_id(), MARKET, USER_CANCEL) chain.apply_cancel_command(cancel_command) cancel_report = CancelReport(id_generator.id(), 1234002.123, chain.chain_id(), chain.user_id(), MARKET, cancel_command, USER_CANCEL) chain.apply_cancel_report(cancel_report) ob.handle_cancel_report(cancel_report, chain) assert ob.best_priority_chain(ASK_SIDE).chain_id() == ask_oec.chain_id() # best price should now be 34.53 assert ob.best_ask_price() == Price('34.53') # visible qty is 20 assert ob.visible_qty_at_price(ASK_SIDE, ob.best_ask_price()) == 20 # hidden qty is 0 assert ob.hidden_qty_at_price(ASK_SIDE, ob.best_ask_price()) == 0 # num orders is 1 assert ob.num_orders_at_price(ASK_SIDE, ob.best_ask_price()) == 1 # bids shouldn't change bid_prices = ob.bid_prices() assert len(bid_prices) == 1 assert bid_prices[0] == Price("34.50") assert ob.best_bid_price() == Price("34.50") assert ob.best_bid_level() == PriceLevel(Price("34.50"), 120, 0, 2)
def test_populating_tob(): ob = OrderLevelBook(MARKET, LOGGER) b1 = NewOrderCommand(1, 1234000.123, 1001, "user_a", MARKET, BID_SIDE, FAR, Price("34.50"), 50) bid_oec1 = OrderEventChain(b1, LOGGER, SUBCHAIN_ID_GENERATOR) b1_ack = AcknowledgementReport(2, 1234000.123, 1001, "user_a", MARKET, b1, Price("34.50"), 50, 50) bid_oec1.apply_acknowledgement_report(b1_ack) ob.handle_acknowledgement_report(b1_ack, bid_oec1) assert ob.best_bid_price() == Price("34.50") assert ob.best_priority_chain(BID_SIDE).chain_id() == bid_oec1.chain_id() assert ob.best_bid_level().price() == Price("34.50") assert ob.best_bid_level().visible_qty() == 50 assert ob.best_bid_level().hidden_qty() == 0 # now add an offer a1 = NewOrderCommand(3, 1234000.888, 1002, "user_b", MARKET, ASK_SIDE, FAR, Price("34.52"), 35, 10) ask_oec1 = OrderEventChain(a1, LOGGER, SUBCHAIN_ID_GENERATOR) a1_ack = AcknowledgementReport(4, 1234000.888, 1002, "user_b", MARKET, a1, Price("34.52"), 35, 10) ask_oec1.apply_acknowledgement_report(a1_ack) ob.handle_acknowledgement_report(a1_ack, ask_oec1) assert ob.best_ask_price() == Price("34.52") assert ob.best_priority_chain(ASK_SIDE).chain_id() == ask_oec1.chain_id() assert ob.best_ask_level().price() == Price("34.52") assert ob.best_ask_level().visible_qty() == 10 assert ob.best_ask_level().hidden_qty() == 25 assert ob.best_ask_level().number_of_orders() == 1 # bid should not have changed assert ob.best_bid_price() == Price("34.50") assert ob.best_priority_chain(BID_SIDE).chain_id() == bid_oec1.chain_id() assert ob.best_bid_level().price() == Price("34.50") assert ob.best_bid_level().visible_qty() == 50 assert ob.best_bid_level().hidden_qty() == 0 assert ob.best_ask_level().number_of_orders() == 1 assert ob.num_orders_at_price(BID_SIDE, Price("34.50")) == 1 # add a second ask order to top of book a2 = NewOrderCommand(5, 1234001.888, 1003, "user_c", MARKET, ASK_SIDE, FAR, Price("34.52"), 20, 20) ask_oec2 = OrderEventChain(a2, LOGGER, SUBCHAIN_ID_GENERATOR) a2_ack = AcknowledgementReport(6, 1234001.888, 1003, "user_b", MARKET, a2, Price("34.52"), 20, 20) ask_oec2.apply_acknowledgement_report(a2_ack) ob.handle_acknowledgement_report(a2_ack, ask_oec2) assert ob.best_ask_price() == Price("34.52") # best price has not changed assert ob.best_priority_chain(ASK_SIDE).chain_id() == ask_oec1.chain_id() # best priority chain has not changed assert ob.best_ask_level().price() == Price("34.52") # price of best level has not changed print(ob.best_ask_level().visible_qty()) assert ob.best_ask_level().visible_qty() == 10 + 20 # visible qty should have got up by 20 assert ob.best_ask_level().hidden_qty() == 25 # hidden qty should not have changed assert ob.best_ask_level().number_of_orders() == 2 # num orders should go up 2 assert ob.num_orders_at_price(ASK_SIDE, Price("34.52")) == 2 # bid should not have changed assert ob.best_bid_price() == Price("34.50") assert ob.best_priority_chain(BID_SIDE).chain_id() == bid_oec1.chain_id() assert ob.best_bid_level().price() == Price("34.50") assert ob.best_bid_level().visible_qty() == 50 assert ob.best_bid_level().hidden_qty() == 0 assert ob.best_bid_level().number_of_orders() == 1 assert ob.num_orders_at_price(BID_SIDE, Price("34.50")) == 1 # add a new bid at tob price b2 = NewOrderCommand(9, 1234000.123, 1004, "user_z", MARKET, BID_SIDE, FAR, Price("34.50"), 70) bid_oec2 = OrderEventChain(b2, LOGGER, SUBCHAIN_ID_GENERATOR) b2_ack = AcknowledgementReport(10, 1234000.123, 1004, "user_z", MARKET, b2, Price("34.50"), 70, 70) bid_oec2.apply_acknowledgement_report(b2_ack) ob.handle_acknowledgement_report(b2_ack, bid_oec2) assert ob.best_bid_price() == Price("34.50") assert ob.best_priority_chain(BID_SIDE).chain_id() == bid_oec1.chain_id() assert ob.best_bid_level().price() == Price("34.50") assert ob.best_bid_level().visible_qty() == 120 assert ob.best_bid_level().hidden_qty() == 0 assert ob.best_ask_level().number_of_orders() == 2 assert ob.num_orders_at_price(BID_SIDE, Price("34.50")) == 2 # asks should not have changed assert ob.best_ask_price() == Price("34.52") # best price has not changed assert ob.best_priority_chain(ASK_SIDE).chain_id() == ask_oec1.chain_id() # best priority chain has not changed assert ob.best_ask_level().price() == Price("34.52") # price of best level has not changed assert ob.best_ask_level().visible_qty() == 10 + 20 # visible qty should have got up by 20 assert ob.best_ask_level().hidden_qty() == 25 # hidden qty should not have changed assert ob.best_ask_level().number_of_orders() == 2 # num orders should go up 2 assert ob.num_orders_at_price(ASK_SIDE, Price("34.52")) == 2