Пример #1
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
    def send_order(self, req: OrderRequest):
        """"""
        orderid = 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,
            "timeInForce": "IOC",
            "side": DIRECTION_VT2BINANCE[req.direction],
            "type": ORDERTYPE_VT2BINANCE[req.type],
            "price": str(req.price),
            "quantity": str(req.volume),
            "newClientOrderId": orderid,
        }

        self.add_request(method="POST",
                         path="/fapi/v1/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)
        msg = f'Futures Gateway send order:{order.vt_orderid}'
        self.gateway.write_log(msg, level=DEBUG)
        return order.vt_orderid
Пример #3
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
Пример #4
0
    def send_order(self, req: OrderRequest):
        """"""
        local_orderid = self.new_local_orderid()
        order = req.create_order_data(local_orderid, self.gateway_name)
        order.time = datetime.now().strftime("%H:%M:%S")

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

        self.add_request(method="POST",
                         path="/api/v1/contract_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
Пример #5
0
    def send_order(self, req: OrderRequest):
        """"""
        huobi_type = ORDERTYPE_VT2HUOBI.get((req.direction, req.type), "")

        local_orderid = self.order_manager.new_local_orderid()
        order = req.create_order_data(local_orderid, self.gateway_name)
        order.time = datetime.now().strftime("%H:%M:%S")

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

        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.order_manager.on_order(order)
        return order.vt_orderid
Пример #6
0
    def update_order_request(self, req: OrderRequest, vt_orderid: str) -> None:
        """"""
        gateway_name, orderid = vt_orderid.split(".")

        order = req.create_order_data(orderid, gateway_name)
        self.update_order(order)
Пример #7
0
    def send_order(self,
                   algo: SpreadAlgoTemplate,
                   vt_symbol: str,
                   price: float,
                   volume: float,
                   direction: Direction,
                   borrowmoney=False,
                   lock: bool = False):
        """"""
        holding = self.offset_converter.get_position_holding(vt_symbol)

        contract = self.get_contract(vt_symbol)

        if direction == Direction.LONG:
            available = holding.short_pos - holding.short_pos_frozen
        else:
            available = holding.long_pos - holding.long_pos_frozen

        # If no position to close, just open new
        if not available:
            offset = Offset.OPEN
        # If enougth position to close, just close old
        elif volume < available:
            offset = Offset.CLOSE
        # Otherwise, just close existing position
        else:
            volume = available
            offset = Offset.CLOSE

        original_req = OrderRequest(symbol=contract.symbol,
                                    exchange=contract.exchange,
                                    direction=direction,
                                    offset=offset,
                                    type=OrderType.LIMIT,
                                    price=price,
                                    volume=volume,
                                    borrowmoney=borrowmoney)

        # Convert with offset converter
        req_list = self.offset_converter.convert_order_request(
            original_req, lock)

        # Send Orders
        vt_orderids = []

        for req in req_list:
            vt_orderid = self.spread_engine.send_order(req,
                                                       contract.gateway_name)

            # Check if sending order successful
            if not vt_orderid:
                continue

            vt_orderids.append(vt_orderid)

            self.offset_converter.update_order_request(req, vt_orderid)
            # Save relationship between orderid and algo.
            self.order_algo_map[vt_orderid] = algo
            self.write_log(
                f'send_order vt_orderid: {vt_orderid}, event_engine size: {self.event_engine.get_qsize()}',
                level=INFO)
            # print('%s algo engine send_order vt_orderid:%s,price: %s' % (algo.algoid, vt_orderid, req.price))

        return vt_orderids