positional argument follows keyword argument看不懂这个错误信息,请教

策略分享
新手专区
标签: #<Tag:0x00007fb11d7f74e0> #<Tag:0x00007fb11d7f73a0>

(189) #1

没有发现语法问题,请教老师们

克隆策略
In [36]:
m14.data.read_df().tail(10)
Out[36]:
date instrument risk ref12 ma13
1842 2019-11-15 510880.HOF 1.0 2.777 2.768231
1843 2019-11-15 510300.HOF 1.0 3.947 3.995462
1844 2019-11-15 510500.HOF 1.0 5.343 5.335769
1845 2019-11-18 159905.ZOF 0.0 1.941 1.974231
1846 2019-11-18 159915.ZOF 0.0 1.608 1.627385
1847 2019-11-18 159949.ZOF 0.0 0.587 0.598769
1848 2019-11-18 510880.HOF 0.0 2.765 2.764538
1849 2019-11-18 510050.HOF 1.0 3.003 3.039692
1850 2019-11-18 510500.HOF 1.0 5.315 5.331385
1851 2019-11-18 510300.HOF 1.0 3.945 3.997308

    {"Description":"实验创建于2019/12/3","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-25:input_data","SourceOutputPortId":"-14:data"},{"DestinationInputPortId":"-14:features","SourceOutputPortId":"-20:data"},{"DestinationInputPortId":"-25:features","SourceOutputPortId":"-20:data"},{"DestinationInputPortId":"-14:instruments","SourceOutputPortId":"-5:data"},{"DestinationInputPortId":"-1397:instruments","SourceOutputPortId":"-5:data"},{"DestinationInputPortId":"-115:features","SourceOutputPortId":"-95:data"},{"DestinationInputPortId":"-124:features","SourceOutputPortId":"-95:data"},{"DestinationInputPortId":"-108:input_ds","SourceOutputPortId":"-115:data"},{"DestinationInputPortId":"-115:input_data","SourceOutputPortId":"-124:data"},{"DestinationInputPortId":"-1428:input_1","SourceOutputPortId":"-108:data"},{"DestinationInputPortId":"-124:instruments","SourceOutputPortId":"-339:data"},{"DestinationInputPortId":"-561:input_data","SourceOutputPortId":"-1418:data"},{"DestinationInputPortId":"-1418:data2","SourceOutputPortId":"-1428:data_1"},{"DestinationInputPortId":"-556:input_ds","SourceOutputPortId":"-25:data"},{"DestinationInputPortId":"-1418:data1","SourceOutputPortId":"-556:data"},{"DestinationInputPortId":"-1397:options_data","SourceOutputPortId":"-561:data"}],"ModuleNodes":[{"Id":"-14","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1d_CN_FUND","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-14"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-14"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-14","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-20","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nref12=shift(close, 12)\nma13=mean(close, 13)\n\nopen\nlow\nhigh\nvolume\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-20"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-20","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-5","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2018-10-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-11-18","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_FUND","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"510300.HOF\n510050.HOF\n510500.HOF\n159915.ZOF\n159905.ZOF\n159949.ZOF\n510880.HOF\n\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":"-5"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-5","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-95","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\n#avg_volume=mean(volume, 10)\n#risk=where(amount<avg_amount, 1, 0)\navg_volume=mean(volume, 7)\nup_down=where(volume<avg_volume, 1, 0)\ncount_risk=sum(up_down, 6)\nrisk=where(count_risk>=6, 1, 0)","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-95"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-95","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-115","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-115"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-115"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-115","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"Comment":"","CommentCollapsed":true},{"Id":"-124","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1d_index_CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-124"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-124"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-124","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"Comment":"","CommentCollapsed":true},{"Id":"-108","ModuleId":"BigQuantSpace.select_columns.select_columns-v3","ModuleParameters":[{"Name":"columns","Value":"date,instrument,risk","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"reverse_select","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_ds","NodeId":"-108"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"columns_ds","NodeId":"-108"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-108","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":7,"Comment":"","CommentCollapsed":true},{"Id":"-339","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2018-10-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-11-18","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"000300.HIX\n000016.HIX\n000905.HIX\n000015.HIX\n399673.ZIX\n399006.ZIX\n399324.ZIX","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-339"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-339","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"Comment":"","CommentCollapsed":true},{"Id":"-1397","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 # 加载预测数据\n context.op= context.options['data'].read_df()\n \n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)\n # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只\n \n stock_count = 3\n context.hold_days = 5\n context.his_bars=13\n etf_dic={\n '000016.HIX':'510050.HOF',\n '000300.HIX':'510300.HOF',\n '000905.HIX':'510500.HOF',\n '000015.HIX':'510880.HOF',\n '399673.ZIX':'159949.ZOF',\n '399006.ZIX':'159915.ZOF',\n '399324.ZIX':'159905.ZOF',\n }\n context.ind=[i for i in context.etf_dic.keys()]\n context.etf=[i for i in context.etf_dic.values()]\n context.ind_dic=dict(zip(context.ind,context.etf))\n \n schedule_function(func=trade, date_rules.every_day(), time_rule=time_rules.market_close(minutes=15))\n ","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n pass\ndef trade(context,data):\n today = data.current_dt.strftime('%Y-%m-%d')\n context.today=context.op[['date'==today]]\n print(context.today.tail(5))\n \n context.daily_stock_buy=[]\n context.daily_stock_sell=[]\n \n stock_hold_now = [equity for equity in context.portfolio.positions.keys()]\n print('当前持仓:%s',stock_hold_now)\n \n for sec in context.etf:\n curr = data.current(context.symbol(sec), ['price'])\n \n if curr>context.today.iloc[sec,'ref12'] & curr>context.today.iloc[sec,'ma13'] & context.today.iloc[sec,'risk']==0:\n context.daily_stock_buy.append(sec)\n else:\n context.daily_stock_sell.append(sec)\n \n try:\n\t buy_stock = context.daily_stock_buy# 当日符合买入条件的股票\n except:\n\t buy_stock=[] # 如果没有符合条件的股票,就设置为空\n try:\n\t sell_stock = context.daily_stock_sell # 当日符合卖出条件的股票\n except:\n\t sell_stock=[] # 如果没有符合条件的股票,就设置为空\n \n\t# 需要卖出的股票:已有持仓中符合卖出条件的股票\n stock_to_sell = [ i for i in stock_hold_now if i in sell_stock]\n stock_to_buy = [ i for i in buy_stock if i not in stock_hold_now]\n\t# 如果有卖出信号\n if len(stock_to_sell)>0:\n\t for instrument in stock_to_sell:\n\t cur_position = context.portfolio.positions[instrument].closeable_amount # 持仓\n\t if cur_position > 0 :\n\t order_target_value(instrument, 0)\n\t print('正在卖出%s',instrument)\n if len(stock_to_buy)>0:\n weight =1/len(stock_to_buy)\n for instrument in stock_to_buy:\n\t order_target_value(instrument, weight*context.portfolio.total_value) # 买入\n\t print('正在买入%s',instrument)\n\t ","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":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":1000000,"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":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-1397"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-1397"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-1397"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-1397"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-1397"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-1397","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":9,"Comment":"","CommentCollapsed":true},{"Id":"-1418","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date,instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how","Value":"left","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sort","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data1","NodeId":"-1418"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"-1418"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1418","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":10,"Comment":"","CommentCollapsed":true},{"Id":"-1428","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_1.read_df()\n \n etf_dic={\n '000016.HIX':'510050.HOF',\n '000300.HIX':'510300.HOF',\n '000905.HIX':'510500.HOF',\n '000015.HIX':'510880.HOF',\n '399673.ZIX':'159949.ZOF',\n '399006.ZIX':'159915.ZOF',\n '399324.ZIX':'159905.ZOF',\n }\n df['instrument']=df['instrument'].map(lambda x:etf_dic[x])\n print(df.tail(10))\n data_1= DataSource.write_df(df)\n return Outputs(data_1=data_1, 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":"-1428"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-1428"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-1428"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-1428","OutputType":null},{"Name":"data_2","NodeId":"-1428","OutputType":null},{"Name":"data_3","NodeId":"-1428","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":11,"Comment":"","CommentCollapsed":true},{"Id":"-25","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-25"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-25"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-25","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":12,"Comment":"","CommentCollapsed":true},{"Id":"-556","ModuleId":"BigQuantSpace.select_columns.select_columns-v3","ModuleParameters":[{"Name":"columns","Value":"date,instrument,ref12,ma13","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"reverse_select","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_ds","NodeId":"-556"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"columns_ds","NodeId":"-556"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-556","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":13,"Comment":"","CommentCollapsed":true},{"Id":"-561","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-561"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-561","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":14,"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='-14' Position='60,98,200,200'/><NodePosition Node='-20' Position='187,-82,200,200'/><NodePosition Node='-5' Position='-103,-28,200,200'/><NodePosition Node='-95' Position='843,-72,200,200'/><NodePosition Node='-115' Position='755,106,200,200'/><NodePosition Node='-124' Position='705,32,200,200'/><NodePosition Node='-108' Position='665,197,200,200'/><NodePosition Node='-339' Position='535,-91,200,200'/><NodePosition Node='-1397' Position='192.1807403564453,582.0563659667969,200,200'/><NodePosition Node='-1418' Position='160,313,200,200'/><NodePosition Node='-1428' Position='626.2957153320312,281.2253723144531,200,200'/><NodePosition Node='-25' Position='76,176,200,200'/><NodePosition Node='-556' Position='194,244,200,200'/><NodePosition Node='-561' Position='245,402,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 [42]:
    # 本代码由可视化策略环境自动生成 2019年12月4日 01:17
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m11_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        df = input_1.read_df()
        
        etf_dic={
                '000016.HIX':'510050.HOF',
                '000300.HIX':'510300.HOF',
                '000905.HIX':'510500.HOF',
                '000015.HIX':'510880.HOF',
                '399673.ZIX':'159949.ZOF',
                '399006.ZIX':'159915.ZOF',
                '399324.ZIX':'159905.ZOF',
            }
        df['instrument']=df['instrument'].map(lambda x:etf_dic[x])
        print(df.tail(10))
        data_1= DataSource.write_df(df)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m11_post_run_bigquant_run(outputs):
        return outputs
    
    # 回测引擎:初始化函数,只执行一次
    def m9_initialize_bigquant_run(context):
        # 加载预测数据
        context.op= context.options['data'].read_df()
        
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
        # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
        # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
        
        stock_count = 3
        context.hold_days = 5
        context.his_bars=13
        etf_dic={
                '000016.HIX':'510050.HOF',
                '000300.HIX':'510300.HOF',
                '000905.HIX':'510500.HOF',
                '000015.HIX':'510880.HOF',
                '399673.ZIX':'159949.ZOF',
                '399006.ZIX':'159915.ZOF',
                '399324.ZIX':'159905.ZOF',
            }
        context.ind=[i for i in context.etf_dic.keys()]
        context.etf=[i for i in context.etf_dic.values()]
        context.ind_dic=dict(zip(context.ind,context.etf))
        
        schedule_function(func=trade, date_rules.every_day(), time_rule=time_rules.market_close(minutes=15))
       
    # 回测引擎:每日数据处理函数,每天执行一次
    def m9_handle_data_bigquant_run(context, data):
        pass
    def trade(context,data):
        today = data.current_dt.strftime('%Y-%m-%d')
        context.today=context.op[['date'==today]]
        print(context.today.tail(5))
        
        context.daily_stock_buy=[]
        context.daily_stock_sell=[]
        
        stock_hold_now = [equity for equity in context.portfolio.positions.keys()]
        print('当前持仓:%s',stock_hold_now)
        
        for sec in context.etf:
            curr = data.current(context.symbol(sec), ['price'])
            
            if curr>context.today.iloc[sec,'ref12'] & curr>context.today.iloc[sec,'ma13'] & context.today.iloc[sec,'risk']==0:
                context.daily_stock_buy.append(sec)
            else:
                context.daily_stock_sell.append(sec)
        
        try:
    	    buy_stock = context.daily_stock_buy# 当日符合买入条件的股票
        except:
    	    buy_stock=[]  # 如果没有符合条件的股票,就设置为空
        try:
    	    sell_stock = context.daily_stock_sell # 当日符合卖出条件的股票
        except:
    	    sell_stock=[] # 如果没有符合条件的股票,就设置为空
        
    	# 需要卖出的股票:已有持仓中符合卖出条件的股票
        stock_to_sell = [ i for i in stock_hold_now if i in sell_stock]
        stock_to_buy = [ i for i in buy_stock if i not in stock_hold_now]
    	# 如果有卖出信号
        if len(stock_to_sell)>0:
    	    for instrument in stock_to_sell:
    	        cur_position = context.portfolio.positions[instrument].closeable_amount # 持仓
    	        if cur_position > 0 :
    	            order_target_value(instrument, 0)
    	            print('正在卖出%s',instrument)
        if len(stock_to_buy)>0:
            weight =1/len(stock_to_buy)
            for instrument in stock_to_buy:
    	        order_target_value(instrument, weight*context.portfolio.total_value) # 买入
    	        print('正在买入%s',instrument)
    	    
    # 回测引擎:准备数据,只执行一次
    def m9_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m9_before_trading_start_bigquant_run(context, data):
        pass
    
    
    m2 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    ref12=shift(close, 12)
    ma13=mean(close, 13)
    
    open
    low
    high
    volume
    """
    )
    
    m3 = M.instruments.v2(
        start_date='2018-10-01',
        end_date='2019-11-18',
        market='CN_FUND',
        instrument_list="""510300.HOF
    510050.HOF
    510500.HOF
    159915.ZOF
    159905.ZOF
    159949.ZOF
    510880.HOF
    
    """,
        max_count=0
    )
    
    m1 = M.use_datasource.v1(
        instruments=m3.data,
        features=m2.data,
        datasource_id='bar1d_CN_FUND',
        start_date='',
        end_date=''
    )
    
    m12 = M.derived_feature_extractor.v3(
        input_data=m1.data,
        features=m2.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m13 = M.select_columns.v3(
        input_ds=m12.data,
        columns='date,instrument,ref12,ma13',
        reverse_select=False,
        m_cached=False
    )
    
    m4 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    #avg_volume=mean(volume, 10)
    #risk=where(amount<avg_amount, 1, 0)
    avg_volume=mean(volume, 7)
    up_down=where(volume<avg_volume, 1, 0)
    count_risk=sum(up_down, 6)
    risk=where(count_risk>=6, 1, 0)"""
    )
    
    m8 = M.instruments.v2(
        start_date='2018-10-01',
        end_date='2019-11-18',
        market='CN_STOCK_A',
        instrument_list="""000300.HIX
    000016.HIX
    000905.HIX
    000015.HIX
    399673.ZIX
    399006.ZIX
    399324.ZIX""",
        max_count=0
    )
    
    m6 = M.use_datasource.v1(
        instruments=m8.data,
        features=m4.data,
        datasource_id='bar1d_index_CN_STOCK_A',
        start_date='',
        end_date=''
    )
    
    m5 = M.derived_feature_extractor.v3(
        input_data=m6.data,
        features=m4.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=True,
        user_functions={}
    )
    
    m7 = M.select_columns.v3(
        input_ds=m5.data,
        columns='date,instrument,risk',
        reverse_select=False,
        m_cached=False
    )
    
    m11 = M.cached.v3(
        input_1=m7.data,
        run=m11_run_bigquant_run,
        post_run=m11_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m10 = M.join.v3(
        data1=m13.data,
        data2=m11.data_1,
        on='date,instrument',
        how='left',
        sort=False
    )
    
    m14 = M.dropnan.v1(
        input_data=m10.data
    )
    
    m9 = M.trade.v4(
        instruments=m3.data,
        options_data=m14.data,
        start_date='',
        end_date='',
        initialize=m9_initialize_bigquant_run,
        handle_data=m9_handle_data_bigquant_run,
        prepare=m9_prepare_bigquant_run,
        before_trading_start=m9_before_trading_start_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='close',
        capital_base=1000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='真实价格',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
      File "<ipython-input-42-ae5587bd7f5e>", line 54
        schedule_function(func=trade, date_rules.every_day(), time_rule=time_rules.market_close(minutes=15))
                                     ^
    SyntaxError: positional argument follows keyword argument
    

    (iQuant) #2

    您好,收到您的提问,已提交至策略工程师,会尽快为您回复。


    (189) #3

    修改了一些低级错误,不过还是搞不出来结果,有劳老师检查一下,多谢

    克隆策略
    In [66]:
    m14.data.read_df().head(10)
    
    Out[66]:
    date instrument risk ref12 ma13
    42 2018-10-24 159915.ZOF 0.0 1.289 1.218385
    43 2018-10-24 510500.HOF 0.0 4.942 4.575692
    44 2018-10-24 159905.ZOF 0.0 1.348 1.306385
    45 2018-10-24 510300.HOF 0.0 3.341 3.238308
    46 2018-10-24 510050.HOF 0.0 2.533 2.500615
    47 2018-10-24 159949.ZOF 0.0 0.485 0.454462
    48 2018-10-24 510880.HOF 0.0 2.707 2.619846
    49 2018-10-25 510500.HOF 0.0 4.927 4.542923
    50 2018-10-25 510050.HOF 0.0 2.545 2.500462
    51 2018-10-25 510880.HOF 0.0 2.699 2.613231

      {"Description":"实验创建于2019/12/3","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-25:input_data","SourceOutputPortId":"-14:data"},{"DestinationInputPortId":"-14:features","SourceOutputPortId":"-20:data"},{"DestinationInputPortId":"-25:features","SourceOutputPortId":"-20:data"},{"DestinationInputPortId":"-14:instruments","SourceOutputPortId":"-5:data"},{"DestinationInputPortId":"-1397:instruments","SourceOutputPortId":"-5:data"},{"DestinationInputPortId":"-115:features","SourceOutputPortId":"-95:data"},{"DestinationInputPortId":"-124:features","SourceOutputPortId":"-95:data"},{"DestinationInputPortId":"-108:input_ds","SourceOutputPortId":"-115:data"},{"DestinationInputPortId":"-115:input_data","SourceOutputPortId":"-124:data"},{"DestinationInputPortId":"-1428:input_1","SourceOutputPortId":"-108:data"},{"DestinationInputPortId":"-124:instruments","SourceOutputPortId":"-339:data"},{"DestinationInputPortId":"-561:input_data","SourceOutputPortId":"-1418:data"},{"DestinationInputPortId":"-1418:data2","SourceOutputPortId":"-1428:data_1"},{"DestinationInputPortId":"-556:input_ds","SourceOutputPortId":"-25:data"},{"DestinationInputPortId":"-1418:data1","SourceOutputPortId":"-556:data"},{"DestinationInputPortId":"-1397:options_data","SourceOutputPortId":"-561:data"}],"ModuleNodes":[{"Id":"-14","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1d_CN_FUND","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-14"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-14"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-14","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-20","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nref12=shift(close, 12)\nma13=mean(close, 13)\n\nopen\nlow\nhigh\nvolume\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-20"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-20","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-5","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2018-10-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-11-18","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_FUND","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"510300.HOF\n510050.HOF\n510500.HOF\n159915.ZOF\n159905.ZOF\n159949.ZOF\n510880.HOF\n\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":"-5"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-5","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-95","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\n#avg_volume=mean(volume, 10)\n#risk=where(amount<avg_amount, 1, 0)\navg_volume=mean(volume, 7)\nup_down=where(volume<avg_volume, 1, 0)\ncount_risk=sum(up_down, 6)\nrisk=where(count_risk>=6, 1, 0)","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-95"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-95","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-115","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-115"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-115"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-115","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"Comment":"","CommentCollapsed":true},{"Id":"-124","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1d_index_CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-124"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-124"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-124","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"Comment":"","CommentCollapsed":true},{"Id":"-108","ModuleId":"BigQuantSpace.select_columns.select_columns-v3","ModuleParameters":[{"Name":"columns","Value":"date,instrument,risk","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"reverse_select","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_ds","NodeId":"-108"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"columns_ds","NodeId":"-108"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-108","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":7,"Comment":"","CommentCollapsed":true},{"Id":"-339","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2018-10-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-11-18","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"000300.HIX\n000016.HIX\n000905.HIX\n000015.HIX\n399673.ZIX\n399006.ZIX\n399324.ZIX","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-339"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-339","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"Comment":"","CommentCollapsed":true},{"Id":"-1397","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 # 加载预测数据\n context.op= context.options['data'].read_df()\n print(context.op.tail())\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)\n # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只\n \n stock_count = 3\n context.hold_days = 5\n context.his_bars=13\n context.etf_dic={\n '000016.HIX':'510050.HOF',\n '000300.HIX':'510300.HOF',\n '000905.HIX':'510500.HOF',\n '000015.HIX':'510880.HOF',\n '399673.ZIX':'159949.ZOF',\n '399006.ZIX':'159915.ZOF',\n '399324.ZIX':'159905.ZOF',\n }\n context.ind=[i for i in context.etf_dic.keys()]\n context.etf=[i for i in context.etf_dic.values()]\n context.ind_dic=dict(zip(context.etf,context.ind))#次序\n \n schedule_function(func=trade, date_rule=date_rules.every_day(), time_rule=time_rules.market_close(minutes=15))\n ","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\n#def bigquant_run(context, data):\n #pass\ndef trade(context,data):\n import pandas as pd\n date = data.current_dt.strftime('%Y-%m-%d')\n print(date)\n context.today=pd.DataFrame()\n try:\n context.today=context.op.loc[date,:]\n except:\n return\n \n print(context.today)\n \n context.daily_stock_buy=[]\n context.daily_stock_sell=[]\n \n stock_hold_now = [equity for equity in context.portfolio.positions.keys()]\n print('当前持仓:%s',stock_hold_now)\n \n for sec in context.etf:\n curr = data.current(context.symbol(sec), ['price'])\n \n if curr>context.today.loc[sec,'ref12'] & curr>context.today.loc[sec,'ma13'] & context.today.loc[sec,'risk']==0:\n context.daily_stock_buy.append(sec)\n else:\n context.daily_stock_sell.append(sec)\n \n try:\n\t buy_stock = context.daily_stock_buy# 当日符合买入条件的股票\n except:\n\t buy_stock=[] # 如果没有符合条件的股票,就设置为空\n try:\n\t sell_stock = context.daily_stock_sell # 当日符合卖出条件的股票\n except:\n\t sell_stock=[] # 如果没有符合条件的股票,就设置为空\n \n\t# 需要卖出的股票:已有持仓中符合卖出条件的股票\n stock_to_sell = [ i for i in stock_hold_now if i in sell_stock]\n stock_to_buy = [ i for i in buy_stock if i not in stock_hold_now]\n\t# 如果有卖出信号\n if len(stock_to_sell)>0:\n\t for instrument in stock_to_sell:\n\t cur_position = context.portfolio.positions[instrument].closeable_amount # 持仓\n\t if cur_position > 0 :\n\t order_target_value(instrument, 0)\n\t print('正在卖出%s',instrument)\n if len(stock_to_buy)>0:\n weight =1/len(stock_to_buy)\n for instrument in stock_to_buy:\n\t order_target_value(instrument, weight*context.portfolio.total_value) # 买入\n\t print('正在买入%s',instrument)\n\t ","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":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":"1000000","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"minute","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":"-1397"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-1397"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-1397"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-1397"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-1397"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-1397","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":9,"Comment":"","CommentCollapsed":true},{"Id":"-1418","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date,instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how","Value":"left","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sort","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data1","NodeId":"-1418"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"-1418"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1418","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":10,"Comment":"","CommentCollapsed":true},{"Id":"-1428","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_1.read_df()\n \n etf_dic={\n '000016.HIX':'510050.HOF',\n '000300.HIX':'510300.HOF',\n '000905.HIX':'510500.HOF',\n '000015.HIX':'510880.HOF',\n '399673.ZIX':'159949.ZOF',\n '399006.ZIX':'159915.ZOF',\n '399324.ZIX':'159905.ZOF',\n }\n df['instrument']=df['instrument'].map(lambda x:etf_dic[x])\n \n data_1= DataSource.write_df(df)\n return Outputs(data_1=data_1, 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":"-1428"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-1428"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-1428"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-1428","OutputType":null},{"Name":"data_2","NodeId":"-1428","OutputType":null},{"Name":"data_3","NodeId":"-1428","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":11,"Comment":"","CommentCollapsed":true},{"Id":"-25","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-25"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-25"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-25","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":12,"Comment":"","CommentCollapsed":true},{"Id":"-556","ModuleId":"BigQuantSpace.select_columns.select_columns-v3","ModuleParameters":[{"Name":"columns","Value":"date,instrument,ref12,ma13","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"reverse_select","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_ds","NodeId":"-556"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"columns_ds","NodeId":"-556"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-556","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":13,"Comment":"","CommentCollapsed":true},{"Id":"-561","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-561"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-561","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":14,"Comment":"","CommentCollapsed":true},{"Id":"-203","ModuleId":"BigQuantSpace.filter.filter-v3","ModuleParameters":[{"Name":"expr","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_left_data","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-203"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-203","OutputType":null},{"Name":"left_data","NodeId":"-203","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"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='-14' Position='58.52679443359375,99.47321319580078,200,200'/><NodePosition Node='-20' Position='187,-82,200,200'/><NodePosition Node='-5' Position='-103,-28,200,200'/><NodePosition Node='-95' Position='843,-72,200,200'/><NodePosition Node='-115' Position='755,106,200,200'/><NodePosition Node='-124' Position='705,32,200,200'/><NodePosition Node='-108' Position='665,197,200,200'/><NodePosition Node='-339' Position='535,-91,200,200'/><NodePosition Node='-1397' Position='192,582,200,200'/><NodePosition Node='-1418' Position='224.82144165039062,329.2053527832031,200,200'/><NodePosition Node='-1428' Position='626,281,200,200'/><NodePosition Node='-25' Position='76,176,200,200'/><NodePosition Node='-556' Position='194,244,200,200'/><NodePosition Node='-561' Position='245,402,200,200'/><NodePosition Node='-203' Position='-128.99998950958252,464.9732360839844,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 [72]:
      # 本代码由可视化策略环境自动生成 2019年12月5日 00:59
      # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
      
      
      # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
      def m11_run_bigquant_run(input_1, input_2, input_3):
          # 示例代码如下。在这里编写您的代码
          df = input_1.read_df()
          
          etf_dic={
                  '000016.HIX':'510050.HOF',
                  '000300.HIX':'510300.HOF',
                  '000905.HIX':'510500.HOF',
                  '000015.HIX':'510880.HOF',
                  '399673.ZIX':'159949.ZOF',
                  '399006.ZIX':'159915.ZOF',
                  '399324.ZIX':'159905.ZOF',
              }
          df['instrument']=df['instrument'].map(lambda x:etf_dic[x])
          
          data_1= DataSource.write_df(df)
          return Outputs(data_1=data_1, data_2=None, data_3=None)
      
      # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
      def m11_post_run_bigquant_run(outputs):
          return outputs
      
      # 回测引擎:初始化函数,只执行一次
      def m9_initialize_bigquant_run(context):
          # 加载预测数据
          context.op= context.options['data'].read_df()
          print(context.op.tail())
          # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
          context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
          # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
          # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
          
          stock_count = 3
          context.hold_days = 5
          context.his_bars=13
          context.etf_dic={
                  '000016.HIX':'510050.HOF',
                  '000300.HIX':'510300.HOF',
                  '000905.HIX':'510500.HOF',
                  '000015.HIX':'510880.HOF',
                  '399673.ZIX':'159949.ZOF',
                  '399006.ZIX':'159915.ZOF',
                  '399324.ZIX':'159905.ZOF',
              }
          context.ind=[i for i in context.etf_dic.keys()]
          context.etf=[i for i in context.etf_dic.values()]
          context.ind_dic=dict(zip(context.etf,context.ind))#次序
          
          schedule_function(func=trade, date_rule=date_rules.every_day(), time_rule=time_rules.market_close(minutes=15))
         
      # 回测引擎:每日数据处理函数,每天执行一次
      #def m9_handle_data_bigquant_run(context, data):
          #pass
      def trade(context,data):
          import pandas as pd
          date = data.current_dt.strftime('%Y-%m-%d')
          print(date)
          context.today=pd.DataFrame()
          try:
              context.today=context.op.loc[date,:]
          except:
              return
          
          print(context.today)
          
          context.daily_stock_buy=[]
          context.daily_stock_sell=[]
          
          stock_hold_now = [equity for equity in context.portfolio.positions.keys()]
          print('当前持仓:%s',stock_hold_now)
          
          for sec in context.etf:
              curr = data.current(context.symbol(sec), ['price'])
              
              if curr>context.today.loc[sec,'ref12'] & curr>context.today.loc[sec,'ma13'] & context.today.loc[sec,'risk']==0:
                  context.daily_stock_buy.append(sec)
              else:
                  context.daily_stock_sell.append(sec)
          
          try:
      	    buy_stock = context.daily_stock_buy# 当日符合买入条件的股票
          except:
      	    buy_stock=[]  # 如果没有符合条件的股票,就设置为空
          try:
      	    sell_stock = context.daily_stock_sell # 当日符合卖出条件的股票
          except:
      	    sell_stock=[] # 如果没有符合条件的股票,就设置为空
          
      	# 需要卖出的股票:已有持仓中符合卖出条件的股票
          stock_to_sell = [ i for i in stock_hold_now if i in sell_stock]
          stock_to_buy = [ i for i in buy_stock if i not in stock_hold_now]
      	# 如果有卖出信号
          if len(stock_to_sell)>0:
      	    for instrument in stock_to_sell:
      	        cur_position = context.portfolio.positions[instrument].closeable_amount # 持仓
      	        if cur_position > 0 :
      	            order_target_value(instrument, 0)
      	            print('正在卖出%s',instrument)
          if len(stock_to_buy)>0:
              weight =1/len(stock_to_buy)
              for instrument in stock_to_buy:
      	        order_target_value(instrument, weight*context.portfolio.total_value) # 买入
      	        print('正在买入%s',instrument)
      	    
      # 回测引擎:准备数据,只执行一次
      def m9_prepare_bigquant_run(context):
          pass
      
      # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
      def m9_before_trading_start_bigquant_run(context, data):
          pass
      
      
      m2 = M.input_features.v1(
          features="""
      # #号开始的表示注释,注释需单独一行
      # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
      ref12=shift(close, 12)
      ma13=mean(close, 13)
      
      open
      low
      high
      volume
      """
      )
      
      m3 = M.instruments.v2(
          start_date='2018-10-01',
          end_date='2019-11-18',
          market='CN_FUND',
          instrument_list="""510300.HOF
      510050.HOF
      510500.HOF
      159915.ZOF
      159905.ZOF
      159949.ZOF
      510880.HOF
      
      """,
          max_count=0
      )
      
      m1 = M.use_datasource.v1(
          instruments=m3.data,
          features=m2.data,
          datasource_id='bar1d_CN_FUND',
          start_date='',
          end_date=''
      )
      
      m12 = M.derived_feature_extractor.v3(
          input_data=m1.data,
          features=m2.data,
          date_col='date',
          instrument_col='instrument',
          drop_na=False,
          remove_extra_columns=False,
          user_functions={}
      )
      
      m13 = M.select_columns.v3(
          input_ds=m12.data,
          columns='date,instrument,ref12,ma13',
          reverse_select=False,
          m_cached=False
      )
      
      m4 = M.input_features.v1(
          features="""
      # #号开始的表示注释,注释需单独一行
      # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
      #avg_volume=mean(volume, 10)
      #risk=where(amount<avg_amount, 1, 0)
      avg_volume=mean(volume, 7)
      up_down=where(volume<avg_volume, 1, 0)
      count_risk=sum(up_down, 6)
      risk=where(count_risk>=6, 1, 0)"""
      )
      
      m8 = M.instruments.v2(
          start_date='2018-10-01',
          end_date='2019-11-18',
          market='CN_STOCK_A',
          instrument_list="""000300.HIX
      000016.HIX
      000905.HIX
      000015.HIX
      399673.ZIX
      399006.ZIX
      399324.ZIX""",
          max_count=0
      )
      
      m6 = M.use_datasource.v1(
          instruments=m8.data,
          features=m4.data,
          datasource_id='bar1d_index_CN_STOCK_A',
          start_date='',
          end_date=''
      )
      
      m5 = M.derived_feature_extractor.v3(
          input_data=m6.data,
          features=m4.data,
          date_col='date',
          instrument_col='instrument',
          drop_na=True,
          remove_extra_columns=True,
          user_functions={}
      )
      
      m7 = M.select_columns.v3(
          input_ds=m5.data,
          columns='date,instrument,risk',
          reverse_select=False,
          m_cached=False
      )
      
      m11 = M.cached.v3(
          input_1=m7.data,
          run=m11_run_bigquant_run,
          post_run=m11_post_run_bigquant_run,
          input_ports='',
          params='{}',
          output_ports=''
      )
      
      m10 = M.join.v3(
          data1=m13.data,
          data2=m11.data_1,
          on='date,instrument',
          how='left',
          sort=False
      )
      
      m14 = M.dropnan.v1(
          input_data=m10.data
      )
      
      m9 = M.trade.v4(
          instruments=m3.data,
          options_data=m14.data,
          start_date='',
          end_date='',
          initialize=m9_initialize_bigquant_run,
          handle_data=m9_handle_data_bigquant_run,
          prepare=m9_prepare_bigquant_run,
          before_trading_start=m9_before_trading_start_bigquant_run,
          volume_limit=0.025,
          order_price_field_buy='open',
          order_price_field_sell='close',
          capital_base=1000000,
          auto_cancel_non_tradable_orders=True,
          data_frequency='minute',
          price_type='真实价格',
          product_type='股票',
          plot_charts=True,
          backtest_only=False,
          benchmark='000300.SHA'
      )
      
      m15 = M.filter.v3(
          expr='',
          output_left_data=False
      )
      
      列: ['date', 'instrument', 'ref12', 'ma13']
      /data: 1894
      
      列: ['date', 'instrument', 'risk']
      /data: 1869
      
                 date  instrument  risk  ref12      ma13
      1847 2019-11-18  159949.ZOF   0.0  0.587  0.598769
      1848 2019-11-18  510880.HOF   0.0  2.765  2.764538
      1849 2019-11-18  510050.HOF   1.0  3.003  3.039692
      1850 2019-11-18  510500.HOF   1.0  5.315  5.331385
      1851 2019-11-18  510300.HOF   1.0  3.945  3.997308
      
      2018-10-08
      2018-10-09
      2018-10-10
      2018-10-11
      2018-10-12
      2018-10-15
      2018-10-16
      2018-10-17
      2018-10-18
      2018-10-19
      2018-10-22
      2018-10-23
      2018-10-24
      2018-10-25
      2018-10-26
      2018-10-29
      2018-10-30
      2018-10-31
      2018-11-01
      2018-11-02
      2018-11-05
      2018-11-06
      2018-11-07
      2018-11-08
      2018-11-09
      2018-11-12
      2018-11-13
      2018-11-14
      2018-11-15
      2018-11-16
      2018-11-19
      2018-11-20
      2018-11-21
      2018-11-22
      2018-11-23
      2018-11-26
      2018-11-27
      2018-11-28
      2018-11-29
      2018-11-30
      2018-12-03
      2018-12-04
      2018-12-05
      2018-12-06
      2018-12-07
      2018-12-10
      2018-12-11
      2018-12-12
      2018-12-13
      2018-12-14
      2018-12-17
      2018-12-18
      2018-12-19
      2018-12-20
      2018-12-21
      2018-12-24
      2018-12-25
      2018-12-26
      2018-12-27
      2018-12-28
      2019-01-02
      2019-01-03
      2019-01-04
      2019-01-07
      2019-01-08
      2019-01-09
      2019-01-10
      2019-01-11
      2019-01-14
      2019-01-15
      2019-01-16
      2019-01-17
      2019-01-18
      2019-01-21
      2019-01-22
      2019-01-23
      2019-01-24
      2019-01-25
      2019-01-28
      2019-01-29
      2019-01-30
      2019-01-31
      2019-02-01
      2019-02-11
      2019-02-12
      2019-02-13
      2019-02-14
      2019-02-15
      2019-02-18
      2019-02-19
      2019-02-20
      2019-02-21
      2019-02-22
      2019-02-25
      2019-02-26
      2019-02-27
      2019-02-28
      2019-03-01
      2019-03-04
      2019-03-05
      2019-03-06
      2019-03-07
      2019-03-08
      2019-03-11
      2019-03-12
      2019-03-13
      2019-03-14
      2019-03-15
      2019-03-18
      2019-03-19
      2019-03-20
      2019-03-21
      2019-03-22
      2019-03-25
      2019-03-26
      2019-03-27
      2019-03-28
      2019-03-29
      2019-04-01
      2019-04-02
      2019-04-03
      2019-04-04
      2019-04-08
      2019-04-09
      2019-04-10
      2019-04-11
      2019-04-12
      2019-04-15
      2019-04-16
      2019-04-17
      2019-04-18
      2019-04-19
      2019-04-22
      2019-04-23
      2019-04-24
      2019-04-25
      2019-04-26
      2019-04-29
      2019-04-30
      2019-05-06
      2019-05-07
      2019-05-08
      2019-05-09
      2019-05-10
      2019-05-13
      2019-05-14
      2019-05-15
      2019-05-16
      2019-05-17
      2019-05-20
      2019-05-21
      2019-05-22
      2019-05-23
      2019-05-24
      2019-05-27
      2019-05-28
      2019-05-29
      2019-05-30
      2019-05-31
      2019-06-03
      2019-06-04
      2019-06-05
      2019-06-06
      2019-06-10
      2019-06-11
      2019-06-12
      2019-06-13
      2019-06-14
      2019-06-17
      2019-06-18
      2019-06-19
      2019-06-20
      2019-06-21
      2019-06-24
      2019-06-25
      2019-06-26
      2019-06-27
      2019-06-28
      2019-07-01
      2019-07-02
      2019-07-03
      2019-07-04
      2019-07-05
      2019-07-08
      2019-07-09
      2019-07-10
      2019-07-11
      2019-07-12
      2019-07-15
      2019-07-16
      2019-07-17
      2019-07-18
      2019-07-19
      2019-07-22
      2019-07-23
      2019-07-24
      2019-07-25
      2019-07-26
      2019-07-29
      2019-07-30
      2019-07-31
      2019-08-01
      2019-08-02
      2019-08-05
      2019-08-06
      2019-08-07
      2019-08-08
      2019-08-09
      2019-08-12
      2019-08-13
      2019-08-14
      2019-08-15
      2019-08-16
      2019-08-19
      2019-08-20
      2019-08-21
      2019-08-22
      2019-08-23
      2019-08-26
      2019-08-27
      2019-08-28
      2019-08-29
      2019-08-30
      2019-09-02
      2019-09-03
      2019-09-04
      2019-09-05
      2019-09-06
      2019-09-09
      2019-09-10
      2019-09-11
      2019-09-12
      2019-09-16
      2019-09-17
      2019-09-18
      2019-09-19
      2019-09-20
      2019-09-23
      2019-09-24
      2019-09-25
      2019-09-26
      2019-09-27
      2019-09-30
      2019-10-08
      2019-10-09
      2019-10-10
      2019-10-11
      2019-10-14
      2019-10-15
      2019-10-16
      2019-10-17
      2019-10-18
      2019-10-21
      2019-10-22
      2019-10-23
      2019-10-24
      2019-10-25
      2019-10-28
      2019-10-29
      2019-10-30
      2019-10-31
      2019-11-01
      2019-11-04
      2019-11-05
      2019-11-06
      2019-11-07
      2019-11-08
      2019-11-11
      2019-11-12
      2019-11-13
      2019-11-14
      2019-11-15
      2019-11-18
      
      • 收益率0.0%
      • 年化收益率0.0%
      • 基准收益率13.64%
      • 阿尔法-0.03
      • 贝塔-0.0
      • 夏普比率0.0
      • 胜率0.0
      • 盈亏比0.0
      • 收益波动率0.0%
      • 信息比率-0.04
      • 最大回撤0.0%
      bigcharts-data-start/{"__id":"bigchart-cf9545edcb3f435e80b193ed884afb50","__type":"tabs"}/bigcharts-data-end