async def setUp(self): super(AbstractTestOrderBook, self).setUp() # Object creation self.ask = Ask( OrderId(TraderId(b'0' * 20), OrderNumber(1)), AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now()) self.invalid_ask = Ask( OrderId(TraderId(b'0' * 20), OrderNumber(1)), AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MB')), Timeout(0), Timestamp(0)) self.ask2 = Ask( OrderId(TraderId(b'1' * 20), OrderNumber(1)), AssetPair(AssetAmount(400, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now()) self.bid = Bid( OrderId(TraderId(b'2' * 20), OrderNumber(1)), AssetPair(AssetAmount(200, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now()) self.invalid_bid = Bid( OrderId(TraderId(b'0' * 20), OrderNumber(1)), AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MB')), Timeout(0), Timestamp(0)) self.bid2 = Bid( OrderId(TraderId(b'3' * 20), OrderNumber(1)), AssetPair(AssetAmount(300, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now()) self.trade = Trade.propose( TraderId(b'0' * 20), OrderId(TraderId(b'0' * 20), OrderNumber(1)), OrderId(TraderId(b'0' * 20), OrderNumber(1)), AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MB')), Timestamp(1462224447117)) self.order_book = OrderBook()
def setUp(self): yield super(PriceTimeStrategyTestSuite, self).setUp() # Object creation self.ask = Ask( OrderId(TraderId(b'0' * 20), OrderNumber(1)), AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now()) self.ask2 = Ask( OrderId(TraderId(b'1' * 20), OrderNumber(2)), AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now()) self.ask3 = Ask( OrderId(TraderId(b'0' * 20), OrderNumber(3)), AssetPair(AssetAmount(40000, 'BTC'), AssetAmount(200, 'MB')), Timeout(100), Timestamp.now()) self.ask4 = Ask( OrderId(TraderId(b'1' * 20), OrderNumber(4)), AssetPair(AssetAmount(3000, 'A'), AssetAmount(3000, 'MB')), Timeout(100), Timestamp.now()) self.ask5 = Ask( OrderId(TraderId(b'1' * 20), OrderNumber(4)), AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'C')), Timeout(100), Timestamp.now()) self.bid = Bid( OrderId(TraderId(b'0' * 20), OrderNumber(5)), AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now()) self.bid2 = Bid( OrderId(TraderId(b'0' * 20), OrderNumber(6)), AssetPair(AssetAmount(6000, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now()) self.ask_order = Order( OrderId(TraderId(b'9' * 20), OrderNumber(11)), AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now(), True) self.ask_order2 = Order( OrderId(TraderId(b'9' * 20), OrderNumber(12)), AssetPair(AssetAmount(600, 'BTC'), AssetAmount(60, 'MB')), Timeout(100), Timestamp.now(), True) self.bid_order = Order( OrderId(TraderId(b'9' * 20), OrderNumber(13)), AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now(), False) self.bid_order2 = Order( OrderId(TraderId(b'9' * 20), OrderNumber(14)), AssetPair(AssetAmount(6000, 'BTC'), AssetAmount(60, 'MB')), Timeout(100), Timestamp.now(), False) self.order_book = OrderBook() self.price_time_strategy = PriceTimeStrategy(self.order_book)
def setUp(self): yield super(MatchingEngineTestSuite, self).setUp() # Object creation self.ask = Ask( OrderId(TraderId(b'2' * 20), OrderNumber(1)), AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(30), Timestamp.now()) self.bid = Bid( OrderId(TraderId(b'4' * 20), OrderNumber(2)), AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(30), Timestamp.now()) self.ask_order = Order( OrderId(TraderId(b'5' * 20), OrderNumber(3)), AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(30), Timestamp.now(), True) self.bid_order = Order( OrderId(TraderId(b'6' * 20), OrderNumber(4)), AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(30), Timestamp.now(), False) self.order_book = OrderBook() self.matching_engine = MatchingEngine( PriceTimeStrategy(self.order_book)) self.ask_count = 2 self.bid_count = 2
def insert_bid(order_book, amount1, amount2): """ Insert a bid with a specific price and quantity """ new_bid = Bid(OrderId(TraderId(b'3' * 20), OrderNumber(len(order_book.bids) + 1)), AssetPair(AssetAmount(amount1, 'BTC'), AssetAmount(amount2, 'MB')), Timeout(30), Timestamp.now()) order_book.insert_bid(new_bid) return new_bid
def create_bid(self, amount1, amount2): """ Create a bid with a specific price and quantity """ new_bid = Bid( OrderId(TraderId(b'3' * 20), OrderNumber(self.bid_count)), AssetPair(AssetAmount(amount1, 'BTC'), AssetAmount(amount2, 'MB')), Timeout(30), Timestamp.now()) self.bid_count += 1 return new_bid
def update_ticks(self, ask_order_dict, bid_order_dict, traded_quantity): """ Update ticks according to a TrustChain block containing the status of the ask/bid orders. :type ask_order_dict: dict :type bid_order_dict: dict :type traded_quantity: int """ ask_order_id = OrderId(TraderId(unhexlify(ask_order_dict["trader_id"])), OrderNumber(ask_order_dict["order_number"])) bid_order_id = OrderId(TraderId(unhexlify(bid_order_dict["trader_id"])), OrderNumber(bid_order_dict["order_number"])) self._logger.debug("Updating ticks in order book: %s and %s (traded quantity: %s)", str(ask_order_id), str(bid_order_id), str(traded_quantity)) # Update ask tick ask_exists = self.tick_exists(ask_order_id) if ask_exists and ask_order_dict["traded"] >= self.get_tick(ask_order_id).traded: tick = self.get_tick(ask_order_id) tick.traded = ask_order_dict["traded"] if tick.traded >= tick.assets.first.amount: self.remove_tick(tick.order_id) self.completed_orders.add(tick.order_id) elif not ask_exists and ask_order_dict["traded"] < ask_order_dict["assets"]["first"]["amount"] and \ ask_order_id not in self.completed_orders: new_pair = AssetPair.from_dictionary(ask_order_dict["assets"]) ask = Ask(ask_order_id, new_pair, Timeout(ask_order_dict["timeout"]), Timestamp(ask_order_dict["timestamp"]), traded=ask_order_dict["traded"]) self.insert_ask(ask) elif not ask_exists and ask_order_dict["traded"] >= ask_order_dict["assets"]["first"]["amount"]: self.completed_orders.add(ask_order_id) # Update bid tick bid_exists = self.tick_exists(bid_order_id) if bid_exists and bid_order_dict["traded"] >= self.get_tick(bid_order_id).traded: tick = self.get_tick(bid_order_id) tick.traded = bid_order_dict["traded"] if tick.traded >= tick.assets.first.amount: self.remove_tick(tick.order_id) self.completed_orders.add(tick.order_id) elif not bid_exists and bid_order_dict["traded"] < bid_order_dict["assets"]["first"]["amount"] and \ bid_order_id not in self.completed_orders: new_pair = AssetPair.from_dictionary(bid_order_dict["assets"]) bid = Bid(bid_order_id, new_pair, Timeout(bid_order_dict["timeout"]), Timestamp(bid_order_dict["timestamp"]), traded=bid_order_dict["traded"]) self.insert_bid(bid) elif not bid_exists and bid_order_dict["traded"] >= bid_order_dict["assets"]["first"]["amount"]: self.completed_orders.add(bid_order_id)
def bid2(): return Bid(OrderId(TraderId(b'3' * 20), OrderNumber(1)), AssetPair(AssetAmount(300, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now())