예제 #1
0
    def send_order(self, req: OrderRequest):
        """
        Send a new order.
        """
        if not self.status:
            return ""

        if req.exchange not in EXCHANGE_VT2IB:
            self.gateway.write_log(f"不支持的交易所:{req.exchange}")
            return ""

        if req.type not in ORDERTYPE_VT2IB:
            self.gateway.write_log(f"不支持的价格类型:{req.type}")
            return ""

        self.orderid += 1

        ib_contract = Contract()
        ib_contract.conId = str(req.symbol)
        ib_contract.exchange = EXCHANGE_VT2IB[req.exchange]

        ib_order = Order()
        ib_order.orderId = self.orderid
        ib_order.clientId = self.clientid
        ib_order.action = DIRECTION_VT2IB[req.direction]
        ib_order.orderType = ORDERTYPE_VT2IB[req.type]
        ib_order.lmtPrice = req.price
        ib_order.totalQuantity = req.volume

        self.client.placeOrder(self.orderid, ib_contract, ib_order)
        self.client.reqIds(1)

        order = req.create_order_data(str(self.orderid), self.gateway_name)
        self.gateway.on_order(order)
        return order.vt_orderid
예제 #2
0
    def send_order(self, req: OrderRequest):
        """"""
        side = DIRECTION_VT2FUTU[req.direction]
        futu_order_type = OrderType.NORMAL  # Only limit order is supported.

        # Set price adjustment mode to inside adjustment.
        if req.direction is Direction.LONG:
            adjust_limit = 0.05
        else:
            adjust_limit = -0.05

        futu_symbol = convert_symbol_vt2futu(req.symbol, req.exchange)
        code, data = self.trade_ctx.place_order(
            req.price,
            req.volume,
            futu_symbol,
            side,
            futu_order_type,
            trd_env=self.env,
            adjust_limit=adjust_limit,
        )

        if code:
            self.write_log(f"委托失败:{data}")
            return ""

        for ix, row in data.iterrows():
            orderid = str(row["order_id"])

        order = req.create_order_data(orderid, self.gateway_name)
        self.on_order(order)
        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) -> str:
        """"""
        if req.exchange not in MARKET_VT2XTP:
            self.gateway.write_log(f"委托失败,不支持的交易所{req.exchange.value}")
            return ""

        if req.type not in ORDERTYPE_VT2XTP:
            self.gateway.write_log(f"委托失败,不支持的委托类型{req.type.value}")
            return ""

        xtp_req = XTPOrderInsertInfo()
        xtp_req.ticker = req.symbol
        xtp_req.market = MARKET_VT2XTP[req.exchange]
        xtp_req.price = req.price
        xtp_req.quantity = int(req.volume)
        xtp_req.side = DIRECTION_VT2XTP[req.direction]
        xtp_req.price_type = ORDERTYPE_VT2XTP[req.type]
        xtp_req.business_type = XTP_BUSINESS_TYPE.XTP_BUSINESS_TYPE_CASH

        orderid = self.api.InsertOrder(xtp_req, self.session_id)

        order = req.create_order_data(str(orderid), self.gateway_name)
        self.gateway.on_order(order)

        return order.vt_orderid
예제 #5
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
예제 #6
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.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
예제 #7
0
 def send_order(self, req: OrderRequest):
     """
     Send new order.
     """
     self.order_ref += 1
     
     ctp_req = {
         "InstrumentID": req.symbol,
         "LimitPrice": req.price,
         "VolumeTotalOriginal": int(req.volume),
         "OrderPriceType": ORDERTYPE_VT2CTP.get(req.type, ""),
         "Direction": DIRECTION_VT2CTP.get(req.direction, ""),
         "CombOffsetFlag": OFFSET_VT2CTP.get(req.offset, ""),
         "OrderRef": str(self.order_ref),
         "InvestorID": self.userid,
         "UserID": self.userid,
         "BrokerID": self.brokerid,
         "CombHedgeFlag": THOST_FTDC_HF_Speculation,
         "ContingentCondition": THOST_FTDC_CC_Immediately,
         "ForceCloseReason": THOST_FTDC_FCC_NotForceClose,
         "IsAutoSuspend": 0,
         "TimeCondition": THOST_FTDC_TC_GFD,
         "VolumeCondition": THOST_FTDC_VC_AV,
         "MinVolume": 1
     }
     
     if req.type == OrderType.FAK:
         ctp_req["OrderPriceType"] = THOST_FTDC_OPT_LimitPrice
         ctp_req["TimeCondition"] = THOST_FTDC_TC_IOC
         ctp_req["VolumeCondition"] = THOST_FTDC_VC_AV
     elif req.type == OrderType.FOK:
         ctp_req["OrderPriceType"] = THOST_FTDC_OPT_LimitPrice
         ctp_req["TimeCondition"] = THOST_FTDC_TC_IOC
         ctp_req["VolumeCondition"] = THOST_FTDC_VC_CV            
     
     self.reqid += 1
     self.reqOrderInsert(ctp_req, self.reqid)
     
     orderid = f"{self.frontid}_{self.sessionid}_{self.order_ref}"
     order = req.create_order_data(orderid, self.gateway_name)
     self.gateway.on_order(order)
     
     return order.vt_orderid
예제 #8
0
    def send_order(self, req: OrderRequest):
        """"""
        orderid = str(self.connect_time + self._new_order_id())

        data = {
            "symbol": req.symbol,
            "side": DIRECTION_VT2BITMEX[req.direction],
            "ordType": ORDERTYPE_VT2BITMEX[req.type],
            "orderQty": int(req.volume),
            "clOrdID": orderid,
        }

        inst = []  # Order special instructions

        # Only add price for limit order.
        if req.type == OrderType.LIMIT:
            data["price"] = req.price
        elif req.type == OrderType.STOP:
            data["stopPx"] = req.price
            inst.append("LastPrice")

        # Check for close order
        if req.offset == Offset.CLOSE:
            inst.append("ReduceOnly")

        # Generate execInst
        if inst:
            data["execInst"] = ",".join(inst)

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

        self.add_request(
            "POST",
            "/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
예제 #9
0
    def send_order(self, req: OrderRequest):
        """
        Send new order.
        """
        self.localid += 1
        orderid = str(self.localid).rjust(12, "0")

        femas_req = {
            "InstrumentID": req.symbol,
            "ExchangeID": str(req.exchange).split(".")[1],
            "BrokerID": self.brokerid,
            "InvestorID": self.userid,
            "UserID": self.userid,
            "LimitPrice": req.price,
            "Volume": int(req.volume),
            "OrderPriceType": ORDERTYPE_VT2FEMAS.get(req.type, ""),
            "Direction": DIRECTION_VT2FEMAS.get(req.direction, ""),
            "OffsetFlag": OFFSET_VT2FEMAS.get(req.offset, ""),
            "UserOrderLocalID": orderid,
            "HedgeFlag": USTP_FTDC_CHF_Speculation,
            "ForceCloseReason": USTP_FTDC_FCR_NotForceClose,
            "IsAutoSuspend": 0,
            "TimeCondition": USTP_FTDC_TC_GFD,
            "VolumeCondition": USTP_FTDC_VC_AV,
            "MinVolume": 1,
        }

        if req.type == OrderType.FAK:
            femas_req["OrderPriceType"] = USTP_FTDC_OPT_LimitPrice
            femas_req["TimeCondition"] = USTP_FTDC_TC_IOC
            femas_req["VolumeCondition"] = USTP_FTDC_VC_AV
        elif req.type == OrderType.FOK:
            femas_req["OrderPriceType"] = USTP_FTDC_OPT_LimitPrice
            femas_req["TimeCondition"] = USTP_FTDC_TC_IOC
            femas_req["VolumeCondition"] = USTP_FTDC_VC_CV

        self.reqid += 1
        self.reqOrderInsert(femas_req, self.reqid)

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

        return order.vt_orderid
예제 #10
0
    def send_order(self, req: OrderRequest):
        orderid = self._gen_unqiue_cid()

        if req.direction == Direction.LONG:
            amount = req.volume
        else:
            amount = -req.volume

        o = {
            "cid": orderid,
            "type": ORDERTYPE_VT2BITFINEX[req.type],
            "symbol": "t" + req.symbol,
            "amount": str(amount),
            "price": str(req.price),
        }

        request = [0, "on", None, o]

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

        self.gateway.on_order(order)
        return order.vt_orderid
예제 #11
0
    def send_order(self, req: OrderRequest):
        """"""
        # Need both offset and direction for sending order.
        if (req.offset, req.direction) not in TYPE_VT2OKEXF:
            return ""

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

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

        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/futures/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
예제 #12
0
    def send_order(self, algo: AlgoTemplate, vt_symbol: str,
                   direction: Direction, price: float, volume: float,
                   order_type: OrderType, offset: Offset):
        """"""
        contract = self.main_engine.get_contract(vt_symbol)
        if not contract:
            self.write_log(f'委托下单失败,找不到合约:{vt_symbol}', algo)
            return

        volume = round_to(volume, contract.min_volume)
        if not volume:
            return ""

        req = OrderRequest(symbol=contract.symbol,
                           exchange=contract.exchange,
                           direction=direction,
                           type=order_type,
                           volume=volume,
                           price=price,
                           offset=offset)
        vt_orderid = self.main_engine.send_order(req, contract.gateway_name)

        self.orderid_algo_map[vt_orderid] = algo
        return vt_orderid
예제 #13
0
    def send_server_order(self, strategy: CtaTemplate, contract: ContractData,
                          direction: Direction, offset: Offset, price: float,
                          volume: float, type: OrderType, lock: bool):
        """
        Send a new order to server.
        """
        # Create request and send order.
        original_req = OrderRequest(
            symbol=contract.symbol,
            exchange=contract.exchange,
            direction=direction,
            offset=offset,
            type=type,
            price=price,
            volume=volume,
        )

        # 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.main_engine.send_order(req,
                                                     contract.gateway_name)
            vt_orderids.append(vt_orderid)

            self.offset_converter.update_order_request(req, vt_orderid)

            # Save relationship between orderid and strategy.
            self.orderid_strategy_map[vt_orderid] = strategy
            self.strategy_orderid_map[strategy.strategy_name].add(vt_orderid)

        return vt_orderids
예제 #14
0
    def update_order_request(self, req: OrderRequest, vt_orderid: str):
        """"""
        gateway_name, orderid = vt_orderid.split(".")

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