Exemplo n.º 1
0
    def hedge_order(self, hedge_amount, hedge_price):
        # hedge sell in binance
        can_sell_max = self.hedge_broker.zrx_available

        sell_amount_limit = self.cal_hedge_amount_min(hedge_price)
        if can_sell_max < hedge_amount:
            # post email
            if can_sell_max < sell_amount_limit:
                logging.error(
                    'liquid_zrx======>hedge_order failed, because can_sell_max: %s < %s'
                    % (can_sell_max, sell_amount_limit))
                raise Exception(
                    'liquid_zrx======>hedge_order failed, because can_sell_max: %s < %s'
                    % (can_sell_max, sell_amount_limit))
            sell_amount = can_sell_max
        else:
            sell_amount = hedge_amount

        sell_price = hedge_price
        hedge_index = 0
        while True:
            try:
                order_id = self.hedge_broker.sell_limit_c(amount=sell_amount,
                                                          price=sell_price)
            except Exception as e:
                logging.error(
                    'liquid_zrx======>hedge sell order failed when sell_limit_c, error=%s'
                    % e)
                raise Exception(
                    'liquid_zrx======>hedge sell order failed when sell_limit_c, error=%s'
                    % e)
            deal_amount, avg_price = self.get_deal_amount(
                self.hedge_market, order_id)
            self.local_order['hedge_amount'] += deal_amount
            logging.info(
                "liquid_zrx======>hedge sell %s, order_id=%s, amount=%s, price=%s, deal_amount=%s"
                % (hedge_index, order_id, sell_amount, avg_price, deal_amount))
            diff_amount = round(sell_amount - deal_amount, 8)

            sell_amount_limit = self.cal_hedge_amount_min(sell_price)
            if diff_amount < sell_amount_limit:
                hedge_amount_current = self.local_order['hedge_amount']
                hedge_amount_target = self.local_order['deal_amount']

                logging.info(
                    'liquid_zrx======>hedge sell order success, target=%s, current=%s'
                    % (hedge_amount_target, hedge_amount_current))

                email_box.send_mail(
                    'liquid_zrx======>hedge sell order success, target=%s, current=%s'
                    % (hedge_amount_target, hedge_amount_current))
                break
            time.sleep(config.INTERVAL_API)
            ticker = self.get_latest_ticker(self.hedge_market)
            sell_amount = diff_amount
            sell_price = ticker['bid']
            hedge_index += 1
Exemplo n.º 2
0
    def hedge_order_buy(self, amount, price):
        """confirm hedge order all executed"""
        buy_price = price
        buy_amount_target = amount
        hedge_index = 0
        hedge_total_amount = 0
        while True:
            can_buy_max = self.hedge_broker.btc_available / buy_price
            if can_buy_max < buy_amount_target:
                if can_buy_max < self.LIQUID_HEDGE_MIN_AMOUNT:
                    logging.error(
                        'Liquid_BCH======>hedge buy order failed, because can_buy_max: %s < %s'
                        % (can_buy_max, self.LIQUID_HEDGE_MIN_AMOUNT))
                    raise Exception(
                        'hedge buy order failed, because can_buy_max: %s < %s'
                        % (can_buy_max, self.LIQUID_HEDGE_MIN_AMOUNT))
                buy_amount = can_buy_max
            else:
                buy_amount = buy_amount_target

            # sell_limit_c confirm sell_limit success, order_id must exist
            try:
                order_id = self.brokers[self.hedge_market].buy_limit_c(
                    amount=buy_amount, price=buy_price)
            except Exception as e:
                logging.error(
                    'Liquid_BCH======>hedge buy order failed when buy_limit_c, error=%s'
                    % e)
                raise Exception(
                    'hedge buy order failed when buy_limit_c, error=%s' % e)

            time.sleep(config.INTERVAL_API)
            deal_amount, avg_price = self.get_deal_amount(
                self.hedge_market, order_id)
            hedge_total_amount += deal_amount
            logging.info(
                "Liquid_BCH======>hedge buy %s, order_id=%s, amount=%s, price=%s, deal_amount=%s"
                % (hedge_index, order_id, buy_amount, avg_price, deal_amount))

            diff_amount = round(buy_amount - deal_amount, 8)
            if diff_amount < self.LIQUID_HEDGE_MIN_AMOUNT:
                logging.info(
                    'Liquid_BCH======>hedge buy order success, target=%s, total=%s, left=%s'
                    % (amount, hedge_total_amount, diff_amount))
                email_box.send_mail(
                    'hedge buy order success, target=%s, total=%s, left=%s' %
                    (amount, hedge_total_amount, diff_amount))
                break
            time.sleep(config.INTERVAL_API)
            ticker = self.get_latest_ticker(self.hedge_market)
            buy_amount_target = diff_amount
            buy_price = ticker['ask']
            hedge_index += 1
Exemplo n.º 3
0
    def hedge_order_sell(self, amount, price):
        """confirm hedge order all executed"""
        hedge_index = 0
        hedge_total_amount = 0

        can_sell_max = self.hedge_broker.zrx_available
        if can_sell_max < amount:
            # post email
            if can_sell_max < self.LIQUID_HEDGE_MIN_AMOUNT:
                logging.error(
                    'Liquid_ZRX======>hedge sell order failed, because can_sell_max: %s < %s'
                    % (can_sell_max, self.LIQUID_HEDGE_MIN_AMOUNT))
                raise Exception(
                    'hedge sell order failed, because can_sell_max: %s < %s' %
                    (can_sell_max, self.LIQUID_HEDGE_MIN_AMOUNT))
            sell_amount = can_sell_max
        else:
            sell_amount = amount
        sell_price = price
        while True:
            # sell_limit_c confirm sell_limit success, order_id must exist
            try:
                order_id = self.brokers[self.hedge_market].sell_limit_c(
                    amount=sell_amount, price=sell_price)
            except Exception as e:
                logging.error(
                    'Liquid_ZRX======>hedge sell order failed when sell_limit_c, error=%s'
                    % e)
                raise Exception(
                    'hedge sell order failed when sell_limit_c, error=%s' % e)

            time.sleep(config.INTERVAL_API)
            deal_amount, avg_price = self.get_deal_amount(
                self.hedge_market, order_id)
            hedge_total_amount += deal_amount
            logging.info(
                "Liquid_ZRX======>hedge sell %s, order_id=%s, amount=%s, price=%s, deal_amount=%s"
                % (hedge_index, order_id, sell_amount, avg_price, deal_amount))

            diff_amount = round(sell_amount - deal_amount, 8)
            if diff_amount < self.LIQUID_HEDGE_MIN_AMOUNT:
                logging.info(
                    'Liquid_ZRX======>hedge sell order success, target=%s, total=%s, left=%s'
                    % (amount, hedge_total_amount, diff_amount))
                email_box.send_mail(
                    'hedge sell order success, target=%s, total=%s, left=%s' %
                    (amount, hedge_total_amount, diff_amount))
                break
            time.sleep(config.INTERVAL_API)
            ticker = self.get_latest_ticker(self.hedge_market)
            sell_amount = diff_amount
            sell_price = ticker['bid']
            hedge_index += 1
Exemplo n.º 4
0
    def place_order(self, buy_price, buy_amount):
        eth_num = self.mm_broker.eth_available
        can_buy_max = eth_num / buy_price

        buy_amount = min(buy_amount, can_buy_max)

        if buy_amount < self.min_amount_trade:
            logging.error(
                'liquid_zrx======>buy failed because %s < %s, maybe bfx eth is not enough'
                % (eth_num, self.min_amount_trade))
            raise Exception(
                'liquid_zrx======>buy failed because %s < %s, maybe bfx eth is not enough'
                % (eth_num, self.min_amount_trade))

        try:
            order_id = self.mm_broker.buy_limit_c(price=buy_price,
                                                  amount=buy_amount)
        except Exception as e:
            logging.error(
                "liquid_zrx======>place_order failed, exception: %s" % e)
            email_box.send_mail(
                "liquid_zrx======>place_order failed, exception: %s" % e)
            return
        if not order_id:
            logging.error(
                'liquid_zrx======>place_order failed, because order_id is none,that must not happen'
            )
            raise Exception(
                'liquid_zrx======>place_order failed, because order_id is none,that must not happen'
            )
        self.local_order = {
            'order_id': order_id,
            'price': buy_price,
            'amount': buy_amount,
            'deal_amount': 0,
            'hedge_amount': 0,
            'type': 'buy',
            'status': constant.ORDER_STATE_PENDING,
            'time': time.time()
        }
        logging.info('liquid_zrx======>place_order success, order_id: %s' %
                     order_id)
Exemplo n.º 5
0
    def run_loop(self):
        if len(self.markets) == 0:
            print('empty markets')
            return

        if len(self.observers) == 0:
            print('empty observers')
            return
        #
        signal.signal(signal.SIGINT, sigint_handler)
        # 以下那句在windows python2.4不通过,但在freebsd下通过
        signal.signal(signal.SIGHUP, sigint_handler)
        signal.signal(signal.SIGTERM, sigint_handler)

        while True:
            try:
                self.update_balance()
                self.update_other()
                time.sleep(config.INTERVAL_API)

                self.depths = self.update_depths()

                self.tick()
            except Exception as ex:
                logging.warn("datafeed exception:%s" % ex)
                traceback.print_exc()
                self.terminate()
                email_box.send_mail("datafeed exception:%s" % ex)
                return

            if is_sigint_up:
                # 中断时需要处理的代码
                logging.info("APP Exit")
                self.terminate()
                break
            sys.stdout.write(".\n\n")
            sys.stdout.flush()
            time.sleep(config.INTERVAL_MARKET)