def create_new_orders(self, trade_amount: Wad, trade_price: Wad,
                          our_buy_balance: Wad, our_sell_balance: Wad,
                          band: Band) -> list:
        assert (isinstance(our_buy_balance, Wad))
        assert (isinstance(our_sell_balance, Wad))
        assert (isinstance(trade_price, Wad))

        # 1、构建需要创建的订单
        new_buy_orders = []
        buy_amount = trade_amount
        # pay_amount要付出的token数量, 买单时如usdt
        pay_amount = Wad.min(buy_amount * trade_price, our_buy_balance)
        if (trade_price > Wad(0)) and (pay_amount > Wad(0)) and (buy_amount >
                                                                 Wad(0)):
            new_buy_orders.append(
                NewOrder(is_sell=False,
                         price=trade_price,
                         amount=buy_amount,
                         pay_amount=pay_amount,
                         buy_amount=buy_amount,
                         band=band,
                         confirm_function=lambda: self.buy_limits.use_limit(
                             time.time(), pay_amount)))
            logging.info(
                "Trading new_buy_order, price:%s, buy_amount:%s, pay_amount:%s"
                % (trade_price, buy_amount, pay_amount))

        # 2、构建等量的卖出订单
        new_sell_orders = []
        # pay_amount要付出的token数量,卖单时如tokenx
        pay_amount = Wad.min(trade_amount, our_sell_balance)
        buy_amount = pay_amount * trade_price
        if (trade_price > Wad(0)) and (pay_amount > Wad(0)) and (buy_amount >
                                                                 Wad(0)):
            self.logger.info(
                f"Trading creating new sell order amount {pay_amount} with price {trade_price}"
            )
            new_buy_orders.append(
                NewOrder(is_sell=True,
                         price=trade_price,
                         amount=pay_amount,
                         pay_amount=pay_amount,
                         buy_amount=buy_amount,
                         band=band,
                         confirm_function=lambda: self.buy_limits.use_limit(
                             time.time(), pay_amount)))
            logging.info(
                "Trading new_sell_order, price:%s, buy_amount:%s, pay_amount:%s"
                % (trade_price, buy_amount, pay_amount))

        # 先放卖单,再放买单
        return new_sell_orders + new_buy_orders
Beispiel #2
0
    def place_order_function(self, new_order: NewOrder):
        assert(isinstance(new_order, NewOrder))

        if new_order.is_sell:
            buy_or_sell = "SELL"
            pay_token = self.token_sell.address
            buy_token = self.token_buy.address
            new_order.buy_amount = self.buy_token.unnormalize_amount(new_order.buy_amount)
            b_token = self.buy_token
            p_token = self.sell_token
            new_order.pay_amount = self.sell_token.unnormalize_amount(new_order.pay_amount)
            token_name = self.sell_token.name
            quote_token = self.buy_token.name

        else:
            buy_or_sell = "BUY"
            pay_token = self.token_buy.address
            buy_token = self.token_sell.address
            new_order.pay_amount = self.buy_token.unnormalize_amount(new_order.pay_amount)
            p_token = self.buy_token
            b_token = self.sell_token
            new_order.buy_amount = self.sell_token.unnormalize_amount(new_order.buy_amount)
            token_name = self.sell_token.name
            quote_token = self.buy_token.name


        transact = self.otc.make(p_token=p_token, pay_amount=new_order.pay_amount,
                                 b_token=b_token, buy_amount=new_order.buy_amount).transact(gas_price=self.gas_price)

        if new_order.is_sell:
            new_order.buy_amount = self.buy_token.normalize_amount(new_order.buy_amount)
            new_order.pay_amount = self.sell_token.normalize_amount(new_order.pay_amount)
            buy_or_sell_price = new_order.buy_amount/new_order.pay_amount
            amount = new_order.pay_amount

        else:
            new_order.pay_amount = self.buy_token.normalize_amount(new_order.pay_amount)
            new_order.buy_amount = self.sell_token.normalize_amount(new_order.buy_amount)
            buy_or_sell_price = new_order.pay_amount/new_order.buy_amount
            amount = new_order.buy_amount

        if transact is not None and transact.successful and transact.result is not None:
            self.logger.info(f'Placing {buy_or_sell} order of amount {amount} {token_name} @ price {buy_or_sell_price} {quote_token}') 
            self.logger.info(f'Placing {buy_or_sell} order pay token: {p_token.name} with amount: {new_order.pay_amount}, buy token: {b_token.name} with amount: {new_order.buy_amount}')
            return Order(market=self.otc,
                         order_id=transact.result,
                         maker=self.our_address,
                         pay_token=pay_token,
                         pay_amount=new_order.pay_amount,
                         buy_token=buy_token,
                         buy_amount=new_order.buy_amount,
                         timestamp=0)
        else:
            return None
Beispiel #3
0
    def initialize_orders(self, each_order_amount, arbitrage_percent,
                          band_order_limit):
        orders = []
        i = 1
        base_price = self.get_last_price(self.pair())
        while band_order_limit + 1 > i:
            # place sell order
            price = float(base_price) * (1 + arbitrage_percent * i)
            # pay_amount = Wad.min(band.avg_amount - total_amount, our_sell_balance, limit_amount)
            pay_amount = each_order_amount * self.amount_disguise(
            )  #bix amount
            pay_amount = pay_amount + self.suffix_amount_identify()
            buy_amount = pay_amount * price  #eth money
            # print(pay_amount)

            orders.append(
                NewOrder(is_sell=True,
                         price=Wad.from_number(price),
                         amount=Wad.from_number(pay_amount),
                         pay_amount=Wad.from_number(pay_amount),
                         buy_amount=Wad.from_number(buy_amount),
                         confirm_function=lambda: self.sell_limits.use_limit(
                             time.time(), pay_amount)))

            # place buy order, pay attention to rotate bix - eth
            price = float(base_price) * (1 - arbitrage_percent * i)
            # pay_amount = Wad.min(band.avg_amount - total_amount, our_sell_balance, limit_amount)
            tmp = each_order_amount * self.amount_disguise()
            pay_amount = tmp * price  #eth money 25
            buy_amount = tmp  #bix amount 0.05
            buy_amount = buy_amount + self.suffix_amount_identify()
            # print(buy_amount)
            orders.append(
                NewOrder(is_sell=False,
                         price=Wad.from_number(price),
                         amount=Wad.from_number(buy_amount),
                         pay_amount=Wad.from_number(pay_amount),
                         buy_amount=Wad.from_number(buy_amount),
                         confirm_function=lambda: self.sell_limits.use_limit(
                             time.time(), pay_amount)))
            i = i + 1

        self.place_orders(orders)
    def initialize_orders(self, base_price, each_order_amount,
                          arbitrage_percent, band_order_limit):
        orders = []
        i = 1
        while band_order_limit + 1 > i:
            # place sell order
            price = base_price * (1 + arbitrage_percent * i)
            # pay_amount = Wad.min(band.avg_amount - total_amount, our_sell_balance, limit_amount)
            pay_amount = each_order_amount * self.amount_disguise(
            )  #bix amount
            # add unique amount number to identify different order for result performance statics
            pay_amount = pay_amount + self.amount_identify()
            buy_amount = pay_amount * price  #eth money

            orders.append(
                NewOrder(is_sell=True,
                         price=Wad.from_number(price),
                         pay_amount=Wad.from_number(pay_amount),
                         buy_amount=Wad.from_number(buy_amount),
                         confirm_function=lambda: self.sell_limits.use_limit(
                             time.time(), pay_amount)))

            # place buy order, pay attention to rotate bix - eth
            price = base_price * (1 - arbitrage_percent * i)
            # pay_amount = Wad.min(band.avg_amount - total_amount, our_sell_balance, limit_amount)
            tmp = each_order_amount * self.amount_disguise(
            ) + self.amount_identify()
            pay_amount = tmp * price  #eth money 25
            buy_amount = tmp  #bix amount 0.05
            orders.append(
                NewOrder(is_sell=False,
                         price=Wad.from_number(price),
                         pay_amount=Wad.from_number(pay_amount),
                         buy_amount=Wad.from_number(buy_amount),
                         confirm_function=lambda: self.sell_limits.use_limit(
                             time.time(), pay_amount)))
            i = i + 1

        self.place_orders(orders)
    def _new_sell_orders(self, our_sell_orders: list, our_sell_balance: Wad,
                         target_price: Wad):
        """Return sell orders which need to be placed to bring total amounts within all sell bands above minimums."""
        assert (isinstance(our_sell_orders, list))
        assert (isinstance(our_sell_balance, Wad))
        assert (isinstance(target_price, Wad))

        new_orders = []
        limit_amount = self.sell_limits.available_limit(time.time())
        missing_amount = Wad(0)

        for band in self.sell_bands:
            orders = [
                order for order in our_sell_orders
                if band.includes(order, target_price)
            ]
            total_amount = self.total_amount(orders)
            if total_amount < band.min_amount:
                price = self._calculate_price(band, target_price)
                pay_amount = Wad.min(band.avg_amount - total_amount,
                                     our_sell_balance, limit_amount)
                buy_amount = self._calculate_buy_amount_for_sell_orders(
                    price, pay_amount)
                missing_amount += Wad.max(
                    (band.avg_amount - total_amount) - our_sell_balance,
                    Wad(0))
                if (price > Wad(0)) and (pay_amount >= band.dust_cutoff) and (
                        pay_amount > Wad(0)) and (buy_amount > Wad(0)):
                    self.logger.info(
                        f"Sell band (spread <{band.min_margin}, {band.max_margin}>,"
                        f" amount <{band.min_amount}, {band.max_amount}>) has amount {total_amount},"
                        f" creating new sell order with price {price}")

                    our_sell_balance = our_sell_balance - pay_amount
                    limit_amount = limit_amount - pay_amount

                    new_orders.append(
                        NewOrder(is_sell=True,
                                 price=price,
                                 amount=pay_amount,
                                 pay_amount=pay_amount,
                                 buy_amount=buy_amount,
                                 band=band,
                                 confirm_function=lambda: self.sell_limits.
                                 use_limit(time.time(), pay_amount)))

        return new_orders, missing_amount
Beispiel #6
0
    def synchronize_orders(self):
        # bands = Bands.read(self.bands_config, self.spread_feed, self.history)
        order_book = self.order_book_manager.get_order_book()

        self.logger.info(
            "---**----synchronize_orders: The length of local_orders " +
            str(self.local_orders.__len__()))
        self.logger.info(
            "---**----synchronize_orders: The length of order_book.orders " +
            str(len(order_book.orders)))

        local_order_ids = set(order.order_id for order in self.local_orders)
        order_book_ids = set(order.order_id for order in order_book.orders)
        completed_order_ids = list(local_order_ids - order_book_ids)

        # 如果没有后续的更新 local orders,只有这里的更新模块,肯定有问题的,因为一旦有成交,
        # completed_order_ids 不为0,则永远更新不了local orders 了
        # return if there none order be completed
        # 下面这种情况,只有在order_book订单完全"包含"local_order订单时,但是两者并不相等时,才会让本地订单等于远程订单;
        # 这种一般是远程订单比本地订单多,往往比如人工在系统提交了新的订单
        # 这段是必须的,比如程序起步的时候,同时提交一批订单,导致速度慢,导致 local_orders 只有真正订单的一部分
        if completed_order_ids.__len__() == 0:
            if local_order_ids.__len__() != order_book_ids.__len__():
                self.logger.info(
                    "---**---update local order with remote order while no completed order in past cycle"
                )
                self.local_orders = order_book.orders
            return

        # completed_orders = list(filter(lambda order: order.order_id in completed_order_ids, self.local_orders))
        completed_orders = [
            order for order in self.local_orders
            if order.order_id in completed_order_ids
        ]

        # completed_orders = list(filter(lambda order: order.order_id in local_order_ids, order_book.orders))
        self.logger.info("---**---The number of completed orders is " +
                         str(len(completed_orders)))
        self.logger.info("---**---Below is/are completed orders : ")
        self.logger.info(completed_orders)

        # completed_orders_new = list(set(self.local_orders) - set(order_book.orders))
        # print("---**---The lenght of completed new orders " + str(len(completed_orders_new)))
        # print(completed_orders_new)

        # completed_orders = [{'amount': Wad(2220000000000000000),
        #              'amount_symbol': 'BIX',
        #              'created_at': 1528203670000,
        #              'is_sell': True,
        #              'money': Wad(52779250000000000),
        #              'money_symbol': 'ETH',
        #              'order_id': 606026215,
        #              'price': Wad(2294750000000000)}, {'amount': Wad(2990000000000000000),
        #              'amount_symbol': 'BIX',
        #              'created_at': 1528203670000,
        #              'is_sell': False,
        #              'money': Wad(55779250000000000),
        #              'money_symbol': 'ETH',
        #              'order_id': 606026215,
        #              'price': Wad(2394750000000000)}]

        # our_buy_orders = self.our_buy_orders(order_book.orders)
        # our_sell_orders = self.our_sell_orders(order_book.orders)
        # print(our_buy_orders)
        # print(our_sell_orders)
        # Do not place new orders if order book state is not confirmed
        if order_book.orders_being_placed or order_book.orders_being_cancelled:
            self.logger.debug(
                "Order book is in progress, not placing new orders")
            return

        # order_book.orders 按照从低到高价格排序,获取价格最低(也就是最远)的1个订单作为要取消的订单;
        sorted_buy_orders = sorted(self.our_buy_orders(order_book.orders),
                                   key=lambda order: order.price)
        # order_book.orders 按照从高到低价格排序,获取价格最高(也就是最远)的 1 个订单作为要取消的订单;
        sorted_sell_orders = sorted(self.our_sell_orders(order_book.orders),
                                    key=lambda order: order.price,
                                    reverse=True)

        # if (self.local_orders.__len__() - len(order_book.orders) > 0):
        if len(completed_orders) > 0:
            self.logger.info(
                "---**---some orders have been completed, and new should be submitted"
            )
            new_orders = []
            cancellable_orders = []
            step = 1
            buy_index = 0
            sell_index = 0
            count_sell_order = self.count_sell_orders(order_book.orders)
            count_buy_order = self.count_buy_orders(order_book.orders)

            for cod in completed_orders:
                # print(type(cod))
                # print(cod.is_sell)
                # the completed order is sell order, buy order should be placed
                cancel_order_count = 0

                self.orderlog.logger.info(" - " + str(cod.is_sell) + " - " +
                                          str(cod.price) + " - " +
                                          str(cod.amount) + " - " +
                                          str(cod.price * cod.amount))
                if cod.is_sell:
                    #(1-1 buy) place buy order, pay attention to rotate bix - eth
                    price = float(cod.price) * (1 - self.arbitrage_percent)
                    self.logger.info("To submit a new buy order with price " +
                                     str(price))
                    pay_amount = float(cod.amount) * price  # eth money 25
                    buy_amount = float(cod.amount)  # bix amount 0.05
                    new_orders.append(
                        NewOrder(is_sell=False,
                                 price=Wad.from_number(price),
                                 amount=Wad.from_number(buy_amount),
                                 pay_amount=Wad.from_number(pay_amount),
                                 buy_amount=Wad.from_number(buy_amount),
                                 confirm_function=lambda: self.sell_limits.
                                 use_limit(time.time(), pay_amount)))

                    #(1-2 buy) 取消超出订单数量上限的buy订单,价格最远的订单--就是最低价格的订单
                    buyo = count_buy_order + 1 - self.band_order_limit
                    self.logger.info(
                        "Number of exceed the upper limit of buy order: " +
                        str(buyo))
                    # 加上后面这个条件,防止buy订单也成交,当前的订单列表里实际已经没有那么多数量的 buy 订单供取消
                    # 比如同一周期,buy 订单被吃了1,sell 订单被吃了3个,那么剩下的buy订单就不会被取消3次了
                    if buyo > 0 and len(sorted_buy_orders) > buy_index:
                        # order_book.orders 按照从低到高价格排序,获取价格最低(也就是最远)的1个订单作为要取消的订单;
                        # sorted_buy_orders = sorted(self.our_buy_orders(order_book.orders),
                        #                             key=lambda order: order.price)
                        print(sorted_buy_orders)
                        self.logger.info(
                            "Cancel buy order which exceed band order limit ")
                        self.logger.info(
                            str(sorted_buy_orders[buy_index].order_id))
                        self.logger.info(str(sorted_buy_orders[buy_index]))
                        print(type(sorted_buy_orders[buy_index]))
                        cancellable_orders.append(sorted_buy_orders[buy_index])
                        # self.bibox_api.cancel_order(sorted_buy_orders[index].order_id)
                        buy_index = buy_index + 1
                    else:  #如果没有对应取消订单,则 buy 订单数量增加
                        count_buy_order = count_buy_order + 1

                    #(2 sell) 以当前价格为基数,重新submit一个高价格的 sell 订单,补充 sell list
                    # place sell a new order with higher price
                    # 需要判断订单的数量是否小于band order limits,并且按照差异补充订单
                    # count_sell_order = self.count_sell_orders(order_book.orders)
                    band_sell_order_gap = self.band_order_limit - count_sell_order
                    self.logger.info(
                        "The number of sell order which need to submit " +
                        str(band_sell_order_gap))
                    # while band_sell_order_gap > 0: # 外部已经有循环了,不需要这个循环了,否则在多订单被吃时,会加倍补充
                    # 这里只需要判断,控制数量就够了
                    if band_sell_order_gap > 0:
                        current_price = self.get_last_price(self.pair())
                        self.logger.info("---**---current price is " +
                                         str(current_price))
                        price = float(current_price) * (
                            1 + self.arbitrage_percent *
                            (step + count_sell_order))
                        self.logger.info(
                            "The higher price of new sell order is " +
                            str(price))
                        pay_amount = self.each_order_amount * self.amount_disguise(
                        )  # bix amount
                        pay_amount = pay_amount + self.suffix_amount_identify(
                        )  # add unique identify
                        buy_amount = pay_amount * price  # eth money
                        new_orders.append(
                            NewOrder(is_sell=True,
                                     price=Wad.from_number(price),
                                     amount=Wad.from_number(pay_amount),
                                     pay_amount=Wad.from_number(pay_amount),
                                     buy_amount=Wad.from_number(buy_amount),
                                     confirm_function=lambda: self.sell_limits.
                                     use_limit(time.time(), pay_amount)))
                        count_sell_order = count_sell_order + 1

                else:  # buy order had been completed
                    #(1-1 sell) to place a corresponding sell order
                    price = float(cod.price) * (1 + self.arbitrage_percent)
                    self.logger.info(
                        "To submit new sell order with arbitrage price: " +
                        str(price))
                    pay_amount = float(cod.amount)  # bix amount
                    buy_amount = pay_amount * price  # eth money
                    new_orders.append(
                        NewOrder(is_sell=True,
                                 price=Wad.from_number(price),
                                 amount=Wad.from_number(pay_amount),
                                 pay_amount=Wad.from_number(pay_amount),
                                 buy_amount=Wad.from_number(buy_amount),
                                 confirm_function=lambda: self.sell_limits.
                                 use_limit(time.time(), pay_amount)))

                    #(1-2 sell) 取消超出band order limits数量的 sell 订单
                    self.logger.info(
                        "--------debug--------------Number of exceed the upper limit of sell order: "
                        + str(count_sell_order + 1 - self.band_order_limit))
                    sello = count_sell_order + 1 - self.band_order_limit
                    if sello > 0 and len(sorted_sell_orders) > sell_index:
                        # order_book.orders 按照从高到低价格排序,获取价格最高(也就是最远)的 1 个订单作为要取消的订单;
                        # sorted_sell_orders = sorted(self.our_sell_orders(order_book.orders),
                        #                             key=lambda order: order.price,
                        #                             reverse=True)
                        print(sorted_sell_orders)
                        self.logger.info(
                            "Cancel sell order which exceed band order limit ")
                        self.logger.info(
                            str(sorted_sell_orders[sell_index].order_id))
                        self.logger.info(str(sorted_sell_orders[sell_index]))
                        cancellable_orders.append(
                            sorted_sell_orders[sell_index])
                        # self.bibox_api.cancel_order(sorted_sell_orders[index].order_id)
                        sell_index = sell_index + 1
                    else:
                        count_sell_order = count_sell_order + 1

                    #(2 buy) 以当前价格为基数,重新submit一个 buy 订单,补充 buy list
                    # 需要判断订单的数量是否小于band order limits,并且按照差异补充订单
                    # count_buy_order = self.count_buy_orders(order_book.orders)
                    band_buy_order_gap = self.band_order_limit - count_buy_order
                    self.logger.info(
                        "The number of buy order which need to submit " +
                        str(band_buy_order_gap))
                    if band_buy_order_gap > 0:
                        #基础价格放在循环里的话,能快速反映当前价格,特保是激烈波动的时候;但是增加了请求次数
                        current_price = self.get_last_price(self.pair())
                        price = float(current_price) * (
                            1 - self.arbitrage_percent *
                            (step + count_buy_order))
                        self.logger.info(
                            "The lower price order of new buy order is " +
                            str(price))
                        tmp = self.each_order_amount * self.amount_disguise()
                        pay_amount = tmp * price  # eth money 25
                        buy_amount = tmp  # bix amount 0.05
                        buy_amount = buy_amount + self.suffix_amount_identify(
                        )  # add unique identify
                        new_orders.append(
                            NewOrder(is_sell=False,
                                     price=Wad.from_number(price),
                                     amount=Wad.from_number(buy_amount),
                                     pay_amount=Wad.from_number(pay_amount),
                                     buy_amount=Wad.from_number(buy_amount),
                                     confirm_function=lambda: self.sell_limits.
                                     use_limit(time.time(), pay_amount)))

                        # band_buy_order_gap = band_buy_order_gap - 1
                        count_buy_order = count_buy_order + 1
                step = step + 1

            # Cancel orders
            if len(cancellable_orders) > 0:
                self.order_book_manager.cancel_orders(cancellable_orders)
                time.sleep(1)
            while order_book.orders_being_placed or order_book.orders_being_cancelled:
                self.logger.info(
                    "Sleep 1 s while order manager is in progress after cancelling exceed orders"
                )
                time.sleep(1)

            # Submit new orders
            self.place_orders(new_orders)
            time.sleep(1)
            while order_book.orders_being_placed or order_book.orders_being_cancelled:
                self.logger.info(
                    "Sleep 1 s while order manager is in progress after placing new orders"
                )
                time.sleep(1)

            # update local orders, 前面有更新模块,与这边不完全相同,尤其是有成交的情况下,必须要更新
            # 是这样吗? 似乎也不是的,有成交的情况下,下一次订单也会让 set(local) - set(order book)=0的,集合相减的特殊之处
            # 如果这样就没有必要了。
            # 是这样简单的复制更新,还是本地自己维护一个 id list 好呢? 也就是把(1)确定成交的从 local 删除;
            # (2)确定提交的add 到本地;
            # 缩进到循环: if len(completed_orders) > 0:,在出现两者不一致的时候,同步更新订单;
            # 但是这个会导致一个问题,就是初始化的订单里,有price 为0,导致两者不一致的情况,怎么办?这里解决了,是通过 order id对比而不是
            # 直接的 order 对比,所以应该是解决了才对

            # 默认有3秒的刷新周期,导致被成功取消的订单,不会马上出现在order_book_manager
            # 导致 local 比真实的多了,使得取消的订单在下一次被误认为是成交了
            # 以上虽然 while 循环了,但是如果 while 循环的时候,有订单被成交了,这个订单就会丢掉,被误认为是撤销的订单了
            self.logger.info(
                "Update local order with latest remote order at the end of synchronize cycle"
            )
            self.local_orders = self.order_book_manager.get_order_book().orders
    def synchronize_orders(self):
        bands = Bands.read(self.bands_config, self.spread_feed, self.history)
        order_book = self.order_book_manager.get_order_book()
        target_price = self.price_feed.get_price()
        # print(type(self.local_orders))
        # print(self.local_orders)
        # print(type(order_book.orders))
        # print(order_book.orders)

        print("---**---The lenght of local_orders " +
              str(self.local_orders.__len__()))
        print("---**---The lenght of order_book.orders " +
              str(len(order_book.orders)))

        local_order_ids = set(order.order_id for order in self.local_orders)
        order_book_ids = set(order.order_id for order in order_book.orders)

        completed_order_ids = list(local_order_ids - order_book_ids)
        # 如果没有后续的更新 local orders,只有这里的更新模块,肯定有问题的,因为一旦有成交,
        # completed_order_ids 不为0,则永远更新不了local orders 了
        # return if there none order be completed
        # 下面这种情况,只有在order_book订单完全"包含"local_order订单时,但是两者并不相等时,才会让本地订单等于远程订单;
        # 这种一般是远程订单比本地订单多,往往比如人工在系统提交了新的订单
        if completed_order_ids.__len__() == 0:
            if local_order_ids.__len__() != order_book_ids.__len__():
                print("update local order")
                self.local_orders = order_book.orders
            return

        # completed_orders = list(filter(lambda order: order.order_id in completed_order_ids, self.local_orders))
        completed_orders = [
            order for order in self.local_orders
            if order.order_id in completed_order_ids
        ]

        # completed_orders = list(filter(lambda order: order.order_id in local_order_ids, order_book.orders))
        print("---**---The lenght of completed orders " +
              str(len(completed_orders)))
        print(completed_orders)

        # completed_orders_new = list(set(self.local_orders) - set(order_book.orders))
        # print("---**---The lenght of completed new orders " + str(len(completed_orders_new)))
        # print(completed_orders_new)

        # completed_orders = [{'amount': Wad(2220000000000000000),
        #              'amount_symbol': 'BIX',
        #              'created_at': 1528203670000,
        #              'is_sell': True,
        #              'money': Wad(52779250000000000),
        #              'money_symbol': 'ETH',
        #              'order_id': 606026215,
        #              'price': Wad(2294750000000000)}, {'amount': Wad(2990000000000000000),
        #              'amount_symbol': 'BIX',
        #              'created_at': 1528203670000,
        #              'is_sell': False,
        #              'money': Wad(55779250000000000),
        #              'money_symbol': 'ETH',
        #              'order_id': 606026215,
        #              'price': Wad(2394750000000000)}]

        # our_buy_orders = self.our_buy_orders(order_book.orders)
        # our_sell_orders = self.our_sell_orders(order_book.orders)
        # print(our_buy_orders)
        # print(our_sell_orders)
        # Do not place new orders if order book state is not confirmed
        if order_book.orders_being_placed or order_book.orders_being_cancelled:
            self.logger.debug(
                "Order book is in progress, not placing new orders")
            return

        # if (self.local_orders.__len__() - len(order_book.orders) > 0):
        if len(completed_orders) > 0:
            print("--------- some orders have been done --------")
            new_orders = []
            step = 1
            count_sell_order = self.count_sell_orders(order_book.orders)
            count_buy_order = self.count_buy_orders(order_book.orders)

            for cod in completed_orders:
                # print(type(cod))
                # print(cod.is_sell)
                # the completed order is sell order, buy order should be placed
                if cod.is_sell:
                    # place buy order, pay attention to rotate bix - eth
                    price = float(cod.price) * (1 - self.arbitrage_percent)
                    print("----to submit a new buy order with price " +
                          str(price))
                    pay_amount = float(cod.amount) * price  # eth money 25
                    buy_amount = float(cod.amount)  # bix amount 0.05
                    new_orders.append(
                        NewOrder(is_sell=False,
                                 price=Wad.from_number(price),
                                 pay_amount=Wad.from_number(pay_amount),
                                 buy_amount=Wad.from_number(buy_amount),
                                 confirm_function=lambda: self.sell_limits.
                                 use_limit(time.time(), pay_amount)))
                    # 以当前价格为基数,重新submit一个高价格的 sell 订单,补充 sell list
                    # place sell a new order with higher price
                    # 需要判断订单的数量是否小于band order limits,并且按照差异补充订单
                    # count_sell_order = self.count_sell_orders(order_book.orders)
                    print(count_sell_order)
                    band_sell_order_gap = self.band_order_limit - count_sell_order
                    print("---band gap---- " + str(band_sell_order_gap))
                    # while band_sell_order_gap > 0: # 外部已经有循环了,不需要这个循环了,否则在多订单被吃时,会加倍补充
                    # 这里只需要判断,控制数量就够了
                    if band_sell_order_gap > 0:
                        current_price = self.bibox_api.get_last_price(
                            self.pair())
                        print("------current price---- " + str(current_price))
                        price = float(current_price) * (
                            1 + self.arbitrage_percent *
                            (step + count_sell_order))
                        print("----higher price to sell--- " + str(price))
                        pay_amount = self.each_order_amount * self.amount_disguise(
                        )  # bix amount
                        buy_amount = pay_amount * price  # eth money
                        new_orders.append(
                            NewOrder(is_sell=True,
                                     price=Wad.from_number(price),
                                     pay_amount=Wad.from_number(pay_amount),
                                     buy_amount=Wad.from_number(buy_amount),
                                     confirm_function=lambda: self.sell_limits.
                                     use_limit(time.time(), pay_amount)))
                        # step = step + 1
                        # band_sell_order_gap = band_sell_order_gap - 1
                        count_sell_order = count_sell_order + 1

                else:  # buy order had been completed
                    # to place a sell order
                    price = float(cod.price) * (1 + self.arbitrage_percent)
                    print("----price--- sell--- ")
                    print(price)
                    pay_amount = float(cod.amount)  # bix amount
                    buy_amount = pay_amount * price  # eth money
                    new_orders.append(
                        NewOrder(is_sell=True,
                                 price=Wad.from_number(price),
                                 pay_amount=Wad.from_number(pay_amount),
                                 buy_amount=Wad.from_number(buy_amount),
                                 confirm_function=lambda: self.sell_limits.
                                 use_limit(time.time(), pay_amount)))
                    # 以当前价格为基数,重新submit一个 buy 订单,补充 buy list
                    # 需要判断订单的数量是否小于band order limits,并且按照差异补充订单
                    # count_buy_order = self.count_buy_orders(order_book.orders)
                    band_buy_order_gap = self.band_order_limit - count_buy_order
                    print("---band gap----" + str(band_buy_order_gap))

                    # while band_buy_order_gap > 0:
                    if band_buy_order_gap > 0:
                        #基础价格放在循环里的话,能快速反映当前价格,特保是激烈波动的时候;但是增加了请求次数
                        current_price = self.bibox_api.get_last_price(
                            self.pair())
                        price = float(current_price) * (
                            1 - self.arbitrage_percent *
                            (step + count_buy_order))
                        print("----lower price order to buy--- " + str(price))
                        tmp = self.each_order_amount * self.amount_disguise()
                        pay_amount = tmp * price  # eth money 25
                        buy_amount = tmp  # bix amount 0.05
                        new_orders.append(
                            NewOrder(is_sell=False,
                                     price=Wad.from_number(price),
                                     pay_amount=Wad.from_number(pay_amount),
                                     buy_amount=Wad.from_number(buy_amount),
                                     confirm_function=lambda: self.sell_limits.
                                     use_limit(time.time(), pay_amount)))

                        # band_buy_order_gap = band_buy_order_gap - 1
                        count_buy_order = count_buy_order + 1
                    step = step + 1

            self.place_orders(new_orders)

            # update local orders, 前面有更新模块,与这边不完全相同,尤其是有成交的情况下,必须要更新
            # 是这样吗? 似乎也不是的,有成交的情况下,下一次订单也会让 set(local) - set(order book)=0的,集合相减的特殊之处
            # 如果这样就没有必要了。
            # 是这样简单的复制更新,还是本地自己维护一个 id list 好呢? 也就是把(1)确定成交的从 local 删除;
            # (2)确定提交的add 到本地;
            # 缩进到循环: if len(completed_orders) > 0:,在出现两者不一致的时候,同步更新订单;
            # 但是这个会导致一个问题,就是初始化的订单里,有price 为0,导致两者不一致的情况,怎么办?这里解决了,是通过 order id对比而不是
            # 直接的 order 对比,所以应该是解决了才对
            print("-----update local order------")
            self.local_orders = self.order_book_manager.get_order_book().orders