Beispiel #1
0
    def create_order(self, asset, amount, is_buy, style):
        log.info('creating {} order'.format('buy' if is_buy else 'sell'))
        exchange_symbol = self.get_symbol(asset)

        if isinstance(style, LimitOrder) or isinstance(style, StopLimitOrder):
            if isinstance(style, StopLimitOrder):
                log.warn('{} will ignore the stop price'.format(self.name))

            price = style.get_limit_price(is_buy)
            try:
                if is_buy:
                    order_status = self.api.buylimit(exchange_symbol, amount,
                                                     price)
                else:
                    order_status = self.api.selllimit(exchange_symbol,
                                                      abs(amount), price)
            except Exception as e:
                raise ExchangeRequestError(error=e)

            if 'uuid' in order_status:
                order_id = order_status['uuid']
                order = Order(dt=pd.Timestamp.utcnow(),
                              asset=asset,
                              amount=amount,
                              stop=style.get_stop_price(is_buy),
                              limit=style.get_limit_price(is_buy),
                              id=order_id)
                return order
            else:
                raise CreateOrderError(exchange=self.name, error=order_status)
        else:
            raise InvalidOrderStyle(exchange=self.name,
                                    style=style.__class__.__name__)
Beispiel #2
0
    def create_order(self, asset, amount, is_buy, style):
        symbol = self.get_symbol(asset)

        if isinstance(style, ExchangeLimitOrder):
            price = style.get_limit_price(is_buy)
            order_type = 'limit'

        elif isinstance(style, MarketOrder):
            price = None
            order_type = 'market'

        else:
            raise InvalidOrderStyle(exchange=self.name,
                                    style=style.__class__.__name__)

        side = 'buy' if amount > 0 else 'sell'

        if hasattr(self.api, 'amount_to_lots'):
            adj_amount = self.api.amount_to_lots(
                symbol=symbol,
                amount=abs(amount),
            )
            if adj_amount != abs(amount):
                log.info(
                    'adjusted order amount {} to {} based on lot size'.format(
                        abs(amount),
                        adj_amount,
                    ))
        else:
            adj_amount = abs(amount)

        try:
            result = self.api.create_order(symbol=symbol,
                                           type=order_type,
                                           side=side,
                                           amount=adj_amount,
                                           price=price)
        except ExchangeNotAvailable as e:
            log.debug('unable to create order: {}'.format(e))
            raise ExchangeRequestError(error=e)

        except InvalidOrder as e:
            log.warn('the exchange rejected the order: {}'.format(e))
            raise CreateOrderError(exchange=self.name, error=e)

        if 'info' not in result:
            raise ValueError('cannot use order without info attribute')

        final_amount = adj_amount if side == 'buy' else -adj_amount
        order_id = result['id']
        order = Order(dt=pd.Timestamp.utcnow(),
                      asset=asset,
                      amount=final_amount,
                      stop=style.get_stop_price(is_buy),
                      limit=style.get_limit_price(is_buy),
                      id=order_id)
        return order
Beispiel #3
0
    def create_order(self, asset, amount, is_buy, style):
        log.info('creating {} order'.format('buy' if is_buy else 'sell'))
        exchange_symbol = self.get_symbol(asset)

        if isinstance(style, LimitOrder) or isinstance(style, StopLimitOrder):
            if isinstance(style, StopLimitOrder):
                log.warn('{} will ignore the stop price'.format(self.name))

            price = style.get_limit_price(is_buy)
            try:
                self.ask_request()
                if is_buy:
                    order_status = self.api.buylimit(exchange_symbol, amount,
                                                     price)
                else:
                    order_status = self.api.selllimit(exchange_symbol,
                                                      abs(amount), price)
            except Exception as e:
                raise ExchangeRequestError(error=e)

            if 'uuid' in order_status:
                order_id = order_status['uuid']
                order = Order(dt=pd.Timestamp.utcnow(),
                              asset=asset,
                              amount=amount,
                              stop=style.get_stop_price(is_buy),
                              limit=style.get_limit_price(is_buy),
                              id=order_id)
                return order
            else:
                if order_status == 'INSUFFICIENT_FUNDS':
                    log.warn('not enough funds to create order')
                    return None
                elif order_status == 'DUST_TRADE_DISALLOWED_MIN_VALUE_50K_SAT':
                    log.warn('Your order is too small, order at least 50K'
                             ' Satoshi')
                    return None
                else:
                    raise CreateOrderError(exchange=self.name,
                                           error=order_status)
        else:
            raise InvalidOrderStyle(exchange=self.name,
                                    style=style.__class__.__name__)
Beispiel #4
0
    def create_order(self, asset, amount, is_buy, style):
        symbol = self.get_symbol(asset)

        if isinstance(style, ExchangeLimitOrder):
            price = style.get_limit_price(is_buy)
            order_type = 'limit'

        elif isinstance(style, MarketOrder):
            price = None
            order_type = 'market'

        else:
            raise InvalidOrderStyle(exchange=self.name,
                                    style=style.__class__.__name__)

        side = 'buy' if amount > 0 else 'sell'
        if hasattr(self.api, 'amount_to_lots'):
            # TODO: is this right?
            if self.api.markets is None:
                self.api.load_markets()

            # https://github.com/ccxt/ccxt/issues/1483
            adj_amount = round(abs(amount), asset.decimals)
            market = self.api.markets[symbol]
            if 'lots' in market and market['lots'] > amount:
                raise CreateOrderError(
                    exchange=self.name,
                    e='order amount lower than the smallest lot: {}'.format(
                        amount))

        else:
            adj_amount = round(abs(amount), asset.decimals)

        before_order_dt = pd.Timestamp.utcnow()
        try:
            result = self.api.create_order(symbol=symbol,
                                           type=order_type,
                                           side=side,
                                           amount=adj_amount,
                                           price=price)
        except InvalidOrder as e:
            log.warn('the exchange rejected the order: {}'.format(e))
            raise CreateOrderError(exchange=self.name, error=e)

        except RequestTimeout as e:
            log.info('received a RequestTimeout exception while creating '
                     'an order on {} / {}\n Checking if an order was filled '
                     'during the timeout'.format(self.name, symbol))

            missing_order = self._handle_request_timeout(
                before_order_dt, asset, amount, is_buy, style, adj_amount)
            if missing_order is None:
                # no order was found
                log.warn(
                    'no order was identified during timeout exception.'
                    'Please double check for inconsistency with the exchange. '
                    'We encourage you to report any issue on GitHub: '
                    'https://github.com/enigmampc/catalyst/issues')
                raise ExchangeRequestError(error=e)
            else:
                return missing_order

        except (ExchangeError, NetworkError) as e:
            log.warn('unable to create order {} / {}: {}'.format(
                self.name, symbol, e))
            raise ExchangeRequestError(error=e)

        exchange_amount = None
        if 'amount' in result and result['amount'] != adj_amount:
            exchange_amount = result['amount']

        elif 'info' in result:
            if 'origQty' in result['info']:
                exchange_amount = float(result['info']['origQty'])

        if exchange_amount:
            log.info('order amount adjusted by {} from {} to {}'.format(
                self.name, adj_amount, exchange_amount))
            adj_amount = exchange_amount

        if 'info' not in result:
            raise ValueError('cannot use order without info attribute')

        final_amount = adj_amount if side == 'buy' else -adj_amount
        order_id = result['id']
        order = Order(dt=pd.Timestamp.utcnow(),
                      asset=asset,
                      amount=final_amount,
                      stop=style.get_stop_price(is_buy),
                      limit=style.get_limit_price(is_buy),
                      id=order_id)
        return order
Beispiel #5
0
    def create_order(self, asset, amount, is_buy, style):
        symbol = self.get_symbol(asset)

        if isinstance(style, ExchangeLimitOrder):
            price = style.get_limit_price(is_buy)
            order_type = 'limit'

        elif isinstance(style, MarketOrder):
            price = None
            order_type = 'market'

        else:
            raise InvalidOrderStyle(exchange=self.name,
                                    style=style.__class__.__name__)

        side = 'buy' if amount > 0 else 'sell'
        if hasattr(self.api, 'amount_to_lots'):
            # TODO: is this right?
            if self.api.markets is None:
                self.api.load_markets()

            # https://github.com/ccxt/ccxt/issues/1483
            adj_amount = round(abs(amount), asset.decimals)
            market = self.api.markets[symbol]
            if 'lots' in market and market['lots'] > amount:
                raise CreateOrderError(
                    exchange=self.name,
                    e='order amount lower than the smallest lot: {}'.format(
                        amount))

        else:
            adj_amount = round(abs(amount), asset.decimals)

        try:
            result = self.api.create_order(symbol=symbol,
                                           type=order_type,
                                           side=side,
                                           amount=adj_amount,
                                           price=price)
        except InvalidOrder as e:
            log.warn('the exchange rejected the order: {}'.format(e))
            raise CreateOrderError(exchange=self.name, error=e)

        except (ExchangeError, NetworkError) as e:
            log.warn('unable to create order {} / {}: {}'.format(
                self.name, symbol, e))
            raise ExchangeRequestError(error=e)

        exchange_amount = None
        if 'amount' in result and result['amount'] != adj_amount:
            exchange_amount = result['amount']

        elif 'info' in result:
            if 'origQty' in result['info']:
                exchange_amount = float(result['info']['origQty'])

        if exchange_amount:
            log.info('order amount adjusted by {} from {} to {}'.format(
                self.name, adj_amount, exchange_amount))
            adj_amount = exchange_amount

        if 'info' not in result:
            raise ValueError('cannot use order without info attribute')

        final_amount = adj_amount if side == 'buy' else -adj_amount
        order_id = result['id']
        order = Order(dt=pd.Timestamp.utcnow(),
                      asset=asset,
                      amount=final_amount,
                      stop=style.get_stop_price(is_buy),
                      limit=style.get_limit_price(is_buy),
                      id=order_id)
        return order