Beispiel #1
0
 def updateOpenOrder(self, order):
     """
     Checks the status of an open order and updates it
     """
     pair = self.bot_model.getPair(self.session, symbol=order.symbol)
     self.log('Checking open order on {}.'.format(order.symbol),
              should_print=False)
     # get info of order from exchange
     if not self.test_mode:
         try:
             exchange_order_info = self.exchange.getOrder(order.symbol,
                                                          order.id,
                                                          is_custom_id=True)
         except Exception as e:
             self.log('Error getting data from the exchange for'
                      ' updating open order on {}.'.format(order.symbol))
             self.logError(sys.exc_info())
             return
     else:
         try:
             exchange_order_info = simulateOrderInfo(
                 self.exchange, order, self.kline_interval)
         except Exception as e:
             self.log('Error simulating open order on {}.'.format(
                 order.symbol))
             self.logError(sys.exc_info())
             return
     self.exchange.updateSQLOrderModel(order, exchange_order_info, None)
     self.session.commit()
     # update order params.
     # order.side = exchange_order_info['side']
     # order.status = exchange_order_info['status']
     # order.executed_quantity = exchange_order_info['filled']
     if (order.side == 'buy'):
         # Order has been canceled by the user
         if (order.status == 'canceled'):
             self.log(
                 'CANCELED BUY order on {}, exiting remaining quantity: {} at market price.'
                 .format(order.symbol, order.executed_quantity))
             self.instantlyExitOrder(order, pair)
             self.processClosedPosition(order, pair)
         # buy order was filled, place exit order.
         if (order.status == 'closed'):
             self.log('FILLED BUY order on {}, try exit.'.format(
                 order.symbol))
             self.tryExitOrder(order, pair)
         # buy order has been accepted by engine
         if (order.status == 'open'):
             self.log('OPEN BUY order on {}, UPDATING.'.format(
                 order.symbol))
             self.updateOpenBuyOrder(order, pair)
         # buy order was rejected, not processed by engine
         if (order.status == 'rejected'):
             self.log('REJECTED BUY order on {}.'.format(order.symbol))
             self.processClosedPosition(order, pair)
         # buy order expired, i.e. FOK orders with
         # no fill or due to maintenance by exchange.
         if (order.status == 'expired'):
             self.log(
                 'EXPIRED BUY order on {}, sell what was bought.'.format(
                     order.symbol))
             self.instantlyExitOrder(order, pair)
             self.processClosedPosition(order, pair)
     # sell order
     elif (order.side == 'sell'):
         # was cancelled by user.
         if (order.status == 'canceled'):
             # query buy order to again place a sell order for that buy order.
             # self.log('CANCELED SELL order on {}.'.format(order.symbol))
             # original_buy_order = self.reviveOriginalBuyOrder(order)
             # self.tryExitOrder(original_buy_order, pair)
             self.log('CANCELED SELL order on {}'.format(order.symbol))
             # self.instantlyExitOrder(order, pair)
             self.processClosedPosition(order, pair)
         # sell order was filled
         if (order.status == 'closed'):
             self.log('FILLED SELL order on {}.'.format(order.symbol))
             self.processClosedPosition(order, pair)
         # sell order was accepted by engine of exchange
         if (order.status == 'open'):
             self.log('OPEN SELL order on {}, UPDATING.'.format(
                 order.symbol))
             self.updateOpenSellOrder(order, pair)
         # sell order was rejected by engine of exchange
         if (order.status == 'rejected'):
             self.log('REJECTED SELL order on {}.'.format(order.symbol))
             original_buy_order = self.reviveOriginalBuyOrder(order)
             self.tryExitOrder(original_buy_order, pair)
         # sell order expired, i.e. due to FOK orders or partially filled market orders
         if (order.status == 'expired'):
             self.log('EXPIRED SELL order on {}.'.format(order.symbol))
             original_buy_order = self.reviveOriginalBuyOrder(order)
             self.tryExitOrder(original_buy_order, pair)
     self.session.commit()
Beispiel #2
0
    def updateOpenOrder(self, order):
        """
        Checks the status of an open order and updates it
        """
        exchange = self.exchange

        # get pair from database
        pair:Pair = self.bot.getPairWithSymbol(self.session, order.symbol)

        self.logOrSp('Checking open order on {}.'.format(pair.symbol))
        # get info of order from exchange
        if not self.test_mode: 
            try:
                exchange_order_info = exchange.getOrder(order.symbol, order.id, is_custom_id=True)
                print(exchange_order_info)
            except ExchangeConnectionException:
                self.logOrSp('Error getting data from the exchange for updating open order on {}.'.format(pair.symbol), should_print=True)
                self.logOrSp(sys.exc_info(), should_print=True)
                return
        else:
            try:
                exchange_order_info = simulateOrderInfo(exchange, order, self.kline_interval)
            except ExchangeConnectionException:
                self.logOrSp('Error simulating open order on {}.'.format(pair.symbol), should_print=True)
                self.logOrSp(sys.exc_info(), should_print=True)
                return
        # check for valid response from exchange
        if not exchange.isValidResponse(exchange_order_info):
            logger.warning('Exchange order info could not be retrieved!, \
                message from exchange: {}'.format(exchange_order_info))
            return
        
        # update order params.
        order.side = exchange_order_info['side']
        order.status = exchange_order_info['status']
        order.executed_quantity = exchange_order_info['executedQty']

        if (order.side == exchange.ORDER_SIDE_BUY):
             # Order has been canceled by the user
            if (order.status == exchange.ORDER_STATUS_CANCELED):
                if order.executed_quantity > 0:
                    self.tryExitOrder(order, pair)
                else:
                    self.processClosedPosition(order, pair)

            # buy order was filled, place exit order.
            if (order.status == exchange.ORDER_STATUS_FILLED):
                self.logOrSp('BUY order on {} filled, try exit.'.format(pair.symbol), should_print=True)

                self.tryExitOrder(order, pair)

            # buy order has been accepted by engine
            if (order.status == exchange.ORDER_STATUS_NEW):
                self.updateOpenBuyOrder(order, pair)

            # buy order that has been partially filled
            if (order.status == exchange.ORDER_STATUS_PARTIALLY_FILLED):
                self.updateOpenBuyOrder(order, pair)

            # buy order was rejected, not processed by engine
            if (order.status == exchange.ORDER_STATUS_REJECTED):
                self.processClosedPosition(order, pair)

            # buy order expired, i.e. FOK orders with 
            # no fill or due to maintenance by exchange.
            if (order.status == exchange.ORDER_STATUS_EXPIRED):
                if order.executed_quantity > 0:
                    self.tryExitOrder(order, pair)
                else:
                    self.processClosedPosition(order, pair)

        # sell order 
        if (order.side == exchange.ORDER_SIDE_SELL):
            # was cancelled by user.
            if (order.status == exchange.ORDER_STATUS_CANCELED):
                # query buy order to again place a sell order for that buy order.
                original_buy_order = self.reviveOriginalBuyOrder(order)
                self.tryExitOrder(original_buy_order, pair)

            # sell order was filled
            if (order.status == exchange.ORDER_STATUS_FILLED):
                self.processClosedPosition(order, pair)

            # sell order was accepted by engine of exchange
            if (order.status == exchange.ORDER_STATUS_NEW):
                self.updateOpenSellOrder(order, pair)

            # sell order was partially filled
            if (order.status == exchange.ORDER_STATUS_PARTIALLY_FILLED):
                self.updateOpenSellOrder(order, pair)

            # sell order was rejected by engine of exchange
            if (order.status == exchange.ORDER_STATUS_REJECTED):
                original_buy_order = self.reviveOriginalBuyOrder(order)
                self.tryExitOrder(original_buy_order, pair)

            # sell order expired, i.e. due to FOK orders or partially filled market orders
            if (order.status == exchange.ORDER_STATUS_EXPIRED):
                original_buy_order = self.reviveOriginalBuyOrder(order)
                self.tryExitOrder(original_buy_order, pair)

        self.session.commit()
Beispiel #3
0
 def updateOpenOrder(self, order):
     """
     Checks the status of an open order and updates it
     """
     pair = self.bot_model.getPairWithSymbol(self.session, order.symbol)
     self.log('Checking open order on {}.'.format(order.symbol),
              should_print=False)
     # get info of order from exchange
     if not self.test_mode:
         try:
             exchange_order_info = self.exchange.getOrder(order.symbol,
                                                          order.id,
                                                          is_custom_id=True)
         except Exception as e:
             self.log('Error getting data from the exchange for'
                      ' updating open order on {}.'.format(order.symbol))
             self.logError(sys.exc_info())
             return
     else:
         try:
             exchange_order_info = simulateOrderInfo(
                 self.exchange, order, self.kline_interval)
         except Exception as e:
             self.log('Error simulating open order on {}.'.format(
                 order.symbol))
             self.logError(sys.exc_info())
             return
     # update order params.
     order.side = exchange_order_info['side']
     order.status = exchange_order_info['status']
     order.executed_quantity = exchange_order_info['filled']
     if (order.side == 'buy'):
         # Order has been canceled by the user
         if (order.status == 'canceled'):
             if order.executed_quantity > 0:
                 self.tryExitOrder(order, pair)
             else:
                 self.processClosedPosition(order, pair)
         # buy order was filled, place exit order.
         if (order.status == 'closed'):
             self.log('BUY order on {} filled, try exit.'.format(
                 order.symbol))
             self.tryExitOrder(order, pair)
         # buy order has been accepted by engine
         if (order.status == 'open'):
             self.updateOpenBuyOrder(order, pair)
         # buy order was rejected, not processed by engine
         if (order.status == 'rejected'):
             self.processClosedPosition(order, pair)
         # buy order expired, i.e. FOK orders with
         # no fill or due to maintenance by exchange.
         if (order.status == 'expired'):
             if order.executed_quantity > 0:
                 self.tryExitOrder(order, pair)
             else:
                 self.processClosedPosition(order, pair)
     # sell order
     if (order.side == 'sell'):
         # was cancelled by user.
         if (order.status == 'canceled'):
             # query buy order to again place a sell order for that buy order.
             original_buy_order = self.reviveOriginalBuyOrder(order)
             self.tryExitOrder(original_buy_order, pair)
         # sell order was filled
         if (order.status == 'closed'):
             self.processClosedPosition(order, pair)
         # sell order was accepted by engine of exchange
         if (order.status == 'open'):
             self.updateOpenSellOrder(order, pair)
         # sell order was rejected by engine of exchange
         if (order.status == 'rejected'):
             original_buy_order = self.reviveOriginalBuyOrder(order)
             self.tryExitOrder(original_buy_order, pair)
         # sell order expired, i.e. due to FOK orders or partially filled market orders
         if (order.status == 'expired'):
             original_buy_order = self.reviveOriginalBuyOrder(order)
             self.tryExitOrder(original_buy_order, pair)
     self.session.commit()