Esempio n. 1
0
    def sell(self, symbol_exchange, price, qty):
        """
        :param symbol_exchange: "BTC/USDT.binance"
        :param price:  限价
        :param qty: 数量
        :return:
        """
        exchange = symbol_exchange.split('.')[1]
        symbol = symbol_exchange.split('.')[0]
        base_currency = symbol.split('/')[0].lower()  # btc  基础货币
        quote_currency = symbol.split('/')[1].lower()  # usdt 计价货币
        if not isinstance(price, (int, float)) or np.isnan(price):
            raise Errors.INVALID_PRICE
        if not isinstance(qty, (int, float)) or np.isnan(qty):
            raise Errors.INVALID_AMOUNT
        # 低于current_price才能卖出,高于current_price才能买入
        current_price = self.context.get_price(symbol_exchange)
        if price > current_price:
            logger.debug('限价单价格需要小于等于市场价才能卖出,卖出失败.')
            return
        if base_currency not in self.current_position.keys():
            # logger.debug('没有 {} 资产,卖出失败.'.format(base_currency))
            return

        qty = math.floor(qty * 100000000) / 100000000
        price = math.floor(price * 100000000) / 100000000
        amount = math.floor((qty * price) * 100000000) / 100000000

        # 判断是否有足够资金下单
        if self.current_position[base_currency].detail()['available'] < qty:
            # logger.debug('{} 不足,卖出失败.'.format(base_currency))
            return
        # 判断是否小于最下下单精度、最小下单金额
        min_order_qty = self.context.min_order[exchange][
            symbol.lower()]['min_order_qty']
        min_order_amount = self.context.min_order[exchange][
            symbol.lower()]['min_order_qty']
        if qty < min_order_qty:
            logger.debug('不足下单最小精度 {} {},卖出失败.'.format('%.6f' % min_order_qty,
                                                       base_currency.upper()))
            return
        if amount < min_order_amount:
            logger.debug('不足下单最小金额 {} {},卖出失败.'.format(
                '%.6f' % min_order_amount, quote_currency.upper()))
            return

        # 下单
        order = Order(self.context, self.name, symbol, self.exchange,
                      self.asset_class, 'sell', 'limit', price, qty)
        order_id = order['id']
        self.history_orders[order_id] = order
        order_update_position(self.context, self.current_position,
                              order['side'], exchange, symbol, price, qty)
        match_engine(self.current_position, self.context, order)
        if not self.orders or self.orders[0][
                'create_time'] == self.context.current_datetime():
            self.orders.append(order.detail())
        # logger.debug('{} 卖出成功! 价格:{} 数量:{}'.format(base_currency, price, qty))
        # return order.detail()
        return order_id
Esempio n. 2
0
    def buy_pct(self, symbol_exchange, price, pct):
        """
        :param symbol_exchange: "BTC/USDT.binance"
        :param price:  限价
        :param pct: 买入资产百分比的数量
        :return:
        """
        exchange = symbol_exchange.split('.')[1]
        symbol = symbol_exchange.split('.')[0]
        base_currency = symbol.split('/')[0].lower()  # btc  基础货币
        quote_currency = symbol.split('/')[1].lower()  # usdt 计价货币
        if not isinstance(price, (int, float)) or np.isnan(price):
            raise Errors.INVALID_PRICE

        if quote_currency not in self.current_position.keys():
            logger.debug('没有 {} 资产,买入失败.'.format(quote_currency))
            return

        quote_min_amount = self.context.min_order[exchange][
            symbol.lower()]['min_order_amount']
        if self.current_position[quote_currency].detail(
        )['available'] < quote_min_amount:
            logger.debug('{} 不足下单最小精度 {} {},买入失败.'.format(
                quote_currency, quote_min_amount, quote_currency.upper()))
            return
        qty = math.floor(
            (self.current_position[quote_currency].detail()['available'] * pct)
            / price * 1000000) / 1000000
        if not isinstance(qty, (int, float)) or np.isnan(qty):
            raise Errors.INVALID_AMOUNT
        # 低于current_price才能卖出,高于current_price才能买入
        current_price = self.context.get_price(symbol_exchange)
        if price < current_price:
            logger.debug('限价单价格需要大于等于市场价才能买入,买入失败.')
            return

        price = math.floor(price * 1000000) / 1000000
        order = Order(self.context, self.name, symbol, self.exchange,
                      self.asset_class, 'buy', 'limit', price, qty)
        order_id = order['id']
        self.history_orders[order_id] = order
        order_update_position(self.context, self.current_position,
                              order['side'], exchange, symbol, price, qty)
        match_engine(self.current_position, self.context, order)
        if not self.orders or self.orders[0][
                'create_time'] == self.context.current_datetime():
            self.orders.append(order.detail())
        # logger.debug('{} 买入成功! 价格:{} 数量:{}'.format(base_currency,price,qty))
        # return order.detail()
        return order_id
Esempio n. 3
0
    def sell_pct(self, symbol_exchange, price, pct):
        """
        :param symbol_exchange: "BTC/USDT.binance"
        :param price: 限价
        :param pct: 卖出资产百分比的数量
        :return:
        """
        exchange = symbol_exchange.split('.')[1]
        symbol = symbol_exchange.split('.')[0]
        base_currency = symbol.split('/')[0].lower()  # btc  基础货币
        # quote_currency = symbol.split('/')[1].lower()  # usdt 计价货币
        if not isinstance(price, (int, float)) or np.isnan(price):
            raise Errors.INVALID_PRICE
        if not isinstance(pct, (int, float)) or np.isnan(pct):
            raise Errors.INVALID_AMOUNT

        if base_currency not in self.current_position.keys():
            logger.warning('没有 {} 资产,卖出失败.'.format(base_currency))
            return
        base_min_qty = self.context.min_order[exchange][
            symbol.lower()]['min_order_qty']
        if self.current_position[base_currency].detail(
        )['available'] < base_min_qty:
            logger.warning('{} 不足下单最小精度 {} {},卖出失败.'.format(
                base_currency, '%.6f' % base_min_qty, base_currency.upper()))
            return
        # 低于current_price才能卖出,高于current_price才能买入
        current_price = self.context.get_price(symbol_exchange)
        if price > current_price:
            logger.warning('限价单价格需要小于等于市场价才能卖出,卖出失败.')
            return

        qty = math.floor(
            (self.current_position[base_currency].detail()['available'] * pct)
            * 1000000) / 1000000
        price = math.floor(price * 1000000) / 1000000
        order = Order(self.context, self.name, symbol, self.exchange,
                      self.asset_class, 'sell', 'limit', price, qty)
        order_id = order['id']
        self.history_orders[order_id] = order
        order_update_position(self.context, self.current_position,
                              order['side'], exchange, symbol, price, qty)
        match_engine(self.current_position, self.context, order)
        if not self.orders or self.orders[0][
                'create_time'] == self.context.current_datetime():
            self.orders.append(order.detail())
        logger.info('{} 卖出成功! 价格:{} 数量:{}'.format(base_currency, price, qty))
        # return order.detail()
        return order_id