def __onOrderUpdated(self, broker, order): position = self.__orderToPosition.get(order.getId(), None) if position == None: self.onOrderUpdated(order) elif position.getEntryOrder().getId() == order.getId(): if order.isFilled(): self.__unregisterOrder(position, order) self.onEnterOk(position) elif order.isCanceled(): self.__unregisterOrder(position, order) self.onEnterCanceled(position) else: assert (order.isAccepted()) elif position.getExitOrder().getId() == order.getId(): if order.isFilled(): self.__unregisterOrder(position, order) self.onExitOk(position) elif order.isCanceled(): self.__unregisterOrder(position, order) self.onExitCanceled(position) else: assert (order.isAccepted()) else: # The order used to belong to a position but it was ovewritten with a new one # and the previous order should have been canceled. assert (order.isCanceled())
def __onOrderUpdated(self, broker, order): position = self.__orderToPosition.get(order.getId(), None) if position == None: self.onOrderUpdated(order) elif position.getEntryOrder().getId() == order.getId(): if order.isFilled(): self.__unregisterOrder(position, order) self.onEnterOk(position) elif order.isCanceled(): self.__unregisterOrder(position, order) self.onEnterCanceled(position) else: assert(order.isAccepted()) elif position.getExitOrder().getId() == order.getId(): if order.isFilled(): self.__unregisterOrder(position, order) self.onExitOk(position) elif order.isCanceled(): self.__unregisterOrder(position, order) self.onExitCanceled(position) else: assert(order.isAccepted()) else: # The order used to belong to a position but it was ovewritten with a new one # and the previous order should have been canceled. assert(order.isCanceled())
def onExitOk(self, position): extorder = position.getExitOrder() execInfo = extorder.getExecutionInfo() inst = position.getEntryOrder().getInstrument() price = format(execInfo.getPrice(),'<6.2f') shares = format(execInfo.getQuantity(),'<10.2f') self.debug("SELL: " + str(inst) + " at $%s shares:%s orderid:%d" % (price,shares,extorder.getId()))
def __registerActivePosition(self, position): for order in [position.getEntryOrder(), position.getExitOrder()]: if order and order.isAccepted(): self.__registerOrder(position, order)