def build_order_book_series(self, limit_order_book): bid_price_columns = list(filter(lambda x: 'BidPrice' in x, self.order_data.columns)) bid_size_columns = list(filter(lambda x: 'BidSize' in x, self.order_data.columns)) ask_price_columns = list(filter(lambda x: 'AskPrice' in x, self.order_data.columns)) ask_size_columns = list(filter(lambda x: 'AskSize' in x, self.order_data.columns)) books = list() times = self.order_data['Time'] for idx in range(0, len(self.order_data)): # for each row in the order book data populate bid/ask queues with orders row = self.order_data.iloc[idx] ask = Queue() bid = Queue() for ask_p, ask_s in zip(ask_price_columns, ask_size_columns): #limit_order_book.ask_queue.queue.append(Order(row['Time'], OrderType.Submission, 0, row[ask_s], row[ask_p], OrderDirection.Sell)) ask.queue.append(Order(row['Time'], OrderType.Submission, 0, row[ask_s], row[ask_p], OrderDirection.Sell)) for bid_p, bid_s in zip(bid_price_columns, bid_size_columns): #limit_order_book.bid_queue.queue.append(Order(row['Time'], OrderType.Submission, 0, row[bid_s], row[bid_p], OrderDirection.Buy)) bid.queue.append(Order(row['Time'], OrderType.Submission, 0, row[bid_s], row[bid_p], OrderDirection.Buy)) #bid = Queue() #ask = Queue() #bid.queue = limit_order_book.bid_queue.queue.copy() #ask.queue = limit_order_book.ask_queue.queue.copy() data_reader_result = DataReaderResult(row['Time'], bid, ask) books.append(data_reader_result) return LimitOrderBookSeries(books, times)
class LimitOrderBook: def __init__(self): self.bid_queue = Queue() self.ask_queue = Queue() def get_bid_price_size(self): size = 0 for order in self.bid_queue.queue: size += order.size return size def get_ask_price_size(self): size = 0 for order in self.ask_queue.queue: size += order.size return size def get_best_bid(self): return self.bid_queue.get_top() def get_best_ask(self): return self.ask_queue.get_top()
class NestedQueue: def __init__(self): self.queue = Queue() def enqueue(self, item): if self.queue.size() == 0: self.queue.enqueue(item) return for price_queue in self.queue.queue: if price_queue.price == item.price: self.queue.enqueue(item) return self.queue.enqueue(item) return def dequeue(self, item): for price_queue in self.queue.queue: if price_queue.price == item.price: price_queue.dequeue(item) if len(price_queue.queue) == 0: self.queue.dequeue(price_queue) return return
def generate_books_from_message_data(self, limit_order_book): books = list() time_series_idx = list() if len(limit_order_book.ask_queue.queue) == 0 and len(limit_order_book.bid_queue.queue) ==0: self.update_empty_order_book(limit_order_book) for order in self.messages: if order.direction == OrderDirection.Buy: if order.order_type == OrderType.Submission: limit_order_book.bid_queue.enqueue(order) else: limit_order_book.bid_queue.dequeue(order) else: if order.order_type == OrderType.Submission: limit_order_book.ask_queue.enqueue(order) else: limit_order_book.ask_queue.dequeue(order) time = order.submission_time bid = Queue() ask = Queue() bid.queue = [val for val in limit_order_book.bid_queue.queue] ask.queue = [val for val in limit_order_book.ask_queue.queue] data_reader_result = DataReaderResult(time, bid, ask) time_series_idx.append(time) books.append(data_reader_result) return LimitOrderBookSeries(books, time_series_idx)
def __init__(self): self.queue = Queue()
def __init__(self): self.bid_queue = Queue() self.ask_queue = Queue()