Beispiel #1
0
    def on_query_order(self, data, request):
        """"""
        if self.check_error(data, "查询活动委托"):
            return

        for d in data["data"]["orders"]:
            timestamp = d["created_at"]
            dt = datetime.fromtimestamp(timestamp / 1000)
            time = dt.strftime("%H:%M:%S")

            if d["client_order_id"]:
                orderid = d["client_order_id"]
            else:
                orderid = d["order_id"]

            order = OrderData(
                orderid=orderid,
                symbol=d["contract_code"],
                exchange=Exchange.HUOBI,
                price=d["price"],
                volume=d["volume"],
                type=ORDERTYPE_HBDM2VT[d["order_price_type"]],
                direction=DIRECTION_HBDM2VT[d["direction"]],
                offset=OFFSET_HBDM2VT[d["offset"]],
                traded=d["trade_volume"],
                status=STATUS_HBDM2VT[d["status"]],
                time=time,
                gateway_name=self.gateway_name,
            )
            self.gateway.on_order(order)

        self.gateway.write_log(f"{request.extra}活动委托信息查询成功")
Beispiel #2
0
    def on_order(self, d):
        """"""
        order = OrderData(
            symbol=d["instrument_id"],
            exchange=Exchange.OKEX,
            type=ORDERTYPE_OKEX2VT[d["type"]],
            orderid=d["client_oid"],
            direction=DIRECTION_OKEX2VT[d["side"]],
            price=float(d["price"]),
            volume=float(d["size"]),
            traded=float(d["filled_size"]),
            time=d["timestamp"][11:19],
            status=STATUS_OKEX2VT[d["status"]],
            gateway_name=self.gateway_name,
        )
        self.gateway.on_order(copy(order))

        trade_volume = d.get("last_fill_qty", 0)
        if not trade_volume or float(trade_volume) == 0:
            return

        self.trade_count += 1
        tradeid = f"{self.connect_time}{self.trade_count}"

        trade = TradeData(symbol=order.symbol,
                          exchange=order.exchange,
                          orderid=order.orderid,
                          tradeid=tradeid,
                          direction=order.direction,
                          price=float(d["last_fill_px"]),
                          volume=float(trade_volume),
                          time=d["last_fill_time"][11:19],
                          gateway_name=self.gateway_name)
        self.gateway.on_trade(trade)
Beispiel #3
0
    def on_query_order(self, data, request):
        """"""
        if self.check_error(data, "查询委托"):
            return

        for d in data["data"]:
            sys_orderid = d["id"]
            local_orderid = self.order_manager.get_local_orderid(sys_orderid)

            direction, order_type = ORDERTYPE_HUOBI2VT[d["type"]]
            dt = datetime.fromtimestamp(d["created-at"] / 1000)
            time = dt.strftime("%H:%M:%S")

            order = OrderData(
                orderid=local_orderid,
                symbol=d["symbol"],
                exchange=Exchange.HUOBI,
                price=float(d["price"]),
                volume=float(d["amount"]),
                type=order_type,
                direction=direction,
                traded=float(d["filled-amount"]),
                status=STATUS_HUOBI2VT.get(d["state"], None),
                time=time,
                gateway_name=self.gateway_name,
            )

            self.order_manager.on_order(order)

        self.gateway.write_log("委托信息查询成功")
Beispiel #4
0
    def update_order(self, order: OrderData) -> None:
        """"""
        if order.is_active():
            self.active_orders[order.vt_orderid] = order
        else:
            if order.vt_orderid in self.active_orders:
                self.active_orders.pop(order.vt_orderid)

        self.calculate_frozen()
Beispiel #5
0
    def update_order(self, order: OrderData):
        """"""
        # print('algo template update_order, order.is_active:%s' % order.is_active())
        if not order.is_active():
            for l in self.leg_orders[order.vt_symbol]:
                if order.vt_orderid == l[0]:
                    self.leg_orders[order.vt_symbol].remove(l)

        self.on_order(order)
Beispiel #6
0
    def update_order(self, order: OrderData):
        """"""
        # print('algo template update_order, order.is_active:%s' % order.is_active())
        if not order.is_active():
            vt_orderids = self.leg_orders[order.vt_symbol]
            if order.vt_orderid in vt_orderids:
                vt_orderids.remove(order.vt_orderid)

            # if order.vt_orderid in self.active_short_orderids:
            #     self.active_short_orderids.remove(order.vt_orderid)
        self.on_order(order)
    def on_order(self, packet: dict):
        """"""
        dt = datetime.fromtimestamp(packet["O"] / 1000)
        time = dt.strftime("%Y-%m-%d %H:%M:%S")
        # print('*'*50)
        # print(packet)
        # print('^'*50)
        if packet["C"] == "null" or packet["C"] == "":
            orderid = packet["c"]
        else:
            orderid = packet["C"]

        order = OrderData(
            symbol=packet["s"],
            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"]],
            time=time,
            gateway_name=self.gateway_name
        )

        self.gateway.on_order(order)
        msg = f"Websocket get order response: {order.vt_orderid}, status: {order.status}, packet status: {packet['X']}"
        self.gateway.write_log(msg, level=DEBUG)
        # Push trade event
        trade_volume = float(packet["l"])
        if not trade_volume:
            return

        trade_dt = datetime.fromtimestamp(packet["T"] / 1000)
        trade_time = trade_dt.strftime("%Y-%m-%d %H:%M:%S")

        trade = TradeData(
            symbol=order.symbol,
            exchange=order.exchange,
            orderid=order.orderid,
            tradeid=packet["t"],
            direction=order.direction,
            price=float(packet["L"]),
            volume=trade_volume,
            time=trade_time,
            gateway_name=self.gateway_name,
        )
        self.gateway.on_trade(trade)
Beispiel #8
0
    def on_order(self, data: dict):
        """"""
        dt = datetime.fromtimestamp(data["created_at"] / 1000)
        time = dt.strftime("%H:%M:%S")

        if data["client_order_id"]:
            orderid = data["client_order_id"]
        else:
            orderid = data["order_id"]

        order = OrderData(symbol=data["contract_code"],
                          exchange=Exchange.HUOBI,
                          orderid=orderid,
                          type=ORDERTYPE_HBDM2VT[data["order_price_type"]],
                          direction=DIRECTION_HBDM2VT[data["direction"]],
                          offset=OFFSET_HBDM2VT[data["offset"]],
                          price=data["price"],
                          volume=data["volume"],
                          traded=data["trade_volume"],
                          status=STATUS_HBDM2VT[data["status"]],
                          time=time,
                          gateway_name=self.gateway_name)
        self.gateway.on_order(order)

        # Push trade event
        trades = data["trade"]
        if not trades:
            return

        for d in trades:
            dt = datetime.fromtimestamp(d["created_at"] / 1000)
            time = dt.strftime("%H:%M:%S")

            trade = TradeData(
                symbol=order.symbol,
                exchange=Exchange.HUOBI,
                orderid=order.orderid,
                tradeid=str(d["trade_id"]),
                direction=order.direction,
                offset=order.offset,
                price=d["trade_price"],
                volume=d["trade_volume"],
                time=time,
                gateway_name=self.gateway_name,
            )
            self.gateway.on_trade(trade)
Beispiel #9
0
    def on_query_order(self, data, request):
        """"""
        for order_data in data:
            order = OrderData(
                symbol=order_data["instrument_id"],
                exchange=Exchange.OKEX,
                type=ORDERTYPE_OKEX2VT[order_data["type"]],
                orderid=order_data["client_oid"],
                direction=DIRECTION_OKEX2VT[order_data["side"]],
                price=float(order_data["price"]),
                volume=float(order_data["size"]),
                traded=float(order_data["filled_size"]),
                time=order_data["timestamp"][11:19],
                status=STATUS_OKEX2VT[order_data["status"]],
                gateway_name=self.gateway_name,
            )
            self.gateway.on_order(order)

        self.gateway.write_log("委托信息查询成功")
Beispiel #10
0
def _parse_order_info(order_info, gateway_name: str):
    offset, direction = TYPE_OKEXS2VT[order_info["type"]]
    order_id = order_info["client_oid"]
    if not order_id:
        order_id = order_info["order_id"]
    order = OrderData(
        symbol=order_info["instrument_id"],
        exchange=Exchange.OKEX,
        type=ORDERTYPE_OKEXS2VT[order_info["order_type"]],
        orderid=order_id,
        direction=direction,
        offset=offset,
        traded=int(order_info["filled_qty"]),
        price=float(order_info["price"]),
        volume=float(order_info["size"]),
        time=_parse_timestamp(order_info["timestamp"]).strftime("%H:%M:%S"),
        status=STATUS_OKEXS2VT[order_info["status"]],
        gateway_name=gateway_name,
    )
    return order
Beispiel #11
0
 def on_query_order(self, data, request):
     """"""
     for order_data in data["order_info"]:
         offset, direction = TYPE_OKEXF2VT[order_data["type"]]
         order = OrderData(
             symbol=order_data["instrument_id"],
             exchange=Exchange.OKEX,
             type=ORDERTYPE_OKEXF2VT[order_data["order_type"]],
             orderid=order_data["client_oid"],
             direction=direction,
             offset=offset,
             traded=int(order_data["filled_qty"]),
             price=float(order_data["price"]),
             volume=float(order_data["size"]),
             time=utc_to_local(
                 order_data["timestamp"]).strftime("%H:%M:%S"),
             status=STATUS_OKEXF2VT[order_data["status"]],
             gateway_name=self.gateway_name,
         )
         self.gateway.on_order(order)
    def on_query_order(self, data, request):
        """"""
        for d in data:
            dt = datetime.fromtimestamp(d["time"] / 1000)
            time = dt.strftime("%Y-%m-%d %H:%M:%S")

            order = OrderData(
                orderid=d["clientOrderId"],
                symbol=d["symbol"],
                exchange=Exchange.BINANCEFUTURES,
                price=float(d["price"]),
                volume=float(d["origQty"]),
                type=ORDERTYPE_BINANCE2VT[d["type"]],
                direction=DIRECTION_BINANCE2VT[d["side"]],
                traded=float(d["executedQty"]),
                status=STATUS_BINANCE2VT.get(d["status"], None),
                time=time,
                gateway_name=self.gateway_name,
            )
            self.gateway.on_order(order)

        self.gateway.write_log("FUTURES委托信息查询成功")
Beispiel #13
0
    def on_order(self, d):
        """"""
        offset, direction = TYPE_OKEXF2VT[d["type"]]
        order = OrderData(
            symbol=d["instrument_id"],
            exchange=Exchange.OKEX,
            type=ORDERTYPE_OKEXF2VT[d["order_type"]],
            orderid=d["client_oid"],
            offset=offset,
            direction=direction,
            price=float(d["price"]),
            volume=float(d["size"]),
            traded=float(d["filled_qty"]),
            time=utc_to_local(d["timestamp"]).strftime("%H:%M:%S"),
            status=STATUS_OKEXF2VT[d["status"]],
            gateway_name=self.gateway_name,
        )
        self.gateway.on_order(copy(order))

        trade_volume = d.get("last_fill_qty", 0)
        if not trade_volume or float(trade_volume) == 0:
            return

        self.trade_count += 1
        tradeid = f"{self.connect_time}{self.trade_count}"

        trade = TradeData(
            symbol=order.symbol,
            exchange=order.exchange,
            orderid=order.orderid,
            tradeid=tradeid,
            direction=order.direction,
            offset=order.offset,
            price=float(d["last_fill_px"]),
            volume=float(trade_volume),
            time=order.time,
            gateway_name=self.gateway_name,
        )
        self.gateway.on_trade(trade)