克隆策略

本代码为《监督式机器学习算法的应用:择时》一文的策略源代码,欢迎克隆

    {"Description":"实验创建于2018/1/30","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-757:input_1","SourceOutputPortId":"-741:data"},{"DestinationInputPortId":"-765:input_1","SourceOutputPortId":"-741:data"},{"DestinationInputPortId":"-949:data2","SourceOutputPortId":"-757:data_1"},{"DestinationInputPortId":"-949:data1","SourceOutputPortId":"-765:data_1"},{"DestinationInputPortId":"-959:input_1","SourceOutputPortId":"-949:data"},{"DestinationInputPortId":"-967:input_1","SourceOutputPortId":"-959:data_1"},{"DestinationInputPortId":"-991:input_1","SourceOutputPortId":"-967:data_1"},{"DestinationInputPortId":"-983:input_1","SourceOutputPortId":"-971:data"},{"DestinationInputPortId":"-996:instruments","SourceOutputPortId":"-971:data"},{"DestinationInputPortId":"-991:input_2","SourceOutputPortId":"-983:data_1"},{"DestinationInputPortId":"-996:options_data","SourceOutputPortId":"-991:data_1"}],"ModuleNodes":[{"Id":"-741","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2015-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2017-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"600548.SHA\n ","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-741"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-741","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-757","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n # 计算标注数据\n start_date = input_1.read_pickle()['start_date']\n end_date = input_1.read_pickle()['end_date']\n ins = input_1.read_pickle()['instruments']\n raw_data = D.history_data(ins,start_date,end_date,['close'])\n raw_data['bear_bull'] = raw_data['bear_bull'] = np.where(raw_data['close'] - raw_data['close'].shift(30)>0,1,0)\n raw_data.fillna(0, inplace=True)\n raw_data = raw_data.drop('close',axis=1)\n \n ds = DataSource.write_df(raw_data)\n return Outputs(data_1=ds, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-757"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-757"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-757"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-757","OutputType":null},{"Name":"data_2","NodeId":"-757","OutputType":null},{"Name":"data_3","NodeId":"-757","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-765","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n \n start_date = input_1.read_pickle()['start_date']\n end_date = input_1.read_pickle()['end_date']\n ins =input_1.read_pickle()['instruments']\n print('训练集上:,开始时间:{0},结束时间:{1},证券代码:{2}'.format(start_date,end_date,ins))\n # 计算特征数据\n fields = ['close','volume']\n features_data = D.history_data(ins,start_date,end_date,fields)\n features_data['ma_10'] = features_data['close'].rolling(10).mean()\n features_data['std_10'] = features_data['close'].rolling(10).std()\n features_data['ma_50'] = features_data['close'].rolling(50).mean()\n features_data['std_50'] = features_data['close'].rolling(50).std()\n features_data['return'] = features_data['close'].pct_change()\n benchmark = ['000300.SHA'] # 以沪深300为基准计算beta值\n benchmark_df=D.history_data(benchmark,fields=['close'],start_date=start_date,end_date=end_date)\n ols_ret_10=pd.ols(y=features_data['close'].pct_change() ,x=benchmark_df['close'].pct_change(), window=10, window_type='rolling')\n features_data['beta_10']=ols_ret_10.beta['x']\n ols_ret_50=pd.ols(y=features_data['close'].pct_change() ,x=benchmark_df['close'].pct_change(), window=50, window_type='rolling')\n features_data['beta_50']=ols_ret_50.beta['x']\n features_data.fillna(0, inplace=True)\n factor_list = ['close','volume','return','std_10','std_50','ma_10','ma_50','beta_10','beta_50']\n features_data = features_data[factor_list+['date','instrument']]\n ds = DataSource.write_df(features_data)\n return Outputs(data_1=ds, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-765"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-765"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-765"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-765","OutputType":null},{"Name":"data_2","NodeId":"-765","OutputType":null},{"Name":"data_3","NodeId":"-765","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-949","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date,instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how","Value":"inner","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sort","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data1","NodeId":"-949"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"-949"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-949","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-959","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n \n factor_list = ['close','volume','return','std_10','std_50','ma_10','ma_50','beta_10','beta_50']\n data = input_1.read_df()\n data.fillna(-9999999,inplace=True)\n features_array = np.array(data[factor_list]) \n from sklearn import preprocessing\n X = preprocessing.scale(features_array) # 标准化\n print('训练集特征的维度:','X shape',X.shape)\n print('训练集标注0-1分布统计: ',data['bear_bull'].value_counts())\n y = np.array(data['bear_bull'])\n print('训练集标注的维度','y shape',y.shape)\n ds = DataSource.write_pickle({'X':X,'y':y})\n return Outputs(data_1=ds, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-959"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-959"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-959"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-959","OutputType":null},{"Name":"data_2","NodeId":"-959","OutputType":null},{"Name":"data_3","NodeId":"-959","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":5,"Comment":"","CommentCollapsed":true},{"Id":"-967","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n df = input_1.read_pickle()\n X = df['X']\n y = df['y']\n from sklearn.cross_validation import train_test_split\n X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.1)\n from sklearn.linear_model import LogisticRegression\n # 模型训练\n logreg = LogisticRegression()\n logreg.fit(X_train,y_train)\n logreg_score = float(\"{0:.3f}\".format(logreg.score(X_test,y_test)*100))\n print('验证集上准确率: ',logreg_score)\n model_result = {'model':logreg,'score':logreg_score}\n ds = DataSource.write_pickle(model_result)\n return Outputs(data_1=ds, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-967"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-967"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-967"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-967","OutputType":null},{"Name":"data_2","NodeId":"-967","OutputType":null},{"Name":"data_3","NodeId":"-967","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":6,"Comment":"","CommentCollapsed":true},{"Id":"-971","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2017-01-02","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2018-01-29","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"600548.SHA\n ","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-971"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-971","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"Comment":"","CommentCollapsed":true},{"Id":"-983","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n \n # 测试集特征数据抽取\n start_date = input_1.read_pickle()['start_date']\n end_date = input_1.read_pickle()['end_date']\n ins = input_1.read_pickle()['instruments']\n print('测试集上:,开始时间:{0},结束时间:{1},证券代码:{2}'.format(start_date,end_date,ins))\n fields = ['close','volume']\n features_data = D.history_data(ins,start_date,end_date,fields)\n features_data['ma_10'] = features_data['close'].rolling(10).mean()\n features_data['std_10'] = features_data['close'].rolling(10).std()\n features_data['ma_50'] = features_data['close'].rolling(50).mean()\n features_data['std_50'] = features_data['close'].rolling(50).std()\n features_data['return'] = features_data['close'].pct_change()\n benchmark = ['000300.SHA'] \n benchmark_df=D.history_data(benchmark,fields=['close'],start_date=start_date,end_date=end_date)\n ols_ret_10=pd.ols(y=features_data['close'].pct_change() ,x=benchmark_df['close'].pct_change(), window=10, window_type='rolling')\n features_data['beta_10']=ols_ret_10.beta['x']\n ols_ret_50=pd.ols(y=features_data['close'].pct_change() ,x=benchmark_df['close'].pct_change(), window=50, window_type='rolling')\n features_data['beta_50']=ols_ret_50.beta['x']\n features_data.fillna(0, inplace=True)\n factor_list = ['close','volume','return','std_10','std_50','ma_10','ma_50','beta_10','beta_50']\n features_data = features_data[factor_list+['date','instrument']]\n \n \n ds = DataSource.write_df(features_data)\n return Outputs(data_1=ds, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-983"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-983"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-983"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-983","OutputType":null},{"Name":"data_2","NodeId":"-983","OutputType":null},{"Name":"data_3","NodeId":"-983","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"Comment":"","CommentCollapsed":true},{"Id":"-991","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n \n df = input_2.read_df()\n factor_list = ['close','volume','return','std_10','std_50','ma_10','ma_50','beta_10','beta_50']\n from sklearn import preprocessing\n X = preprocessing.scale(np.array(df[factor_list]))\n # 模型预测\n result = input_1.read_pickle()['model'].predict(X)\n print('预测完成!','预测结果为:',result)\n return Outputs(data_1=DataSource.write_pickle(result), data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-991"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-991"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-991"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-991","OutputType":null},{"Name":"data_2","NodeId":"-991","OutputType":null},{"Name":"data_3","NodeId":"-991","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":9,"Comment":"","CommentCollapsed":true},{"Id":"-996","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 \n current_signal = context.signal[context.count]\n \n k = context.instruments[0]\n sid = context.symbol(k)\n \n # 账户持仓\n cur_position = context.portfolio.positions[sid].amount \n \n if cur_position == 0 and current_signal == 1 and data.can_trade(sid):\n context.order_target_percent(sid,1)\n print(data.current_dt,'买入')\n \n if cur_position >0 and current_signal == 0 and data.can_trade(sid):\n context.order_target_percent(sid,0)\n print(data.current_dt,'卖出')\n \n context.count += 1","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n context.signal = context.options['data'].read_pickle()\n context.count = 0\n \n \n \n \n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。\ndef bigquant_run(context, data):\n pass\n","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":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":"100000","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":"-996"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-996"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-996","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":10,"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='-741' Position='809,-95,200,200'/><NodePosition Node='-757' Position='987,9,200,200'/><NodePosition Node='-765' Position='667,6,200,200'/><NodePosition Node='-949' Position='790,98,200,200'/><NodePosition Node='-959' Position='845,184,200,200'/><NodePosition Node='-967' Position='920,267,200,200'/><NodePosition Node='-971' Position='1330.5257568359375,99,200,200'/><NodePosition Node='-983' Position='1332,175,200,200'/><NodePosition Node='-991' Position='1109,334,200,200'/><NodePosition Node='-996' Position='1202,438,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":true}
    In [17]:
    # 本代码由可视化策略环境自动生成 2018年1月30日 22:06
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m1 = M.instruments.v2(
        start_date='2015-01-01',
        end_date='2017-01-01',
        market='CN_STOCK_A',
        instrument_list="""600548.SHA
     """,
        max_count=0
    )
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m2_run_bigquant_run(input_1, input_2, input_3):
        # 计算标注数据
        start_date = input_1.read_pickle()['start_date']
        end_date = input_1.read_pickle()['end_date']
        ins = input_1.read_pickle()['instruments']
        raw_data = D.history_data(ins,start_date,end_date,['close'])
        raw_data['bear_bull'] = raw_data['bear_bull'] = np.where(raw_data['close'] - raw_data['close'].shift(30)>0,1,0)
        raw_data.fillna(0, inplace=True)
        raw_data  = raw_data.drop('close',axis=1)
        
        ds =  DataSource.write_df(raw_data)
        return Outputs(data_1=ds, data_2=None, data_3=None)
    
    m2 = M.cached.v3(
        input_1=m1.data,
        run=m2_run_bigquant_run
    )
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m3_run_bigquant_run(input_1, input_2, input_3):
        
        start_date = input_1.read_pickle()['start_date']
        end_date = input_1.read_pickle()['end_date']
        ins =input_1.read_pickle()['instruments']
        print('训练集上:,开始时间:{0},结束时间:{1},证券代码:{2}'.format(start_date,end_date,ins))
        # 计算特征数据
        fields = ['close','volume']
        features_data = D.history_data(ins,start_date,end_date,fields)
        features_data['ma_10'] = features_data['close'].rolling(10).mean()
        features_data['std_10'] = features_data['close'].rolling(10).std()
        features_data['ma_50'] = features_data['close'].rolling(50).mean()
        features_data['std_50'] = features_data['close'].rolling(50).std()
        features_data['return'] =  features_data['close'].pct_change()
        benchmark = ['000300.SHA']  # 以沪深300为基准计算beta值
        benchmark_df=D.history_data(benchmark,fields=['close'],start_date=start_date,end_date=end_date)
        ols_ret_10=pd.ols(y=features_data['close'].pct_change() ,x=benchmark_df['close'].pct_change(), window=10, window_type='rolling')
        features_data['beta_10']=ols_ret_10.beta['x']
        ols_ret_50=pd.ols(y=features_data['close'].pct_change() ,x=benchmark_df['close'].pct_change(), window=50, window_type='rolling')
        features_data['beta_50']=ols_ret_50.beta['x']
        features_data.fillna(0, inplace=True)
        factor_list = ['close','volume','return','std_10','std_50','ma_10','ma_50','beta_10','beta_50']
        features_data = features_data[factor_list+['date','instrument']]
        ds = DataSource.write_df(features_data)
        return Outputs(data_1=ds, data_2=None, data_3=None)
    
    m3 = M.cached.v3(
        input_1=m1.data,
        run=m3_run_bigquant_run
    )
    
    m4 = M.join.v3(
        data1=m3.data_1,
        data2=m2.data_1,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m5_run_bigquant_run(input_1, input_2, input_3):
        
        factor_list = ['close','volume','return','std_10','std_50','ma_10','ma_50','beta_10','beta_50']
        data = input_1.read_df()
        data.fillna(-9999999,inplace=True)
        features_array = np.array(data[factor_list]) 
        from sklearn import preprocessing
        X = preprocessing.scale(features_array) # 标准化
        print('训练集特征的维度:','X shape',X.shape)
        print('训练集标注0-1分布统计: ',data['bear_bull'].value_counts())
        y = np.array(data['bear_bull'])
        print('训练集标注的维度','y shape',y.shape)
        ds = DataSource.write_pickle({'X':X,'y':y})
        return Outputs(data_1=ds, data_2=None, data_3=None)
    
    m5 = M.cached.v3(
        input_1=m4.data,
        run=m5_run_bigquant_run,
        m_cached=False
    )
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m6_run_bigquant_run(input_1, input_2, input_3):
        df = input_1.read_pickle()
        X = df['X']
        y = df['y']
        from sklearn.cross_validation import train_test_split
        X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.1)
        from sklearn.linear_model import LogisticRegression
        # 模型训练
        logreg = LogisticRegression()
        logreg.fit(X_train,y_train)
        logreg_score = float("{0:.3f}".format(logreg.score(X_test,y_test)*100))
        print('验证集上准确率: ',logreg_score)
        model_result = {'model':logreg,'score':logreg_score}
        ds = DataSource.write_pickle(model_result)
        return Outputs(data_1=ds, data_2=None, data_3=None)
    
    m6 = M.cached.v3(
        input_1=m5.data_1,
        run=m6_run_bigquant_run,
        m_cached=False
    )
    
    m7 = M.instruments.v2(
        start_date='2017-01-02',
        end_date='2018-01-29',
        market='CN_STOCK_A',
        instrument_list="""600548.SHA
     """,
        max_count=0
    )
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m8_run_bigquant_run(input_1, input_2, input_3):
        
        # 测试集特征数据抽取
        start_date = input_1.read_pickle()['start_date']
        end_date = input_1.read_pickle()['end_date']
        ins =  input_1.read_pickle()['instruments']
        print('测试集上:,开始时间:{0},结束时间:{1},证券代码:{2}'.format(start_date,end_date,ins))
        fields = ['close','volume']
        features_data = D.history_data(ins,start_date,end_date,fields)
        features_data['ma_10'] = features_data['close'].rolling(10).mean()
        features_data['std_10'] = features_data['close'].rolling(10).std()
        features_data['ma_50'] = features_data['close'].rolling(50).mean()
        features_data['std_50'] = features_data['close'].rolling(50).std()
        features_data['return'] =  features_data['close'].pct_change()
        benchmark = ['000300.SHA'] 
        benchmark_df=D.history_data(benchmark,fields=['close'],start_date=start_date,end_date=end_date)
        ols_ret_10=pd.ols(y=features_data['close'].pct_change() ,x=benchmark_df['close'].pct_change(), window=10, window_type='rolling')
        features_data['beta_10']=ols_ret_10.beta['x']
        ols_ret_50=pd.ols(y=features_data['close'].pct_change() ,x=benchmark_df['close'].pct_change(), window=50, window_type='rolling')
        features_data['beta_50']=ols_ret_50.beta['x']
        features_data.fillna(0, inplace=True)
        factor_list = ['close','volume','return','std_10','std_50','ma_10','ma_50','beta_10','beta_50']
        features_data = features_data[factor_list+['date','instrument']]
        
         
        ds = DataSource.write_df(features_data)
        return Outputs(data_1=ds, data_2=None, data_3=None)
    
    m8 = M.cached.v3(
        input_1=m7.data,
        run=m8_run_bigquant_run
    )
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m9_run_bigquant_run(input_1, input_2, input_3):
         
        df = input_2.read_df()
        factor_list = ['close','volume','return','std_10','std_50','ma_10','ma_50','beta_10','beta_50']
        from sklearn import preprocessing
        X = preprocessing.scale(np.array(df[factor_list]))
        # 模型预测
        result = input_1.read_pickle()['model'].predict(X)
        print('预测完成!','预测结果为:',result)
        return Outputs(data_1=DataSource.write_pickle(result), data_2=None, data_3=None)
    
    m9 = M.cached.v3(
        input_1=m6.data_1,
        input_2=m8.data_1,
        run=m9_run_bigquant_run,
        m_cached=False
    )
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m10_handle_data_bigquant_run(context, data): 
       
        current_signal = context.signal[context.count]
        
        k = context.instruments[0]
        sid = context.symbol(k)
        
        # 账户持仓
        cur_position = context.portfolio.positions[sid].amount 
        
        if cur_position == 0 and  current_signal == 1 and data.can_trade(sid):
            context.order_target_percent(sid,1)
            print(data.current_dt,'买入')
         
        if cur_position >0 and  current_signal == 0 and data.can_trade(sid):
            context.order_target_percent(sid,0)
            print(data.current_dt,'卖出')
        
        context.count += 1
    # 回测引擎:准备数据,只执行一次
    def m10_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:初始化函数,只执行一次
    def m10_initialize_bigquant_run(context):
        context.signal = context.options['data'].read_pickle()
        context.count = 0
         
        
        
        
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m10_before_trading_start_bigquant_run(context, data):
        pass
    
    m10 = M.trade.v3(
        instruments=m7.data,
        options_data=m9.data_1,
        start_date='',
        end_date='',
        handle_data=m10_handle_data_bigquant_run,
        prepare=m10_prepare_bigquant_run,
        initialize=m10_initialize_bigquant_run,
        before_trading_start=m10_before_trading_start_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=100000,
        benchmark='000300.SHA',
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        plot_charts=True,
        backtest_only=False,
        amount_integer=False
    )
    
    [2018-01-30 21:59:03.055450] INFO: bigquant: instruments.v2 开始运行..
    [2018-01-30 21:59:03.059109] INFO: bigquant: 命中缓存
    [2018-01-30 21:59:03.060264] INFO: bigquant: instruments.v2 运行完成[0.004929s].
    [2018-01-30 21:59:03.070749] INFO: bigquant: cached.v3 开始运行..
    [2018-01-30 21:59:03.073571] INFO: bigquant: 命中缓存
    [2018-01-30 21:59:03.074826] INFO: bigquant: cached.v3 运行完成[0.004069s].
    [2018-01-30 21:59:03.087489] INFO: bigquant: cached.v3 开始运行..
    [2018-01-30 21:59:03.090159] INFO: bigquant: 命中缓存
    [2018-01-30 21:59:03.091151] INFO: bigquant: cached.v3 运行完成[0.003675s].
    [2018-01-30 21:59:03.099471] INFO: bigquant: join.v3 开始运行..
    [2018-01-30 21:59:03.102357] INFO: bigquant: 命中缓存
    [2018-01-30 21:59:03.103687] INFO: bigquant: join.v3 运行完成[0.0042s].
    [2018-01-30 21:59:03.111456] INFO: bigquant: cached.v3 开始运行..
    训练集特征的维度: X shape (488, 9)
    训练集标注0-1分布统计:  1    266
    0    222
    Name: bear_bull, dtype: int64
    训练集标注的维度 y shape (488,)
    [2018-01-30 21:59:03.147728] INFO: bigquant: cached.v3 运行完成[0.036238s].
    [2018-01-30 21:59:03.156006] INFO: bigquant: cached.v3 开始运行..
    验证集上准确率:  75.51
    [2018-01-30 21:59:03.162511] INFO: bigquant: cached.v3 运行完成[0.006502s].
    [2018-01-30 21:59:03.167806] INFO: bigquant: instruments.v2 开始运行..
    [2018-01-30 21:59:03.170457] INFO: bigquant: 命中缓存
    [2018-01-30 21:59:03.171505] INFO: bigquant: instruments.v2 运行完成[0.003696s].
    [2018-01-30 21:59:03.184202] INFO: bigquant: cached.v3 开始运行..
    [2018-01-30 21:59:03.187001] INFO: bigquant: 命中缓存
    [2018-01-30 21:59:03.187860] INFO: bigquant: cached.v3 运行完成[0.003669s].
    [2018-01-30 21:59:03.196115] INFO: bigquant: cached.v3 开始运行..
    预测完成! 预测结果为: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0
     0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
     0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1
     1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 0 0 1 0 1 1 1 1 1 1 1 1 1
     1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
     1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
     1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     1 1 0 0 0]
    [2018-01-30 21:59:03.227968] INFO: bigquant: cached.v3 运行完成[0.031827s].
    [2018-01-30 21:59:03.251951] INFO: bigquant: backtest.v7 开始运行..
    [2018-01-30 21:59:03.376301] INFO: algo: set price type:backward_adjusted
    2017-02-20 07:00:00+00:00 买入
    2017-02-24 07:00:00+00:00 卖出
    2017-03-06 07:00:00+00:00 买入
    2017-03-09 07:00:00+00:00 卖出
    2017-03-21 07:00:00+00:00 买入
    2017-03-22 07:00:00+00:00 卖出
    2017-03-23 07:00:00+00:00 买入
    2017-03-27 07:00:00+00:00 卖出
    2017-04-05 07:00:00+00:00 买入
    2017-04-12 07:00:00+00:00 卖出
    2017-04-28 07:00:00+00:00 买入
    2017-05-05 07:00:00+00:00 卖出
    2017-05-15 07:00:00+00:00 买入
    2017-06-12 07:00:00+00:00 卖出
    2017-06-19 07:00:00+00:00 买入
    2017-07-17 07:00:00+00:00 卖出
    2017-07-18 07:00:00+00:00 买入
    2017-07-19 07:00:00+00:00 卖出
    2017-07-20 07:00:00+00:00 买入
    2017-07-25 07:00:00+00:00 卖出
    2017-07-27 07:00:00+00:00 买入
    2017-07-28 07:00:00+00:00 卖出
    2017-07-31 07:00:00+00:00 买入
    2017-08-21 07:00:00+00:00 卖出
    2017-08-23 07:00:00+00:00 买入
    2017-12-06 07:00:00+00:00 卖出
    2017-12-07 07:00:00+00:00 买入
    2017-12-11 07:00:00+00:00 卖出
    2018-01-23 07:00:00+00:00 买入
    2018-01-25 07:00:00+00:00 卖出
    [2018-01-30 21:59:04.946390] INFO: Performance: Simulated 264 trading days out of 264.
    [2018-01-30 21:59:04.947504] INFO: Performance: first open: 2017-01-03 01:30:00+00:00
    [2018-01-30 21:59:04.948634] INFO: Performance: last close: 2018-01-29 07:00:00+00:00
    
    • 收益率17.36%
    • 年化收益率16.51%
    • 基准收益率29.97%
    • 阿尔法0.08
    • 贝塔0.15
    • 夏普比率0.85
    • 胜率0.6
    • 盈亏比1.778
    • 收益波动率14.18%
    • 信息比率-0.72
    • 最大回撤7.76%
    [2018-01-30 21:59:06.156194] INFO: bigquant: backtest.v7 运行完成[2.904235s].