Пример #1
0
    def cross_limit_order(self) -> None:
        """
        Cross limit order with last bar/tick data.
        """
        for order in list(self.active_limit_orders.values()):
            bar = self.bars[order.vt_symbol]

            long_cross_price = bar.low_price
            short_cross_price = bar.high_price
            long_best_price = bar.open_price
            short_best_price = bar.open_price

            # Push order update with status "not traded" (pending).
            if order.status == Status.SUBMITTING:
                order.status = Status.NOTTRADED
                self.strategy.update_order(order)

            # Check whether limit orders can be filled.
            long_cross = (order.direction == Direction.LONG
                          and order.price >= long_cross_price
                          and long_cross_price > 0)

            short_cross = (order.direction == Direction.SHORT
                           and order.price <= short_cross_price
                           and short_cross_price > 0)

            if not long_cross and not short_cross:
                continue

            # Push order update with status "all traded" (filled).
            order.traded = order.volume
            order.status = Status.ALLTRADED
            self.strategy.update_order(order)

            self.active_limit_orders.pop(order.vt_orderid)

            # Push trade update
            self.trade_count += 1

            if long_cross:
                trade_price = min(order.price, long_best_price)
            else:
                trade_price = max(order.price, short_best_price)

            trade = TradeData(
                symbol=order.symbol,
                exchange=order.exchange,
                orderid=order.orderid,
                tradeid=str(self.trade_count),
                direction=order.direction,
                offset=order.offset,
                price=trade_price,
                volume=order.volume,
                datetime=self.datetime,
                gateway_name=self.gateway_name,
            )

            self.strategy.update_trade(trade)
            self.trades[trade.vt_tradeid] = trade
Пример #2
0
    def cross_order(self, order: OrderData, tick: TickData):
        """"""
        contract = self.main_engine.get_contract(order.vt_symbol)

        trade_price = 0

        # Cross market order immediately after received
        if order.type == OrderType.MARKET:
            if order.direction == Direction.LONG:
                trade_price = tick.ask_price_1 + self.trade_slippage * contract.pricetick
            else:
                trade_price = tick.bid_price_1 - self.trade_slippage * contract.pricetick
        # Cross limit order only if price touched
        elif order.type == OrderType.LIMIT:
            if order.direction == Direction.LONG:
                if order.price >= tick.ask_price_1:
                    trade_price = tick.ask_price_1
            else:
                if order.price <= tick.bid_price_1:
                    trade_price = tick.bid_price_1
        # Cross limit order only if price broken
        elif order.type == OrderType.STOP:
            if order.direction == Direction.LONG:
                if tick.ask_price_1 >= order.price:
                    trade_price = tick.ask_price_1 + self.trade_slippage * contract.pricetick
            else:
                if tick.bid_price_1 <= order.price:
                    trade_price = tick.bid_price_1 - self.trade_slippage * contract.pricetick

        if trade_price:
            order.status = Status.ALLTRADED
            order.traded = order.volume
            self.put_event(EVENT_ORDER, order)

            trade = TradeData(symbol=order.symbol,
                              exchange=order.exchange,
                              orderid=order.orderid,
                              tradeid=order.orderid,
                              direction=order.direction,
                              offset=order.offset,
                              price=trade_price,
                              volume=order.volume,
                              datetime=datetime.now(LOCAL_TZ),
                              gateway_name=order.gateway_name)
            self.put_event(EVENT_TRADE, trade)

            self.update_position(trade, contract)
Пример #3
0
    def on_order(self, packet: dict) -> None:
        """"""
        ord_data = packet["o"]
        key = (ord_data["o"], ord_data["f"])
        order_type = ORDERTYPE_BINANCES2VT.get(key, None)
        if not order_type:
            return

        order = OrderData(
            symbol=ord_data["s"],
            exchange=Exchange.BINANCE,
            orderid=str(ord_data["c"]),
            type=order_type,
            direction=DIRECTION_BINANCES2VT[ord_data["S"]],
            price=float(ord_data["p"]),
            volume=float(ord_data["q"]),
            traded=float(ord_data["z"]),
            status=STATUS_BINANCES2VT[ord_data["X"]],
            datetime=generate_datetime(packet["E"]),
            gateway_name=self.gateway_name
        )

        # Push trade event
        trade_volume = float(ord_data["l"])
        if trade_volume:
            trade_data = TradeData(
                symbol=order.symbol,
                exchange=order.exchange,
                orderid=order.orderid,
                tradeid=ord_data["t"],
                direction=order.direction,
                price=float(ord_data["L"]),
                volume=trade_volume,
                datetime=generate_datetime(ord_data["T"]),
                gateway_name=self.gateway_name,
            )
            order.trade_data = trade_data

        else:
            order.trade_data = None

        self.gateway.on_order(order)
Пример #4
0
    def on_order(self, packet: dict):
        """"""
        if packet["C"] == "":
            orderid = packet["c"]
        else:
            orderid = packet["C"]

        order = OrderData(symbol=packet["s"].lower(),
                          exchange=Exchange.BINANCE,
                          orderid=orderid,
                          type=ORDERTYPE_BINANCE2VT[packet["o"]],
                          direction=DIRECTION_BINANCE2VT[packet["S"]],
                          price=float(packet["p"]),
                          volume=float(packet["q"]),
                          traded=float(packet["z"]),
                          status=STATUS_BINANCE2VT[packet["X"]],
                          datetime=generate_datetime(packet["O"]),
                          gateway_name=self.gateway_name)

        # Push trade event
        trade_volume = float(packet["l"])
        if trade_volume:
            trade_data = TradeData(
                symbol=order.symbol,
                exchange=order.exchange,
                orderid=order.orderid,
                tradeid=packet["t"],
                direction=order.direction,
                price=float(packet["L"]),
                volume=trade_volume,
                datetime=generate_datetime(packet["T"]),
                gateway_name=self.gateway_name,
            )

            order.trade_data = trade_data
        else:
            order.trade_data = None

        self.gateway.on_order(order)
Пример #5
0
    def cross_stop_order(self):
        """
        Cross stop order with last bar/tick data.
        """
        if self.mode == BacktestingMode.BAR:
            long_cross_price = self.bar.high_price
            short_cross_price = self.bar.low_price
            long_best_price = self.bar.open_price
            short_best_price = self.bar.open_price
        else:
            long_cross_price = self.tick.last_price
            short_cross_price = self.tick.last_price
            long_best_price = long_cross_price
            short_best_price = short_cross_price

        for stop_order in list(self.active_stop_orders.values()):
            # Check whether stop order can be triggered.
            long_cross = (stop_order.direction == Direction.LONG
                          and stop_order.price <= long_cross_price)

            short_cross = (stop_order.direction == Direction.SHORT
                           and stop_order.price >= short_cross_price)

            if not long_cross and not short_cross:
                continue
            '''
            # # Create order data.
            # self.limit_order_count += 1

            # order = OrderData(
            #     symbol=self.symbol,
            #     exchange=self.exchange,
            #     orderid=str(self.limit_order_count),
            #     direction=stop_order.direction,
            #     offset=stop_order.offset,
            #     price=stop_order.price,
            #     volume=stop_order.volume,
            #     traded=stop_order.volume,
            #     status=Status.ALLTRADED,
            #     gateway_name=self.gateway_name,
            #     datetime=self.datetime
            # )

            # self.limit_orders[order.vt_orderid] = order

            # # Create trade data.
            # if long_cross:
            #     trade_price = max(stop_order.price, long_best_price)
            #     pos_change = order.volume
            # else:
            #     trade_price = min(stop_order.price, short_best_price)
            #     pos_change = -order.volume

            # self.trade_count += 1

            # trade = TradeData(
            #     symbol=order.symbol,
            #     exchange=order.exchange,
            #     orderid=order.orderid,
            #     tradeid=str(self.trade_count),
            #     direction=order.direction,
            #     offset=order.offset,
            #     price=trade_price,
            #     volume=order.volume,
            #     datetime=self.datetime,
            #     gateway_name=self.gateway_name,
            # )

            # self.trades[trade.vt_tradeid] = trade

            # # Update stop order.
            # stop_order.vt_orderids.append(order.vt_orderid)
            # stop_order.status = StopOrderStatus.TRIGGERED

            # if stop_order.stop_orderid in self.active_stop_orders:
            #     self.active_stop_orders.pop(stop_order.stop_orderid)

            # # Push update to strategy.
            # self.strategy.on_stop_order(stop_order)
            # self.strategy.on_order(order)

            # self.strategy.pos += pos_change
            # self.strategy.on_trade(trade)
            
            # Create order data.
            '''

            # self.limit_order_count += 1

            # order = OrderData(
            #     symbol=self.symbol,
            #     exchange=self.exchange,
            #     orderid=str(self.limit_order_count),
            #     direction=stop_order.direction,
            #     offset=stop_order.offset,
            #     price=stop_order.price,
            #     volume=stop_order.volume,
            #     traded=stop_order.volume,
            #     status=Status.ALLTRADED,
            #     gateway_name=self.gateway_name,
            #     datetime=self.datetime
            # )

            # self.limit_orders[order.vt_orderid] = order

            # Create trade data.
            if long_cross:
                trade_price = max(stop_order.price, long_best_price)
                pos_change = stop_order.volume
            else:
                trade_price = min(stop_order.price, short_best_price)
                pos_change = -stop_order.volume

            self.trade_count += 1

            trade = TradeData(
                symbol=self.symbol,
                exchange=self.exchange,
                orderid=stop_order.stop_orderid,
                tradeid=str(self.trade_count),
                direction=stop_order.direction,
                offset=stop_order.offset,
                price=trade_price,
                volume=stop_order.volume,
                datetime=self.datetime,
                gateway_name=self.gateway_name,
            )

            self.trades[trade.vt_tradeid] = trade

            # Update stop order.
            #stop_order.vt_orderids.append(order.vt_orderid)
            stop_order.status = StopOrderStatus.TRIGGERED

            if stop_order.stop_orderid in self.active_stop_orders:
                self.active_stop_orders.pop(stop_order.stop_orderid)

            # Push update to strategy.
            self.strategy.on_stop_order(stop_order)
            self.strategy.on_order(stop_order)

            self.strategy.pos += pos_change
            self.strategy.on_trade(trade)
Пример #6
0
    def cross_order(self):
        """
        Cross order with last bar/tick data.
        """

        for order in list(self.active_orders.values()):
            # Push order update with status "not traded" (pending).
            if order.status == Status.SUBMITTING:
                order.status = Status.NOTTRADED
                self.strategy.on_order(order)

            if order.type == OrderType.LIMIT:
                if self.mode == BacktestingMode.BAR:
                    long_cross_price = self.bar.low_price
                    short_cross_price = self.bar.high_price
                    long_best_price = self.bar.open_price
                    short_best_price = self.bar.open_price
                else:
                    long_cross_price = self.tick.ask_price_1
                    short_cross_price = self.tick.bid_price_1
                    long_best_price = long_cross_price
                    short_best_price = short_cross_price
                # Check whether limit orders can be filled.
                long_cross = (order.direction == Direction.LONG
                              and order.price >= long_cross_price
                              and long_cross_price > 0)

                short_cross = (order.direction == Direction.SHORT
                               and order.price <= short_cross_price
                               and short_cross_price > 0)
            elif order.type == OrderType.STOP:
                if self.mode == BacktestingMode.BAR:
                    long_cross_price = self.bar.high_price
                    short_cross_price = self.bar.low_price
                    long_best_price = self.bar.open_price
                    short_best_price = self.bar.open_price
                else:
                    long_cross_price = self.tick.last_price
                    short_cross_price = self.tick.last_price
                    long_best_price = long_cross_price
                    short_best_price = short_cross_price
                # Check whether stop order can be triggered.
                long_cross = (order.direction == Direction.LONG
                              and order.price <= long_cross_price)

                short_cross = (order.direction == Direction.SHORT
                               and order.price >= short_cross_price)

            if not long_cross and not short_cross:
                continue

            # Push order udpate with status "all traded" (filled).
            order.traded = order.volume
            order.status = Status.ALLTRADED
            self.strategy.on_order(order)

            self.active_orders.pop(order.vt_orderid)

            # Push trade update
            self.trade_count += 1

            if long_cross:
                if order.type == OrderType.LIMIT:
                    trade_price = min(order.price, long_best_price)
                    pos_change = order.volume
                elif order.type == OrderType.STOP:
                    trade_price = max(order.price, long_best_price)
                    pos_change = order.volume
            else:
                if order.type == OrderType.LIMIT:
                    trade_price = max(order.price, short_best_price)
                    pos_change = -order.volume
                elif order.type == OrderType.STOP:
                    trade_price = min(order.price, short_best_price)
                    pos_change = -order.volume

            trade = TradeData(
                symbol=order.symbol,
                exchange=order.exchange,
                orderid=order.orderid,
                tradeid=str(self.trade_count),
                direction=order.direction,
                offset=order.offset,
                price=trade_price,
                volume=order.volume,
                datetime=self.datetime,
                gateway_name=self.gateway_name,
            )

            self.strategy.pos += pos_change
            self.strategy.on_trade(trade)
            self.trades[trade.vt_tradeid] = trade
Пример #7
0
    def cross_algo(self):
        """
        Cross limit order with last bar/tick data.
        """
        if self.mode == BacktestingMode.BAR:
            long_cross_price = self.bar.close_price
            short_cross_price = self.bar.close_price
        else:
            long_cross_price = self.tick.ask_price_1
            short_cross_price = self.tick.bid_price_1

        for algo in list(self.active_algos.values()):
            # Check whether limit orders can be filled.
            long_cross = (algo.direction == Direction.LONG
                          and algo.price >= long_cross_price)

            short_cross = (algo.direction == Direction.SHORT
                           and algo.price <= short_cross_price)

            if not long_cross and not short_cross:
                continue

            # Push order udpate with status "all traded" (filled).
            algo.traded = algo.volume
            algo.status = Status.ALLTRADED
            self.strategy.update_spread_algo(algo)

            self.active_algos.pop(algo.algoid)

            # Push trade update
            self.trade_count += 1

            if long_cross:
                trade_price = long_cross_price
                pos_change = algo.volume
            else:
                trade_price = short_cross_price
                pos_change = -algo.volume

            trade = TradeData(
                symbol=self.spread.name,
                exchange=Exchange.LOCAL,
                orderid=algo.algoid,
                tradeid=str(self.trade_count),
                direction=algo.direction,
                offset=algo.offset,
                price=trade_price,
                volume=algo.volume,
                datetime=self.datetime,
                gateway_name=self.gateway_name,
            )

            if self.mode == BacktestingMode.BAR:
                trade.value = self.bar.value
            else:
                trade.value = trade_price

            self.spread.net_pos += pos_change
            self.strategy.on_spread_pos()

            self.trades[trade.vt_tradeid] = trade