def _call(self): if self.events.is_valid(): next_event = self.events.get_value() if isinstance(next_event, OrderBookSnapshot): self.order_book = OrderBook(next_event.get_bids(), next_event.get_asks()) else: self.order_book.apply_order_book_update(next_event) self._update(self.order_book)
class OrderBookBuilder(Function): def __init__(self, network: Network, events: Signal): super().__init__(network, events) self.events = events self.order_book = OrderBook([], []) def _call(self): if self.events.is_valid(): next_event = self.events.get_value() if isinstance(next_event, OrderBookSnapshot): self.order_book = OrderBook(next_event.get_bids(), next_event.get_asks()) else: self.order_book.apply_order_book_update(next_event) self._update(self.order_book)
def generate(self, scheduler: NetworkScheduler): tickstore = AzureBlobTickstore(self.mds.credential, f'{exchange_code_upper}_BOOKS', timestamp_column='time') books_df = tickstore.select(symbol, self.mds.start_time, self.mds.end_time) for row in books_df.itertuples(): at_time = row[0].asm8.astype('int64') / 10**6 best_bid = BookLevel(row[2], row[1]) best_ask = BookLevel(row[4], row[3]) book = OrderBook(bids=[best_bid], asks=[best_ask]) scheduler.schedule_update_at(order_books, book, at_time) tickstore.close()
def on_book_update(self, book: OrderBook): self.appender.write_double(datetime.utcnow().timestamp()) if len(book.get_bids()) > 0: self.appender.write_long(book.get_best_bid().get_qty()) self.appender.write_double(book.get_best_bid().get_px()) else: self.appender.write_long(0) self.appender.write_double(0) if len(book.get_asks()) > 0: self.appender.write_long(book.get_best_ask().get_qty()) self.appender.write_double(book.get_best_ask().get_px()) else: self.appender.write_long(0) self.appender.write_double(0)
def on_book_update(self, book: OrderBook): book_update_counter.inc() book_msg = capnp_def.Level1BookUpdateMessage.new_message() book_msg.time = datetime.utcnow().timestamp() if len(book.get_bids()) > 0: book_msg.bestBidQty = book.get_best_bid().get_qty() book_msg.bestBidPx = book.get_best_bid().get_px() else: book_msg.bestBidQty = 0 book_msg.bestBidPx = 0 if len(book.get_asks()) > 0: book_msg.bestAskQty = book.get_best_ask().get_qty() book_msg.bestAskPx = book.get_best_ask().get_px() else: book_msg.bestAskQty = 0 book_msg.bestAskPx = 0 self.tx_writer.append_msg(book_msg)
def __init__(self, network: Network, events: Signal): super().__init__(network, events) self.events = events self.order_book = OrderBook([], [])