Beispiel #1
0
    def __init__(self, extension_name, app=None, **kwargs):
        """
        init function
        :param name: extension name , 插件名字
        :param app: CtpBee 实例
        """
        self.instrument_set: List or Set = set()
        self.extension_name = extension_name
        self.app = app
        self.func = []
        init = False
        if self.app is not None:
            self.init_app(self.app)
        # 是否冻结
        self.frozen = False
        if "cache_path" in kwargs:
            self.path = kwargs.get("cache_path")
            if not os.path.isdir(self.path):
                raise ValueError("请填写正确的缓存绝对路径")
        else:
            self.path = get_ctpbee_path()
        init = kwargs.get("init_position")
        if init and not isinstance(init, bool):
            raise TypeError(f"init参数应该设置为True或者False,而不是{type(init)}")

        # 单号如
        self.order_id_mapping = {}

        self.api_path = self.get_dir(self.path)
        self.level_position_manager = ApiPositionManager(
            self.extension_name, self.api_path, init)
Beispiel #2
0
def render_result(result,
                  kline=None,
                  trades=None,
                  datetimed=None,
                  trade_data=None,
                  strategy=[],
                  position_data=None,
                  account_data=None,
                  net_pnl=None,
                  cost_time=None,
                  **kwargs):
    """
    渲染结果并写入到本地html文件, 并返回html文件地址
    """
    """默认回测文件存放文件夹地址"""
    path = os.path.join(get_ctpbee_path(), "looper")
    time = str(datetimed).replace(" ", "_").replace(".", "_").replace(":", "_")
    filename = "{}_{}".format("_".join(strategy), time)
    abs_path = os.path.join(path, filename)
    print(abs_path)
    datetimed = str(datetimed.strftime("%Y-%m-%d_%H_%M_%S"))
    code_string = main_template.render(result=result,
                                       strategy=strategy,
                                       account_data=account_data,
                                       net_pnl=net_pnl,
                                       cost_time=cost_time,
                                       datetime=datetimed,
                                       file_name=filename,
                                       abs_path=abs_path)
    trade_code_string = trade_template.render(trade_data=trade_data,
                                              position_data=position_data)
    """ 回测主文件存放地址"""
    file_path = kwargs.get("file_path", None)
    """ 成交单文件存放地址 """
    trade_path = kwargs.get("trade_file_path", None)

    if not file_path:
        if not os.path.isdir(path):
            os.mkdir(path)

        file_path = join_path(path, filename + ".html")

    if not trade_path:
        trade_path = join_path(path, filename + "-trade.html")
    with open(file_path, "w", encoding="utf8") as f:
        f.write(code_string)
    with open(trade_path, "w", encoding="utf8") as f:
        f.write(trade_code_string)
    if kwargs.get("auto_open", None):
        webbrowser.open(file_path)
    return file_path
Beispiel #3
0
    def __init__(self, extension_name, app=None, **kwargs):
        """
        初始化调用函数

        Args:
          extension_name: 插件名称

          app(CtpBee): App核心

        Return:
            CtpbeeApi: 实例
        """
        self.instrument_set: List or Set = set()
        self.extension_name = extension_name
        self.app = app
        self.func = []
        self._count = 0
        self.__init_ready = False
        if self.app is not None:
            self.init_app(self.app)
        # 是否冻结
        self.frozen = False
        if "cache_path" in kwargs:
            self.path = kwargs.get("cache_path")
            if not os.path.isdir(self.path):
                raise ValueError("请填写正确的缓存绝对路径")
        else:
            self.path = get_ctpbee_path()
        init = kwargs.get("init_position", False)
        if init and not isinstance(init, bool):
            raise TypeError(f"init参数应该设置为True或者False,而不是{type(init)}")

        # 单号如
        self.order_id_mapping = {}

        self.api_path = self.get_dir(self.path)

        self.description = ""