Exemplo n.º 1
0
    def __init__(self, name: Text, import_name, action_class: Action = None, engine_method: str = "thread",
                 logger_class=None, logger_config_path=None,
                 refresh: bool = False, risk=None,
                 instance_path=None):
        """ 初始化 """
        self.name = name if name else 'ctpbee'
        self.import_name = import_name
        self.engine_method = engine_method
        self.refresh = refresh
        self.active = False
        # 是否加载以使用默认的logger类/ choose if use the default logging class
        if logger_class is None:
            self.logger = VLogger(CP, app_name=self.name)
            self.logger.set_default(name=self.logger.app_name, owner='App')
        else:
            if logger_config_path:
                self.logger = logger_class(logger_config_path, app_name=self.name)
            else:
                self.logger = logger_class(CP, app_name=self.name)
            self.logger.set_default(name=self.logger.app_name, owner='App')
        if engine_method == "thread":
            self.event_engine = EventEngine()
            self.recorder = Recorder(self, self.event_engine)
        elif engine_method == "async":
            self.event_engine = AsyncEngine()
            self.recorder = AsyncRecorder(self, self.event_engine)
        else:
            raise TypeError("引擎参数错误,只支持 thread 和 async,请检查代码")

        """
              If no risk is specified by default, set the risk_decorator to None
              如果默认不指定action参数, 那么使用设置风控装饰器为空
              """
        if risk is None:
            self.risk_decorator = None
        else:
            self.risk_decorator = risk
        """
        If no action is specified by default, use the default Action class
        如果默认不指定action参数, 那么使用默认的Action类 
        """
        if action_class is None:
            self.action = Action(self)
        else:
            self.action = action_class(self)
        """
        根据action里面的函数更新到CtpBee上面来
        bind the function of action to CtpBee
        """

        """ update """
        if self.risk_decorator is not None:
            self.risk_decorator.update_app(self)

        for x in dir(self.action):
            func = getattr(self.action, x)
            if x.startswith("__"):
                continue
            if ismethod(func):
                setattr(self, func.__name__, func)
        """
        If engine_method is specified by default, use the default EventEngine and Recorder or use the engine
            and recorder basis on your choice
        如果不指定engine_method参数,那么使用默认的事件引擎 或者根据你的参数使用不同的引擎和记录器
        """

        if instance_path is None:
            instance_path = self.auto_find_instance_path()
        elif not os.path.isabs(instance_path):
            raise ValueError(
                'If an instance path is provided it must be absolute.'
                ' A relative path was given instead.'
            )
        self.instance_path = instance_path
        self.config = self.make_config()
        self.init_finished = False

        # default monitor and flag
        self.p = None
        self.p_flag = True

        self.r = None
        self.r_flag = True

        _app_context_ctx.push(self.name, self)
Exemplo n.º 2
0
    def __init__(self,
                 name: Text,
                 import_name,
                 action_class: Action or None = None,
                 engine_method: str = "thread",
                 logger_class=None,
                 logger_config=None,
                 refresh: bool = False,
                 risk: RiskLevel = None,
                 instance_path=None):
        """
        name: 创建运行核心的名字
        import_name: 导入包的名字, 用__name__即可'
        action_class: 执行器 > 默认使用系统自带的Action, 或者由用户继承,然后传入类
        engine_method: Actor模型采用的底层的引擎
        logger_class: logger类,可以自己定义
        refresh: 是否自己主动持仓
        risk: 风险管理类, 可以自己继承RiskLevel进行定制
        sim: 是否进行模拟
        """
        self.start_datetime = datetime.now()
        self.basic_info = None
        self._extensions = {}
        self.name = name if name else 'ctpbee'
        self.import_name = import_name
        self.engine_method = engine_method
        self.refresh = refresh
        self.active = False
        # 是否加载以使用默认的logger类/ choose if use the default logging class
        if logger_class is None:
            self.logger = VLogger(CP, app_name=self.name)
            self.logger.set_default(name=self.logger.app_name, owner=self.name)
        else:
            if logger_config:
                self.logger = logger_class(logger_config, app_name=self.name)
            else:
                self.logger = logger_class(CP, app_name=self.name)
            self.logger.set_default(name=self.logger.app_name, owner='App')

        self.app_signal = AppSignal(self.name)

        if engine_method == "thread":
            self.recorder = Recorder(self)
        else:
            raise TypeError("引擎参数错误,只支持 thread 和 async,请检查代码")
        """
              If no risk is specified by default, set the risk_decorator to None
              如果默认不指定action参数, 那么使用设置风控装饰器为空
              """
        if risk is None:
            self.risk_decorator = None
        else:
            self.risk_decorator = risk
        """
        If no action is specified by default, use the default Action class
        如果默认不指定action参数, 那么使用默认的Action类 
        """
        if action_class is None:
            self.action: Action = Action(self)
        else:
            self.action: Action = action_class(self)
        """
        根据action里面的函数更新到CtpBee上面来
        bind the function of action to CtpBee
        """
        """
        If engine_method is specified by default, use the default EventEngine and Recorder or use the engine
            and recorder basis on your choice
        如果不指定engine_method参数,那么使用默认的事件引擎 或者根据你的参数使用不同的引擎和记录器
        """

        if instance_path is None:
            instance_path = self.auto_find_instance_path()
        elif not os.path.isabs(instance_path):
            raise ValueError(
                'If an instance path is provided it must be absolute.'
                ' A relative path was given instead.')
        self.instance_path = instance_path
        self.config = self.make_config()
        self.init_finished = False
        self.qifi = None
        # default monitor and flag
        self.p = None
        self.p_flag = True

        self.r = None
        self.r_flag = True

        self.center: Center = Center(self)

        self._init_interface = False
        """ update """
        if self.risk_decorator is not None:
            self.risk_decorator.update_app(self)

        for x in dir(self.action):
            func = getattr(self.action, x)
            if x.startswith("__"):
                continue
            if ismethod(func):
                setattr(self, func.__name__, func)
        _app_context_ctx.push(self.name, self)

        self.data = []