Example #1
0
def _test_use(is_plot):
    stghandler = get_stg_handler(retrain_period=7)
    stghandler.start()
    time.sleep(10)
    stghandler.keep_running = False
    stghandler.join()
    stg_run_id = stghandler.stg_run_id
    logging.info("执行结束 stg_run_id = %d", stg_run_id)

    if is_plot:
        # from ibats_common.analysis.plot_db import show_order, show_cash_and_margin, show_rr_with_md
        # from ibats_common.analysis.summary import summary_rr
        # show_order(stg_run_id)
        # df = show_cash_and_margin(stg_run_id)
        # sum_df, symbol_rr_dic, save_file_path_dic = show_rr_with_md(stg_run_id)
        # for symbol, rr_df in symbol_rr_dic.items():
        #     col_transfer_dic = {'return': rr_df.columns}
        #     summary_rr(rr_df, figure_4_each_col=True, col_transfer_dic=col_transfer_dic)
        from ibats_common.analysis.summary import summary_stg_2_docx
        from ibats_utils.mess import open_file_with_system_app
        file_path = summary_stg_2_docx(stg_run_id)
        if file_path is not None:
            open_file_with_system_app(file_path)

    return stg_run_id
Example #2
0
def plot_or_show(enable_save_plot=True,
                 enable_show_plot=True,
                 file_name=None,
                 stg_run_id=None,
                 do_clr=True):
    if enable_save_plot:
        if stg_run_id is None:
            folder_path = get_cache_folder_path()
        else:
            folder_path = os.path.join(get_cache_folder_path(),
                                       str(stg_run_id))

        if not os.path.exists(folder_path):
            os.makedirs(folder_path)

        file_path = os.path.join(folder_path, file_name)
        plt.savefig(file_path, dpi=75)
    else:
        file_path = None

    if enable_show_plot:
        if file_path is not None and is_windows_os():
            open_file_with_system_app(file_path)
        else:
            plt.show()

    if do_clr:
        plt.cla()
        plt.clf()
        plt.close()

    return file_path
Example #3
0
def _test_summary_md_2_docx(auto_open_file=True):
    from ibats_common.example.data import load_data
    instrument_type = 'RU'  # 'RB' 'RU'
    file_name = f"{instrument_type}.csv"

    factor_df = load_data(file_name).set_index('trade_date').drop(
        'instrument_type', axis=1)
    factor_df.index = pd.DatetimeIndex(factor_df.index)
    column_list_oraginal = list(factor_df.columns)
    ohlcav_col_name_list = ["open", "high", "low", "close", "amount", "volume"]

    from ibats_common.backend.factor import get_factor
    from ibats_common.example.data import get_trade_date_series
    from ibats_common.example.data import get_delivery_date_series
    factor_df = get_factor(
        factor_df,
        ohlcav_col_name_list=ohlcav_col_name_list,
        trade_date_series=get_trade_date_series(),
        delivery_date_series=get_delivery_date_series(instrument_type))

    col_transfer_dic = {'return': ['open', 'high', 'low', 'close', 'volume']}
    file_path = summary_md_2_docx(
        factor_df,
        enable_show_plot=False,
        enable_save_plot=True,
        close_key='close',
        name=instrument_type,
        func_kwargs_dic={
            "hist": {
                "figure_4_each_col": False,
                "columns": column_list_oraginal,
                "col_transfer_dic": col_transfer_dic,
            },
            "drawdown": {
                "col_name_list": ['close'],
            },
            "rr": {
                "col_name_list": ['close'],
            },
            "hist_future_n_rr": {
                'n_days': [3, 5],
                "columns": ['close'],
            },
            "rr_quantile": {
                'columns': ['close']
            },
            "validation": {
                'trade_date_max_gap': 10
            },
            # "": {},
        })
    if auto_open_file:
        open_file_with_system_app(file_path)
Example #4
0
def _test_df_2_table():
    df = pd.DataFrame({'a': list(range(10, 20)), 'b': list(range(10, 0, -1))})
    # 生成 docx 文件
    document = docx.Document()
    # 设置默认字体
    document.styles['Normal'].font.name = '微软雅黑'
    document.styles['Normal']._element.rPr.rFonts.set(
        docx.oxml.ns.qn('w:eastAsia'), '微软雅黑')

    document.add_heading(f'测试使用', 1)
    df_2_table(document, df, mark_top_n_on_cols=df.columns, mark_top_n=3)
    # file_path = os.path.abspath(os.path.join(os.path.curdir, 'test.docx'))
    file_path = "/home/mg/github/code_mess/drl/drl_off_example/d3qn_replay_2019_08_25/output/2013-11-08/model/reports/test.docx"
    document.save(file_path)
    open_file_with_system_app(file_path)
Example #5
0
def _test_dic_2_table():
    # 生成 docx 文件
    document = docx.Document()
    # 设置默认字体
    document.styles['Normal'].font.name = '微软雅黑'
    document.styles['Normal']._element.rPr.rFonts.set(
        docx.oxml.ns.qn('w:eastAsia'), '微软雅黑')
    # 创建自定义段落样式(第一个参数为样式名, 第二个参数为样式类型, 1为段落样式, 2为字符样式, 3为表格样式)
    UserStyle1 = document.styles.add_style('UserStyle1', 1)
    # 设置字体尺寸
    UserStyle1.font.size = docx.shared.Pt(40)
    # 设置字体颜色
    UserStyle1.font.color.rgb = docx.shared.RGBColor(0xff, 0xde, 0x00)
    # 居中文本
    UserStyle1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    # 设置中文字体
    UserStyle1.font.name = '微软雅黑'
    UserStyle1._element.rPr.rFonts.set(docx.oxml.ns.qn('w:eastAsia'), '微软雅黑')

    # 文件内容
    heading_count, sub_heading_count = 0, 0
    document.add_heading('测试使用', 0).alignment = WD_ALIGN_PARAGRAPH.CENTER
    document.add_paragraph('')
    document.add_paragraph('')

    dic = {
        'from_date':
        str_2_date('2018-08-09'),
        'name':
        'name test',
        'num':
        12345,
        'float':
        23456.789,
        'list': [1, 2, 3, 4],
        'long text':
        """bla bla bla(唧唧歪歪) 在英语中是“等等,之类的”的意思。
        当别人知道你要表达的意思时,用blablabla会比较方便,可要注意用的场合与人。在某种场合也形容某些人比较八卦。"""
    }
    file_path = 'test.docx'
    dic_2_table(
        document,
        dic,
        col_group_num=2,
    )
    document.save(file_path)
    open_file_with_system_app(file_path)
    return file_path
Example #6
0
def _test_summary_release_2_docx():
    real_ys, pred_ys = np.random.randint(1, 3, size=100), np.random.randint(1, 3, size=100)
    date_arr = pd.date_range(pd.to_datetime('2018-01-01'),
                             pd.to_datetime('2018-01-01') + pd.Timedelta(days=99))
    date_index = pd.DatetimeIndex(date_arr)
    close_df = pd.DataFrame({'close': np.sin(np.linspace(0, 10, 100))}, index=date_index)

    split_point_list = np.concatenate((np.random.randint(1, len(date_arr) - 1, size=10), [1, len(date_arr) - 1]))
    split_point_list.sort()
    split_point_list = date_arr[split_point_list]
    base_line_list = [0.3, 0.6]
    img_meta_dic_list = []
    img_file_path = show_dl_accuracy(real_ys, pred_ys, close_df, split_point_list, base_line_list, show_moving_avg=True)
    img_meta_dic_list.append({
        'img_file_path': img_file_path,
        'trade_date_last_train': pd.to_datetime('2018-01-01'),
        'module_file_path':
            "/home/mg/github/IBATS_Common/ibats_common/tf_saves_2019-06-25_08_13_36/model_tfls/2012-12-31",
        'predict_test_random_state': 1,
        'split_point_list': split_point_list,
        'in_range_count': close_df.shape[0],
        'trade_date_end': pd.to_datetime('2018-01-01') + pd.Timedelta(days=99),
    })
    img_file_path = show_dl_accuracy(real_ys, pred_ys, close_df, split_point_list, base_line_list, show_moving_avg=False)
    img_meta_dic_list.append({
        'img_file_path': img_file_path,
        'trade_date_last_train': pd.to_datetime('2018-01-01'),
        'module_file_path':
            "/home/mg/github/IBATS_Common/ibats_common/tf_saves_2019-06-25_08_13_36/model_tfls/2012-12-31",
        'predict_test_random_state': 1,
        'split_point_list': split_point_list,
        'in_range_count': close_df.shape[0],
        'trade_date_end': pd.to_datetime('2018-01-01') + pd.Timedelta(days=99),
    })
    file_path = summary_release_2_docx('test_only', img_meta_dic_list)
    open_file_with_system_app(file_path)
Example #7
0
def _test_summary_stg_2_docx(auto_open_file=True):
    stg_run_id = None
    file_path = summary_stg_2_docx(stg_run_id, enable_clean_cache=True)
    if auto_open_file and file_path is not None:
        open_file_with_system_app(file_path)
Example #8
0
def _test_use(is_plot):
    from ibats_common.backend.mess import get_folder_path
    import os
    # 参数设置
    run_mode = RunMode.Backtest
    trade_mode = BacktestTradeMode.Order_2_Deal
    calc_mode = CalcMode.Normal
    md_agent_params_list = [{
        'md_period':
        PeriodType.Min1,
        'instrument_id_list': ['RB'],
        'datetime_key':
        'trade_date',
        'init_md_date_from':
        '1995-1-1',  # 行情初始化加载历史数据,供策略分析预加载使用
        'init_md_date_to':
        '2010-1-1',
        'file_path':
        os.path.abspath(
            os.path.join(get_folder_path('example', create_if_not_found=False),
                         'data', 'RB.csv')),
        'symbol_key':
        'instrument_type',
    }]
    if run_mode == RunMode.Realtime:
        strategy_params = {'unit': 100}
        trade_agent_params = {}
        strategy_handler_param = {}
    elif run_mode == RunMode.Backtest:
        strategy_params = {'unit': 100}
        trade_agent_params = {
            'trade_mode': trade_mode,
            'init_cash': 1000000,
            "calc_mode": calc_mode,
        }
        strategy_handler_param = {
            'date_from': '2010-1-1',  # 策略回测历史数据,回测指定时间段的历史行情
            'date_to': '2018-10-18',
        }
    else:
        # RunMode.Backtest_FixPercent
        strategy_params = {'unit': 1}
        trade_agent_params = {
            'trade_mode': trade_mode,
            "calc_mode": calc_mode,
        }
        strategy_handler_param = {
            'date_from': '2010-1-1',  # 策略回测历史数据,回测指定时间段的历史行情
            'date_to': '2018-10-18',
        }

    # 初始化策略处理器
    stghandler = strategy_handler_factory(
        stg_class=MACrossStg,
        strategy_params=strategy_params,
        md_agent_params_list=md_agent_params_list,
        exchange_name=ExchangeName.LocalFile,
        run_mode=run_mode,
        trade_agent_params=trade_agent_params,
        strategy_handler_param=strategy_handler_param,
    )
    stghandler.start()
    time.sleep(10)
    stghandler.keep_running = False
    stghandler.join()
    stg_run_id = stghandler.stg_run_id
    logging.info("执行结束 stg_run_id = %d", stg_run_id)

    if is_plot:
        # from ibats_common.analysis.plot_db import show_order, show_cash_and_margin, show_rr_with_md
        # from ibats_common.analysis.summary import summary_rr
        # show_order(stg_run_id)
        # df = show_cash_and_margin(stg_run_id)
        # sum_df, symbol_rr_dic, save_file_path_dic = show_rr_with_md(stg_run_id)
        # for symbol, rr_df in symbol_rr_dic.items():
        #     col_transfer_dic = {'return': rr_df.columns}
        #     summary_rr(rr_df, figure_4_each_col=True, col_transfer_dic=col_transfer_dic)
        from ibats_common.analysis.summary import summary_stg_2_docx
        from ibats_utils.mess import open_file_with_system_app
        file_path = summary_stg_2_docx(stg_run_id)
        if file_path is not None:
            open_file_with_system_app(file_path)

    return stg_run_id