Ejemplo n.º 1
0
	def login(self, cfg_path = None):
		import os
		import hashlib
		if cfg_path is None:
			cfg = util.read_config( os.getcwd() + "/config.json" )
		else:
			cfg = util.read_config( cfg_path )
		username = cfg["xueqiuUsername"]
		pwd = cfg["xueqiuPassword"].encode("utf8")
		pwd = hashlib.md5( pwd ).hexdigest().upper()
		res = self.session.post( url = URL_XUEQIU_LOGIN, data = DATA_XUEQIU_LOGIN(username, pwd), headers = HEADERS_XUEQIU )
		res_json = res.json()
		if "error_code" in res_json:
			self.logger.warning( "{}".format(res_json) )

		return res_json()
Ejemplo n.º 2
0
 def __init__(
     self,
     account="ctp_real.json",
     config="CtpMd.json",
     instrument_ids=[],  # 如果有传入instrument_ids,以传入的为准
     simnow=False,  # 如果account配置的是simnow的debug账户,请务必将这个flag设置为True
     # 以免扰乱依赖此Worker广播数据的数据处理模块
     **kwargs):
     """
     行情源
     :param account:
     :param config:
     :param instrument_ids:
     :param simnow:
     :param kwargs:
     """
     super().__init__(**kwargs)  # You are not supposed to change THIS
     self.simnow = simnow
     self.mdapi = None
     if instrument_ids == []:
         cfg = util.read_config(os.path.join("config", config))
         self.instrument_ids = cfg["instrument_ids"]
     else:
         self.instrument_ids = instrument_ids
     self.__account__ = account
Ejemplo n.º 3
0
 def init_accounts(self, accounts="easyuncle.json"):
     """
     初始化acctouns中配置的所有帐号
     @return dict
     {
         "account_name": {
             "type": "ctp/easytrader/other",
             "trader": "生成的实例对象",
             "timestamp": "对象生成时间",
         }
     }
     """
     result = {}
     if accounts[0] != '/':
         accounts = os.getcwd() + '/' + accounts
     accounts_dict = util.read_config(accounts)
     for account in accounts_dict.keys():
         result[account] = dict()
         result[account]["type"] = accounts_dict[account]["type"]
         if result[account]["type"] == "ctp":
             result[account]["trader"] = get_vendor(
                 "CTPTraderApi",
                 account=copy.deepcopy(accounts_dict[account])
                 console_log=True,  # 关闭它的log
                 info_log=False,
                 error_log=False,
             )
             result[account]["timestamp"] = datetime.now()
     return result
Ejemplo n.º 4
0
def start(worker_name=None, nickname=None):
    """
    主要依靠nickname来指定配置文件,通过配置文件传入参数

    :param worker_name:
    :param nickname:
     如果nickname以.json结尾,那么程序会去"./config/"目录下寻找对应配置文件
     如果不是以.json结尾,则认为传入的就是nickname,同时会去"./config/"目录下寻找
     nickname.json
    :return:
    """
    kwargs = dict()
    kwargs["worker_name"] = worker_name
    if nickname[-5:] == ".json":
        kwargs["config"] = nickname
        kwargs["nickname"] = nickname[0:-5]
    else:
        kwargs["config"] = nickname + ".json"
        kwargs["nickname"] = nickname
    config_file = os.path.join(os.getcwd(), "config", kwargs["config"])
    if os.path.exists(config_file):
        config = util.read_config(config_file)
    else:
        # 命令指定了配置文件,如果不存在,就结束
        print("未能找到配置文件:", config_file)
        print("默认无配置启动")
        config = {}

    for k in config.keys():
        kwargs[k] = config[k]

    msg = {"type": "sys", "operation_name": "start_worker", "kwargs": kwargs}
    __redis__.publish("dHydra.Command", pickle.dumps(msg))
Ejemplo n.º 5
0
    def __init__(self, account="sina.json", login=False, **kwargs):
        super().__init__(**kwargs)
        config = util.read_config(os.path.join(os.getcwd(), account))

        if "username" in config.keys():
            self.username = config["username"]
        else:
            if login:
                self.username = input('请输入新浪登录帐号:')
            else:
                self.username = None


        if "password" in config.keys():
            self.pwd = config["password"]
        else:
            if login:
                self.pwd = getpass.getpass("输入登录密码(密码不会显示在屏幕上,输入后按回车确定):")
            else:
                self.username = None

        self.rsa_pubkey = '10001'
        self.ip = util.get_client_ip()
        self.session = requests.Session()
        self.quote = None
        self.is_login = False
Ejemplo n.º 6
0
    def __init__(self, account="account/sina.json", login=False, **kwargs):
        super().__init__(**kwargs)
        config = util.read_config(os.path.join(os.getcwd(), account))

        if "username" in config.keys():
            self.username = config["username"]
        else:
            if login:
                self.username = input('请输入新浪登录帐号:')
            else:
                self.username = None


        if "password" in config.keys():
            self.pwd = config["password"]
        else:
            if login:
                self.pwd = getpass.getpass("输入登录密码(密码不会显示在屏幕上,输入后按回车确定):")
            else:
                self.username = None

        self.rsa_pubkey = '10001'
        self.ip = util.get_client_ip()
        self.session = requests.Session()
        self.quote = None
        self.is_login = False
Ejemplo n.º 7
0
    def __init__(
        self,
        config="SinaL2.json",
        **kwargs
    ):
        super().__init__(**kwargs)  # You are not supposed to change THIS
        if "/" not in config:
            cfg = util.read_config(os.path.join(os.getcwd(), "config", config))
        else:
            cfg = util.read_config(config)
        self.symbols = cfg["symbols"]
        self.hq = cfg["hq"]
        self.query = cfg["query"]

        self.count_transaction = 0
        self.count_quotation = 0
        self.count_orders = 0
        self.trading_date = util.get_trading_date()
Ejemplo n.º 8
0
    def get_redis(self, config="redis.json"):
        import os
        if "/" in config:
            cfg = util.read_config(config)
        else:
            cfg = util.read_config(os.path.join(os.getcwd(), "config", config))
        host = cfg["host"]
        port = cfg["port"]

        try:
            self.logger.info("Trying to connect to redis")
            self.redis = redis.StrictRedis(decode_responses=True,
                                           host=host,
                                           port=port)
            self.redis.client_list()
            return self.redis
        except:
            self.logger.warning("Failed to connect to redis")
            return False
Ejemplo n.º 9
0
    def __init__(
            self,
            config="CtpMd.json",
            **kwargs
    ):
        super().__init__(**kwargs)  # You ae not supposed to change THIS

        cfg = util.read_config(os.path.join("config", config))
        self.instrument_ids = cfg["instrument_ids"]
        self.__account__ = cfg["account"]
Ejemplo n.º 10
0
    def __init__(
        self,
        config="SinaL2.json",
        **kwargs
    ):
        super().__init__(**kwargs)  # You are not supposed to change THIS
        if "/" not in config:
            cfg = util.read_config(os.path.join(os.getcwd(), "config", config))
        else:
            cfg = util.read_config(config)
        self.symbols = cfg["symbols"]
        self.hq = cfg["hq"]
        self.query = cfg["query"]
        self.account= cfg["account"]

        self.count_transaction = 0
        self.count_quotation = 0
        self.count_orders = 0
        self.trading_date = util.get_trading_date()
Ejemplo n.º 11
0
 def get_mongodb(self, config="mongodb.json", timeout=1500):
     import os
     if "/" in config:
         cfg = util.read_config(config)
     else:
         cfg = util.read_config(os.path.join(os.getcwd(), "config", config))
     host = cfg["host"]
     port = cfg["port"]
     try:
         self.logger.info("尝试连接到Mongodb")
         client = MongoClient(host=host,
                              port=port,
                              serverSelectionTimeoutMS=timeout,
                              connect=False)
         client.server_info()
         self.logger.info("已经成功连接到mongodb")
         return client
     except:
         self.logger.warning(
             ">>>>>>>>>>>>>>>>>>连接到mongodb失败<<<<<<<<<<<<<<<<<<<")
         return False
Ejemplo n.º 12
0
def discount():
    while True:
        discount_tier = util.read_config( os.getcwd()+"/data/discount_tier.json" )
        print( "利润率\t品种\t卖1贡献总金额" )
        print( "> 0.0%\t{}".format( discount_tier["> 0.0%"] ) )
        print( "> 0.1%\t{}".format( discount_tier["> 0.1%"] ) )
        print( "> 0.2%\t{}".format( discount_tier["> 0.2%"] ) )
        print( "> 0.3%\t{}".format( discount_tier["> 0.3%"] ) )
        print( "> 0.4%\t{}".format( discount_tier["> 0.4%"] ) )
        print( "> 0.5%\t{}".format( discount_tier["> 0.5%"] ) )
        print( "> 0.6%\t{}".format( discount_tier["> 0.6%"] ) )
        time.sleep(3)
Ejemplo n.º 13
0
def discount():
    while True:
        discount_tier = util.read_config(os.getcwd() +
                                         "/data/discount_tier.json")
        print("利润率\t品种\t卖1贡献总金额")
        print("> 0.0%\t{}".format(discount_tier["> 0.0%"]))
        print("> 0.1%\t{}".format(discount_tier["> 0.1%"]))
        print("> 0.2%\t{}".format(discount_tier["> 0.2%"]))
        print("> 0.3%\t{}".format(discount_tier["> 0.3%"]))
        print("> 0.4%\t{}".format(discount_tier["> 0.4%"]))
        print("> 0.5%\t{}".format(discount_tier["> 0.5%"]))
        print("> 0.6%\t{}".format(discount_tier["> 0.6%"]))
        time.sleep(3)
Ejemplo n.º 14
0
    def __init__(
            self,
            config="CtpMd.json",
            instrument_ids=[],
            **kwargs
    ):
        super().__init__(**kwargs)  # You ae not supposed to change THIS

        if instrument_ids == []:
            cfg = util.read_config(os.path.join("config",config))
            self.instrument_ids = cfg["instrument_ids"]
        else:
            self.instrument_ids = instrument_ids
Ejemplo n.º 15
0
    def __init__(
        self,
        broker_id=None,
        user_id=None,
        password=None,
        instrument_ids=None,
        register_front=None,
        **kwargs
    ):
        super().__init__(**kwargs)  # You are not supposed to change THIS

        # The following is customized:
        # In this case, the worker is listening to itself.
        self.__listener__.subscribe([self.redis_key + "Pub"])

        # Note (IMPORTANT):
        # broker_id, user_id, password, instrument_ids, register_front should
        # be bytes
        cfg = util.read_config(os.getcwd() + "/ctp.json")
        if "broker_id" in cfg:
            broker_id = cfg["broker_id"]
        if "password" in cfg:
            password = cfg["password"]
        if "user_id" in cfg:
            user_id = cfg["user_id"]
        if "register_front" in cfg:
            register_front = cfg["register_front"]
        if instrument_ids is None:
            self.logger.warning("没有初始化订阅的合约品种,将采用默认品种作为演示:")
            import pandas
            instrument_ids = list(pandas.DataFrame.from_csv(
                os.getcwd()+"/data/instruments.csv"
            ).index)
            # instrument_ids = [
            #     'IF1609', 'IF1612', 'IF1703', 'IC1609',
            #     'IF1610', 'IC1610', 'IC1612', 'IC1703'
            # ]
            self.logger.warning("{}".format(instrument_ids))
        if (broker_id is None) or (user_id is None) or (password is None) \
                or (register_front is None):
            import sys
            self.logger.error("CTP连接信息不完整")
            sys.exit(0)
        self.broker_id = broker_id.encode()
        self.user_id = user_id.encode()
        self.password = password.encode()
        self.instrument_ids = list()
        self.register_front = register_front.encode()
        for i in instrument_ids:
            self.instrument_ids.append(i.encode())
Ejemplo n.º 16
0
    def get_redis(self, config="redis.json"):
        import os
        if "/" in config:
            cfg = util.read_config(config)
        else:
            cfg = util.read_config(
                os.path.join(os.getcwd(), "config", config)
            )
        host = cfg["host"]
        port = cfg["port"]

        try:
            self.logger.info("Trying to connect to redis")
            self.redis = redis.StrictRedis(
                decode_responses=True,
                host=host,
                port=port
            )
            self.redis.client_list()
            return self.redis
        except:
            self.logger.warning("Failed to connect to redis")
            return False
Ejemplo n.º 17
0
 def get_mongodb(self, config="mongodb.json", timeout=1500):
     import os
     if "/" in config:
         cfg = util.read_config(config)
     else:
         cfg = util.read_config(
             os.path.join(os.getcwd(), "config", config)
         )
     host = cfg["host"]
     port = cfg["port"]
     try:
         self.logger.info("尝试连接到Mongodb")
         client = MongoClient(host=host, port=port,
                              serverSelectionTimeoutMS=timeout,
                              connect=False
                              )
         client.server_info()
         self.logger.info("已经成功连接到mongodb")
         return client
     except:
         self.logger.warning(
             ">>>>>>>>>>>>>>>>>>连接到mongodb失败<<<<<<<<<<<<<<<<<<<")
         return False
Ejemplo n.º 18
0
 def __init__(self, config="CtpMd.json", **kwargs):
     """
     行情源
     :param account:
     :param config:
     :param instrument_ids:
     :param simnow:
     :param kwargs:
     """
     super().__init__(**kwargs)  # You are not supposed to change THIS
     self.mdapi = None
     self.__config__ = config
     cfg = util.read_config(os.path.join("config", config))
     self.__account__ = cfg["account"]
     self.init_config_instrument_ids()
Ejemplo n.º 19
0
    def __init__(
        self,
        account="ctp.json",
        instrument_ids=None,
        **kwargs
    ):
        super().__init__(**kwargs)
        self.request_id = 0

        # Note (IMPORTANT):
        # broker_id, user_id, password, instrument_ids, market_front should
        # be bytes
        cfg = util.read_config(os.getcwd() + "/" + account)
        if "broker_id" in cfg:
            broker_id = cfg["broker_id"]
        if "password" in cfg:
            password = cfg["password"]
        if "user_id" in cfg:
            user_id = cfg["user_id"]
        if "market_front" in cfg:
            market_front = cfg["market_front"]
        if instrument_ids is None:
            self.logger.warning("没有初始化订阅的合约品种,将采用默认品种作为演示:")
            import pandas
            instrument_ids = list(pandas.DataFrame.from_csv(
                os.getcwd() + "/data/instruments.csv"
            ).index)
            self.logger.info("订阅品种:{}".format(instrument_ids))
        if (broker_id is None) or (user_id is None) or (password is None) \
                or (market_front is None):
            import sys
            self.logger.error("CTP连接信息不完整")
            sys.exit(0)
        self.broker_id = broker_id.encode()
        self.user_id = user_id.encode()
        self.password = password.encode()
        self.instrument_ids = list()
        self.market_front = market_front.encode()
        for i in instrument_ids:
            self.instrument_ids.append(i.encode())

        self.Create()
        self.RegisterFront(self.market_front)

        t = threading.Thread(target=self.start_listening)
        t.setDaemon(True)
        t.start()
Ejemplo n.º 20
0
    def __init__(
        self,
        account="ctp.json",
        instrument_ids=None,
        **kwargs
    ):
        super().__init__(**kwargs)
        self.request_id = 0

        # Note (IMPORTANT):
        # broker_id, user_id, password, instrument_ids, market_front should
        # be bytes
        cfg = util.read_config(os.getcwd() + "/" + account)
        if "broker_id" in cfg:
            broker_id = cfg["broker_id"]
        if "password" in cfg:
            password = cfg["password"]
        if "user_id" in cfg:
            user_id = cfg["user_id"]
        if "market_front" in cfg:
            market_front = cfg["market_front"]
        if instrument_ids is None:
            self.logger.warning("没有初始化订阅的合约品种,将采用默认品种作为演示:")
            import pandas
            instrument_ids = list(pandas.DataFrame.from_csv(
                os.getcwd() + "/data/instruments.csv"
            ).index)
            self.logger.info("订阅品种:{}".format(instrument_ids))
        if (broker_id is None) or (user_id is None) or (password is None) \
                or (market_front is None):
            import sys
            self.logger.error("CTP连接信息不完整")
            sys.exit(0)
        self.broker_id = broker_id.encode()
        self.user_id = user_id.encode()
        self.password = password.encode()
        self.instrument_ids = list()
        self.market_front = market_front.encode()
        for i in instrument_ids:
            self.instrument_ids.append(i.encode())

        self.Create()
        self.RegisterFront(self.market_front)

        t = threading.Thread(target=self.start_listening)
        t.setDaemon(True)
        t.start()
Ejemplo n.º 21
0
 def __init__(
     self,
     config="CtpMd.json",
     **kwargs
 ):
     """
     行情源
     :param account:
     :param config:
     :param instrument_ids:
     :param simnow:
     :param kwargs:
     """
     super().__init__(**kwargs)  # You are not supposed to change THIS
     self.mdapi = None
     self.__config__ = config
     cfg = util.read_config(os.path.join("config", config))
     self.__account__ = cfg["account"]
     self.init_config_instrument_ids()
Ejemplo n.º 22
0
    def __init__(self,
                 user_id=None,
                 password=None,
                 broker_id=None,
                 investor_id=None,
                 trade_front=None,
                 **kwargs):
        super().__init__(**kwargs)

        # Get Api Version
        self.api_version = self.GetApiVersion().decode("utf-8")
        self.logger.info("API Version:{}".format(self.api_version))
        self.trading_day = None

        self.req_id = 0  # 操作请求编号

        self.is_connected = False  # 连接状态
        self.is_login = False  # 登录状态

        cfg = util.read_config(os.getcwd() + "/ctp.json")
        try:
            if investor_id is None:
                self.investor_id = cfg["investor_id"].encode()
            if user_id is None:
                self.user_id = cfg["user_id"].encode()  # 账号
            if password is None:
                self.password = cfg["password"].encode()  # 密码
            if broker_id is None:
                self.broker_id = cfg["broker_id"].encode()  # 经纪商代码
            if trade_front is None:
                self.trade_front = cfg["trade_front"].encode()  # 服务器地址
        except Exception as e:
            self.logger.error("没有从{}读取到正确格式的配置".format(os.getcwd() +
                                                       "/ctp.json"))
            return None

        self.front_id = None  # 前置机编号
        self.session_id = None  # 会话编号

        self.connect(self.user_id, self.password, self.broker_id,
                     self.trade_front)
Ejemplo n.º 23
0
 def __init__(self, username=None, pwd=None, **kwargs):
     super().__init__(**kwargs)
     config = util.read_config(os.getcwd() + "/sina.json")
     if (username is None):
         if "username" in config.keys():
             self.username = config["username"]
         else:
             self.username = input('请输入新浪登录帐号:')
     else:
         self.username = username
     if (pwd is None):
         if "password" in config.keys():
             self.pwd = config["password"]
         else:
             self.pwd = getpass.getpass("输入登录密码(密码不会显示在屏幕上,输入后按回车确定):")
     else:
         self.pwd = pwd
     self.rsa_pubkey = '10001'
     self.ip = util.get_client_ip()
     self.session = requests.Session()
     self.quote = None
     self.is_login = False
Ejemplo n.º 24
0
def start(worker_name=None, nickname=None):
    """
    主要依靠nickname来指定配置文件,通过配置文件传入参数

    :param worker_name:
    :param nickname:
     如果nickname以.json结尾,那么程序会去"./config/"目录下寻找对应配置文件
     如果不是以.json结尾,则认为传入的就是nickname,同时会去"./config/"目录下寻找
     nickname.json
    :return:
    """
    kwargs = dict()
    kwargs["worker_name"] = worker_name
    if nickname[-5:] == ".json":
        kwargs["config"] = nickname
        kwargs["nickname"] = nickname[0:-5]
    else:
        kwargs["config"] = nickname + ".json"
        kwargs["nickname"] = nickname
    config_file = os.path.join(os.getcwd(), "config", kwargs["config"])
    if os.path.exists(config_file):
        config = util.read_config(config_file)
    else:
        # 命令指定了配置文件,如果不存在,就结束
        print("未能找到配置文件:", config_file)
        print("默认无配置启动")
        config = {}

    for k in config.keys():
        kwargs[k] = config[k]

    msg = {
        "type": "sys",
        "operation_name": "start_worker",
        "kwargs": kwargs
    }
    __redis__.publish("dHydra.Command", pickle.dumps(msg))
Ejemplo n.º 25
0
 def init_config_instrument_ids(self):
     cfg = util.read_config(os.path.join("config", self.__config__))
     self.instrument_ids = cfg["instrument_ids"]
Ejemplo n.º 26
0
    def __init__(self, config="CtpMd.json", **kwargs):
        super().__init__(**kwargs)  # You ae not supposed to change THIS

        cfg = util.read_config(os.path.join("config", config))
        self.instrument_ids = cfg["instrument_ids"]
        self.__account__ = cfg["account"]
Ejemplo n.º 27
0
    def __init__(self, account="ctp.json"):
        self.logger = get_logger(
            log_path="log",  #
            console_log=True,  # 屏幕打印日志开关,默认True
            console_log_level=logging.INFO,  # 屏幕打印日志的级别,默认为INFO
            critical_log=False,  # critical单独写文件日志,默认关闭
            error_log=True,  # error级别单独写文件日志,默认开启
            warning_log=False,  # warning级别单独写日志,默认关闭
            info_log=True,  # info级别单独写日志,默认开启
            debug_log=False,
            logger_name=self.__class__.__name__,
        )
        super().__init__()
        self.shfe_time = None
        self.dce_time = None
        self.czce_time = None
        self.ffex_time = None
        self.ine_time = None
        self.login_time = None

        # 将语义化的"buy","sell"映射到ApiStruct.D_Buy, ApiStruct.D_Sell
        self.direction_map = {
            "buy": ApiStruct.D_Buy,
            "sell": ApiStruct.D_Sell,
        }
        self.direction_inverse_map = {
            ApiStruct.D_Buy: "buy",
            ApiStruct.D_Sell: "sell",
        }
        self.posi_direction_map = {
            "long": ApiStruct.PD_Long,
            "short": ApiStruct.PD_Short
        }
        # 将语义化的"open","close"映射到ApiStruct.OF_Open, ApiStruct.OF_Close
        self.offset_flag_map = {
            "open": ApiStruct.OF_Open,
            "close": ApiStruct.OF_Close,
            "force_close": ApiStruct.OF_ForceClose,  # 强平
            "close_today": ApiStruct.OF_CloseToday,  # 平今
            "close_yesterday": ApiStruct.OF_CloseYesterday,  # 平昨
            "force_off": ApiStruct.OF_ForceOff,  # 强减
        }
        self.offset_flag_inverse_map = {
            ApiStruct.OF_Open: "open",
            ApiStruct.OF_Close: "close",
            ApiStruct.OF_CloseToday: "close_today",
            ApiStruct.OF_CloseYesterday: "close_yesterday",
        }

        # Get Api Version
        self.api_version = self.GetApiVersion().decode("utf-8")
        self.trading_day = None

        self.instruments = DataFrame(columns=[
            'CombinationType', 'CreateDate', 'DeliveryMonth', 'DeliveryYear',
            'EndDelivDate', 'ExchangeID', 'ExchangeInstID', 'ExpireDate',
            'InstLifePhase', 'InstrumentName', 'IsTrading', 'LongMarginRatio',
            'MaxLimitOrderVolume', 'MaxMarginSideAlgorithm',
            'MaxMarketOrderVolume', 'MinLimitOrderVolume',
            'MinMarketOrderVolume', 'OpenDate', 'OptionsType',
            'PositionDateType', 'PositionType', 'PriceTick', 'ProductClass',
            'ProductID', 'ShortMarginRatio', 'StartDelivDate', 'StrikePrice',
            'UnderlyingInstrID', 'UnderlyingMultiple', 'VolumeMultiple',
            "InstrumentID"
        ])
        self.instruments_last_updated = None
        self.instrument_status = dict()

        self.position_last_updated = None  # 持仓最近更新时间:datetime类型
        self.position_updating = False  # 用于标记position是否正在更新
        self.position_timeout = False  # 当获取持仓超时的时候标记,当获取完毕一次持仓以后取消标记
        self.position_buffer = list()  # list内嵌dict
        self.position_latest_request_id = None  # 最新的对position的request_id
        self.position_buffer_request_id = None  # 正在buffer中的request_id
        # 委托明细
        self.orders = None  # DataFrame类型
        # 成交明细
        self.trade = None  # 成交明细,DataFrame类型

        self.__request_counter__ = itertools.count()  # self.request_ref的迭代器
        self.__request_counter__.__next__()
        self.__order_counter__ = itertools.count()  # self.order_ref的迭代器
        self.__order_counter__.__next__()

        self.requests_map = defaultdict(Queue)
        self.requests_safe_queue = Queue()

        self.is_connected = False  # 连接状态
        self.is_login = False  # 登录状态

        account_path = ""
        if isinstance(account, dict):
            cfg = account
        else:
            if account[0] != '/':
                account = '/' + account
            account_path = os.getcwd() + "{}".format(account)
            cfg = util.read_config(account_path)
        try:
            self.investor_id = cfg["investor_id"].encode()
            self.user_id = cfg["user_id"].encode()  # 账号
            self.password = cfg["password"].encode()  # 密码
            self.broker_id = cfg["broker_id"].encode()  # 经纪商代码
            self.trade_front = cfg["trade_front"].encode()  # 服务器地址
        except KeyError as e:
            self.logger.error("没有从{}读取到正确格式的配置, Error: {}".format(
                account_path, e))
            raise KeyError

        self.front_id = None  # 前置机编号
        self.session_id = None  # 会话编号

        self.connect(self.user_id, self.password, self.broker_id,
                     self.trade_front)

        request_sender = threading.Thread(target=self.request_sender,
                                          daemon=True)
        request_sender.start()
Ejemplo n.º 28
0
 def init_config_instrument_ids(self):
     cfg = util.read_config(os.path.join("config", self.__config__))
     self.instrument_ids = cfg["instrument_ids"]
Ejemplo n.º 29
0
    def __init__(
            self,
            account="ctp.json"
    ):
        self.logger = get_logger(
            log_path="log",                     #
            console_log=True,              # 屏幕打印日志开关,默认True
            console_log_level=logging.INFO,  # 屏幕打印日志的级别,默认为INFO
            critical_log=False,        # critical单独写文件日志,默认关闭
            error_log=True,             # error级别单独写文件日志,默认开启
            warning_log=False,         # warning级别单独写日志,默认关闭
            info_log=True,               # info级别单独写日志,默认开启
            debug_log=False,
            logger_name=self.__class__.__name__,
        )
        super().__init__()
        self.shfe_time = None
        self.dce_time = None
        self.czce_time = None
        self.ffex_time = None
        self.ine_time = None
        self.login_time = None

        # 将语义化的"buy","sell"映射到ApiStruct.D_Buy, ApiStruct.D_Sell
        self.direction_map = {
            "buy": ApiStruct.D_Buy,
            "sell": ApiStruct.D_Sell,
        }
        self.direction_inverse_map = {
            ApiStruct.D_Buy: "buy",
            ApiStruct.D_Sell: "sell",
        }
        self.posi_direction_map = {
            "long": ApiStruct.PD_Long,
            "short": ApiStruct.PD_Short
        }
        # 将语义化的"open","close"映射到ApiStruct.OF_Open, ApiStruct.OF_Close
        self.offset_flag_map = {
            "open": ApiStruct.OF_Open,
            "close": ApiStruct.OF_Close,
            "force_close": ApiStruct.OF_ForceClose,  # 强平
            "close_today": ApiStruct.OF_CloseToday,  # 平今
            "close_yesterday": ApiStruct.OF_CloseYesterday,  # 平昨
            "force_off": ApiStruct.OF_ForceOff,  # 强减
        }
        self.offset_flag_inverse_map = {
            ApiStruct.OF_Open: "open",
            ApiStruct.OF_Close: "close",
            ApiStruct.OF_CloseToday: "close_today",
            ApiStruct.OF_CloseYesterday: "close_yesterday",
        }

        # Get Api Version
        self.api_version = self.GetApiVersion().decode("utf-8")
        self.trading_day = None

        self.instruments = DataFrame(
            columns=[
                'CombinationType', 'CreateDate', 'DeliveryMonth',
                'DeliveryYear', 'EndDelivDate', 'ExchangeID',
                'ExchangeInstID', 'ExpireDate', 'InstLifePhase',
                'InstrumentName', 'IsTrading', 'LongMarginRatio',
                'MaxLimitOrderVolume', 'MaxMarginSideAlgorithm',
                'MaxMarketOrderVolume', 'MinLimitOrderVolume',
                'MinMarketOrderVolume', 'OpenDate', 'OptionsType',
                'PositionDateType', 'PositionType', 'PriceTick',
                'ProductClass', 'ProductID', 'ShortMarginRatio',
                'StartDelivDate', 'StrikePrice', 'UnderlyingInstrID',
                'UnderlyingMultiple', 'VolumeMultiple', "InstrumentID"
            ]
        )
        self.instruments_last_updated = None
        self.instrument_status = dict()

        self.position_last_updated = None  # 持仓最近更新时间:datetime类型
        self.position_updating = False   # 用于标记position是否正在更新
        self.position_timeout = False    # 当获取持仓超时的时候标记,当获取完毕一次持仓以后取消标记
        self.position_buffer = list()  # list内嵌dict
        self.position_latest_request_id = None  # 最新的对position的request_id
        self.position_buffer_request_id = None  # 正在buffer中的request_id
        # 委托明细
        self.orders = None      # DataFrame类型
        # 成交明细
        self.trade = None       # 成交明细,DataFrame类型

        self.__request_counter__ = itertools.count()  # self.request_ref的迭代器
        self.__request_counter__.__next__()
        self.__order_counter__ = itertools.count()  # self.order_ref的迭代器
        self.__order_counter__.__next__()

        self.requests_map = defaultdict(Queue)
        self.requests_safe_queue = Queue()

        self.is_connected = False       # 连接状态
        self.is_login = False            # 登录状态

        account_path = ""
        if isinstance(account, dict):
            cfg = account
        else:
            if account[0] != '/':
                account = '/' + account
            account_path = os.getcwd() + "{}".format(account)
            cfg = util.read_config(account_path)
        try:
            self.investor_id = cfg["investor_id"].encode()
            self.user_id = cfg["user_id"].encode()                # 账号
            self.password = cfg["password"].encode()            # 密码
            self.broker_id = cfg["broker_id"].encode()            # 经纪商代码
            self.trade_front = cfg["trade_front"].encode()  # 服务器地址
        except KeyError as e:
            self.logger.error(
                "没有从{}读取到正确格式的配置, Error: {}"
                .format(account_path, e)
            )
            raise KeyError

        self.front_id = None            # 前置机编号
        self.session_id = None          # 会话编号

        self.connect(
            self.user_id,
            self.password,
            self.broker_id,
            self.trade_front
        )

        request_sender = threading.Thread(
            target=self.request_sender, daemon=True
        )
        request_sender.start()