Exemplo n.º 1
0
    def send_order(self, req: OrderRequest):
        """"""
        orderid = "x-A6SIDXVS" + str(self.connect_time + self._new_order_id())
        order = req.create_order_data(orderid, self.gateway_name)
        self.gateway.on_order(order)

        data = {"security": Security.SIGNED}

        params = {
            "symbol": req.symbol.upper(),
            "timeInForce": "GTC",
            "side": DIRECTION_VT2BINANCE[req.direction],
            "type": ORDERTYPE_VT2BINANCE[req.type],
            "price": str(req.price),
            "quantity": str(req.volume),
            "newClientOrderId": orderid,
            "newOrderRespType": "ACK"
        }

        self.add_request(method="POST",
                         path="/api/v3/order",
                         callback=self.on_send_order,
                         data=data,
                         params=params,
                         extra=order,
                         on_error=self.on_send_order_error,
                         on_failed=self.on_send_order_failed)

        return order.vt_orderid
Exemplo n.º 2
0
    def send_order(self, req: OrderRequest):
        """"""
        orderid = f"a{self.connect_time}{self._new_order_id()}"

        data = {
            "client_oid": orderid,
            "type": ORDERTYPE_VT2OKEX[req.type],
            "side": DIRECTION_VT2OKEX[req.direction],
            "instrument_id": req.symbol
        }

        if req.type == OrderType.MARKET:
            if req.direction == Direction.LONG:
                data["notional"] = req.volume
            else:
                data["size"] = req.volume
        else:
            data["price"] = req.price
            data["size"] = req.volume

        order = req.create_order_data(orderid, self.gateway_name)

        self.add_request(
            "POST",
            "/api/spot/v3/orders",
            callback=self.on_send_order,
            data=data,
            extra=order,
            on_failed=self.on_send_order_failed,
            on_error=self.on_send_order_error,
        )

        self.gateway.on_order(order)
        return order.vt_orderid
Exemplo n.º 3
0
    def send_order(self, req: OrderRequest) -> str:
        """"""
        local_orderid = self.new_local_orderid()
        order = req.create_order_data(local_orderid, self.gateway_name)
        order.datetime = datetime.now(CHINA_TZ)

        data = {
            "contract_code": req.symbol,
            "client_order_id": int(local_orderid),
            "price": req.price,
            "volume": int(req.volume),
            "direction": DIRECTION_VT2HUOBIS.get(req.direction, ""),
            "offset": OFFSET_VT2HUOBIS.get(req.offset, ""),
            "order_price_type": ORDERTYPE_VT2HUOBIS.get(req.type, ""),
            "lever_rate": 20
        }

        self.add_request(method="POST",
                         path="/swap-api/v1/swap_order",
                         callback=self.on_send_order,
                         data=data,
                         extra=order,
                         on_error=self.on_send_order_error,
                         on_failed=self.on_send_order_failed)

        self.gateway.on_order(order)
        return order.vt_orderid
Exemplo n.º 4
0
    def send_order(self, req: OrderRequest):
        """"""
        # Need both offset and direction for sending order.
        if (req.offset, req.direction) not in TYPE_VT2OKEXS:
            return ""

        orderid = f"a{self.connect_time}{self._new_order_id()}"

        data = {
            "client_oid": orderid,
            "type": TYPE_VT2OKEXS[(req.offset, req.direction)],
            "instrument_id": req.symbol,
            "price": str(req.price),
            "size": str(int(req.volume)),
        }

        if req.type == OrderType.MARKET:
            data["match_price"] = "1"
        else:
            data["match_price"] = "0"

        order = req.create_order_data(orderid, self.gateway_name)

        self.add_request(
            "POST",
            "/api/swap/v3/order",
            callback=self.on_send_order,
            data=data,
            extra=order,
            on_failed=self.on_send_order_failed,
            on_error=self.on_send_order_error,
        )

        self.gateway.on_order(order)
        return order.vt_orderid
Exemplo n.º 5
0
    def send_order(self, req: OrderRequest) -> str:
        """"""
        huobi_type = ORDERTYPE_VT2HUOBI.get((req.direction, req.type), "")

        orderid = self.new_orderid()
        order = req.create_order_data(orderid, self.gateway_name)
        order.datetime = datetime.now(CHINA_TZ)

        data = {
            "account-id": self.account_id,
            "amount": str(req.volume),
            "symbol": req.symbol,
            "type": huobi_type,
            "price": str(req.price),
            "source": "api",
            "client-order-id": orderid
        }

        self.add_request(method="POST",
                         path="/v1/order/orders/place",
                         callback=self.on_send_order,
                         data=data,
                         extra=order,
                         on_error=self.on_send_order_error,
                         on_failed=self.on_send_order_failed)

        self.gateway.on_order(order)
        return order.vt_orderid
Exemplo n.º 6
0
    def send_order(self, req: OrderRequest) -> str:
        """"""
        orderid = "x-cLbi5uMH" + str(self.connect_time + self._new_order_id())
        order = req.create_order_data(
            orderid,
            self.gateway_name
        )
        self.gateway.on_order(order)

        data = {
            "security": Security.SIGNED
        }

        order_type, time_condition = ORDERTYPE_VT2BINANCES[req.type]

        params = {
            "symbol": req.symbol,
            "side": DIRECTION_VT2BINANCES[req.direction],
            "type": order_type,
            "timeInForce": "GTX",
            "price": float(req.price),
            "quantity": float(req.volume),
            "newClientOrderId": orderid,
        }

        if req.offset == Offset.CLOSE:
            params["reduceOnly"] = True

        if self.usdt_base:
            path = "/fapi/v1/order"
        else:
            path = "/dapi/v1/order"

        self.add_request(
            method="POST",
            path=path,
            callback=self.on_send_order,
            data=data,
            params=params,
            extra=order,
            on_error=self.on_send_order_error,
            on_failed=self.on_send_order_failed
        )

        return order.vt_orderid
Exemplo n.º 7
0
    def send_order(self, req: OrderRequest, gateway_name: str) -> str:
        """"""
        contract: ContractData = self.main_engine.get_contract(req.vt_symbol)
        if not contract:
            self.write_log(f"委托失败,找不到该合约{req.vt_symbol}")
            return ""

        self.order_count += 1
        now = datetime.now().strftime("%y%m%d%H%M%S")
        orderid = now + str(self.order_count)
        vt_orderid = f"{GATEWAY_NAME}.{orderid}"

        # Put simulated order update event from gateway
        order = req.create_order_data(orderid, GATEWAY_NAME)
        self.put_event(EVENT_ORDER, copy(order))

        # Check if order is valid
        updated_position = self.check_order_valid(order, contract)

        # Put simulated order update event from exchange
        if order.status != Status.REJECTED:
            order.datetime = datetime.now(LOCAL_TZ)
            order.status = Status.NOTTRADED
            active_orders = self.active_orders.setdefault(order.vt_symbol, {})
            active_orders[orderid] = order

        self.put_event(EVENT_ORDER, copy(order))

        # Update position frozen for close order
        if updated_position:
            self.put_event(EVENT_POSITION, copy(updated_position))

        # Cross order immediately with last tick data
        if self.instant_trade:
            tick = self.ticks.get(order.vt_symbol, None)
            if tick:
                self.cross_order(order, tick)

                if not order.is_active():
                    active_orders = self.active_orders[order.vt_symbol]
                    active_orders.pop(orderid)

        return vt_orderid
Exemplo n.º 8
0
    def send_order(self, req: OrderRequest) -> str:
        """"""
        # Need both offset and direction for sending order.

        orderid = f"a{self.connect_time}{self._new_order_id()}"

        if req.direction == Direction.LONG:
            side = "buy"
        else:
            side = "sell"

        data = {
            "client_oid": orderid,
            "instrument_id": req.symbol,
            "price": str(req.price),
            "size": str(int(req.volume)),
            "side": side,
        }

        if req.type == OrderType.MARKET:
            data["match_price"] = "1"
        else:
            data["match_price"] = "0"

        order = req.create_order_data(orderid, self.gateway_name)

        self.add_request(
            "POST",
            "/api/option/v3/order",
            callback=self.on_send_order,
            data=data,
            extra=order,
            on_failed=self.on_send_order_failed,
            on_error=self.on_send_order_error,
        )

        self.gateway.on_order(order)
        return order.vt_orderid
Exemplo n.º 9
0
    def send_order(self, req: OrderRequest):
        """"""
        local_orderid = self.order_manager.new_local_orderid()
        order = req.create_order_data(
            local_orderid,
            self.gateway_name
        )
        order.datetime = datetime.now(UTC_TZ)

        if req.direction == Direction.SHORT:
            volume = -int(req.volume)
        else:
            volume = int(req.volume)

        request_body = {
            "contract": req.symbol,
            "size": volume,
            "price": str(req.price),
            "close": False,
            "tif": "gtc",
            "text": f"t-{local_orderid}"
        }

        data = json.dumps(request_body)

        self.add_request(
            method="POST",
            path="/api/v4/futures/usdt/orders",
            callback=self.on_send_order,
            data=data,
            extra=order,
            on_error=self.on_send_order_error,
            on_failed=self.on_send_order_failed
        )

        self.order_manager.on_order(order)
        return order.vt_orderid