克隆策略

海龟模板策略

版本 v1.0

目录

策略交易规则

策略构建步骤

正文

一、海龟模板策略的交易规则

当今天的收盘价大于过去20个交易日中的最高价时,以开盘价买入。 买入后,当收盘价小于过去10个交易日中的最低价时,以开盘价卖出。

二、策略构建步骤

1、确定股票池和回测时间 通过证券代码列表输入要回测的两只股票,以及回测的起止日期。

2、确定买卖原则

当今天的收盘价大于过去20个交易日中的最高价时,买入;买入后,收盘价小于过去10个交易日中的最低价时,卖出。

3、模拟回测

通过 trade 模块中的初始化函数定义交易手续费。
通过 trade 模块中的主函数(handle函数)实现交易规则,并打印交易日志。

    {"description":"实验创建于2021/11/25","graph":{"edges":[{"to_node_id":"-150:instruments","from_node_id":"-290:data"},{"to_node_id":"-169:instruments","from_node_id":"-290:data"},{"to_node_id":"-150:features","from_node_id":"-145:data"},{"to_node_id":"-157:features","from_node_id":"-145:data"},{"to_node_id":"-157:input_data","from_node_id":"-150:data"},{"to_node_id":"-169:options_data","from_node_id":"-157:data"}],"nodes":[{"node_id":"-290","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2018-07-17","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2021-11-18","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"600519.SHA","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":"0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-290"}],"output_ports":[{"name":"data","node_id":"-290"}],"cacheable":true,"seq_num":1,"comment":"输入证券","comment_collapsed":true},{"node_id":"-145","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nhigh_20=ts_max(high_1,20)\nlow_10=ts_min(low_1,10)\n ","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-145"}],"output_ports":[{"name":"data","node_id":"-145"}],"cacheable":true,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-150","module_id":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"before_start_days","value":90,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-150"},{"name":"features","node_id":"-150"}],"output_ports":[{"name":"data","node_id":"-150"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-157","module_id":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","parameters":[{"name":"date_col","value":"date","type":"Literal","bound_global_parameter":null},{"name":"instrument_col","value":"instrument","type":"Literal","bound_global_parameter":null},{"name":"drop_na","value":"True","type":"Literal","bound_global_parameter":null},{"name":"remove_extra_columns","value":"False","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"{}","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-157"},{"name":"features","node_id":"-157"}],"output_ports":[{"name":"data","node_id":"-157"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-169","module_id":"BigQuantSpace.trade.trade-v4","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"initialize","value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载外部数据\n context.ranker_prediction = context.options['data'].read_df()\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n ","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n # 按日期过滤得到今日的预测数据\n option_data = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n\n sid = context.symbol(context.instruments[0])\n price = data.current(sid, 'price') # 最新价\n high_point = option_data['high_20'].values # 20日高点 \n low_point = option_data['low_10'].values # 10日低点 \n cur_position = context.portfolio.positions[sid].amount # 持仓\n \n # 交易逻辑\n # 最新价大于等于20日高点,并且处于空仓状态,并且该股票当日可以交易\n if price >= high_point and cur_position == 0 and data.can_trade(sid): \n context.order_target_percent(sid, 1) \n print('{}全仓买入{}股票'.format(data.current_dt.strftime('%Y-%m-%d'),sid.symbol))\n elif price <= low_point and cur_position > 0 and data.can_trade(sid): \n context.order_target_percent(sid, 0) \n print('{}卖出全部{}股票'.format(data.current_dt.strftime('%Y-%m-%d'),sid.symbol))","type":"Literal","bound_global_parameter":null},{"name":"prepare","value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"before_trading_start","value":"# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"volume_limit","value":0.025,"type":"Literal","bound_global_parameter":null},{"name":"order_price_field_buy","value":"open","type":"Literal","bound_global_parameter":null},{"name":"order_price_field_sell","value":"open","type":"Literal","bound_global_parameter":null},{"name":"capital_base","value":1000000,"type":"Literal","bound_global_parameter":null},{"name":"auto_cancel_non_tradable_orders","value":"True","type":"Literal","bound_global_parameter":null},{"name":"data_frequency","value":"daily","type":"Literal","bound_global_parameter":null},{"name":"price_type","value":"后复权","type":"Literal","bound_global_parameter":null},{"name":"product_type","value":"股票","type":"Literal","bound_global_parameter":null},{"name":"plot_charts","value":"True","type":"Literal","bound_global_parameter":null},{"name":"backtest_only","value":"False","type":"Literal","bound_global_parameter":null},{"name":"benchmark","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-169"},{"name":"options_data","node_id":"-169"},{"name":"history_ds","node_id":"-169"},{"name":"benchmark_ds","node_id":"-169"},{"name":"trading_calendar","node_id":"-169"}],"output_ports":[{"name":"raw_perf","node_id":"-169"}],"cacheable":false,"seq_num":5,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-290' Position='101,160,200,200'/><node_position Node='-145' Position='429,152,200,200'/><node_position Node='-150' Position='434,265,200,200'/><node_position Node='-157' Position='441,369,200,200'/><node_position Node='-169' Position='217,484,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [123]:
    # 本代码由可视化策略环境自动生成 2021年11月25日 17:47
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    def m5_initialize_bigquant_run(context):
        # 加载外部数据
        context.ranker_prediction = context.options['data'].read_df()
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
       
    # 回测引擎:每日数据处理函数,每天执行一次
    def m5_handle_data_bigquant_run(context, data):
        # 按日期过滤得到今日的预测数据
        option_data = context.ranker_prediction[
            context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
    
        sid = context.symbol(context.instruments[0])
        price = data.current(sid, 'price') # 最新价
        high_point = option_data['high_20'].values # 20日高点 
        low_point = option_data['low_10'].values  # 10日低点    
        cur_position = context.portfolio.positions[sid].amount   # 持仓
        
        # 交易逻辑
        #  最新价大于等于20日高点,并且处于空仓状态,并且该股票当日可以交易
        if price >= high_point and cur_position == 0 and data.can_trade(sid):  
            context.order_target_percent(sid, 1) 
            print('{}全仓买入{}股票'.format(data.current_dt.strftime('%Y-%m-%d'),sid.symbol))
        elif price <= low_point  and cur_position > 0 and data.can_trade(sid): 
            context.order_target_percent(sid, 0) 
            print('{}卖出全部{}股票'.format(data.current_dt.strftime('%Y-%m-%d'),sid.symbol))
    # 回测引擎:准备数据,只执行一次
    def m5_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m5_before_trading_start_bigquant_run(context, data):
        pass
    
    
    m1 = M.instruments.v2(
        start_date='2018-07-17',
        end_date='2021-11-18',
        market='CN_STOCK_A',
        instrument_list='600519.SHA',
        max_count=0
    )
    
    m2 = M.input_features.v1(
        features="""# #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    high_20=ts_max(high_1,20)
    low_10=ts_min(low_1,10)
     """
    )
    
    m3 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m2.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m4 = M.derived_feature_extractor.v3(
        input_data=m3.data,
        features=m2.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m5 = M.trade.v4(
        instruments=m1.data,
        options_data=m4.data,
        start_date='',
        end_date='',
        initialize=m5_initialize_bigquant_run,
        handle_data=m5_handle_data_bigquant_run,
        prepare=m5_prepare_bigquant_run,
        before_trading_start=m5_before_trading_start_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=1000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
    2018-09-21全仓买入600519.SHA股票
    2018-10-11卖出全部600519.SHA股票
    2018-12-03全仓买入600519.SHA股票
    2018-12-21卖出全部600519.SHA股票
    2019-01-09全仓买入600519.SHA股票
    2019-05-06卖出全部600519.SHA股票
    2019-06-20全仓买入600519.SHA股票
    2019-07-18卖出全部600519.SHA股票
    2019-08-12全仓买入600519.SHA股票
    2019-09-11卖出全部600519.SHA股票
    2019-09-20全仓买入600519.SHA股票
    2019-11-25卖出全部600519.SHA股票
    2019-12-30全仓买入600519.SHA股票
    2020-01-03卖出全部600519.SHA股票
    2020-03-04全仓买入600519.SHA股票
    2020-03-17卖出全部600519.SHA股票
    2020-04-14全仓买入600519.SHA股票
    2020-09-17卖出全部600519.SHA股票
    2020-11-23全仓买入600519.SHA股票
    2021-02-25卖出全部600519.SHA股票
    2021-04-02全仓买入600519.SHA股票
    2021-05-06卖出全部600519.SHA股票
    2021-05-25全仓买入600519.SHA股票
    2021-06-18卖出全部600519.SHA股票
    2021-09-27全仓买入600519.SHA股票
    2021-11-10卖出全部600519.SHA股票
    
    • 收益率50.79%
    • 年化收益率13.6%
    • 基准收益率39.33%
    • 阿尔法0.08
    • 贝塔0.5
    • 夏普比率0.54
    • 胜率0.38
    • 盈亏比2.82
    • 收益波动率23.02%
    • 信息比率0.01
    • 最大回撤34.51%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-a8455226040744e198a84947f26ab04a"}/bigcharts-data-end