Beispiel #1
0
    def updateOpenSellOrder(self, order, pair):
        """
        Update open sell order based on given params in exit settings.
        For now only supports placing stop loss market order when 
        position goes against us (basically simple oco order variant)
        """
        try:
            candlestick_data = self.exchange.getOHLCV(order.symbol, self.kline_interval)
        except Exception:
            self.log('Error getting data from the exchange '
                'for updating open sell order on {}:'.format(pair.symbol))
            self.logError(sys.exc_info())
            return
        current_price = Decimal(candlestick_data.iloc[-1]['close'])
        stop_loss_value = self.bot_model.exit_settings.stop_loss_value

        exit_signal, last_price = self.checkExitStrategy(order.symbol)
        side = 'sell'
        order_type = 'market'
        if (stop_loss_value != None and (order.stop_price != None and order.stop_price > current_price)) or \
            (self.bot_model.exit_settings.exit_on_signal and exit_signal):
            quantity = self.computeMatchingOrderQuantity(order)
            if not self.test_mode:
                try:
                    cancelOrder(self.exchange, order)
                    if order.executed_quantity > 0:
                        self.bot_model.current_balance += \
                            order.executed_quantity * order.price
                except Exception as e:
                    self.log('cancelOrder() failed on {}'.format(order.symbol))
                    self.log(e)
                    return
                # try:
                #     order_result = self.exchange.getOrder(order.symbol, order.id, is_custom_id=True)
                # except Exception as e:
                #     self.log('getting order failed after canceling order')
                #     self.log(e)
                #     return
            else:
                order_result = dict(status='canceled', filled=0)

            if (stop_loss_value != None and (order.stop_price > current_price)):
                self.log('Canceling existing sell order and placing market sell on {} due to stop loss.'.format(order.symbol))
            elif (self.bot_model.exit_settings.exit_on_signal and exit_signal):
                self.log('Canceling existing sell order and placing market sell on {} due to exit signal.'.format(order.symbol))
            else:
                self.log('Canceling existing sell order and placing market sell on {} ...'.format(order.symbol))
            # pprint(order_result)
            order.status = 'canceled' #order_result['status']
            # order.executed_quantity = order_result['filled']
            order.is_closed = True
            self.placeOrder(
                symbol=order.symbol,
                pair=pair,
                order=order,
                quantity=quantity,
                order_type=order_type,
                side=side)
Beispiel #2
0
 def updateOpenBuyOrder(self, order, pair):
     """
     Looks to close an open buy order based on strategy specified 
     in entry settings. For now you can only close orders based on 
     time passed.
     """
     # TODO: place a sell order to exit if holding any
     # asset bought through this order.
     if (time.time() * 1000 - (order.timestamp).timestamp()) \
         > self.bot_model.entry_settings.open_buy_order_time_out:
         if not self.test_mode:
             try:
                 order_result = cancelOrder(self.exchange, order)
                 if order.executed_quantity > 0:
                     self.instantlyExitOrder(order, pair)
                     self.bot_model.current_balance += \
                         order.executed_quantity * order.price
                 else:
                     self.bot_model.current_balance += \
                         order.original_quantity * order.price
             except Exception as e:
                 self.log('cancelOrder() failed on {}'.format(order.symbol))
                 self.log(e)
                 return
             # try:
             #     order_result = self.exchange.getOrder(order.symbol, order.id, is_custom_id=True)
             # except Exception as e:
             #     self.log('getting order failed after canceling order on {}'.format(order.symbol))
             #     self.log(e)
             #     return
         self.log('Canceled BUY order on {} due to timeout.'.format(
             order.symbol))
         # self.exchange.updateSQLOrderModel(order, order_result, None)
         self.processClosedPosition(order, pair)
Beispiel #3
0
    def updateOpenSellOrder(self, order, pair):
        """
        Update open sell order based on given params in exit settings.
        For now only supports placing stop loss market order when 
        position goes against us (basically simple oco order variant)
        """
        exchange = self.exchange
        bot = self.bot
        strategy = self.strategy
        try:
            candlestick_data = exchange.getOHLCV(order.symbol,
                                                 self.kline_interval,
                                                 strategy.minimum_period)
        except ExchangeConnectionException:
            self.logOrShow('Error getting data from the exchange \
                for updating open sell order on {}:'.format(pair.symbol),
                           should_print=True)
            self.logOrShow(sys.exc_info(), should_print=True)
            return
        current_price = candlestick_data.iloc[-1]['close']
        stop_loss_value = bot.exit_settings.stop_loss_value

        if stop_loss_value is not None:
            if (Decimal(100 - stop_loss_value) / Decimal(100)) \
                * Decimal(order.entry_price) >= Decimal(current_price):
                quantity = self.computeQuantity(order)
                side = 'sell'
                order_type = 'market'

                if not self.test_mode:
                    try:
                        order_result = cancelOrder(exchange, order)
                    except Exception as e:
                        self.logOrShow('cancelOrder() failed')
                        self.logOrShow(e)
                else:
                    order_result = dict(status='canceled', filled=0)

                order.status = order_result['status']
                order.executed_quantity = order_result['filled']

                self.placeOrder(symbol=order.symbol,
                                pair=pair,
                                order=order,
                                quantity=quantity,
                                order_type=order_type,
                                side=side)
Beispiel #4
0
 def updateOpenBuyOrder(self, order, pair):
     """
     Looks to close an open buy order based on strategy specified 
     in entry settings. For now you can only close orders based on 
     time passed.
     """
     # How can we guarantee timestamp of exchange is always in ms?
     if (time.time() * 1000 - (order.timestamp).timestamp()) \
         > self.bot_model.entry_settings.open_buy_order_time_out:
         if not self.test_mode:
             try:
                 order_result = cancelOrder(self.exchange, order)
             except Exception as e:
                 self.log('cancelOrder() failed')
                 self.log(e)
                 return
         order.status = 'canceled'
         self.processClosedPosition(order, pair)