定期5个交易日买卖选股策略模拟上线后,为什么每天推送股票买入信息?

新手专区
标签: #<Tag:0x00007ff19514d140>

(MOFI007) #1
克隆策略

多条件选股策略

    {"Description":"实验创建于2018/6/27","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-353:instruments","SourceOutputPortId":"-51:data"},{"DestinationInputPortId":"-370:instruments","SourceOutputPortId":"-51:data"},{"DestinationInputPortId":"-353:features","SourceOutputPortId":"-59:data"},{"DestinationInputPortId":"-360:features","SourceOutputPortId":"-59:data"},{"DestinationInputPortId":"-360:input_data","SourceOutputPortId":"-353:data"},{"DestinationInputPortId":"-56:input_data","SourceOutputPortId":"-360:data"},{"DestinationInputPortId":"-370:options_data","SourceOutputPortId":"-56:data"}],"ModuleNodes":[{"Id":"-51","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2013-06-10","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2020-12-17","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"000008.SZA\n000009.SZA\n000012.SZA\n000021.SZA\n000027.SZA\n000028.SZA\n000031.SZA\n000039.SZA\n000046.SZA\n000050.SZA\n000060.SZA\n000061.SZA\n000062.SZA\n000078.SZA\n000089.SZA\n000090.SZA\n000156.SZA\n000158.SZA\n000301.SZA\n000400.SZA\n000401.SZA\n000402.SZA\n000415.SZA\n000488.SZA\n000501.SZA\n000513.SZA\n000519.SZA\n000528.SZA\n000537.SZA\n000540.SZA\n000543.SZA\n000547.SZA\n000553.SZA\n000559.SZA\n000563.SZA\n000564.SZA\n000581.SZA\n000598.SZA\n000600.SZA\n000623.SZA\n000629.SZA\n000630.SZA\n000636.SZA\n000681.SZA\n000685.SZA\n000686.SZA\n000690.SZA\n000709.SZA\n000717.SZA\n000718.SZA\n000729.SZA\n000732.SZA\n000738.SZA\n000739.SZA\n000750.SZA\n000758.SZA\n000778.SZA\n000807.SZA\n000813.SZA\n000825.SZA\n000826.SZA\n000830.SZA\n000869.SZA\n000877.SZA\n000878.SZA\n000883.SZA\n000887.SZA\n000898.SZA\n000930.SZA\n000932.SZA\n000937.SZA\n000959.SZA\n000960.SZA\n000967.SZA\n000970.SZA\n000975.SZA\n000983.SZA\n000988.SZA\n000990.SZA\n000997.SZA\n000998.SZA\n000999.SZA\n001872.SZA\n001914.SZA\n002002.SZA\n002004.SZA\n002010.SZA\n002013.SZA\n002019.SZA\n002028.SZA\n002030.SZA\n002038.SZA\n002048.SZA\n002051.SZA\n002056.SZA\n002064.SZA\n002074.SZA\n002075.SZA\n002078.SZA\n002080.SZA\n002081.SZA\n002085.SZA\n002092.SZA\n002093.SZA\n002110.SZA\n002124.SZA\n002128.SZA\n002131.SZA\n002138.SZA\n002152.SZA\n002155.SZA\n002156.SZA\n002174.SZA\n002183.SZA\n002185.SZA\n002191.SZA\n002195.SZA\n002203.SZA\n002212.SZA\n002217.SZA\n002221.SZA\n002223.SZA\n002233.SZA\n002242.SZA\n002244.SZA\n002249.SZA\n002250.SZA\n002266.SZA\n002268.SZA\n002273.SZA\n002281.SZA\n002294.SZA\n002299.SZA\n002302.SZA\n002317.SZA\n002340.SZA\n002353.SZA\n002368.SZA\n002372.SZA\n002373.SZA\n002375.SZA\n002382.SZA\n002385.SZA\n002387.SZA\n002390.SZA\n002396.SZA\n002399.SZA\n002408.SZA\n002416.SZA\n002419.SZA\n002423.SZA\n002424.SZA\n002429.SZA\n002434.SZA\n002440.SZA\n002444.SZA\n002458.SZA\n002465.SZA\n002468.SZA\n002500.SZA\n002503.SZA\n002505.SZA\n002506.SZA\n002507.SZA\n002511.SZA\n002544.SZA\n002557.SZA\n002563.SZA\n002568.SZA\n002572.SZA\n002583.SZA\n002589.SZA\n002595.SZA\n002603.SZA\n002625.SZA\n002635.SZA\n002640.SZA\n002648.SZA\n002653.SZA\n002670.SZA\n002683.SZA\n002690.SZA\n002701.SZA\n002705.SZA\n002709.SZA\n002745.SZA\n002791.SZA\n002797.SZA\n002807.SZA\n002815.SZA\n002818.SZA\n002831.SZA\n002839.SZA\n002867.SZA\n002901.SZA\n002920.SZA\n002925.SZA\n002926.SZA\n002936.SZA\n002941.SZA\n002946.SZA\n002948.SZA\n002957.SZA\n002966.SZA\n002985.SZA\n300001.SZA\n300002.SZA\n300009.SZA\n300010.SZA\n300012.SZA\n300017.SZA\n300024.SZA\n300026.SZA\n300058.SZA\n300070.SZA\n300072.SZA\n300088.SZA\n300113.SZA\n300115.SZA\n300133.SZA\n300134.SZA\n300146.SZA\n300166.SZA\n300168.SZA\n300180.SZA\n300182.SZA\n300207.SZA\n300212.SZA\n300223.SZA\n300244.SZA\n300257.SZA\n300271.SZA\n300274.SZA\n300285.SZA\n300296.SZA\n300315.SZA\n300316.SZA\n300324.SZA\n300357.SZA\n300376.SZA\n300418.SZA\n300459.SZA\n300463.SZA\n300474.SZA\n300482.SZA\n300496.SZA\n300595.SZA\n300618.SZA\n300630.SZA\n300699.SZA\n600006.SHA\n600008.SHA\n600021.SHA\n600022.SHA\n600026.SHA\n600037.SHA\n600038.SHA\n600039.SHA\n600053.SHA\n600056.SHA\n600060.SHA\n600062.SHA\n600064.SHA\n600073.SHA\n600079.SHA\n600089.SHA\n600094.SHA\n600120.SHA\n600126.SHA\n600131.SHA\n600132.SHA\n600141.SHA\n600143.SHA\n600153.SHA\n600155.SHA\n600158.SHA\n600160.SHA\n600166.SHA\n600167.SHA\n600170.SHA\n600171.SHA\n600188.SHA\n600195.SHA\n600201.SHA\n600216.SHA\n600219.SHA\n600256.SHA\n600258.SHA\n600259.SHA\n600260.SHA\n600266.SHA\n600273.SHA\n600277.SHA\n600282.SHA\n600291.SHA\n600307.SHA\n600312.SHA\n600315.SHA\n600316.SHA\n600325.SHA\n600329.SHA\n600338.SHA\n600339.SHA\n600348.SHA\n600350.SHA\n600372.SHA\n600373.SHA\n600376.SHA\n600380.SHA\n600388.SHA\n600392.SHA\n600398.SHA\n600409.SHA\n600410.SHA\n600415.SHA\n600418.SHA\n600426.SHA\n600435.SHA\n600446.SHA\n600460.SHA\n600466.SHA\n600486.SHA\n600497.SHA\n600500.SHA\n600507.SHA\n600511.SHA\n600515.SHA\n600516.SHA\n600517.SHA\n600528.SHA\n600529.SHA\n600535.SHA\n600545.SHA\n600546.SHA\n600549.SHA\n600556.SHA\n600563.SHA\n600566.SHA\n600567.SHA\n600572.SHA\n600575.SHA\n600580.SHA\n600582.SHA\n600597.SHA\n600598.SHA\n600623.SHA\n600633.SHA\n600639.SHA\n600640.SHA\n600642.SHA\n600643.SHA\n600645.SHA\n600648.SHA\n600649.SHA\n600657.SHA\n600664.SHA\n600667.SHA\n600673.SHA\n600675.SHA\n600699.SHA\n600704.SHA\n600707.SHA\n600717.SHA\n600718.SHA\n600728.SHA\n600729.SHA\n600733.SHA\n600737.SHA\n600739.SHA\n600748.SHA\n600754.SHA\n600755.SHA\n600765.SHA\n600776.SHA\n600779.SHA\n600782.SHA\n600787.SHA\n600801.SHA\n600804.SHA\n600808.SHA\n600811.SHA\n600820.SHA\n600823.SHA\n600827.SHA\n600835.SHA\n600839.SHA\n600859.SHA\n600862.SHA\n600863.SHA\n600867.SHA\n600869.SHA\n600871.SHA\n600875.SHA\n600879.SHA\n600881.SHA\n600884.SHA\n600885.SHA\n600895.SHA\n600901.SHA\n600903.SHA\n600908.SHA\n600909.SHA\n600917.SHA\n600928.SHA\n600959.SHA\n600967.SHA\n600968.SHA\n600970.SHA\n600985.SHA\n601000.SHA\n601003.SHA\n601005.SHA\n601016.SHA\n601068.SHA\n601098.SHA\n601099.SHA\n601106.SHA\n601118.SHA\n601127.SHA\n601128.SHA\n601139.SHA\n601168.SHA\n601179.SHA\n601200.SHA\n601228.SHA\n601233.SHA\n601298.SHA\n601333.SHA\n601456.SHA\n601512.SHA\n601598.SHA\n601608.SHA\n601611.SHA\n601615.SHA\n601689.SHA\n601699.SHA\n601717.SHA\n601718.SHA\n601778.SHA\n601799.SHA\n601801.SHA\n601828.SHA\n601860.SHA\n601865.SHA\n601866.SHA\n601869.SHA\n601880.SHA\n601928.SHA\n601958.SHA\n601966.SHA\n601969.SHA\n601975.SHA\n601992.SHA\n601997.SHA\n603000.SHA\n603056.SHA\n603077.SHA\n603198.SHA\n603225.SHA\n603228.SHA\n603256.SHA\n603260.SHA\n603290.SHA\n603317.SHA\n603328.SHA\n603338.SHA\n603345.SHA\n603355.SHA\n603377.SHA\n603379.SHA\n603444.SHA\n603486.SHA\n603515.SHA\n603568.SHA\n603589.SHA\n603605.SHA\n603638.SHA\n603650.SHA\n603708.SHA\n603712.SHA\n603719.SHA\n603737.SHA\n603786.SHA\n603806.SHA\n603816.SHA\n603858.SHA\n603866.SHA\n603868.SHA\n603882.SHA\n603883.SHA\n603885.SHA\n603888.SHA\n603893.SHA\n603927.SHA\n603983.SHA\n688002.SHA\n688029.SHA\n688088.SHA\n688099.SHA\n688321.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-51"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-51","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-59","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":" # #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\namount_0\nema3=ta_ema(close_0, 3)\nema5=ta_ema(close_0, 5)\nema8=ta_ema(close_0, 8)\nema10=ta_ema(close_0, 10)\nema30=ta_ema(close_0, 30)\nema40=ta_ema(close_0, 40)\nema50=ta_ema(close_0, 50)\nema60=ta_ema(close_0, 60)\nema120=ta_ema(close_0, 120)\nduotou=(amount_0>0)&(ema30>=ema60)\nsell_condition=where(close_0/close_1>2,1,0)#屏蔽该条件,不可能实现的条件\nsell_condition_2=where(duotou,0,1)\nbuy_condition=where(duotou,1,0)\n\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-59"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-59","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-353","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"400","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-353"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-353"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-353","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-360","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":"-360"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-360"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-360","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-370","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 # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.001, sell_cost=0.002, min_cost=5))\n context.rebalance_days = 5 \n# 如果策略运行中,需要将数据(比如运行天数)进行保存,可以借用extension这个对象,类型为dict\n if 'index' not in context.extension:\n context.extension['index'] = 0\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n # 1. 按每个K线递增,记录策略运行天数\n context.extension['index'] += 1\n # 回测引擎:每日数据处理函数,每天执行一次\n today = data.current_dt.strftime('%Y-%m-%d') # 日期\n # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表和对应的最新市值\n #stock_hold_now = {e.symbol: p.amount * p.last_sale_price\n #for e, p in context.perf_tracker.position_tracker.positions.items()}\n stock_hold_now = [equity.symbol for equity in context.portfolio.positions]\n\n # 记录用于买入股票的可用现金\n #cash_for_buy = context.portfolio.cash\n \n # 获取当日符合买入/卖出条件的股票列表\n try:\n buy_stock = context.daily_buy_stock[today] # 当日符合买入条件的股票\n except:\n buy_stock=[]\n try:\n sell_stock = context.daily_sell_stock[today] # 当日符合条件1(择时)卖出条件的股票\n except:\n sell_stock = []\n try:\n sell_stock_2 = context.daily_sell_stock_2[today] # 检查当日有无符合条件2(定期选股卖出)卖出条件的股票\n except:\n sell_stock_2 = [] \n \n # 需要买入的股票:没有持仓且符合买入条件的股票\n stock_to_buy = [i for i in buy_stock if i not in stock_hold_now]\n # 5. 已有持仓中符合定期选股卖出的股票列表 \n #no_need_to_sell=[i for i in stock_to_buy if i in stock_hold_now]\n #stock_to_sell_2 = [i for i in stock_hold_now if i not in no_need_to_sell]\n # 需要卖出的股票:已有持仓中符合择时卖出条件的股票\n stock_to_sell = [i for i in stock_hold_now if i in sell_stock] #择时卖出列表\n stock_to_sell_2 = [i for i in stock_hold_now if i in sell_stock_2] #定期选股卖出列表\n \n # 6. 执行条件2(定期选股卖出)卖出\n if context.extension['index'] % context.rebalance_days == 1 and len(stock_to_sell_2)>0:\n for instrument in stock_to_sell_2:#在符合卖出条件2的列表里面摘出股票代码\n if data.can_trade(context.symbol(instrument)):\n context.order_target_percent(context.symbol(instrument), 0)\n \n # 条件1(择时条件)卖出\n if len(stock_to_sell)>0: \n for instrument in stock_to_sell:\n # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态\n # 如果返回真值,则可以正常下单,否则会出错\n # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式 \n if data.can_trade(context.symbol(instrument)):\n context.order_target_percent(context.symbol(instrument), 0)\n # 开盘卖出后所得资金可用来买入股票,更新当日可用现金\n #cash_for_buy += stock_hold_now[instrument]\n \n # 如果到了换仓周期,并且有符合买入条件的股票,执行买入\n if context.extension['index'] % context.rebalance_days == 1 and len(stock_to_buy)>0:\n weight = min(1 / len(stock_to_buy),0.02) # 等权重\n for instrument in stock_to_buy:#在符合买入条件的列表里面摘出股票代码\n if data.can_trade(context.symbol(instrument)):\n context.order_target_percent(context.symbol(instrument), weight)\n \n \n \n \n ","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":" # 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n df = context.options['data'].read_df()\n # 函数:求满足开仓条件的股票列表\n \n def open_pos_con(df):\n return list(df[df['buy_condition']>0].instrument)\n\n # 函数:求满足平仓条件的股票列表\n def close_pos_con(df):\n return list(df[df['sell_condition']>0].instrument)\n \n def close_pos(df):\n return list(df[df['sell_condition_2']>0].instrument)\n \n # 每日买入股票的数据框\n context.daily_buy_stock= df.groupby('date').apply(open_pos_con)\n # 每日卖出股票的数据框\n context.daily_sell_stock= df.groupby('date').apply(close_pos_con) \n context.daily_sell_stock_2= df.groupby('date').apply(close_pos) \n # 调仓天数,可以理解为换仓一次\n context.rebalance_days = 5 \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.25","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":"800000","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":"000905.HIX","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-370"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-370"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-370"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-370"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-370"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-370","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":8,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-56","ModuleId":"BigQuantSpace.dropnan.dropnan-v2","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-56"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-56"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-56","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='-51' Position='85,74,200,200'/><NodePosition Node='-59' Position='458,74,200,200'/><NodePosition Node='-353' Position='300,218,200,200'/><NodePosition Node='-360' Position='277,350,200,200'/><NodePosition Node='-370' Position='197,590,200,200'/><NodePosition Node='-56' Position='287,480,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 [2]:
    # 本代码由可视化策略环境自动生成 2020年12月21日 09:40
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    def m8_initialize_bigquant_run(context):
    
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.001, sell_cost=0.002, min_cost=5))
        context.rebalance_days = 5      
    # 如果策略运行中,需要将数据(比如运行天数)进行保存,可以借用extension这个对象,类型为dict
        if 'index' not in context.extension:
            context.extension['index'] = 0
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m8_handle_data_bigquant_run(context, data):
            # 1. 按每个K线递增,记录策略运行天数
        context.extension['index']  += 1
        # 回测引擎:每日数据处理函数,每天执行一次
        today = data.current_dt.strftime('%Y-%m-%d') # 日期
        # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表和对应的最新市值
        #stock_hold_now = {e.symbol: p.amount * p.last_sale_price
                     #for e, p in context.perf_tracker.position_tracker.positions.items()}
        stock_hold_now = [equity.symbol for equity in context.portfolio.positions]
    
        # 记录用于买入股票的可用现金
        #cash_for_buy = context.portfolio.cash
        
        # 获取当日符合买入/卖出条件的股票列表
        try:
            buy_stock = context.daily_buy_stock[today]  # 当日符合买入条件的股票
        except:
            buy_stock=[]
        try:
            sell_stock = context.daily_sell_stock[today]  # 当日符合条件1(择时)卖出条件的股票
        except:
            sell_stock = []
        try:
            sell_stock_2 = context.daily_sell_stock_2[today]  # 检查当日有无符合条件2(定期选股卖出)卖出条件的股票
        except:
            sell_stock_2 = []   
        
        # 需要买入的股票:没有持仓且符合买入条件的股票
        stock_to_buy = [i for i in buy_stock if i not in stock_hold_now]
        # 5. 已有持仓中符合定期选股卖出的股票列表 
        #no_need_to_sell=[i for i in stock_to_buy if i in stock_hold_now]
        #stock_to_sell_2 = [i for i in stock_hold_now if i not in no_need_to_sell]
        # 需要卖出的股票:已有持仓中符合择时卖出条件的股票
        stock_to_sell = [i for i in stock_hold_now if i in sell_stock] #择时卖出列表
        stock_to_sell_2 = [i for i in stock_hold_now if i in sell_stock_2] #定期选股卖出列表
               
        # 6. 执行条件2(定期选股卖出)卖出
        if context.extension['index'] % context.rebalance_days == 1 and len(stock_to_sell_2)>0:
            for instrument in stock_to_sell_2:#在符合卖出条件2的列表里面摘出股票代码
                if data.can_trade(context.symbol(instrument)):
                    context.order_target_percent(context.symbol(instrument), 0)
                  
            # 条件1(择时条件)卖出
        if len(stock_to_sell)>0:    
            for instrument in stock_to_sell:
            # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态
            # 如果返回真值,则可以正常下单,否则会出错
            # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式            
                if data.can_trade(context.symbol(instrument)):
                    context.order_target_percent(context.symbol(instrument), 0)
                # 开盘卖出后所得资金可用来买入股票,更新当日可用现金
                #cash_for_buy += stock_hold_now[instrument]
        
        # 如果到了换仓周期,并且有符合买入条件的股票,执行买入
        if context.extension['index'] % context.rebalance_days == 1 and len(stock_to_buy)>0:
            weight = min(1 / len(stock_to_buy),0.02) # 等权重
            for instrument in stock_to_buy:#在符合买入条件的列表里面摘出股票代码
                if data.can_trade(context.symbol(instrument)):
                    context.order_target_percent(context.symbol(instrument), weight)
            
                
       
            
        
     # 回测引擎:准备数据,只执行一次
    def m8_prepare_bigquant_run(context):
        # 加载预测数据
        df = context.options['data'].read_df()
        # 函数:求满足开仓条件的股票列表
            
        def open_pos_con(df):
            return list(df[df['buy_condition']>0].instrument)
    
        # 函数:求满足平仓条件的股票列表
        def close_pos_con(df):
            return list(df[df['sell_condition']>0].instrument)
        
        def close_pos(df):
            return list(df[df['sell_condition_2']>0].instrument)
        
        # 每日买入股票的数据框
        context.daily_buy_stock= df.groupby('date').apply(open_pos_con)
        # 每日卖出股票的数据框
        context.daily_sell_stock= df.groupby('date').apply(close_pos_con)        
        context.daily_sell_stock_2= df.groupby('date').apply(close_pos)        
        # 调仓天数,可以理解为换仓一次
        context.rebalance_days = 5   
        
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m8_before_trading_start_bigquant_run(context, data):
        pass
    
    
    m1 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2013-06-10'),
        end_date=T.live_run_param('trading_date', '2020-12-17'),
        market='CN_STOCK_A',
        instrument_list="""000008.SZA
    000009.SZA
    000012.SZA
    000021.SZA
    000027.SZA
    000028.SZA
    000031.SZA
    000039.SZA
    000046.SZA
    000050.SZA
    000060.SZA
    000061.SZA
    000062.SZA
    000078.SZA
    000089.SZA
    000090.SZA
    000156.SZA
    000158.SZA
    000301.SZA
    000400.SZA
    000401.SZA
    000402.SZA
    000415.SZA
    000488.SZA
    000501.SZA
    000513.SZA
    000519.SZA
    000528.SZA
    000537.SZA
    000540.SZA
    000543.SZA
    000547.SZA
    000553.SZA
    000559.SZA
    000563.SZA
    000564.SZA
    000581.SZA
    000598.SZA
    000600.SZA
    000623.SZA
    000629.SZA
    000630.SZA
    000636.SZA
    000681.SZA
    000685.SZA
    000686.SZA
    000690.SZA
    000709.SZA
    000717.SZA
    000718.SZA
    000729.SZA
    000732.SZA
    000738.SZA
    000739.SZA
    000750.SZA
    000758.SZA
    000778.SZA
    000807.SZA
    000813.SZA
    000825.SZA
    000826.SZA
    000830.SZA
    000869.SZA
    000877.SZA
    000878.SZA
    000883.SZA
    000887.SZA
    000898.SZA
    000930.SZA
    000932.SZA
    000937.SZA
    000959.SZA
    000960.SZA
    000967.SZA
    000970.SZA
    000975.SZA
    000983.SZA
    000988.SZA
    000990.SZA
    000997.SZA
    000998.SZA
    000999.SZA
    001872.SZA
    001914.SZA
    002002.SZA
    002004.SZA
    002010.SZA
    002013.SZA
    002019.SZA
    002028.SZA
    002030.SZA
    002038.SZA
    002048.SZA
    002051.SZA
    002056.SZA
    002064.SZA
    002074.SZA
    002075.SZA
    002078.SZA
    002080.SZA
    002081.SZA
    002085.SZA
    002092.SZA
    002093.SZA
    002110.SZA
    002124.SZA
    002128.SZA
    002131.SZA
    002138.SZA
    002152.SZA
    002155.SZA
    002156.SZA
    002174.SZA
    002183.SZA
    002185.SZA
    002191.SZA
    002195.SZA
    002203.SZA
    002212.SZA
    002217.SZA
    002221.SZA
    002223.SZA
    002233.SZA
    002242.SZA
    002244.SZA
    002249.SZA
    002250.SZA
    002266.SZA
    002268.SZA
    002273.SZA
    002281.SZA
    002294.SZA
    002299.SZA
    002302.SZA
    002317.SZA
    002340.SZA
    002353.SZA
    002368.SZA
    002372.SZA
    002373.SZA
    002375.SZA
    002382.SZA
    002385.SZA
    002387.SZA
    002390.SZA
    002396.SZA
    002399.SZA
    002408.SZA
    002416.SZA
    002419.SZA
    002423.SZA
    002424.SZA
    002429.SZA
    002434.SZA
    002440.SZA
    002444.SZA
    002458.SZA
    002465.SZA
    002468.SZA
    002500.SZA
    002503.SZA
    002505.SZA
    002506.SZA
    002507.SZA
    002511.SZA
    002544.SZA
    002557.SZA
    002563.SZA
    002568.SZA
    002572.SZA
    002583.SZA
    002589.SZA
    002595.SZA
    002603.SZA
    002625.SZA
    002635.SZA
    002640.SZA
    002648.SZA
    002653.SZA
    002670.SZA
    002683.SZA
    002690.SZA
    002701.SZA
    002705.SZA
    002709.SZA
    002745.SZA
    002791.SZA
    002797.SZA
    002807.SZA
    002815.SZA
    002818.SZA
    002831.SZA
    002839.SZA
    002867.SZA
    002901.SZA
    002920.SZA
    002925.SZA
    002926.SZA
    002936.SZA
    002941.SZA
    002946.SZA
    002948.SZA
    002957.SZA
    002966.SZA
    002985.SZA
    300001.SZA
    300002.SZA
    300009.SZA
    300010.SZA
    300012.SZA
    300017.SZA
    300024.SZA
    300026.SZA
    300058.SZA
    300070.SZA
    300072.SZA
    300088.SZA
    300113.SZA
    300115.SZA
    300133.SZA
    300134.SZA
    300146.SZA
    300166.SZA
    300168.SZA
    300180.SZA
    300182.SZA
    300207.SZA
    300212.SZA
    300223.SZA
    300244.SZA
    300257.SZA
    300271.SZA
    300274.SZA
    300285.SZA
    300296.SZA
    300315.SZA
    300316.SZA
    300324.SZA
    300357.SZA
    300376.SZA
    300418.SZA
    300459.SZA
    300463.SZA
    300474.SZA
    300482.SZA
    300496.SZA
    300595.SZA
    300618.SZA
    300630.SZA
    300699.SZA
    600006.SHA
    600008.SHA
    600021.SHA
    600022.SHA
    600026.SHA
    600037.SHA
    600038.SHA
    600039.SHA
    600053.SHA
    600056.SHA
    600060.SHA
    600062.SHA
    600064.SHA
    600073.SHA
    600079.SHA
    600089.SHA
    600094.SHA
    600120.SHA
    600126.SHA
    600131.SHA
    600132.SHA
    600141.SHA
    600143.SHA
    600153.SHA
    600155.SHA
    600158.SHA
    600160.SHA
    600166.SHA
    600167.SHA
    600170.SHA
    600171.SHA
    600188.SHA
    600195.SHA
    600201.SHA
    600216.SHA
    600219.SHA
    600256.SHA
    600258.SHA
    600259.SHA
    600260.SHA
    600266.SHA
    600273.SHA
    600277.SHA
    600282.SHA
    600291.SHA
    600307.SHA
    600312.SHA
    600315.SHA
    600316.SHA
    600325.SHA
    600329.SHA
    600338.SHA
    600339.SHA
    600348.SHA
    600350.SHA
    600372.SHA
    600373.SHA
    600376.SHA
    600380.SHA
    600388.SHA
    600392.SHA
    600398.SHA
    600409.SHA
    600410.SHA
    600415.SHA
    600418.SHA
    600426.SHA
    600435.SHA
    600446.SHA
    600460.SHA
    600466.SHA
    600486.SHA
    600497.SHA
    600500.SHA
    600507.SHA
    600511.SHA
    600515.SHA
    600516.SHA
    600517.SHA
    600528.SHA
    600529.SHA
    600535.SHA
    600545.SHA
    600546.SHA
    600549.SHA
    600556.SHA
    600563.SHA
    600566.SHA
    600567.SHA
    600572.SHA
    600575.SHA
    600580.SHA
    600582.SHA
    600597.SHA
    600598.SHA
    600623.SHA
    600633.SHA
    600639.SHA
    600640.SHA
    600642.SHA
    600643.SHA
    600645.SHA
    600648.SHA
    600649.SHA
    600657.SHA
    600664.SHA
    600667.SHA
    600673.SHA
    600675.SHA
    600699.SHA
    600704.SHA
    600707.SHA
    600717.SHA
    600718.SHA
    600728.SHA
    600729.SHA
    600733.SHA
    600737.SHA
    600739.SHA
    600748.SHA
    600754.SHA
    600755.SHA
    600765.SHA
    600776.SHA
    600779.SHA
    600782.SHA
    600787.SHA
    600801.SHA
    600804.SHA
    600808.SHA
    600811.SHA
    600820.SHA
    600823.SHA
    600827.SHA
    600835.SHA
    600839.SHA
    600859.SHA
    600862.SHA
    600863.SHA
    600867.SHA
    600869.SHA
    600871.SHA
    600875.SHA
    600879.SHA
    600881.SHA
    600884.SHA
    600885.SHA
    600895.SHA
    600901.SHA
    600903.SHA
    600908.SHA
    600909.SHA
    600917.SHA
    600928.SHA
    600959.SHA
    600967.SHA
    600968.SHA
    600970.SHA
    600985.SHA
    601000.SHA
    601003.SHA
    601005.SHA
    601016.SHA
    601068.SHA
    601098.SHA
    601099.SHA
    601106.SHA
    601118.SHA
    601127.SHA
    601128.SHA
    601139.SHA
    601168.SHA
    601179.SHA
    601200.SHA
    601228.SHA
    601233.SHA
    601298.SHA
    601333.SHA
    601456.SHA
    601512.SHA
    601598.SHA
    601608.SHA
    601611.SHA
    601615.SHA
    601689.SHA
    601699.SHA
    601717.SHA
    601718.SHA
    601778.SHA
    601799.SHA
    601801.SHA
    601828.SHA
    601860.SHA
    601865.SHA
    601866.SHA
    601869.SHA
    601880.SHA
    601928.SHA
    601958.SHA
    601966.SHA
    601969.SHA
    601975.SHA
    601992.SHA
    601997.SHA
    603000.SHA
    603056.SHA
    603077.SHA
    603198.SHA
    603225.SHA
    603228.SHA
    603256.SHA
    603260.SHA
    603290.SHA
    603317.SHA
    603328.SHA
    603338.SHA
    603345.SHA
    603355.SHA
    603377.SHA
    603379.SHA
    603444.SHA
    603486.SHA
    603515.SHA
    603568.SHA
    603589.SHA
    603605.SHA
    603638.SHA
    603650.SHA
    603708.SHA
    603712.SHA
    603719.SHA
    603737.SHA
    603786.SHA
    603806.SHA
    603816.SHA
    603858.SHA
    603866.SHA
    603868.SHA
    603882.SHA
    603883.SHA
    603885.SHA
    603888.SHA
    603893.SHA
    603927.SHA
    603983.SHA
    688002.SHA
    688029.SHA
    688088.SHA
    688099.SHA
    688321.SHA""",
        max_count=0
    )
    
    m2 = M.input_features.v1(
        features=""" # #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    amount_0
    ema3=ta_ema(close_0, 3)
    ema5=ta_ema(close_0, 5)
    ema8=ta_ema(close_0, 8)
    ema10=ta_ema(close_0, 10)
    ema30=ta_ema(close_0, 30)
    ema40=ta_ema(close_0, 40)
    ema50=ta_ema(close_0, 50)
    ema60=ta_ema(close_0, 60)
    ema120=ta_ema(close_0, 120)
    duotou=(amount_0>0)&(ema30>=ema60)
    sell_condition=where(close_0/close_1>2,1,0)#屏蔽该条件,不可能实现的条件
    sell_condition_2=where(duotou,0,1)
    buy_condition=where(duotou,1,0)
    
    """
    )
    
    m5 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m2.data,
        start_date='',
        end_date='',
        before_start_days=400
    )
    
    m7 = M.derived_feature_extractor.v3(
        input_data=m5.data,
        features=m2.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m3 = M.dropnan.v2(
        input_data=m7.data
    )
    
    m8 = M.trade.v4(
        instruments=m1.data,
        options_data=m3.data,
        start_date='',
        end_date='',
        initialize=m8_initialize_bigquant_run,
        handle_data=m8_handle_data_bigquant_run,
        prepare=m8_prepare_bigquant_run,
        before_trading_start=m8_before_trading_start_bigquant_run,
        volume_limit=0.25,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=800000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='真实价格',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark='000905.HIX'
    )
    
    • 收益率128.31%
    • 年化收益率12.01%
    • 基准收益率74.46%
    • 阿尔法0.04
    • 贝塔0.92
    • 夏普比率0.45
    • 胜率0.32
    • 盈亏比1.71
    • 收益波动率27.16%
    • 信息比率0.02
    • 最大回撤70.29%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-704c42de365b4baf8c061dc91d3c939c"}/bigcharts-data-end

    image