复制链接
克隆策略
In [9]:
zz = m3.data.read()
zz[(zz['date']=='2022-07-01')&(zz['instrument'] == '300472.SZA')]
Out[9]:
close_0 date instrument mom1
2461454 29.006531 2022-07-01 300472.SZA -0.195004
In [10]:
qq = zz[zz['instrument'] == '300472.SZA']
In [20]:
qq.iloc[-44:]['close_0'].mean()/qq.iloc[-1]['close_0'] - 1
Out[20]:
-0.19500418733153357

    {"description":"实验创建于2017/8/26","graph":{"edges":[{"to_node_id":"-1080:features","from_node_id":"-331:data"},{"to_node_id":"-228:features","from_node_id":"-331:data"},{"to_node_id":"-1566:features","from_node_id":"-331:data"},{"to_node_id":"-1080:instruments","from_node_id":"-312:data"},{"to_node_id":"-32:instruments","from_node_id":"-312:data"},{"to_node_id":"-421:input_data","from_node_id":"-228:data"},{"to_node_id":"-228:input_data","from_node_id":"-1080:data"},{"to_node_id":"-567:input_ds","from_node_id":"-421:data"},{"to_node_id":"-32:options_data","from_node_id":"-567:sorted_data"}],"nodes":[{"node_id":"-331","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"mom1 = mean(close_0,44)/close_0 - 1\n\n","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-331"}],"output_ports":[{"name":"data","node_id":"-331"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-312","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2020-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2022-07-01","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":"-312"}],"output_ports":[{"name":"data","node_id":"-312"}],"cacheable":true,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-228","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":"False","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":"-228"},{"name":"features","node_id":"-228"}],"output_ports":[{"name":"data","node_id":"-228"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-1080","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":"-1080"},{"name":"features","node_id":"-1080"}],"output_ports":[{"name":"data","node_id":"-1080"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-421","module_id":"BigQuantSpace.dropnan.dropnan-v2","parameters":[],"input_ports":[{"name":"input_data","node_id":"-421"},{"name":"features","node_id":"-421"}],"output_ports":[{"name":"data","node_id":"-421"}],"cacheable":true,"seq_num":5,"comment":"","comment_collapsed":true},{"node_id":"-567","module_id":"BigQuantSpace.sort.sort-v5","parameters":[{"name":"sort_by","value":"mom1","type":"Literal","bound_global_parameter":null},{"name":"group_by","value":"date","type":"Literal","bound_global_parameter":null},{"name":"keep_columns","value":"--","type":"Literal","bound_global_parameter":null},{"name":"ascending","value":"True","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_ds","node_id":"-567"},{"name":"sort_by_ds","node_id":"-567"}],"output_ports":[{"name":"sorted_data","node_id":"-567"}],"cacheable":true,"seq_num":6,"comment":"","comment_collapsed":true},{"node_id":"-32","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 context.indicator_data = context.options['data'].read_df()\n context.set_commission(PerOrder(buy_cost=0.0001, sell_cost=0.001, min_cost=5))\n context.stock_num = 50\n context.rebalance_days = 22\n if 'index' not in context.extension:\n context.extension['index'] = 0\n \n \n","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n #------------------------------------------止损模块START--------------------------------------------\n date = data.current_dt.strftime('%Y-%m-%d')\n positions = {e.symbol: p.cost_basis for e, p in context.portfolio.positions.items()}\n current_stoploss_stock = [] \n if len(positions) > 0:\n for i in positions.keys():\n stock_cost = positions[i] \n stock_market_price = data.current(context.symbol(i), 'price') \n if (stock_market_price - stock_cost) / stock_cost <= -0.1: \n context.order_target_percent(context.symbol(i),0) \n current_stoploss_stock.append(i)\n print('日期:',date,'股票:',i,'出现止损状况')\n #-------------------------------------------止损模块END-------------------------------------------------- \n context.extension['index'] += 1\n if context.extension['index'] % context.rebalance_days != 0:\n return \n \n # 当前的日期\n date = data.current_dt.strftime('%Y-%m-%d')\n \n cur_data = context.indicator_data[context.indicator_data['date'] == date]\n # 根据日期获取调仓需要买入的股票的列表\n stock_to_buy = list(cur_data.instrument[:context.stock_num])\n # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表\n stock_hold_now = [equity.symbol for equity in context.portfolio.positions]\n # 继续持有的股票:调仓时,如果买入的股票已经存在于目前的持仓里,那么应继续持有\n no_need_to_sell = [i for i in stock_hold_now if i in stock_to_buy]\n # 需要卖出的股票\n stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]\n \n # 卖出\n for stock in stock_to_sell:\n if stock in current_stoploss_stock:\n continue\n if data.can_trade(context.symbol(stock)):\n context.order_target_percent(context.symbol(stock), 0)\n \n # 如果当天没有买入的股票,就返回\n if len(stock_to_buy) == 0:\n return\n \n weight = 1 / len(stock_to_buy) \n # 买入\n for stock in stock_to_buy:\n if data.can_trade(context.symbol(stock)):\n context.order_target_percent(context.symbol(stock), weight)\n ","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":"-32"},{"name":"options_data","node_id":"-32"},{"name":"history_ds","node_id":"-32"},{"name":"benchmark_ds","node_id":"-32"},{"name":"trading_calendar","node_id":"-32"}],"output_ports":[{"name":"raw_perf","node_id":"-32"}],"cacheable":false,"seq_num":7,"comment":"回测","comment_collapsed":true},{"node_id":"-1566","module_id":"BigQuantSpace.factorlens.factorlens-v1","parameters":[{"name":"title","value":"因子分析: {factor_name}","type":"Literal","bound_global_parameter":null},{"name":"start_date","value":"2020-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2022-07-01","type":"Literal","bound_global_parameter":null},{"name":"rebalance_period","value":"22","type":"Literal","bound_global_parameter":null},{"name":"delay_rebalance_days","value":0,"type":"Literal","bound_global_parameter":null},{"name":"rebalance_price","value":"close_0","type":"Literal","bound_global_parameter":null},{"name":"stock_pool","value":"全市场","type":"Literal","bound_global_parameter":null},{"name":"quantile_count","value":"5","type":"Literal","bound_global_parameter":null},{"name":"commission_rate","value":"0.0016","type":"Literal","bound_global_parameter":null},{"name":"returns_calculation_method","value":"累乘","type":"Literal","bound_global_parameter":null},{"name":"benchmark","value":"无","type":"Literal","bound_global_parameter":null},{"name":"drop_new_stocks","value":"True","type":"Literal","bound_global_parameter":null},{"name":"drop_price_limit_stocks","value":"True","type":"Literal","bound_global_parameter":null},{"name":"drop_st_stocks","value":"True","type":"Literal","bound_global_parameter":null},{"name":"drop_suspended_stocks","value":"True","type":"Literal","bound_global_parameter":null},{"name":"normalization","value":"True","type":"Literal","bound_global_parameter":null},{"name":"neutralization","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E8%A1%8C%E4%B8%9A%22%2C%22displayValue%22%3A%22%E8%A1%8C%E4%B8%9A%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%B8%82%E5%80%BC%22%2C%22displayValue%22%3A%22%E5%B8%82%E5%80%BC%22%2C%22selected%22%3Afalse%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"metrics","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%9B%A0%E5%AD%90%E8%A1%A8%E7%8E%B0%E6%A6%82%E8%A7%88%22%2C%22displayValue%22%3A%22%E5%9B%A0%E5%AD%90%E8%A1%A8%E7%8E%B0%E6%A6%82%E8%A7%88%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E5%9B%A0%E5%AD%90%E5%88%86%E5%B8%83%22%2C%22displayValue%22%3A%22%E5%9B%A0%E5%AD%90%E5%88%86%E5%B8%83%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E5%9B%A0%E5%AD%90%E8%A1%8C%E4%B8%9A%E5%88%86%E5%B8%83%22%2C%22displayValue%22%3A%22%E5%9B%A0%E5%AD%90%E8%A1%8C%E4%B8%9A%E5%88%86%E5%B8%83%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E5%9B%A0%E5%AD%90%E5%B8%82%E5%80%BC%E5%88%86%E5%B8%83%22%2C%22displayValue%22%3A%22%E5%9B%A0%E5%AD%90%E5%B8%82%E5%80%BC%E5%88%86%E5%B8%83%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22IC%E5%88%86%E6%9E%90%22%2C%22displayValue%22%3A%22IC%E5%88%86%E6%9E%90%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E4%B9%B0%E5%85%A5%E4%BF%A1%E5%8F%B7%E9%87%8D%E5%90%88%E5%88%86%E6%9E%90%22%2C%22displayValue%22%3A%22%E4%B9%B0%E5%85%A5%E4%BF%A1%E5%8F%B7%E9%87%8D%E5%90%88%E5%88%86%E6%9E%90%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E5%9B%A0%E5%AD%90%E4%BC%B0%E5%80%BC%E5%88%86%E6%9E%90%22%2C%22displayValue%22%3A%22%E5%9B%A0%E5%AD%90%E4%BC%B0%E5%80%BC%E5%88%86%E6%9E%90%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E5%9B%A0%E5%AD%90%E6%8B%A5%E6%8C%A4%E5%BA%A6%E5%88%86%E6%9E%90%22%2C%22displayValue%22%3A%22%E5%9B%A0%E5%AD%90%E6%8B%A5%E6%8C%A4%E5%BA%A6%E5%88%86%E6%9E%90%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E5%9B%A0%E5%AD%90%E5%80%BC%E6%9C%80%E5%A4%A7%2F%E6%9C%80%E5%B0%8F%E8%82%A1%E7%A5%A8%22%2C%22displayValue%22%3A%22%E5%9B%A0%E5%AD%90%E5%80%BC%E6%9C%80%E5%A4%A7%2F%E6%9C%80%E5%B0%8F%E8%82%A1%E7%A5%A8%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%9B%A0%E5%AD%90%E5%80%BC%22%2C%22displayValue%22%3A%22%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%9B%A0%E5%AD%90%E5%80%BC%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E5%A4%9A%E5%9B%A0%E5%AD%90%E7%9B%B8%E5%85%B3%E6%80%A7%E5%88%86%E6%9E%90%22%2C%22displayValue%22%3A%22%E5%A4%9A%E5%9B%A0%E5%AD%90%E7%9B%B8%E5%85%B3%E6%80%A7%E5%88%86%E6%9E%90%22%2C%22selected%22%3Atrue%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"factor_coverage","value":"0.5","type":"Literal","bound_global_parameter":null},{"name":"user_data_merge","value":"left","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features","node_id":"-1566"},{"name":"user_factor_data","node_id":"-1566"}],"output_ports":[{"name":"data","node_id":"-1566"}],"cacheable":true,"seq_num":8,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-331' Position='-370,-749,200,200'/><node_position Node='-312' Position='-787,-793,200,200'/><node_position Node='-228' Position='-586,-432,200,200'/><node_position Node='-1080' Position='-561.7906188964844,-604.7905883789062,200,200'/><node_position Node='-421' Position='-632,-322,200,200'/><node_position Node='-567' Position='-634,-182,200,200'/><node_position Node='-32' Position='-808,-75,200,200'/><node_position Node='-1566' Position='-210,-433,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [8]:
    # 本代码由可视化策略环境自动生成 2022年7月25日 16:43
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    def m7_initialize_bigquant_run(context):
        context.indicator_data = context.options['data'].read_df()
        context.set_commission(PerOrder(buy_cost=0.0001, sell_cost=0.001, min_cost=5))
        context.stock_num = 50
        context.rebalance_days = 22
        if 'index' not in context.extension:
            context.extension['index'] = 0
     
        
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m7_handle_data_bigquant_run(context, data):
        #------------------------------------------止损模块START--------------------------------------------
        date = data.current_dt.strftime('%Y-%m-%d')
        positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
        current_stoploss_stock = [] 
        if len(positions) > 0:
            for i in positions.keys():
                stock_cost = positions[i] 
                stock_market_price = data.current(context.symbol(i), 'price') 
                if (stock_market_price - stock_cost) / stock_cost <= -0.1:   
                    context.order_target_percent(context.symbol(i),0)     
                    current_stoploss_stock.append(i)
                    print('日期:',date,'股票:',i,'出现止损状况')
        #-------------------------------------------止损模块END--------------------------------------------------    
        context.extension['index'] += 1
        if  context.extension['index'] % context.rebalance_days != 0:
            return 
        
        # 当前的日期
        date = data.current_dt.strftime('%Y-%m-%d')
        
        cur_data = context.indicator_data[context.indicator_data['date'] == date]
        # 根据日期获取调仓需要买入的股票的列表
        stock_to_buy = list(cur_data.instrument[:context.stock_num])
        # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
        stock_hold_now = [equity.symbol for equity in context.portfolio.positions]
        # 继续持有的股票:调仓时,如果买入的股票已经存在于目前的持仓里,那么应继续持有
        no_need_to_sell = [i for i in stock_hold_now if i in stock_to_buy]
        # 需要卖出的股票
        stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]
      
        # 卖出
        for stock in stock_to_sell:
            if stock in current_stoploss_stock:
                continue
            if data.can_trade(context.symbol(stock)):
                context.order_target_percent(context.symbol(stock), 0)
                
        # 如果当天没有买入的股票,就返回
        if len(stock_to_buy) == 0:
            return
        
        weight =  1 / len(stock_to_buy)    
        # 买入
        for stock in stock_to_buy:
            if data.can_trade(context.symbol(stock)):
                context.order_target_percent(context.symbol(stock), weight)
     
    # 回测引擎:准备数据,只执行一次
    def m7_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m7_before_trading_start_bigquant_run(context, data):
        pass
    
    
    m1 = M.input_features.v1(
        features="""mom1 = mean(close_0,44)/close_0 - 1
    
    """
    )
    
    m8 = M.factorlens.v1(
        features=m1.data,
        title='因子分析: {factor_name}',
        start_date='2020-01-01',
        end_date='2022-07-01',
        rebalance_period=22,
        delay_rebalance_days=0,
        rebalance_price='close_0',
        stock_pool='全市场',
        quantile_count=5,
        commission_rate=0.0016,
        returns_calculation_method='累乘',
        benchmark='无',
        drop_new_stocks=True,
        drop_price_limit_stocks=True,
        drop_st_stocks=True,
        drop_suspended_stocks=True,
        normalization=True,
        neutralization=[],
        metrics=['因子表现概览', '因子分布', '因子行业分布', '因子市值分布', 'IC分析', '买入信号重合分析', '因子估值分析', '因子拥挤度分析', '因子值最大/最小股票', '表达式因子值', '多因子相关性分析'],
        factor_coverage=0.5,
        user_data_merge='left'
    )
    
    m2 = M.instruments.v2(
        start_date='2020-01-01',
        end_date='2022-07-01',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m4 = M.general_feature_extractor.v7(
        instruments=m2.data,
        features=m1.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m3 = M.derived_feature_extractor.v3(
        input_data=m4.data,
        features=m1.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m5 = M.dropnan.v2(
        input_data=m3.data
    )
    
    m6 = M.sort.v5(
        input_ds=m5.data,
        sort_by='mom1',
        group_by='date',
        keep_columns='--',
        ascending=True
    )
    
    m7 = M.trade.v4(
        instruments=m2.data,
        options_data=m6.sorted_data,
        start_date='',
        end_date='',
        initialize=m7_initialize_bigquant_run,
        handle_data=m7_handle_data_bigquant_run,
        prepare=m7_prepare_bigquant_run,
        before_trading_start=m7_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=''
    )