Пример #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
Пример #2
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