Beispiel #1
0
    def sell(self, coin_name, quantity=None, pair_base="BTC", amount=None, order_type="market"):
        try:
            balance = self.get_balance(coin_name)
            if not balance:
                raise UserAdviceException(
                    f"Balance not enough to execute action in {name} exchange")

            symbol = f"{pair_base}-{coin_name}"
            price = self.get_price(symbol)
            quantity = self.calculate_sell_qty(price, amount)

            logger.info(
                f"sell order request:{symbol}>price:{price}>quantity:{quantity} > order_type: {order_type}")

            if order_type.lower() == "market":
                order = self.client.sell_market(
                    market=symbol,
                    quantity=quantity)
            elif order_type.lower() == "market":
                order = self.client.sell_limit(
                    market=symbol,
                    quantity=quantity,
                    rate=price)
            logger.info(
                f"Bought order request:{symbol}>price:{price}>quantity:{quantity}> {order}")
            return order
        except Exception as ex:
            logger.error(ex, exc_info=True)
            raise UserAdviceException(ex)
Beispiel #2
0
    def sell(self,
             coin_name,
             quantity=None,
             pair_base="BTC",
             amount=None,
             order_type="market"):
        try:
            balance = self.get_balance(coin_name)
            if not balance:
                raise UserAdviceException(
                    f"Balance not enough to execute action in {name} exchange")

            symbol = f"{coin_name}{pair_base}".upper()
            precision = self.get_precision(symbol)

            current_price = self.get_price(symbol, precision)
            step_size = self.get_step_size(symbol)
            quantity = self.calculate_sell_qty(price=current_price,
                                               amount=amount,
                                               step_size=step_size)

            side = Client.SIDE_SELL
            time_in_force = None
            if order_type == "limit":
                order_type = Client.ORDER_TYPE_LIMIT
                time_in_force = "GTC"
            else:
                order_type = Client.ORDER_TYPE_MARKET

            logger.info(
                f"{side} order request:{symbol}>type:{order_type}>quantity:{quantity} >> current_price: {current_price}"
            )

            if order_type == Client.ORDER_TYPE_LIMIT:
                order = self.client.create_order(symbol=symbol,
                                                 side=side,
                                                 type=order_type,
                                                 quantity=quantity,
                                                 timeInForce=time_in_force,
                                                 price=amount)
            else:
                order = self.client.create_order(symbol=symbol,
                                                 side=side,
                                                 type=order_type,
                                                 quantity=quantity)
            return order
        except (BinanceAPIException, BinanceRequestException) as ex:
            logger.error(ex, exc_info=True)
            raise UserAdviceException(ex)
Beispiel #3
0
    def buy(self, coin_name, quantity=None, pair_base="BTC", amount=None, order_type="market"):
        try:
            symbol = f"{pair_base}-{coin_name}"
            price = self.get_price(symbol)
            quantity = self.calculate_buy_qty(price, amount)

            logger.info(
                f"buy order request:{symbol}>price:{price}>quantity:{quantity} > order_type: {order_type}")

            if order_type.lower() == "market":
                order = self.client.buy_market(
                    market=symbol,
                    quantity=quantity)
            elif order_type.lower() == "market":
                order = self.client.buy_limit(
                    market=symbol,
                    quantity=quantity,
                    rate=price)

            logger.info(
                f"Bought order request:{symbol}>price:{price}>quantity:{quantity}> {order}")
            return order
        except Exception as ex:
            logger.error(ex, exc_info=True)
            raise UserAdviceException(ex)
Beispiel #4
0
    def sell(self, coin_name, quantity=None, pair_base="BTC", amount=None, order_type="market"):
        try:
            client = Trade(key=self.api_key, secret=self.api_secret, passphrase='chimera',
                           is_sandbox=self.debug_mode)
            symbol = f"BTC-{coin_name}"
            price = self.get_price(symbol)
            balance = self.get_balance(coin_name)

            quantity = self.calculate_sell_qty(price, amount)

            precision = self.get_precision(coin_name)
            price = self.round_value(price, precision)
            quantity = self.round_value(quantity, precision)

            logger.info(
                f"symbol:{symbol}>price:{price}>qty:{quantity} > market: {order_type}")

            if order_type.lower() == "market":
                order_id = client.create_market_order(
                    symbol, 'sell', size=quantity)
            elif order_type.lower() == "limit":
                order_id = client.create_limit_order(
                    symbol, 'sell', quantity, amount)
            logger.info(
                f"sold order request:{symbol}>type:{order_type}>quantity:{quantity}")
            return order_id
        except Exception as ex:
            logger.error(ex, exc_info=True)
            raise UserAdviceException(ex)
Beispiel #5
0
 def get_balance(self, coin_name):
     try:
         user_account = self.get_account()
         balances = user_account.get_account_list(
             currency=coin_name, account_type="trade")
         logger.info(f"Balances: {balances}")
         if not balances.get("data"):
             raise UserAdviceException(
                 f"{coin_name} not found in account. I could not retrieve account balance.")
         balance_info = balances.get("data")[0]
         if not balance_info:
             raise UserAdviceException(
                 f"Coin not found in account in {self.name} exchange")
         return balance_info.get("available")
     except Exception as ex:
         logger.error(ex, exc_info=True)
         raise Exception(ex)
Beispiel #6
0
 def get_balance(self, coin_name):
     pass
     try:
         balance_info = self.client.get_balance(coin_name)
         if not balance_info:
             raise UserAdviceException(
                 f"Coin not found in account in {self.name} exchange")
         if not balance_info.get("success"):
             raise Exception(balance_info.get("message"))
         result = balance_info.get("result")
         return float(result.get("Available"))
     except Exception as ex:
         logger.error(ex, exc_info=True)
         raise Exception(ex)
Beispiel #7
0
    def buy(self,
            coin_name,
            quantity=None,
            pair_base="BTC",
            amount=None,
            order_type="market"):
        try:
            symbol = f"{coin_name}{pair_base}".upper()
            precision = self.get_precision(symbol)
            current_price = self.get_price(symbol, precision)
            step_size = self.get_step_size(symbol)

            quantity = self.calculate_buy_qty(price=current_price,
                                              amount=amount,
                                              step_size=step_size)

            logger.info(
                f"step_size>{step_size}>price>{current_price} > Amount > {amount} > precision: {precision} > qty: {quantity}"
            )

            side = Client.SIDE_BUY
            time_in_force = None
            if order_type == "limit":
                order_type = Client.ORDER_TYPE_LIMIT
                time_in_force = "GTC"
            else:
                order_type = Client.ORDER_TYPE_MARKET

            if order_type == Client.ORDER_TYPE_LIMIT:
                logger.info(
                    f"{side} order request:{symbol}>type:{order_type}>quantity:{quantity} > {amount}"
                )
                order = self.client.create_order(symbol=symbol,
                                                 side=side,
                                                 type=order_type,
                                                 quantity=quantity,
                                                 timeInForce=time_in_force,
                                                 price=amount)
            else:
                logger.info(
                    f"{side} order request:{symbol}>type:{order_type}>quantity:{quantity} > {current_price}"
                )
                order = self.client.create_order(symbol=symbol,
                                                 side=side,
                                                 type=order_type,
                                                 quantity=quantity)
            return order
        except (BinanceAPIException, BinanceRequestException) as ex:
            logger.error(ex, exc_info=True)
            raise UserAdviceException(ex)
Beispiel #8
0
    def get_balance(self, coin_name):
        """Query coin balance in exchange account

        Args:
            coin_name (string): Name of coin

        Raises:
            Exception: Raise exception if coin is not found

        Returns:
            float: Coin balance.

        """
        try:
            balance_info = self.client.get_asset_balance(coin_name)
            if not balance_info:
                raise UserAdviceException(
                    f"Coin not found in account in {self.name} exchange")
            return balance_info.get("free")
        except (BinanceAPIException, BinanceRequestException) as ex:
            logger.error(ex, exc_info=True)
            raise Exception(ex)