为什么我运行的策略没有数据呢?


(dgx1013) #1

https://bigquant.com/trade/strategy?notebook_id=438874f0-6232-11e8-b4a5-00163e004d3f


(达达) #2

您试一下这个

克隆策略

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81:options_data","SourceOutputPortId":"-86:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nclose_0\nlow_0\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2015-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2017-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"预测数据,用于回测和模拟","CommentCollapsed":false},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-70","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v6","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-76","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v2","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-81","ModuleId":"BigQuantSpace.trade.trade-v3","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n date = data.current_dt.strftime('%Y-%m-%d') # 日期\n buy_stock = context.daily_stock_to_buy[date] # 当日符合买入条件的股票\n sell_stock = context.daily_stock_to_sell[date] # 当日符合卖出条件的股票\n weight = 1/context.num_stock # 等权重配置\n stock_hold_num = len(context.portfolio.positions) # 目前持有的股票数量\n remain_num = context.num_stock - stock_hold_num # 还可以买入的股票数量,需要把卖出的股票加回来\n # 初始化当日买入订单的数量为0 \n order_count = 0\n # 买入股票\n if len(buy_stock)>0:\n for i in buy_stock:\n # 如果发送买入订单的股票数量已经超过了还可以买入的股票数量,那么应退出for循环\n if order_count >= remain_num:\n break\n # 对于没有买入的股票且可以交易的股票,应买入\n if context.portfolio.positions[context.symbol(i)].amount == 0 and data.can_trade(context.symbol(i)):\n order_target_percent(context.symbol(i), weight)\n order_count += 1 # 统计一下当天股票买入数量\n # 卖出股票\n if len(sell_stock)>0:\n for j in sell_stock:\n if context.portfolio.positions[context.symbol(j)].amount > 0 and data.can_trade(context.symbol(j)):\n order_target_percent(context.symbol(j), 0)","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n # 加载原始数据\n stock_raw_data = context.options['data'].read_df()\n\n\n # 计算多个周期均线的函数\n def ma_calculate(df):\n ma_list = [5,10,20,30]\n for ma_len in ma_list:\n df['ma_'+str(ma_len)] = pd.rolling_mean(df['close_0'], ma_len)\n return df\n\n # 函数:求满足开仓条件的股票列表\n def open_pos_con(df):\n return list(df[(df['ma_5']>df['ma_10'])&(df['ma_10']>df['ma_20'])&(df['ma_20']>df['ma_30'])&(df['low_0']>df['ma_30'])&(df['low_0']<df['ma_10'])].instrument)\n\n # 函数:求满足平仓条件的股票列表\n def close_pos_con(df):\n return list(df[df['ma_5']<df['ma_10']].instrument)\n \n # 包含多个周期均线值的股票数据\n stock_ma_data = stock_raw_data.groupby('instrument').apply(ma_calculate)\n # 每日买入股票的数据框\n context.daily_stock_to_buy= stock_ma_data.groupby('date').apply(open_pos_con)\n # 每日卖出股票的数据框\n context.daily_stock_to_sell= stock_ma_data.groupby('date').apply(close_pos_con)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n # 设置买入的股票数量\n context.num_stock = 100 # 最多同时持有100只股票\n\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":1000000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"amount_integer","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-86","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-86"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-86","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='765,21,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='1074,127,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-70' Position='1078,236,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-76' Position='1081,327,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-81' Position='1079,566,200,200'/><NodePosition Node='-86' Position='1078,418,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":false}
    In [6]:
    # 本代码由可视化策略环境自动生成 2018年5月31日 10:42
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m1 = M.input_features.v1(
        features="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    close_0
    low_0
    """
    )
    
    m2 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2015-01-01'),
        end_date=T.live_run_param('trading_date', '2017-01-01'),
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m3 = M.general_feature_extractor.v6(
        instruments=m2.data,
        features=m1.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m4 = M.derived_feature_extractor.v2(
        input_data=m3.data,
        features=m1.data,
        date_col='date',
        instrument_col='instrument'
    )
    
    m6 = M.dropnan.v1(
        input_data=m4.data
    )
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m5_handle_data_bigquant_run(context, data):
        date = data.current_dt.strftime('%Y-%m-%d')  # 日期
        buy_stock = context.daily_stock_to_buy[date]  # 当日符合买入条件的股票
        sell_stock = context.daily_stock_to_sell[date]  # 当日符合卖出条件的股票
        weight = 1/context.num_stock   # 等权重配置
        stock_hold_num = len(context.portfolio.positions)  # 目前持有的股票数量
        remain_num = context.num_stock - stock_hold_num   # 还可以买入的股票数量,需要把卖出的股票加回来
        # 初始化当日买入订单的数量为0 
        order_count = 0
        # 买入股票
        if len(buy_stock)>0:
            for i in buy_stock:
                # 如果发送买入订单的股票数量已经超过了还可以买入的股票数量,那么应退出for循环
                if order_count >= remain_num:
                    break
                # 对于没有买入的股票且可以交易的股票,应买入
                if context.portfolio.positions[context.symbol(i)].amount == 0 and data.can_trade(context.symbol(i)):
                    order_target_percent(context.symbol(i), weight)
                    order_count += 1  # 统计一下当天股票买入数量
        # 卖出股票
        if len(sell_stock)>0:
            for j in sell_stock:
                if context.portfolio.positions[context.symbol(j)].amount > 0 and data.can_trade(context.symbol(j)):
                    order_target_percent(context.symbol(j), 0)
    # 回测引擎:准备数据,只执行一次
    def m5_prepare_bigquant_run(context):
        # 加载原始数据
        stock_raw_data = context.options['data'].read_df()
    
    
        # 计算多个周期均线的函数
        def ma_calculate(df):
            ma_list = [5,10,20,30]
            for ma_len in ma_list:
                df['ma_'+str(ma_len)] = pd.rolling_mean(df['close_0'], ma_len)
            return df
    
        # 函数:求满足开仓条件的股票列表
        def open_pos_con(df):
            return list(df[(df['ma_5']>df['ma_10'])&(df['ma_10']>df['ma_20'])&(df['ma_20']>df['ma_30'])&(df['low_0']>df['ma_30'])&(df['low_0']<df['ma_10'])].instrument)
    
        # 函数:求满足平仓条件的股票列表
        def close_pos_con(df):
            return list(df[df['ma_5']<df['ma_10']].instrument)
        
        # 包含多个周期均线值的股票数据
        stock_ma_data = stock_raw_data.groupby('instrument').apply(ma_calculate)
        # 每日买入股票的数据框
        context.daily_stock_to_buy= stock_ma_data.groupby('date').apply(open_pos_con)
        # 每日卖出股票的数据框
        context.daily_stock_to_sell= stock_ma_data.groupby('date').apply(close_pos_con)
    
    # 回测引擎:初始化函数,只执行一次
    def m5_initialize_bigquant_run(context):
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
        # 设置买入的股票数量
        context.num_stock = 100 # 最多同时持有100只股票
    
    
    m5 = M.trade.v3(
        instruments=m2.data,
        options_data=m6.data,
        start_date='',
        end_date='',
        handle_data=m5_handle_data_bigquant_run,
        prepare=m5_prepare_bigquant_run,
        initialize=m5_initialize_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='close',
        capital_base=1000000,
        benchmark='000300.SHA',
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        plot_charts=True,
        backtest_only=False,
        amount_integer=False
    )
    
    [2018-05-31 10:41:44.044836] INFO: bigquant: input_features.v1 开始运行..
    [2018-05-31 10:41:44.047772] INFO: bigquant: 命中缓存
    [2018-05-31 10:41:44.048779] INFO: bigquant: input_features.v1 运行完成[0.003967s].
    [2018-05-31 10:41:44.053025] INFO: bigquant: instruments.v2 开始运行..
    [2018-05-31 10:41:44.059491] INFO: bigquant: 命中缓存
    [2018-05-31 10:41:44.060424] INFO: bigquant: instruments.v2 运行完成[0.007345s].
    [2018-05-31 10:41:44.067939] INFO: bigquant: general_feature_extractor.v6 开始运行..
    [2018-05-31 10:41:44.070052] INFO: bigquant: 命中缓存
    [2018-05-31 10:41:44.070897] INFO: bigquant: general_feature_extractor.v6 运行完成[0.002951s].
    [2018-05-31 10:41:44.076246] INFO: bigquant: derived_feature_extractor.v2 开始运行..
    [2018-05-31 10:41:44.078352] INFO: bigquant: 命中缓存
    [2018-05-31 10:41:44.079200] INFO: bigquant: derived_feature_extractor.v2 运行完成[0.002948s].
    [2018-05-31 10:41:44.084171] INFO: bigquant: dropnan.v1 开始运行..
    [2018-05-31 10:41:44.086209] INFO: bigquant: 命中缓存
    [2018-05-31 10:41:44.087116] INFO: bigquant: dropnan.v1 运行完成[0.002938s].
    [2018-05-31 10:41:44.113682] INFO: bigquant: backtest.v7 开始运行..
    [2018-05-31 10:42:01.509934] INFO: algo: set price type:backward_adjusted
    [2018-05-31 10:42:56.304997] INFO: Blotter: 2016-01-04 cancel order Equity(2530 [002057.SZA]) 
    [2018-05-31 10:43:04.394540] INFO: Blotter: 2016-04-21 cancel order Equity(2518 [000425.SZA]) 
    [2018-05-31 10:43:29.618140] INFO: Performance: Simulated 488 trading days out of 488.
    [2018-05-31 10:43:29.619148] INFO: Performance: first open: 2015-01-05 01:30:00+00:00
    [2018-05-31 10:43:29.619977] INFO: Performance: last close: 2016-12-30 07:00:00+00:00
    
    • 收益率-38.28%
    • 年化收益率-22.06%
    • 基准收益率-6.33%
    • 阿尔法-0.21
    • 贝塔0.67
    • 夏普比率-0.85
    • 胜率0.431
    • 盈亏比1.126
    • 收益波动率30.01%
    • 信息比率-0.79
    • 最大回撤71.29%
    [2018-05-31 10:43:33.573811] INFO: bigquant: backtest.v7 运行完成[109.460093s].
    


    (dgx1013) #3

    谢谢老师指导


    (dgx1013) #4

    老师,如果我要第二天收盘价卖出,策略怎么修改呢?


    (达达) #5

    handle中默认的是当天只计算出信号和需要买卖的股票列表,第二天才进行买入卖出操作,价格是通过控制trade模块中 order_price_field_buy=‘open’,和order_price_field_sell=‘close’,来决定采用开盘还是收盘价买卖,也就是说现在你的策略已经是第二天才实施买卖操作。


    (zhw_henry) #6

    现在老地址进不去了?


    (达达) #7

    您有什么需求么