复制链接
克隆策略

    {"description":"实验创建于2019/4/20","graph":{"edges":[{"to_node_id":"-299:instruments","from_node_id":"-290:data"},{"to_node_id":"-32:instruments","from_node_id":"-290:data"},{"to_node_id":"-299:features","from_node_id":"-286:data"},{"to_node_id":"-606:input_ds","from_node_id":"-299:data"},{"to_node_id":"-53:input_data","from_node_id":"-606:sorted_data"},{"to_node_id":"-32:options_data","from_node_id":"-53:data"}],"nodes":[{"node_id":"-290","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2018-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2020-12-31","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":"-290"}],"output_ports":[{"name":"data","node_id":"-290"}],"cacheable":true,"seq_num":1,"comment":"输入证券","comment_collapsed":true},{"node_id":"-286","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"pb_lf_0\npe_ttm_0\namount_0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-286"}],"output_ports":[{"name":"data","node_id":"-286"}],"cacheable":true,"seq_num":2,"comment":"输入特征","comment_collapsed":true},{"node_id":"-299","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":"60","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-299"},{"name":"features","node_id":"-299"}],"output_ports":[{"name":"data","node_id":"-299"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-606","module_id":"BigQuantSpace.sort.sort-v4","parameters":[{"name":"sort_by","value":"pe_ttm_0,pb_lf_0","type":"Literal","bound_global_parameter":null},{"name":"group_by","value":"instrument","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":"-606"},{"name":"sort_by_ds","node_id":"-606"}],"output_ports":[{"name":"sorted_data","node_id":"-606"}],"cacheable":true,"seq_num":4,"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 # 加载股票指标数据,数据继承自m6模块\n context.indicator_data = context.options['data'].read_df()\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0001, sell_cost=0.001, min_cost=5))\n # 设置股票数量\n context.stock_num = 30\n context.month_data = context.indicator_data.reset_index(drop=True)\n \n #每月调仓,改用每月调用一次主处理函数\n schedule_function(func=month_handle_data,\n date_rule=date_rules.month_start(), # 月初执行\n time_rule=time_rules.market_open()) # 开盘执行\n \n \n","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"from datetime import timedelta\n#每月初调用一次主处理函数,在本函数进行目标股票获取并进行买卖\ndef month_handle_data(context, data):\n # 当前的日期\n date = data.current_dt.strftime('%Y-%m-%d')\n \n #根据当前时间获取上一个月的时间。当前月初往前偏移28天一定是上个月\n last_month = str(pd.to_datetime(date) + timedelta(days=-28))[0:7]\n #获取上个月末的股票池\n df = context.month_data\n tempdf = df[df['date'].astype(str).apply(lambda x:x[0:7])==last_month]\n #对股票池根据因子值列进行排序,并取前30只股票\n stocks = tempdf.sort_values('pe_ttm_0').iloc[:context.stock_num,:]\n #把股票id转换成需要购买的股票列表\n stock_to_buy = stocks['instrument'].values.tolist()\n print(\"选股日期:\",date,',选股数量:',len(stock_to_buy))\n \n \n positions = {e.symbol: p.cost_basis for e, p in context.portfolio.positions.items()}\n # 新建当日止损股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断\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 # 亏10%就止损\n if (stock_market_price - stock_cost) / stock_cost <= -0.10: \n context.order_target_percent(context.symbol(i),0) \n current_stoploss_stock.append(i)\n# print('日期:',date,'股票:',i,'出现止损状况')\n \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 # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态\n # 如果返回真值,则可以正常下单,否则会出错\n # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式\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 # 等权重买入 \n weight = 1 / len(stock_to_buy)\n \n # 买入\n for stock in stock_to_buy:\n if data.can_trade(context.symbol(stock)):\n # 下单使得某只股票的持仓权重达到weight,因为\n # weight大于0,因此是等权重买入\n context.order_target_percent(context.symbol(stock), weight)\n\n \n \ndef bigquant_run(context, data):\n pass","type":"Literal","bound_global_parameter":null},{"name":"prepare","value":"\n\n# 回测引擎:准备数据,只执行一次\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":5,"comment":"回测","comment_collapsed":true},{"node_id":"-53","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"pb_lf_0 < 1.5 & pe_ttm_0 < 15 & amount_0 > 0 & pb_lf_0 > 0 & pe_ttm_0 > 0","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":"-53"}],"output_ports":[{"name":"data","node_id":"-53"},{"name":"left_data","node_id":"-53"}],"cacheable":true,"seq_num":6,"comment":"过滤掉不需要的股票数据","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-290' Position='-104,20,200,200'/><node_position Node='-286' Position='315,17,200,200'/><node_position Node='-299' Position='225,137,200,200'/><node_position Node='-606' Position='229,225,200,200'/><node_position Node='-32' Position='70,430,200,200'/><node_position Node='-53' Position='215,304,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [10]:
    # 本代码由可视化策略环境自动生成 2022年4月21日 15:29
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    def m5_initialize_bigquant_run(context):   
        # 加载股票指标数据,数据继承自m6模块
        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 = 30
        context.month_data =  context.indicator_data.reset_index(drop=True)
     
        #每月调仓,改用每月调用一次主处理函数
        schedule_function(func=month_handle_data,
            date_rule=date_rules.month_start(),   # 月初执行
            time_rule=time_rules.market_open())   # 开盘执行
        
        
    
    from datetime import timedelta
    #每月初调用一次主处理函数,在本函数进行目标股票获取并进行买卖
    def month_handle_data(context, data):
        # 当前的日期
        date = data.current_dt.strftime('%Y-%m-%d')
        
        #根据当前时间获取上一个月的时间。当前月初往前偏移28天一定是上个月
        last_month = str(pd.to_datetime(date) + timedelta(days=-28))[0:7]
        #获取上个月末的股票池
        df = context.month_data
        tempdf = df[df['date'].astype(str).apply(lambda x:x[0:7])==last_month]
        #对股票池根据因子值列进行排序,并取前30只股票
        stocks = tempdf.sort_values('pe_ttm_0').iloc[:context.stock_num,:]
        #把股票id转换成需要购买的股票列表
        stock_to_buy = stocks['instrument'].values.tolist()
        print("选股日期:",date,',选股数量:',len(stock_to_buy))
        
        
        positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
        # 新建当日止损股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
        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') 
                # 亏10%就止损
                if (stock_market_price - stock_cost) / stock_cost <= -0.10:   
                    context.order_target_percent(context.symbol(i),0)     
                    current_stoploss_stock.append(i)
    #                 print('日期:',date,'股票:',i,'出现止损状况')
        
        # 通过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:
            # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态
            # 如果返回真值,则可以正常下单,否则会出错
            # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式
            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)):
                # 下单使得某只股票的持仓权重达到weight,因为
                # weight大于0,因此是等权重买入
                context.order_target_percent(context.symbol(stock), weight)
    
                
                
    def m5_handle_data_bigquant_run(context, data):
        pass
    
    
    # 回测引擎:准备数据,只执行一次
    def m5_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m5_before_trading_start_bigquant_run(context, data):
        pass
    
    
    m1 = M.instruments.v2(
        start_date='2018-01-01',
        end_date='2020-12-31',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m2 = M.input_features.v1(
        features="""pb_lf_0
    pe_ttm_0
    amount_0"""
    )
    
    m3 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m2.data,
        start_date='',
        end_date='',
        before_start_days=60
    )
    
    m4 = M.sort.v4(
        input_ds=m3.data,
        sort_by='pe_ttm_0,pb_lf_0',
        group_by='instrument',
        keep_columns='--',
        ascending=True
    )
    
    m6 = M.filter.v3(
        input_data=m4.sorted_data,
        expr='pb_lf_0 < 1.5 & pe_ttm_0 < 15 & amount_0 > 0 & pb_lf_0 > 0 & pe_ttm_0 > 0',
        output_left_data=False
    )
    
    m5 = M.trade.v4(
        instruments=m1.data,
        options_data=m6.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-01-02 ,选股数量: 30
    选股日期: 2018-02-01 ,选股数量: 30
    选股日期: 2018-03-01 ,选股数量: 30
    选股日期: 2018-04-02 ,选股数量: 30
    选股日期: 2018-05-02 ,选股数量: 30
    选股日期: 2018-06-01 ,选股数量: 30
    选股日期: 2018-07-02 ,选股数量: 30
    选股日期: 2018-08-01 ,选股数量: 30
    选股日期: 2018-09-03 ,选股数量: 30
    选股日期: 2018-10-08 ,选股数量: 30
    选股日期: 2018-11-01 ,选股数量: 30
    选股日期: 2018-12-03 ,选股数量: 30
    选股日期: 2019-01-02 ,选股数量: 30
    选股日期: 2019-02-01 ,选股数量: 30
    选股日期: 2019-03-01 ,选股数量: 30
    选股日期: 2019-04-01 ,选股数量: 30
    选股日期: 2019-05-06 ,选股数量: 30
    选股日期: 2019-06-03 ,选股数量: 30
    选股日期: 2019-07-01 ,选股数量: 30
    选股日期: 2019-08-01 ,选股数量: 30
    选股日期: 2019-09-02 ,选股数量: 30
    选股日期: 2019-10-08 ,选股数量: 30
    选股日期: 2019-11-01 ,选股数量: 30
    选股日期: 2019-12-02 ,选股数量: 30
    选股日期: 2020-01-02 ,选股数量: 30
    选股日期: 2020-02-03 ,选股数量: 30
    选股日期: 2020-03-02 ,选股数量: 30
    选股日期: 2020-04-01 ,选股数量: 30
    选股日期: 2020-05-06 ,选股数量: 30
    选股日期: 2020-06-01 ,选股数量: 30
    选股日期: 2020-07-01 ,选股数量: 30
    选股日期: 2020-08-03 ,选股数量: 30
    选股日期: 2020-09-01 ,选股数量: 30
    选股日期: 2020-10-09 ,选股数量: 30
    选股日期: 2020-11-02 ,选股数量: 30
    选股日期: 2020-12-01 ,选股数量: 30
    
    • 收益率32.17%
    • 年化收益率10.11%
    • 基准收益率29.29%
    • 阿尔法0.06
    • 贝塔0.31
    • 夏普比率0.48
    • 胜率0.61
    • 盈亏比0.89
    • 收益波动率16.7%
    • 信息比率-0.0
    • 最大回撤31.31%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-5dcee5ec341a463db52797860b85ea2b"}/bigcharts-data-end