コード例 #1
0
ファイル: ai_3_stg.py プロジェクト: mesosxzan/IBATS_Common
def _test_use(is_plot):
    from ibats_common import module_root_path
    import os
    # 参数设置
    run_mode = RunMode.Backtest
    strategy_params = {'unit': 100}
    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':
        '2014-1-1',
        # 'C:\GitHub\IBATS_Common\ibats_common\example\ru_price2.csv'
        'file_path':
        os.path.abspath(
            os.path.join(module_root_path, 'example', 'data', 'RB.csv')),
        'symbol_key':
        'instrument_type',
    }]
    if run_mode == RunMode.Realtime:
        trade_agent_params = {}
        strategy_handler_param = {}
    else:
        trade_agent_params = {
            'trade_mode': BacktestTradeMode.Order_2_Deal,
            'init_cash': 1000000,
            "calc_mode": CalcMode.Margin,
        }
        strategy_handler_param = {
            'date_from': '2014-1-1',  # 策略回测历史数据,回测指定时间段的历史行情
            'date_to': '2018-10-18',
        }
    # 初始化策略处理器
    stghandler = strategy_handler_factory(
        stg_class=AIStg,
        strategy_params=strategy_params,
        md_agent_params_list=md_agent_params_list,
        exchange_name=ExchangeName.LocalFile,
        run_mode=RunMode.Backtest,
        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
        show_order(stg_run_id)
        show_cash_and_margin(stg_run_id)
        show_rr_with_md(stg_run_id)

    return stg_run_id
コード例 #2
0
ファイル: run.py プロジェクト: yuanyichuangzhi/IBATS_Main
def run_strategy(num):
    choice = num - 1
    stg_func = strategy_list[choice]

    DEBUG = False
    # 参数设置
    run_mode = RunMode.Backtest
    strategy_params = {'unit': 100}
    md_agent_params_list = [{
        'md_period': PeriodType.Min1,
        'instrument_id_set': {'ETHUSD'},
        'init_md_date_from': '2018-7-19',  # 行情初始化加载历史数据,供策略分析预加载使用
        'init_md_date_to': '2018-10-21',
    }]
    if run_mode == RunMode.Realtime:
        trade_agent_params = {}
        strategy_handler_param = {}
    else:
        trade_agent_params = {
            'trade_mode': BacktestTradeMode.Order_2_Deal,
            'init_cash': 1000000,
        }
        strategy_handler_param = {
            'date_from': '2018-7-19',  # 策略回测历史数据,回测指定时间段的历史行情
            'date_to': '2018-7-20',
        }
    # 初始化策略处理器
    stghandler = strategy_handler_factory(
        stg_class=stg_func,
        strategy_params=strategy_params,
        md_agent_params_list=md_agent_params_list,
        exchange_name=ExchangeName.BitMex,
        run_mode=RunMode.Backtest,
        trade_agent_params=trade_agent_params,
        strategy_handler_param=strategy_handler_param,
    )

    if DEBUG:
        stghandler.run()
    else:
        # 开始执行策略
        stghandler.start()
        try:
            while not stghandler.is_done:
                time.sleep(2)
        except KeyboardInterrupt:
            logger.warning('程序中断中...')
        except RuntimeError:
            logger.exception('策略执行异常')

        stghandler.is_working = False
        stghandler.join(timeout=2)

    logger.info("执行结束")
コード例 #3
0
        'init_md_date_to': '2018-7-18',
    }]
    if run_mode == RunMode.Realtime:
        trade_agent_params = {}
        strategy_handler_param = {}
    else:
        trade_agent_params = {
            'trade_mode': BacktestTradeMode.Order_2_Deal,
            'init_cash': 1000000,
        }
        strategy_handler_param = {
            'date_from': '2018-10-29',  # 策略回测历史数据,回测指定时间段的历史行情
            'date_to': '2018-10-30',
        }
    # run_mode = RunMode.BackTest
    # 初始化策略处理器
    stghandler = strategy_handler_factory(
        stg_class=MACroseStg,
        strategy_params=strategy_params,
        md_agent_params_list=md_agent_params_list,
        exchange_name=ExchangeName.HuoBi,
        run_mode=RunMode.Backtest,
        trade_agent_params=trade_agent_params,
        strategy_handler_param=strategy_handler_param,
    )
    stghandler.start()
    time.sleep(10)
    stghandler.keep_running = False
    stghandler.join()
    logging.info("执行结束")
コード例 #4
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
コード例 #5
0
def get_stg_handler(retrain_period, q_table_key=None):
    from ibats_common import module_root_path
    import os
    # 参数设置
    instrument_type = 'RB'
    run_mode = RunMode.Backtest_FixPercent
    calc_mode = CalcMode.Normal
    if retrain_period == 0:
        strategy_params = {'unit': 1,
                           'module_name': f'ibats_common.example.reinforcement_learning.{module_version}.rl_stg',
                           'class_name': 'RLHandler',
                           'q_table_key': q_table_key}
    else:
        strategy_params = {'unit': 1,
                           'module_name': f'ibats_common.example.reinforcement_learning.{module_version}.rl_stg',
                           'class_name': 'RLHandler4Train',
                           'q_table_key': q_table_key,
                           'retrain_period': retrain_period
                           }

    md_agent_params_list = [{
        'md_period': PeriodType.Min1,
        'instrument_id_list': [instrument_type],
        'datetime_key': 'trade_date',
        'init_md_date_from': '1995-1-1',  # 行情初始化加载历史数据,供策略分析预加载使用
        'init_md_date_to': '2010-1-1',
        # 'C:\GitHub\IBATS_Common\ibats_common\example\ru_price2.csv'
        'file_path': os.path.abspath(os.path.join(module_root_path, 'example', 'data', 'RB.csv')),
        'symbol_key': 'instrument_type',
    }]
    if run_mode == RunMode.Realtime:
        trade_agent_params = {
        }
        strategy_handler_param = {
        }
    elif run_mode == RunMode.Backtest:
        trade_agent_params = {
            'trade_mode': BacktestTradeMode.Order_2_Deal,
            'init_cash': 1000000,
            "calc_mode": calc_mode,
        }
        strategy_handler_param = {
            'date_from': '2010-1-1',  # 策略回测历史数据,回测指定时间段的历史行情
            'date_to': '2018-10-18',
        }
    else:
        # RunMode.Backtest_FixPercent
        trade_agent_params = {
            'trade_mode': BacktestTradeMode.Order_2_Deal,
            "calc_mode": calc_mode,
        }
        strategy_handler_param = {
            'date_from': '2010-1-1',  # 策略回测历史数据,回测指定时间段的历史行情
            'date_to': '2018-10-18',
        }

    # 初始化策略处理器
    stghandler = strategy_handler_factory(
        stg_class=RLStg,
        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,
    )
    return stghandler