def _wakeUp(self, _): if self.suspended: return # best price in our queue price = self._source() if price is not None: # translate it into ticks ticks, _ = self._orderQueue.ticks(price) if self._orders is None: # if we haven't created orders self._orders = deque() # let's create orders with ticks [ticks, ticks + self._size) for i in range(0, self._size): self._orders.append(self._makeOrder(ticks + i)) self._send(self._last) else: orderTicks = self._orders[0].ticks # ticks of the best order if orderTicks == ticks or self._orderQueue.best.owner == self._first.owner: pass else: # if new price is worse than our best order price # (it means that our orders traded) if ticks > orderTicks: # let's remove matched orders while self._orders and self._first.ticks < ticks: order = self._first if not order.cancelled: # should be always False self._send(request.Cancel(order)) self._orders.popleft() # and add new ones to the end of the queue while len(self._orders) < self._size: t = self._last.ticks + 1 if self._orders else ticks self._orders.append(self._makeOrder(t)) self._send(self._last) # if new price is better than our best order price elif ticks < orderTicks: # let's remove bad orders while self._orders and self._last.ticks > ticks + self._size: self._send(request.Cancel(self._last)) self._orders.pop() # calculate how many orders we should insert num_created = self._size - len(self._orders) # create better orders while len(self._orders) < self._size: t = self._first.ticks - 1 if self._orders else ticks + (self._size - 1) self._orders.appendleft(self._makeOrder(t)) # and send them to the market for i in range(num_created): self._send(self._orders[i])
def suspended(self, value): if value and self._orders: # if we are asked to suspend, cancel all our orders for o in self._orders: self._send(request.Cancel(o)) self._orders = None self._suspended = value
def _wakeUp(self, _): # if we have orders to cancel while self._elements <> []: # choose an order N = len(self._elements) idx = self.choiceFunc(N) e = self._elements[idx] # if the order is invalid if e.empty or e.cancelled: # put the last order instead of it and repeat the procedure if e <> self._elements[-1]: self._elements[idx] = self._elements[-1] # it converges since every time we pops an element from the queue self._elements.pop() else: # if order is valid, cancel it self._book.process(request.Cancel(e)) return
def processIn(self, orderBook): self.orderBook = orderBook self.send(self.proto) self.orderBook.process(request.Cancel(self.proto))
def startProcessing(self): self.send(self.proto) self.world.scheduleAfter( self._delay, _(self.orderBook, request.Cancel(self.proto)).process)
def _dispose(self): if self._order is not None: self.orderBook.process(request.Cancel(self._order)) self._order = None