def on_order(self, order: OrderData):
        """
        Callback of new order data update.
        """

        if order.status == Status.ALLTRADED:

            if order.vt_orderid in self.buy_orders:
                self.buy_orders.remove(order.vt_orderid)

            if order.vt_orderid in self.sell_orders:
                self.sell_orders.remove(order.vt_orderid)

            self.cancel_all()
            print(f"订单买卖单完全成交, 先撤销所有订单")

            self.last_filled_order = order

            # tick 存在且仓位数量还没有达到设置的最大值.
            if self.tick and abs(self.pos) < self.max_size * self.trading_size:
                step = self.get_step()

                buy_price = order.price - step * self.grid_step
                sell_price = order.price + step * self.grid_step

                buy_price = min(self.tick.bid_price_1 * (1 - 0.0001),
                                buy_price)  # marker
                sell_price = max(self.tick.ask_price_1 * (1 + 0.0001),
                                 sell_price)

                buy_ids = self.buy(buy_price, self.trading_size)
                sell_ids = self.sell(sell_price, self.trading_size)

                self.buy_orders.extend(buy_ids)
                self.sell_orders.extend(sell_ids)

                print(
                    f"订单完全成交, 分别下双边网格: BUY: {buy_ids}@{buy_price}, SELL: {sell_ids}@{sell_price}"
                )

        if not order.is_active():
            if order.vt_orderid in self.buy_orders:
                self.buy_orders.remove(order.vt_orderid)

            elif order.vt_orderid in self.sell_orders:
                self.sell_orders.remove(order.vt_orderid)

        self.put_event()
    def on_order(self, order: OrderData):
        """
        Callback of new order data update.
        """
        if order.status == Status.ALLTRADED:
            if order.direction == Direction.LONG:
                # 买单成交.
                self.current_increase_pos_times += 1
                self.last_entry_price = order.price  # 记录上一次成绩的价格.

        if not order.is_active():
            if order.vt_orderid in self.sell_orders:
                self.sell_orders.remove(order.vt_orderid)

            elif order.vt_orderid in self.buy_orders:
                self.buy_orders.remove(order.vt_orderid)

        self.put_event()  # 更新UI使用.
예제 #3
0
    def on_order(self, order: OrderData):
        """
        Callback of new order data update.
        """
        self.position.update_position(order)
        self.current_pos = self.position.pos
        self.avg_price = self.position.avg_price

        if order.vt_orderid in self.long_orders:
            if order.status == Status.ALLTRADED:
                self.long_orders.remove(order.vt_orderid)

                print("多头成交,撤销空头订单和止盈订单")
                for vt_id in (self.short_orders + self.profit_orders):
                    self.cancel_order(vt_id)

                self.last_filled_order = order

                if self.position.pos > 0:
                    if abs(self.position.pos
                           ) < self.trading_size * self.max_pos:
                        if not self.tick:
                            return

                        step = self.get_step()
                        price = order.price - self.grid_step * step
                        price = min(price,
                                    self.tick.bid_price_1 * (1 - 0.0001))
                        ids = self.buy(price, self.trading_size)
                        self.long_orders.extend(ids)
                        print(f"多头仓位继续下多头订单: {ids}@{price}")

            elif order.status in [Status.REJECTED, Status.CANCELLED]:
                self.long_orders.remove(order.vt_orderid)

        elif order.vt_orderid in self.short_orders:
            if order.status == Status.ALLTRADED:
                self.short_orders.remove(order.vt_orderid)

                print("空头成交,撤销多头订单和止盈订单")
                for vt_id in (self.long_orders + self.profit_orders):
                    self.cancel_order(vt_id)

                self.last_filled_order = order

                if self.position.pos < 0:
                    if abs(self.position.pos
                           ) < self.trading_size * self.max_pos:
                        if not self.tick:
                            return

                        step = self.get_step()
                        price = order.price + self.grid_step * step
                        price = max(price,
                                    self.tick.ask_price_1 * (1 + 0.0001))

                        ids = self.short(price, self.trading_size)
                        self.short_orders.extend(ids)

                        print(f"空头仓位继续下空头订单: {ids}@{price}")

            elif order.status in [Status.REJECTED, Status.CANCELLED]:
                self.short_orders.remove(order.vt_orderid)  # remove orderid

        elif order.vt_orderid in self.stop_orders:
            if not order.is_active():
                self.stop_orders.remove(order.vt_orderid)

        elif order.vt_orderid in self.profit_orders:
            if not order.is_active():
                self.profit_orders.remove(order.vt_orderid)

        self.put_event()
    def on_order(self, order: OrderData):
        """
        Callback of new order data update.
        """
        self.position_calculator.update_position(order)

        self.current_pos = self.position_calculator.pos
        self.avg_price = self.position_calculator.avg_price

        if order.status == Status.ALLTRADED:
            if order.vt_orderid in (self.long_orders + self.short_orders):

                if order.vt_orderid in self.long_orders:
                    self.long_orders.remove(order.vt_orderid)

                if order.vt_orderid in self.short_orders:
                    self.short_orders.remove(order.vt_orderid)

                self.cancel_all()
                print(f"订单买卖单完全成交, 先撤销所有订单")

                self.last_filled_order = order

                if abs(self.position_calculator.pos) < self.trading_size:
                    print("仓位为零, 需要重新开始.")
                    return

                # tick 存在且仓位数量还没有达到设置的最大值.
                if self.tick and abs(self.position_calculator.pos) < self.max_pos * self.trading_size:
                    buy_step = self.get_step()
                    sell_step = self.get_step()

                    # 解决步长的问题.
                    buy_price = order.price - buy_step * self.grid_step
                    sell_price = order.price + sell_step * self.grid_step

                    buy_price = min(self.tick.bid_price_1 * (1 - 0.0001), buy_price)
                    sell_price = max(self.tick.ask_price_1 * (1 + 0.0001), sell_price)  # BUSD

                    long_ids = self.buy(buy_price, self.trading_size)
                    short_ids = self.sell(sell_price, self.trading_size)

                    self.long_orders.extend(long_ids)
                    self.short_orders.extend(short_ids)

                    print(
                        f"订单完全成交, 分别下双边网格: LONG: {self.long_orders}:{buy_price}, SHORT: {self.short_orders}:{sell_price}")

            elif order.vt_orderid in self.profit_orders:
                self.profit_orders.remove(order.vt_orderid)
                if abs(self.position_calculator.pos) < self.trading_size:
                    self.cancel_all()
                    print(f"止盈单子成交,且仓位为零, 先撤销所有订单,然后重新开始")

            elif order.vt_orderid in self.stop_orders:
                self.stop_orders.remove(order.vt_orderid)
                if abs(self.position_calculator.pos) < self.trading_size:
                    self.trigger_stop_loss = True
                    self.cancel_all()

                    print("止损单子成交,且仓位为零, 先撤销所有订单,然后重新开始")

        if not order.is_active():
            if order.vt_orderid in self.long_orders:
                self.long_orders.remove(order.vt_orderid)

            elif order.vt_orderid in self.short_orders:
                self.short_orders.remove(order.vt_orderid)

            elif order.vt_orderid in self.profit_orders:
                self.profit_orders.remove(order.vt_orderid)

            elif order.vt_orderid in self.stop_orders:
                self.stop_orders.remove(order.vt_orderid)

        self.put_event()