克隆策略

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

    {"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":"-115:instruments","SourceOutputPortId":"-971:data"},{"DestinationInputPortId":"-991:input_2","SourceOutputPortId":"-983:data_1"},{"DestinationInputPortId":"-115: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},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","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 features_data[\"close_pct\"]=features_data['close'].pct_change()\n benchmark_df[\"close_pct\"]=benchmark_df['close'].pct_change()\n \n features_data['beta_10']=(features_data['close_pct'].rolling(10).cov(benchmark_df['close_pct']))/benchmark_df['close_pct'].rolling(10).var()\n #Cal Beta==========================================\n features_data[\"close_pct\"]=features_data['close'].pct_change()\n benchmark_df[\"close_pct\"]=benchmark_df['close'].pct_change()\n features_data['beta_10']=(features_data['close_pct'].rolling(10).cov(benchmark_df['close_pct']))/benchmark_df['close_pct'].rolling(10).var()\n features_data['beta_50']=(features_data['close_pct'].rolling(50).cov(benchmark_df['close_pct']))/benchmark_df['close_pct'].rolling(50).var()\n #==============================================\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},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","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},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","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.model_selection 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},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","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 #Cal Beta==========================================\n features_data[\"close_pct\"]=features_data['close'].pct_change()\n benchmark_df[\"close_pct\"]=benchmark_df['close'].pct_change()\n features_data['beta_10']=(features_data['close_pct'].rolling(10).cov(benchmark_df['close_pct']))/benchmark_df['close_pct'].rolling(10).var()\n features_data['beta_50']=(features_data['close_pct'].rolling(50).cov(benchmark_df['close_pct']))/benchmark_df['close_pct'].rolling(50).var()\n #==============================================\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},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","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},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","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":"-115","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","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":"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":"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":"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":"product_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":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-115"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-115"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-115"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-115"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-115"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-115","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":11,"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,99,200,200'/><NodePosition Node='-983' Position='1332,175,200,200'/><NodePosition Node='-991' Position='1109,334,200,200'/><NodePosition Node='-115' 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 [6]:
    # 本代码由可视化策略环境自动生成 2021年6月25日15:38
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 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)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m2_post_run_bigquant_run(outputs):
        return outputs
    
    # 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)
        features_data["close_pct"]=features_data['close'].pct_change()
        benchmark_df["close_pct"]=benchmark_df['close'].pct_change()
        
        features_data['beta_10']=(features_data['close_pct'].rolling(10).cov(benchmark_df['close_pct']))/benchmark_df['close_pct'].rolling(10).var()
        #Cal Beta==========================================
        features_data["close_pct"]=features_data['close'].pct_change()
        benchmark_df["close_pct"]=benchmark_df['close'].pct_change()
        features_data['beta_10']=(features_data['close_pct'].rolling(10).cov(benchmark_df['close_pct']))/benchmark_df['close_pct'].rolling(10).var()
        features_data['beta_50']=(features_data['close_pct'].rolling(50).cov(benchmark_df['close_pct']))/benchmark_df['close_pct'].rolling(50).var()
        #==============================================
        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)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m3_post_run_bigquant_run(outputs):
        return outputs
    
    # 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)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m5_post_run_bigquant_run(outputs):
        return outputs
    
    # 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.model_selection 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)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m6_post_run_bigquant_run(outputs):
        return outputs
    
    # 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)
        #Cal Beta==========================================
        features_data["close_pct"]=features_data['close'].pct_change()
        benchmark_df["close_pct"]=benchmark_df['close'].pct_change()
        features_data['beta_10']=(features_data['close_pct'].rolling(10).cov(benchmark_df['close_pct']))/benchmark_df['close_pct'].rolling(10).var()
        features_data['beta_50']=(features_data['close_pct'].rolling(50).cov(benchmark_df['close_pct']))/benchmark_df['close_pct'].rolling(50).var()
        #==============================================
        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)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m8_post_run_bigquant_run(outputs):
        return outputs
    
    # 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)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m9_post_run_bigquant_run(outputs):
        return outputs
    
    # 回测引擎:初始化函数,只执行一次
    def m11_initialize_bigquant_run(context):
        context.signal = context.options['data'].read_pickle()
        context.count = 0
         
        
        
        
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m11_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 m11_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m11_before_trading_start_bigquant_run(context, data):
        pass
    
    
    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
    )
    
    m2 = M.cached.v3(
        input_1=m1.data,
        run=m2_run_bigquant_run,
        post_run=m2_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m3 = M.cached.v3(
        input_1=m1.data,
        run=m3_run_bigquant_run,
        post_run=m3_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m4 = M.join.v3(
        data1=m3.data_1,
        data2=m2.data_1,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    m5 = M.cached.v3(
        input_1=m4.data,
        run=m5_run_bigquant_run,
        post_run=m5_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports='',
        m_cached=False
    )
    
    m6 = M.cached.v3(
        input_1=m5.data_1,
        run=m6_run_bigquant_run,
        post_run=m6_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports='',
        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
    )
    
    m8 = M.cached.v3(
        input_1=m7.data,
        run=m8_run_bigquant_run,
        post_run=m8_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m9 = M.cached.v3(
        input_1=m6.data_1,
        input_2=m8.data_1,
        run=m9_run_bigquant_run,
        post_run=m9_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports='',
        m_cached=False
    )
    
    m11 = M.trade.v4(
        instruments=m7.data,
        options_data=m9.data_1,
        start_date='',
        end_date='',
        initialize=m11_initialize_bigquant_run,
        handle_data=m11_handle_data_bigquant_run,
        prepare=m11_prepare_bigquant_run,
        before_trading_start=m11_before_trading_start_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=100000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark='000300.SHA'
    )
    
    训练集特征的维度: X shape (488, 9)
    训练集标注0-1分布统计:  1    266
    0    222
    Name: bear_bull, dtype: int64
    训练集标注的维度 y shape (488,)
    
    验证集上准确率:  83.673
    
    测试集上:,开始时间:2017-01-02,结束时间:2018-01-29,证券代码:['600548.SHA']
              date  instrument      close   volume      ma_10    std_10  \
    0   2017-01-03  600548.SHA  17.148230  3155383        NaN       NaN   
    1   2017-01-04  600548.SHA  17.249220  3805092        NaN       NaN   
    2   2017-01-05  600548.SHA  17.229021  1615819        NaN       NaN   
    3   2017-01-06  600548.SHA  17.269419  2754317        NaN       NaN   
    4   2017-01-09  600548.SHA  17.249220  1184561        NaN       NaN   
    ..         ...         ...        ...      ...        ...       ...   
    259 2018-01-23  600548.SHA  18.869041  2532110  18.655704  0.104729   
    260 2018-01-24  600548.SHA  19.096878  3701008  18.699200  0.174608   
    261 2018-01-25  600548.SHA  19.262579  7999932  18.765479  0.244489   
    262 2018-01-26  600548.SHA  19.221153  2285947  18.825546  0.276589   
    263 2018-01-29  600548.SHA  18.972605  2043215  18.877327  0.246276   
    
             ma_50    std_50    return  close_pct   beta_10   beta_50  
    0          NaN       NaN       NaN        NaN       NaN       NaN  
    1          NaN       NaN  0.005889   0.005889       NaN       NaN  
    2          NaN       NaN -0.001171  -0.001171       NaN       NaN  
    3          NaN       NaN  0.002345   0.002345       NaN       NaN  
    4          NaN       NaN -0.001170  -0.001170       NaN       NaN  
    ..         ...       ...       ...        ...       ...       ...  
    259  18.862000  0.398052  0.006630   0.006630  0.879323  0.138714  
    260  18.856200  0.392320  0.012075   0.012075  0.706485  0.120559  
    261  18.852472  0.387427  0.008677   0.008677  0.223581  0.111288  
    262  18.847087  0.380182 -0.002151  -0.002151  0.231311  0.107202  
    263  18.836731  0.369194 -0.012931  -0.012931  0.516257  0.155667  
    
    [264 rows x 12 columns]
    
    预测完成! 预测结果为: [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 0 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 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 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 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]
    
    2017-02-20 15:00:00+00:00 买入
    2017-02-24 15:00:00+00:00 卖出
    2017-03-06 15:00:00+00:00 买入
    2017-03-09 15:00:00+00:00 卖出
    2017-03-21 15:00:00+00:00 买入
    2017-03-22 15:00:00+00:00 卖出
    2017-03-24 15:00:00+00:00 买入
    2017-03-27 15:00:00+00:00 卖出
    2017-04-05 15:00:00+00:00 买入
    2017-04-12 15:00:00+00:00 卖出
    2017-04-28 15:00:00+00:00 买入
    2017-05-05 15:00:00+00:00 卖出
    2017-05-16 15:00:00+00:00 买入
    2017-05-17 15:00:00+00:00 卖出
    2017-05-18 15:00:00+00:00 买入
    2017-06-13 15:00:00+00:00 卖出
    2017-06-19 15:00:00+00:00 买入
    2017-07-17 15:00:00+00:00 卖出
    2017-07-18 15:00:00+00:00 买入
    2017-07-19 15:00:00+00:00 卖出
    2017-07-20 15:00:00+00:00 买入
    2017-07-25 15:00:00+00:00 卖出
    2017-07-27 15:00:00+00:00 买入
    2017-07-28 15:00:00+00:00 卖出
    2017-07-31 15:00:00+00:00 买入
    2017-12-06 15:00:00+00:00 卖出
    2017-12-07 15:00:00+00:00 买入
    2017-12-11 15:00:00+00:00 卖出
    2018-01-23 15:00:00+00:00 买入
    2018-01-25 15:00:00+00:00 卖出
    
    • 收益率12.96%
    • 年化收益率12.34%
    • 基准收益率29.97%
    • 阿尔法0.06
    • 贝塔0.16
    • 夏普比率0.69
    • 胜率0.47
    • 盈亏比2.94
    • 收益波动率14.09%
    • 信息比率-0.05
    • 最大回撤7.75%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-3d6d9cb07f1f44888e95660b83182843"}/bigcharts-data-end