Exemplo n.º 1
0
    def onQueryOptionAuctionInfo(self, data: dict, error: dict, reqid: int,
                                 last: bool, session: int) -> None:
        """"""
        if not data or not data["ticker"]:
            return

        contract = ContractData(
            symbol=data["ticker"],
            exchange=MARKET_XTP2VT[data["security_id_source"]],
            name=data["symbol"],
            product=Product.OPTION,
            size=data["contract_unit"],
            min_volume=data["qty_unit"],
            pricetick=data["price_tick"],
            gateway_name=self.gateway_name)

        contract.option_portfolio = data["underlying_security_id"] + "_O"
        contract.option_underlying = (data["underlying_security_id"] + "-" +
                                      str(data["delivery_month"]))
        contract.option_type = OPTIONTYPE_XTP2VT.get(data["call_or_put"], None)

        contract.option_strike = data["exercise_price"]
        contract.option_index = str(data["exercise_price"])
        contract.option_expiry = datetime.strptime(str(data["delivery_day"]),
                                                   "%Y%m%d")
        contract.option_index = get_option_index(contract.option_strike,
                                                 data["contract_id"])

        self.gateway.on_contract(contract)

        if last:
            self.gateway.write_log("期权信息查询成功")
Exemplo n.º 2
0
    def onRspQryInstrument(self, data: dict, error: dict, reqid: int,
                           last: bool):
        """
        Callback of instrument query.
        """
        product = PRODUCT_DA2VT.get(data["CommodityType"], None)
        if product:
            contract = ContractData(
                symbol=data["CommodityCode"],
                exchange=EXCHANGE_DA2VT[data["ExchangeNo"]],
                name=data["ContractFName"],
                product=product,
                size=data["ProductDot"] / data["UpperTick"],
                pricetick=data["UpperTick"],
                gateway_name=self.gateway_name)

            if product == Product.OPTION:
                contract.option_type = OPTIONTYPE_DA2VT[data["OptionType"]]
                contract.option_strike = to_float(data["OptionStrikePrice"])
                contract.option_expiry = datetime.strptime(
                    data["LastTradeDay"], "%Y%m%d")

            symbol_name_map[contract.vt_symbol] = contract.name
            symbol_currency_map[contract.symbol] = data["CommodityFCurrencyNo"]

            self.gateway.on_contract(contract)

        if last:
            self.gateway.write_log("合约信息查询成功")
Exemplo n.º 3
0
    def on_query_instrument(self, packet: dict):
        """"""
        currency = self.reqid_currency_map[packet["id"]]

        for d in packet["result"]:
            contract = ContractData(
                symbol=d["instrument_name"],
                exchange=Exchange.DERIBIT,
                name=d["instrument_name"],
                product=PRODUCT_DERIBIT2VT[d["kind"]],
                pricetick=d["tick_size"],
                size=d["contract_size"],
                min_volume=d["min_trade_amount"],
                net_position=True,
                history_data=False,
                gateway_name=self.gateway_name,
            )

            if contract.product == Product.OPTION:
                contract.option_portfolio = d["base_currency"]
                contract.option_strike = d["strike"]
                contract.option_index = str(d["strike"])
                contract.option_underlying = d["base_currency"]
                contract.option_type = OPTIONTYPE_DERIBIT2VT[d["option_type"]]
                contract.option_expiry = datetime.fromtimestamp(
                    d["expiration_timestamp"] / 1000)

            self.gateway.on_contract(contract)

        self.gateway.write_log(f"{currency}合约信息查询成功")
    def onRspQryInstrument(self, data: dict, error: dict, reqid: int, last: bool):
        """
        Callback of instrument query.
        """
        product = PRODUCT_SOPT2VT.get(data["ProductClass"], None)

        if product:
            contract = ContractData(
                symbol=data["InstrumentID"],
                exchange=EXCHANGE_SOPT2VT[data["ExchangeID"]],
                name=data["InstrumentName"],
                product=product,
                size=data["VolumeMultiple"],
                pricetick=data["PriceTick"],
                gateway_name=self.gateway_name
            )

            # For option only
            if contract.product == Product.OPTION:
                contract.option_portfolio = data["UnderlyingInstrID"] + "_O"
                contract.option_underlying = (
                    data["UnderlyingInstrID"]
                    + "-"
                    + str(data["DeliveryYear"])
                    + str(data["DeliveryMonth"]).rjust(2, "0")
                )
                contract.option_type = OPTIONTYPE_SOPT2VT.get(data["OptionsType"], None)
                contract.option_strike = data["StrikePrice"]
                contract.option_index = str(data["StrikePrice"])
                contract.option_expiry = datetime.strptime(data["ExpireDate"], "%Y%m%d")
                contract.option_index = get_option_index(
                    contract.option_strike, data["InstrumentCode"]
                )

            self.gateway.on_contract(contract)

            symbol_exchange_map[contract.symbol] = contract.exchange
            symbol_name_map[contract.symbol] = contract.name
            symbol_size_map[contract.symbol] = contract.size

        if last:
            self.gateway.write_log("合约信息查询成功")

            for data in self.order_data:
                self.onRtnOrder(data)
            self.order_data.clear()

            for data in self.trade_data:
                self.onRtnTrade(data)
            self.trade_data.clear()
Exemplo n.º 5
0
    def onRspQryInstrument(self, data: dict, error: dict, reqid: int,
                           last: bool):
        """
        Callback of instrument query.
        """
        if data:
            product = PRODUCT_SGIT2VT.get(data["ProductClass"], None)

            contract = ContractData(
                symbol=data["InstrumentID"],
                exchange=EXCHANGE_SGIT2VT[data["ExchangeID"]],
                name=data["InstrumentName"],
                product=product,
                size=data["VolumeMultiple"],
                pricetick=data["PriceTick"],
                gateway_name=self.gateway_name)

            # For option only
            if contract.product == Product.OPTION:
                # Remove C/P suffix of CZCE option product name
                if contract.exchange == Exchange.CZCE:
                    contract.option_portfolio = data["ProductID"][:-1]
                else:
                    contract.option_portfolio = data["ProductID"]

                contract.option_underlying = data["UnderlyingInstrID"]
                contract.option_type = OPTIONTYPE_SGIT2VT.get(
                    data["OptionsType"], None)
                contract.option_strike = data["StrikePrice"]
                contract.option_index = str(data["StrikePrice"])
                contract.option_expiry = datetime.strptime(
                    data["ExpireDate"], "%Y%m%d")

            self.gateway.on_contract(contract)

            symbol_exchange_map[contract.symbol] = contract.exchange
            symbol_name_map[contract.symbol] = contract.name
            symbol_size_map[contract.symbol] = contract.size

        if last:
            self.gateway.write_log("合约信息查询成功")

            for data in self.order_data:
                self.onRtnOrder(data)
            self.order_data.clear()

            for data in self.trade_data:
                self.onRtnTrade(data)
            self.trade_data.clear()
Exemplo n.º 6
0
    def OnRspQrySecurity(
        self,
        data: dict,
        error: dict,
        reqid: int,
        last: bool
    ) -> None:
        """
        Callback of instrument query.
        """
        if last:
            self.gateway.write_log("合约信息查询成功")
        if not data:
            return

        contract = ContractData(
            gateway_name=self.gateway.gateway_name,
            symbol=data["SecurityID"],
            exchange=EXCHANGE_TORA2VT[bytes.decode(data["ExchangeID"])],
            name=data["SecurityName"],
            product=PRODUCT_TORA2VT.get(bytes.decode(data["ProductID"]), Product.EQUITY),
            size=data["VolumeMultiple"],
            pricetick=data["PriceTick"],
            min_volume=data["MinLimitOrderBuyVolume"],
            stop_supported=False,
            net_position=True,
            history_data=False,
        )

        contract.option_portfolio = data["UnderlyingSecurityID"] + "_O"
        contract.option_underlying = (
            data["UnderlyingSecurityID"]
            + "-"
            + str(data["LastDate"])
        )
        contract.option_type = OPTIONTYPE_TORA2VT[bytes.decode(
            data["OptionsType"])]

        contract.option_strike = data["StrikePrice"]
        contract.option_expiry = datetime.strptime(
            str(data["LastDate"]), "%Y%m%d"
        )
        contract.option_index = get_option_index(
            contract.option_strike, data["ExchSecurityID"]
        )

        self.gateway.on_contract(contract)
Exemplo n.º 7
0
    def onRspQryInstrument(self, data: dict, error: dict, reqid: int,
                           last: bool):
        """
        Callback of instrument query.
        """
        # Femas gateway provides no ProductClass data, so need to determine
        # product type using following logic.
        option_type = OPTIONTYPE_FEMAS2VT.get(data["OptionsType"], None)
        if option_type:
            product = Product.OPTION
        elif data["InstrumentID_2"]:
            product = Product.SPREAD
        else:
            product = Product.FUTURES

        contract = ContractData(symbol=data["InstrumentID"],
                                exchange=EXCHANGE_FEMAS2VT[data["ExchangeID"]],
                                name=data["InstrumentName"],
                                size=data["VolumeMultiple"],
                                pricetick=data["PriceTick"],
                                product=product,
                                gateway_name=self.gateway_name)

        if product == Product.OPTION:
            # Remove C/P suffix of CZCE option product name
            if contract.exchange == Exchange.CZCE:
                contract.option_portfolio = data["ProductID"][:-1]
            else:
                contract.option_portfolio = data["ProductID"]

            contract.option_underlying = data["UnderlyingInstrID"]
            contract.option_type = OPTIONTYPE_FEMAS2VT.get(
                data["OptionsType"], None)
            contract.option_strike = data["StrikePrice"]
            contract.option_index = str(data["StrikePrice"])
            contract.option_expiry = datetime.strptime(data["ExpireDate"],
                                                       "%Y%m%d")

        self.gateway.on_contract(contract)

        symbol_exchange_map[contract.symbol] = contract.exchange
        symbol_name_map[contract.symbol] = contract.name
        symbol_size_map[contract.symbol] = contract.size

        if last:
            self.gateway.write_log("合约信息查询成功")
Exemplo n.º 8
0
    def on_query_contract(self, data: List[Dict[str, str]]) -> None:
        """"""
        if not data:
            self.gateway.write_log("合约信息查询失败")
            return

        # Process option contract
        for d in data:
            contract = ContractData(
                symbol=d["option_code"],
                exchange=EXCHANGE_HSOPTION2VT[d["exchange_type"]],
                name=d["option_name"],
                size=int(float(d["amount_per_hand"])),
                pricetick=float(d["opt_price_step"]),
                product=Product.OPTION,
                gateway_name=self.gateway_name
            )

            contract.option_portfolio = d["stock_code"] + "_O"
            contract.option_underlying = (
                d["stock_code"]
                + "-"
                + str(d["end_date"])
            )
            contract.option_type = OPTIONTYPE_HSOPTION2VT[d["option_type"]]
            contract.option_strike = float(d["exercise_price"])
            contract.option_expiry = datetime.strptime(d["end_date"], "%Y%m%d")
            contract.option_index = get_option_index(
                contract.option_strike, d["optcontract_id"]
            )

            self.gateway.on_contract(contract)

            symbol_exchange_map[contract.symbol] = contract.exchange
            symbol_name_map[contract.symbol] = contract.name

        if len(data) == 1000:
            position_str = d["position_str"]
            self.query_contract(position_str)
        else:
            self.gateway.write_log("合约信息查询成功")
            self.query_order()
Exemplo n.º 9
0
    def onRspQryInstrument(self, data: dict, error: dict, reqid: int,
                           last: bool):
        """
        Callback of instrument query.
        """
        product = PRODUCT_MINI2VT.get(data.get("ProductClass", None), None)
        if product:
            contract = ContractData(
                symbol=data["InstrumentID"],
                exchange=EXCHANGE_MINI2VT[data["ExchangeID"]],
                name=data["InstrumentName"],
                product=product,
                size=data["VolumeMultiple"],
                pricetick=data["PriceTick"],
                gateway_name=self.gateway_name)

            # For option only
            if contract.product == Product.OPTION:
                contract.option_underlying = data["UnderlyingInstrID"],
                contract.option_type = OPTIONTYPE_MINI2VT.get(
                    data["OptionsType"], None),
                contract.option_strike = data["StrikePrice"],
                contract.option_expiry = datetime.strptime(
                    data["ExpireDate"], "%Y%m%d"),

            self.gateway.on_contract(contract)

            symbol_exchange_map[contract.symbol] = contract.exchange
            symbol_name_map[contract.symbol] = contract.name
            symbol_size_map[contract.symbol] = contract.size

        if last:
            self.gateway.write_log(" contract information inquiry succeed ")

            for data in self.order_data:
                self.onRtnOrder(data)
            self.order_data.clear()

            for data in self.trade_data:
                self.onRtnTrade(data)
            self.trade_data.clear()
Exemplo n.º 10
0
    def onRspQryInstrument(self, data: dict, error: dict, reqid: int,
                           last: bool):
        """
        Callback of instrument query.
        """
        product = PRODUCT_CTP2VT.get(data["ProductClass"], None)
        if product:
            contract = ContractData(
                symbol=data["InstrumentID"],
                exchange=EXCHANGE_CTP2VT[data["ExchangeID"]],
                name=data["InstrumentName"],
                product=product,
                size=data["VolumeMultiple"],
                pricetick=data["PriceTick"],  # 合约最小价格变动
                max_order_volume=data["MaxLimitOrderVolume"],  # 限价单次最大委托量
                gateway_name=self.gateway_name)

            #手续费数据合并到contract
            for symbol in self.commission_data.keys():
                if symbol == contract.symbol:
                    contract.open_commission_ratio = self.commission_data[
                        symbol]['OpenRatioByMoney']  #开仓手续费率
                    contract.open_commission = self.commission_data[symbol][
                        'OpenRatioByVolume']  #开仓手续费
                    contract.close_commission_ratio = self.commission_data[
                        symbol]['CloseRatioByMoney']  #平仓手续费率
                    contract.close_commission = self.commission_data[symbol][
                        'CloseRatioByVolume']  #平仓手续费
                    contract.close_commission_today_ratio = self.commission_data[
                        symbol]['CloseTodayRatioByMoney']  #平今手续费率
                    contract.close_commission_today = self.commission_data[
                        symbol]['CloseTodayRatioByVolume']  #平今手续费
                elif remain_alpha(symbol) == remain_alpha(contract.symbol):
                    contract.open_commission_ratio = self.commission_data[
                        symbol]['OpenRatioByMoney']  #开仓手续费率
                    contract.open_commission = self.commission_data[symbol][
                        'OpenRatioByVolume']  #开仓手续费
                    contract.close_commission_ratio = self.commission_data[
                        symbol]['CloseRatioByMoney']  #平仓手续费率
                    contract.close_commission = self.commission_data[symbol][
                        'CloseRatioByVolume']  #平仓手续费
                    contract.close_commission_today_ratio = self.commission_data[
                        symbol]['CloseTodayRatioByMoney']  #平今手续费率
                    contract.close_commission_today = self.commission_data[
                        symbol]['CloseTodayRatioByVolume']  #平今手续费

            #保证金数据合并到contract
            for symbol in self.margin_ratio_data.keys():
                if symbol == contract.symbol:
                    contract.margin_ratio = max(
                        self.margin_ratio_data[symbol]
                        ['LongMarginRatioByMoney'],
                        self.margin_ratio_data[symbol]
                        ['ShortMarginRatioByMoney'])  #合约保证金比率
                elif remain_alpha(symbol) == remain_alpha(contract.symbol):
                    contract.margin_ratio = max(
                        self.margin_ratio_data[symbol]
                        ['LongMarginRatioByMoney'],
                        self.margin_ratio_data[symbol]
                        ['ShortMarginRatioByMoney'])  #合约保证金比率

            # For option only
            if contract.product == Product.OPTION:
                # Remove C/P suffix of CZCE option product name
                if contract.exchange == Exchange.CZCE:
                    contract.option_portfolio = data["ProductID"][:-1]
                else:
                    contract.option_portfolio = data["ProductID"]

                contract.option_underlying = data["UnderlyingInstrID"]
                contract.option_type = OPTIONTYPE_CTP2VT.get(
                    data["OptionsType"], None)
                contract.option_strike = data["StrikePrice"]
                contract.option_index = str(data["StrikePrice"])
                contract.option_expiry = datetime.strptime(
                    data["ExpireDate"], "%Y%m%d")

            self.gateway.on_contract(contract)

            symbol_exchange_map[contract.symbol] = contract.exchange
            symbol_name_map[contract.symbol] = contract.name
            symbol_size_map[contract.symbol] = contract.size

        if last:
            self.contract_inited = True
            self.gateway.write_log("合约信息查询成功")

            for data in self.order_data:
                self.onRtnOrder(data)
            self.order_data.clear()

            for data in self.trade_data:
                self.onRtnTrade(data)
            self.trade_data.clear()