克隆策略

    {"description":"实验创建于2021/9/2","graph":{"edges":[{"to_node_id":"-17:instruments","from_node_id":"-4:data"},{"to_node_id":"-33:instruments","from_node_id":"-4:data"},{"to_node_id":"-17:features","from_node_id":"-12:data"},{"to_node_id":"-24:input_data","from_node_id":"-17:data"},{"to_node_id":"-33:options_data","from_node_id":"-24:data"}],"nodes":[{"node_id":"-4","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2013-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2017-11-07","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":0,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-4"}],"output_ports":[{"name":"data","node_id":"-4"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-12","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nrank_pb_lf_0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-12"}],"output_ports":[{"name":"data","node_id":"-12"}],"cacheable":true,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-17","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":"-17"},{"name":"features","node_id":"-17"}],"output_ports":[{"name":"data","node_id":"-17"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-24","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"rank_pb_lf_0 < 0.1","type":"Literal","bound_global_parameter":null},{"name":"output_left_data","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-24"}],"output_ports":[{"name":"data","node_id":"-24"},{"name":"left_data","node_id":"-24"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-33","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 # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)\n # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只\n stock_count = 5\n # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]\n context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])\n # 设置每只股票占用的最大资金比例\n context.max_cash_per_instrument = 0.2\n context.hold_days = 5\n","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n # 按日期过滤得到今日的预测数据\n if context.trading_day_index % 22 != 0:\n return\n\n # 按日期过滤得到当日的数据\n cur_data = context.data[context.data.date == data.current_dt.strftime('%Y-%m-%d')]\n buy_stock = list(cur_data['instrument'])\n\n # 买入股票\n for i in buy_stock:\n if data.can_trade(context.symbol(i)):\n context.order_target_percent(context.symbol(i), 1/len(buy_stock)) # 等权重买入\n \n # 持仓股票列表,为字符串\n is_staging = context.trading_day_index < context.hold_days # 是否在建仓期间(前 hold_days 天)\n equities = {e.symbol: p for e,p in context.portfolio.positions.items() if p.amount>0}\n for instrument in equities:\n if data.can_trade(context.symbol(instrument)):\n context.order_target_percent(context.symbol(instrument), 0)","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":"close","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":"-33"},{"name":"options_data","node_id":"-33"},{"name":"history_ds","node_id":"-33"},{"name":"benchmark_ds","node_id":"-33"},{"name":"trading_calendar","node_id":"-33"}],"output_ports":[{"name":"raw_perf","node_id":"-33"}],"cacheable":false,"seq_num":5,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-4' Position='146,111,200,200'/><node_position Node='-12' Position='468,112,200,200'/><node_position Node='-17' Position='287,236,200,200'/><node_position Node='-24' Position='287,322,200,200'/><node_position Node='-33' Position='224,394,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [9]:
    # 本代码由可视化策略环境自动生成 2021年9月2日 09:01
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    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))
        # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
        # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
        stock_count = 5
        # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]
        context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])
        # 设置每只股票占用的最大资金比例
        context.max_cash_per_instrument = 0.2
        context.hold_days = 5
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m5_handle_data_bigquant_run(context, data):
        # 按日期过滤得到今日的预测数据
        if context.trading_day_index % 22 != 0:
            return
    
        # 按日期过滤得到当日的数据
        cur_data = context.data[context.data.date == data.current_dt.strftime('%Y-%m-%d')]
        buy_stock = list(cur_data['instrument'])
    
        # 买入股票
        for i in buy_stock:
            if data.can_trade(context.symbol(i)):
                context.order_target_percent(context.symbol(i), 1/len(buy_stock))  # 等权重买入
                
        # 持仓股票列表,为字符串
        is_staging = context.trading_day_index < context.hold_days # 是否在建仓期间(前 hold_days 天)
        equities = {e.symbol: p for e,p in context.portfolio.positions.items() if p.amount>0}
        for instrument in equities:
            if data.can_trade(context.symbol(instrument)):
                context.order_target_percent(context.symbol(instrument), 0)
    # 回测引擎:准备数据,只执行一次
    def m5_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m5_before_trading_start_bigquant_run(context, data):
        pass
    
    
    m1 = M.instruments.v2(
        start_date='2013-01-01',
        end_date='2017-11-07',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m2 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    rank_pb_lf_0"""
    )
    
    m3 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m2.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m4 = M.filter.v3(
        input_data=m3.data,
        expr='rank_pb_lf_0 < 0.1',
        output_left_data=False
    )
    
    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='close',
        capital_base=1000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='真实价格',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-9-9835637ac2df> in <module>
         78 )
         79 
    ---> 80 m5 = M.trade.v4(
         81     instruments=m1.data,
         82     options_data=m4.data,
    
    <ipython-input-9-9835637ac2df> in m5_handle_data_bigquant_run(context, data)
         26 
         27     # 按日期过滤得到当日的数据
    ---> 28     cur_data = context.data[context.data.date == data.current_dt.strftime('%Y-%m-%d')]
         29     buy_stock = list(cur_data['instrument'])
         30 
    
    AttributeError: 'TradingAlgorithm' object has no attribute 'data'
    In [6]:
    m3.data.read().loc[m3.data.read()['rank_pb_lf_0'] <= 0.01,:]
    
    Out[6]:
    date instrument rank_pb_lf_0
    0 2017-01-03 000001.SZA 0.006716
    1 2017-01-04 000001.SZA 0.006697
    2 2017-01-05 000001.SZA 0.006700
    3 2017-01-06 000001.SZA 0.006334
    4 2017-01-09 000001.SZA 0.006334
    ... ... ... ...
    605104 2016-12-26 601998.SHA 0.009968
    605105 2016-12-27 601998.SHA 0.008878
    605106 2016-12-28 601998.SHA 0.008868
    605107 2016-12-29 601998.SHA 0.008868
    605108 2016-12-30 601998.SHA 0.008478

    30543 rows × 3 columns