def get_last_price(last_price, trades): return trades[len(trades) - 1]['price'] if len(trades) > 0 else last_price def get_traded_volume(trades): return sum([trade['qty'] for trade in trades] + [0]) # Loop for t in range(max_iterations): traded_volume = 0 next_orders = [] # Limit order cancellations for idNum, order in limit_orders.items(): if t >= order['time_limit']: lob.cancelOrder(order['side'], idNum) limit_orders.remove(idNum) # Limit order arrivals df['orders'] = df['lambda'].apply(lambda x: np.random.poisson(x)) for idx, row in df.iterrows(): for i in range(int(row['orders'])): if row['price'] < ask: side = 'bid' order_id = order_id + 1 order = { 'type': 'limit', 'side': side, 'qty': np.random.poisson(row['volume']) * lot_size, 'price': row['price'],
print "Large incoming bid limit crosses best ask.\ Remaining volume is placed in the book.." print lob ############# Market Orders ############## # Market orders only require that the user specifies a side (bid # or ask), a quantity and their unique tid. marketOrder = {'type': 'market', 'side': 'ask', 'qty': 40, 'tid': 111} trades, idNum = lob.processOrder(marketOrder, False, False) print "A limit order takes the specified volume from the\ inside of the book, regardless of price" print "A market ask for 40 results in.." print lob ############ Cancelling Orders ############# # Order can be cancelled simply by submitting an order idNum and a side print "cancelling bid for 5 @ 97.." lob.cancelOrder('bid', 8) print lob ########### Modifying Orders ############# # Orders can be modified by submitting a new order with an old idNum lob.modifyOrder(5, {'side': 'bid', 'qty': 14, 'price': 99, 'tid': 100}) print "book after modify..." print lob