复制链接
克隆策略

    {"description":"实验创建于2018/6/27","graph":{"edges":[{"to_node_id":"-353:instruments","from_node_id":"-51:data"},{"to_node_id":"-370:instruments","from_node_id":"-51:data"},{"to_node_id":"-353:features","from_node_id":"-59:data"},{"to_node_id":"-360:features","from_node_id":"-59:data"},{"to_node_id":"-360:input_data","from_node_id":"-353:data"},{"to_node_id":"-865:input_data","from_node_id":"-360:data"},{"to_node_id":"-531:input_data","from_node_id":"-390:sorted_data"},{"to_node_id":"-370:options_data","from_node_id":"-531:data"},{"to_node_id":"-93:input_data","from_node_id":"-865:data"},{"to_node_id":"-390:input_ds","from_node_id":"-93:data"},{"to_node_id":"-795:input_data","from_node_id":"-93:data"},{"to_node_id":"-801:input_data","from_node_id":"-789:data"},{"to_node_id":"-789:input_data","from_node_id":"-795:data"}],"nodes":[{"node_id":"-51","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2021-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2021-01-06","type":"Literal","bound_global_parameter":"交易日期"},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":"0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-51"}],"output_ports":[{"name":"data","node_id":"-51"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-59","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"\n#昨日MACD.dea-dif在 0到-0.1之间,调整幅度不大\nta_macd_dea(close_1/adjust_factor_1)\nta_macd_dif(close_1/adjust_factor_1)\n#死叉前后两天(昨天,前天)收盘价必须一跌一涨\nclose_2\nclose_1\n\n#昨天ma5死叉ma10\ncon1=ta_ma(close_1, 5)-ta_ma(close_1, 10)<0\n#前天ma5在ma10之上\ncon2=ta_ma(close_2, 5)-ta_ma(close_2, 10)>0\n#ma20上升\ncon3=ta_ma(close_1, 20)-ta_ma(close_2, 20)>0\n#ma30上升\ncon4=ta_ma(close_1, 30)-ta_ma(close_2, 30)>0\n#前天成交量ma5大于ma10\ncon5=ta_ma(volume_2, 5)-ta_ma(volume_2, 10)>0\n#昨天成交量ma5*1.2大于ma10\ncon6=1.2*ta_ma(volume_1, 5)-ta_ma(volume_1, 10)>0\ncon7=ta_macd_dif(close_1)>0#昨天macd.dif大于0\ncon8=ta_macd_dea(close_1)>0#昨天macd.dea大于0\n#昨日MACD.dea-dif在 0到-0.1之间,调整幅度不大\ncon9=ta_macd_dea(close_1)-ta_macd_dif(close_1)<0\ncon10=ta_macd_dea(close_1)-ta_macd_dif(close_1)>-0.1\n#死叉前后两天(昨天,前天)收盘价必须一跌一涨\ncon11=(close_2>close_1)| (close_1>close_2)\ncon12=((open_0)<(close_1))\n#\nmy1=where((con1)&(con2)&(con3)&(con4)&(con5)&(con6),1,0)\n#\nmy2=where((con7)&(con8)&(con9)&(con10),1,0)\n#\nmy3=where((con11),1,0)\n#\nmy4=where((con12),1,0)\n#按策略优质度排序\nyzd1=where(my1==1,8,0)\nyzd2=where(my2==1,6,0)\nyzd3=where(my3==1,4,0)\nyzd4=where(my4==1,2,0)\nyzd=yzd1+yzd2+yzd3+yzd4\nmy=min(my1,my2,my3,my4)\nbuy_condition=where(my==1,1,0)\nsell_condition=where(my<1,1,0)\n# yzd=yzd1+yzd2+yzd3\n# my=my1+my2+my3\n# buy_condition=where(my==3,1,0)\n# sell_condition=where(my<3,1,0)\n","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-59"}],"output_ports":[{"name":"data","node_id":"-59"}],"cacheable":true,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-353","module_id":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"before_start_days","value":"60","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-353"},{"name":"features","node_id":"-353"}],"output_ports":[{"name":"data","node_id":"-353"}],"cacheable":false,"seq_num":5,"comment":"","comment_collapsed":true},{"node_id":"-360","module_id":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","parameters":[{"name":"date_col","value":"date","type":"Literal","bound_global_parameter":null},{"name":"instrument_col","value":"instrument","type":"Literal","bound_global_parameter":null},{"name":"drop_na","value":"False","type":"Literal","bound_global_parameter":null},{"name":"remove_extra_columns","value":"False","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"{}","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-360"},{"name":"features","node_id":"-360"}],"output_ports":[{"name":"data","node_id":"-360"}],"cacheable":true,"seq_num":7,"comment":"","comment_collapsed":true},{"node_id":"-370","module_id":"BigQuantSpace.trade.trade-v4","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"initialize","value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n #context.set_commission(PerOrder(buy_cost=0.00001, sell_cost=0.0001, min_cost=1))\n \n # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只\n context.stock_count = 1\n # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]\n #context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])\n # 每只股票的权重平均分配\n context.stock_weights = 1/context.stock_count\n # 设置每只股票占用的最大资金比例\n context.max_cash_per_instrument = 1\n context.options['hold_days'] = 0\n\n","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n today = data.current_dt.strftime('%Y-%m-%d')\n equities = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0}\n stock_now = len(equities); #获取当前持仓股票数量\n stock_count = context.stock_count\n \n # 按日期过滤得到今日的预测数据\n # 加载预测数据\n df = context.options['data'].read_df()\n df_today = df[df.date == data.current_dt.strftime('%Y-%m-%d')]\n df_today.set_index('instrument')\n \n \n now_stock = []\n sell_stock = []\n \n try:\n buy_list = context.daily_buy_stock[today]\n except:\n buy_list = []\n\n \n # 1. 资金分配\n #is_staging = context.trading_day_index < context.options['hold_days'] # 是否在建仓期间(前 hold_days 天) \n #stock_cash = context.portfolio.portfolio_value/stock_count\n #cash_avg = context.portfolio.portfolio_value\n #cash_for_buy = min(context.portfolio.cash, stock_cash)\n #cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)\n \n positions = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.perf_tracker.position_tracker.positions.items()}\n \n \n #if not is_staging :\n if 1==1 : \n if len(equities) > 0:\n for i in equities.keys():\n last_sale_date = equities[i].last_sale_date\t# 上次交易日期\n delta_days = data.current_dt - last_sale_date \n hold_days = delta_days.days # 持仓天数\n if hold_days >= context.options['hold_days'] and i not in buy_list :\n print('日期:',today,'卖出2:',i)\n context.order_target(context.symbol(i), 0)\n sell_stock.append(i)\n stock_now = stock_now -1\n #print('日期:', today, '股票:', i, ' 卖出')\n \n# 3. 生成买入订单\n buy_num = stock_count - stock_now\n #if is_staging :\n # buy_num = 1\n if len(buy_list)>0:\n print('日期:', today, '选出股票数量:', len(buy_list))\n if buy_num>0 and len(buy_list)>0 :\n # 不再买入已经轮仓卖出和移动止损的股票,以防止出现空头持仓\n buy_instruments = [i for i in buy_list if i not in now_stock][:buy_num]\n print(buy_list)\n cash_for_buy = context.portfolio.cash/len(buy_instruments)\n for i, instrument in enumerate(buy_instruments):\n current_price = data.current(context.symbol(instrument), 'price')\n \n if cash_for_buy>0 and data.can_trade(context.symbol(instrument)): \n amount = math.floor(cash_for_buy / current_price / 100) * 100\n context.order(context.symbol(instrument), amount)\n #if(instrument=='002735.SZA'):\n print('日期:',today,'买入:',instrument)\n else :\n print('日期:',today,'无资金或不能交易未买入:',instrument)","type":"Literal","bound_global_parameter":null},{"name":"prepare","value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n df = context.options['data'].read_df()\n # 函数:求满足开仓条件的股票列表\n def open_pos_con(df):\n return list(df[df['buy_condition']>0].instrument)\n # 函数:求满足平仓条件的股票列表\n def close_pos_con(df):\n return list(df[df['sell_condition']>0].instrument)\n \n # 每日卖出股票的数据框\n context.daily_sell_stock= df.groupby('date').apply(close_pos_con) \n # 每日买入股票的数据框\n context.daily_buy_stock= df.groupby('date').apply(open_pos_con) \n\n\n \n","type":"Literal","bound_global_parameter":null},{"name":"before_trading_start","value":"# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"volume_limit","value":"0","type":"Literal","bound_global_parameter":null},{"name":"order_price_field_buy","value":"open","type":"Literal","bound_global_parameter":null},{"name":"order_price_field_sell","value":"open","type":"Literal","bound_global_parameter":null},{"name":"capital_base","value":"100000","type":"Literal","bound_global_parameter":null},{"name":"auto_cancel_non_tradable_orders","value":"True","type":"Literal","bound_global_parameter":null},{"name":"data_frequency","value":"daily","type":"Literal","bound_global_parameter":null},{"name":"price_type","value":"真实价格","type":"Literal","bound_global_parameter":null},{"name":"product_type","value":"股票","type":"Literal","bound_global_parameter":null},{"name":"plot_charts","value":"True","type":"Literal","bound_global_parameter":null},{"name":"backtest_only","value":"False","type":"Literal","bound_global_parameter":null},{"name":"benchmark","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-370"},{"name":"options_data","node_id":"-370"},{"name":"history_ds","node_id":"-370"},{"name":"benchmark_ds","node_id":"-370"},{"name":"trading_calendar","node_id":"-370"}],"output_ports":[{"name":"raw_perf","node_id":"-370"}],"cacheable":false,"seq_num":8,"comment":"","comment_collapsed":true},{"node_id":"-390","module_id":"BigQuantSpace.sort.sort-v4","parameters":[{"name":"sort_by","value":"yzd","type":"Literal","bound_global_parameter":null},{"name":"group_by","value":"date","type":"Literal","bound_global_parameter":null},{"name":"keep_columns","value":"--","type":"Literal","bound_global_parameter":null},{"name":"ascending","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_ds","node_id":"-390"},{"name":"sort_by_ds","node_id":"-390"}],"output_ports":[{"name":"sorted_data","node_id":"-390"}],"cacheable":true,"seq_num":9,"comment":"","comment_collapsed":true},{"node_id":"-531","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"my==1","type":"Literal","bound_global_parameter":null},{"name":"output_left_data","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-531"}],"output_ports":[{"name":"data","node_id":"-531"},{"name":"left_data","node_id":"-531"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-865","module_id":"BigQuantSpace.chinaa_stock_filter.chinaa_stock_filter-v1","parameters":[{"name":"index_constituent_cond","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22displayValue%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E4%B8%8A%E8%AF%8150%22%2C%22displayValue%22%3A%22%E4%B8%8A%E8%AF%8150%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%B2%AA%E6%B7%B1300%22%2C%22displayValue%22%3A%22%E6%B2%AA%E6%B7%B1300%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%B8%AD%E8%AF%81500%22%2C%22displayValue%22%3A%22%E4%B8%AD%E8%AF%81500%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%B8%AD%E8%AF%81800%22%2C%22displayValue%22%3A%22%E4%B8%AD%E8%AF%81800%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%B8%8A%E8%AF%81180%22%2C%22displayValue%22%3A%22%E4%B8%8A%E8%AF%81180%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%B8%AD%E8%AF%81100%22%2C%22displayValue%22%3A%22%E4%B8%AD%E8%AF%81100%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%B7%B1%E8%AF%81100%22%2C%22displayValue%22%3A%22%E6%B7%B1%E8%AF%81100%22%2C%22selected%22%3Afalse%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"board_cond","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22displayValue%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%B8%8A%E8%AF%81%E4%B8%BB%E6%9D%BF%22%2C%22displayValue%22%3A%22%E4%B8%8A%E8%AF%81%E4%B8%BB%E6%9D%BF%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E6%B7%B1%E8%AF%81%E4%B8%BB%E6%9D%BF%22%2C%22displayValue%22%3A%22%E6%B7%B1%E8%AF%81%E4%B8%BB%E6%9D%BF%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E5%88%9B%E4%B8%9A%E6%9D%BF%22%2C%22displayValue%22%3A%22%E5%88%9B%E4%B8%9A%E6%9D%BF%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E7%A7%91%E5%88%9B%E6%9D%BF%22%2C%22displayValue%22%3A%22%E7%A7%91%E5%88%9B%E6%9D%BF%22%2C%22selected%22%3Afalse%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"industry_cond","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22displayValue%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E4%BA%A4%E9%80%9A%E8%BF%90%E8%BE%93%22%2C%22displayValue%22%3A%22%E4%BA%A4%E9%80%9A%E8%BF%90%E8%BE%93%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%BC%91%E9%97%B2%E6%9C%8D%E5%8A%A1%22%2C%22displayValue%22%3A%22%E4%BC%91%E9%97%B2%E6%9C%8D%E5%8A%A1%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%BC%A0%E5%AA%92%2F%E4%BF%A1%E6%81%AF%E6%9C%8D%E5%8A%A1%22%2C%22displayValue%22%3A%22%E4%BC%A0%E5%AA%92%2F%E4%BF%A1%E6%81%AF%E6%9C%8D%E5%8A%A1%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%85%AC%E7%94%A8%E4%BA%8B%E4%B8%9A%22%2C%22displayValue%22%3A%22%E5%85%AC%E7%94%A8%E4%BA%8B%E4%B8%9A%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%86%9C%E6%9E%97%E7%89%A7%E6%B8%94%22%2C%22displayValue%22%3A%22%E5%86%9C%E6%9E%97%E7%89%A7%E6%B8%94%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%8C%96%E5%B7%A5%22%2C%22displayValue%22%3A%22%E5%8C%96%E5%B7%A5%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%8C%BB%E8%8D%AF%E7%94%9F%E7%89%A9%22%2C%22displayValue%22%3A%22%E5%8C%BB%E8%8D%AF%E7%94%9F%E7%89%A9%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%95%86%E4%B8%9A%E8%B4%B8%E6%98%93%22%2C%22displayValue%22%3A%22%E5%95%86%E4%B8%9A%E8%B4%B8%E6%98%93%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%9B%BD%E9%98%B2%E5%86%9B%E5%B7%A5%22%2C%22displayValue%22%3A%22%E5%9B%BD%E9%98%B2%E5%86%9B%E5%B7%A5%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%AE%B6%E7%94%A8%E7%94%B5%E5%99%A8%22%2C%22displayValue%22%3A%22%E5%AE%B6%E7%94%A8%E7%94%B5%E5%99%A8%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%BB%BA%E7%AD%91%E6%9D%90%E6%96%99%2F%E5%BB%BA%E7%AD%91%E5%BB%BA%E6%9D%90%22%2C%22displayValue%22%3A%22%E5%BB%BA%E7%AD%91%E6%9D%90%E6%96%99%2F%E5%BB%BA%E7%AD%91%E5%BB%BA%E6%9D%90%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%BB%BA%E7%AD%91%E8%A3%85%E9%A5%B0%22%2C%22displayValue%22%3A%22%E5%BB%BA%E7%AD%91%E8%A3%85%E9%A5%B0%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%88%BF%E5%9C%B0%E4%BA%A7%22%2C%22displayValue%22%3A%22%E6%88%BF%E5%9C%B0%E4%BA%A7%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%9C%89%E8%89%B2%E9%87%91%E5%B1%9E%22%2C%22displayValue%22%3A%22%E6%9C%89%E8%89%B2%E9%87%91%E5%B1%9E%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%9C%BA%E6%A2%B0%E8%AE%BE%E5%A4%87%22%2C%22displayValue%22%3A%22%E6%9C%BA%E6%A2%B0%E8%AE%BE%E5%A4%87%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%B1%BD%E8%BD%A6%2F%E4%BA%A4%E8%BF%90%E8%AE%BE%E5%A4%87%22%2C%22displayValue%22%3A%22%E6%B1%BD%E8%BD%A6%2F%E4%BA%A4%E8%BF%90%E8%AE%BE%E5%A4%87%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E7%94%B5%E5%AD%90%22%2C%22displayValue%22%3A%22%E7%94%B5%E5%AD%90%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E7%94%B5%E6%B0%94%E8%AE%BE%E5%A4%87%22%2C%22displayValue%22%3A%22%E7%94%B5%E6%B0%94%E8%AE%BE%E5%A4%87%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E7%BA%BA%E7%BB%87%E6%9C%8D%E8%A3%85%22%2C%22displayValue%22%3A%22%E7%BA%BA%E7%BB%87%E6%9C%8D%E8%A3%85%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E7%BB%BC%E5%90%88%22%2C%22displayValue%22%3A%22%E7%BB%BC%E5%90%88%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E8%AE%A1%E7%AE%97%E6%9C%BA%22%2C%22displayValue%22%3A%22%E8%AE%A1%E7%AE%97%E6%9C%BA%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E8%BD%BB%E5%B7%A5%E5%88%B6%E9%80%A0%22%2C%22displayValue%22%3A%22%E8%BD%BB%E5%B7%A5%E5%88%B6%E9%80%A0%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%80%9A%E4%BF%A1%22%2C%22displayValue%22%3A%22%E9%80%9A%E4%BF%A1%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%87%87%E6%8E%98%22%2C%22displayValue%22%3A%22%E9%87%87%E6%8E%98%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%92%A2%E9%93%81%22%2C%22displayValue%22%3A%22%E9%92%A2%E9%93%81%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%93%B6%E8%A1%8C%22%2C%22displayValue%22%3A%22%E9%93%B6%E8%A1%8C%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%9D%9E%E9%93%B6%E9%87%91%E8%9E%8D%22%2C%22displayValue%22%3A%22%E9%9D%9E%E9%93%B6%E9%87%91%E8%9E%8D%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%A3%9F%E5%93%81%E9%A5%AE%E6%96%99%22%2C%22displayValue%22%3A%22%E9%A3%9F%E5%93%81%E9%A5%AE%E6%96%99%22%2C%22selected%22%3Afalse%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"st_cond","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22displayValue%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%AD%A3%E5%B8%B8%22%2C%22displayValue%22%3A%22%E6%AD%A3%E5%B8%B8%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22ST%22%2C%22displayValue%22%3A%22ST%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22*ST%22%2C%22displayValue%22%3A%22*ST%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%9A%82%E5%81%9C%E4%B8%8A%E5%B8%82%22%2C%22displayValue%22%3A%22%E6%9A%82%E5%81%9C%E4%B8%8A%E5%B8%82%22%2C%22selected%22%3Afalse%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"delist_cond","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22displayValue%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%80%80%E5%B8%82%22%2C%22displayValue%22%3A%22%E9%80%80%E5%B8%82%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%9D%9E%E9%80%80%E5%B8%82%22%2C%22displayValue%22%3A%22%E9%9D%9E%E9%80%80%E5%B8%82%22%2C%22selected%22%3Atrue%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"output_left_data","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-865"}],"output_ports":[{"name":"data","node_id":"-865"},{"name":"left_data","node_id":"-865"}],"cacheable":true,"seq_num":6,"comment":"","comment_collapsed":true},{"node_id":"-93","module_id":"BigQuantSpace.dropnan.dropnan-v2","parameters":[],"input_ports":[{"name":"input_data","node_id":"-93"},{"name":"features","node_id":"-93"}],"output_ports":[{"name":"data","node_id":"-93"}],"cacheable":true,"seq_num":11,"comment":"","comment_collapsed":true},{"node_id":"-789","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"(con7==1) & (con8==1) & (con9==1) & (con10==1)","type":"Literal","bound_global_parameter":null},{"name":"output_left_data","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-789"}],"output_ports":[{"name":"data","node_id":"-789"},{"name":"left_data","node_id":"-789"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-795","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"(con1==1) & (con2==1) & (con3==1) & (con4==1) & (con5==1) & (con6==1 ) &(date=='2021-01-05')","type":"Literal","bound_global_parameter":null},{"name":"output_left_data","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-795"}],"output_ports":[{"name":"data","node_id":"-795"},{"name":"left_data","node_id":"-795"}],"cacheable":true,"seq_num":10,"comment":"","comment_collapsed":true},{"node_id":"-801","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"con11==1","type":"Literal","bound_global_parameter":null},{"name":"output_left_data","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-801"}],"output_ports":[{"name":"data","node_id":"-801"},{"name":"left_data","node_id":"-801"}],"cacheable":true,"seq_num":12,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-51' Position='132,7,200,200'/><node_position Node='-59' Position='478,81,200,200'/><node_position Node='-353' Position='257,236,200,200'/><node_position Node='-360' Position='239,338,200,200'/><node_position Node='-370' Position='478,838,200,200'/><node_position Node='-390' Position='288,618,200,200'/><node_position Node='-531' Position='301,706,200,200'/><node_position Node='-865' Position='261,433,200,200'/><node_position Node='-93' Position='269,519,200,200'/><node_position Node='-789' Position='757.935546875,507.0030517578125,200,200'/><node_position Node='-795' Position='644.5262451171875,405.88720703125,200,200'/><node_position Node='-801' Position='764.9951782226562,612.9430541992188,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [88]:
    # 本代码由可视化策略环境自动生成 2022年3月19日 20:06
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    def m8_initialize_bigquant_run(context):
    
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
        #context.set_commission(PerOrder(buy_cost=0.00001, sell_cost=0.0001, min_cost=1))
        
        # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
        context.stock_count = 1
        # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]
        #context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])
        # 每只股票的权重平均分配
        context.stock_weights = 1/context.stock_count
        # 设置每只股票占用的最大资金比例
        context.max_cash_per_instrument = 1
        context.options['hold_days'] = 0
    
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m8_handle_data_bigquant_run(context, data):
        today = data.current_dt.strftime('%Y-%m-%d')
        equities = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0}
        stock_now = len(equities); #获取当前持仓股票数量
        stock_count = context.stock_count
        
        # 按日期过滤得到今日的预测数据
        # 加载预测数据
        df = context.options['data'].read_df()
        df_today = df[df.date == data.current_dt.strftime('%Y-%m-%d')]
        df_today.set_index('instrument')
        
        
        now_stock = []
        sell_stock = []
           
        try:
            buy_list = context.daily_buy_stock[today]
        except:
            buy_list = []
    
        
        # 1. 资金分配
        #is_staging = context.trading_day_index < context.options['hold_days'] # 是否在建仓期间(前 hold_days 天) 
        #stock_cash = context.portfolio.portfolio_value/stock_count
        #cash_avg = context.portfolio.portfolio_value
        #cash_for_buy = min(context.portfolio.cash,  stock_cash)
        #cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)
        
        positions = {e.symbol: p.amount * p.last_sale_price
                     for e, p in context.perf_tracker.position_tracker.positions.items()}
        
                
        #if not is_staging :
        if 1==1 :    
            if len(equities) > 0:
                for i in equities.keys():
                    last_sale_date = equities[i].last_sale_date	# 上次交易日期
                    delta_days = data.current_dt - last_sale_date  
                    hold_days = delta_days.days # 持仓天数
                    if hold_days >= context.options['hold_days'] and i not in buy_list :
                        print('日期:',today,'卖出2:',i)
                        context.order_target(context.symbol(i), 0)
                        sell_stock.append(i)
                        stock_now = stock_now -1
                        #print('日期:', today, '股票:', i, ' 卖出')
                     
    # 3. 生成买入订单
        buy_num = stock_count - stock_now
        #if is_staging :
        #    buy_num = 1
        if len(buy_list)>0:
            print('日期:', today, '选出股票数量:', len(buy_list))
        if buy_num>0 and len(buy_list)>0 :
            # 不再买入已经轮仓卖出和移动止损的股票,以防止出现空头持仓
            buy_instruments = [i for i in buy_list if i not in now_stock][:buy_num]
            print(buy_list)
            cash_for_buy = context.portfolio.cash/len(buy_instruments)
            for i, instrument in enumerate(buy_instruments):
                current_price = data.current(context.symbol(instrument), 'price')
                
                if cash_for_buy>0 and data.can_trade(context.symbol(instrument)):           
                    amount = math.floor(cash_for_buy / current_price / 100) * 100
                    context.order(context.symbol(instrument), amount)
                    #if(instrument=='002735.SZA'):
                    print('日期:',today,'买入:',instrument)
                else :
                    print('日期:',today,'无资金或不能交易未买入:',instrument)
    # 回测引擎:准备数据,只执行一次
    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)
        
        # 每日卖出股票的数据框
        context.daily_sell_stock= df.groupby('date').apply(close_pos_con)  
        # 每日买入股票的数据框
        context.daily_buy_stock= df.groupby('date').apply(open_pos_con)  
    
    
        
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m8_before_trading_start_bigquant_run(context, data):
        pass
    
    
    m1 = M.instruments.v2(
        start_date='2021-01-01',
        end_date=T.live_run_param('trading_date', '2021-01-06'),
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m2 = M.input_features.v1(
        features="""
    #昨日MACD.dea-dif在 0到-0.1之间,调整幅度不大
    ta_macd_dea(close_1/adjust_factor_1)
    ta_macd_dif(close_1/adjust_factor_1)
    #死叉前后两天(昨天,前天)收盘价必须一跌一涨
    close_2
    close_1
    
    #昨天ma5死叉ma10
    con1=ta_ma(close_1, 5)-ta_ma(close_1, 10)<0
    #前天ma5在ma10之上
    con2=ta_ma(close_2, 5)-ta_ma(close_2, 10)>0
    #ma20上升
    con3=ta_ma(close_1, 20)-ta_ma(close_2, 20)>0
    #ma30上升
    con4=ta_ma(close_1, 30)-ta_ma(close_2, 30)>0
    #前天成交量ma5大于ma10
    con5=ta_ma(volume_2, 5)-ta_ma(volume_2, 10)>0
    #昨天成交量ma5*1.2大于ma10
    con6=1.2*ta_ma(volume_1, 5)-ta_ma(volume_1, 10)>0
    con7=ta_macd_dif(close_1)>0#昨天macd.dif大于0
    con8=ta_macd_dea(close_1)>0#昨天macd.dea大于0
    #昨日MACD.dea-dif在 0到-0.1之间,调整幅度不大
    con9=ta_macd_dea(close_1)-ta_macd_dif(close_1)<0
    con10=ta_macd_dea(close_1)-ta_macd_dif(close_1)>-0.1
    #死叉前后两天(昨天,前天)收盘价必须一跌一涨
    con11=(close_2>close_1)| (close_1>close_2)
    con12=((open_0)<(close_1))
    #
    my1=where((con1)&(con2)&(con3)&(con4)&(con5)&(con6),1,0)
    #
    my2=where((con7)&(con8)&(con9)&(con10),1,0)
    #
    my3=where((con11),1,0)
    #
    my4=where((con12),1,0)
    #按策略优质度排序
    yzd1=where(my1==1,8,0)
    yzd2=where(my2==1,6,0)
    yzd3=where(my3==1,4,0)
    yzd4=where(my4==1,2,0)
    yzd=yzd1+yzd2+yzd3+yzd4
    my=min(my1,my2,my3,my4)
    buy_condition=where(my==1,1,0)
    sell_condition=where(my<1,1,0)
    # yzd=yzd1+yzd2+yzd3
    # my=my1+my2+my3
    # buy_condition=where(my==3,1,0)
    # sell_condition=where(my<3,1,0)
    """
    )
    
    m5 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m2.data,
        start_date='',
        end_date='',
        before_start_days=60,
        m_cached=False
    )
    
    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={}
    )
    
    m6 = M.chinaa_stock_filter.v1(
        input_data=m7.data,
        index_constituent_cond=['全部'],
        board_cond=['上证主板', '深证主板'],
        industry_cond=['全部'],
        st_cond=['正常'],
        delist_cond=['非退市'],
        output_left_data=False
    )
    
    m11 = M.dropnan.v2(
        input_data=m6.data
    )
    
    m9 = M.sort.v4(
        input_ds=m11.data,
        sort_by='yzd',
        group_by='date',
        keep_columns='--',
        ascending=False
    )
    
    m3 = M.filter.v3(
        input_data=m9.sorted_data,
        expr='my==1',
        output_left_data=False
    )
    
    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,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=100000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='真实价格',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
    m10 = M.filter.v3(
        input_data=m11.data,
        expr='(con1==1) & (con2==1) & (con3==1) & (con4==1) & (con5==1) & (con6==1 ) &(date==\'2021-01-05\')',
        output_left_data=False
    )
    
    m4 = M.filter.v3(
        input_data=m10.data,
        expr='(con7==1) & (con8==1) & (con9==1) & (con10==1)',
        output_left_data=False
    )
    
    m12 = M.filter.v3(
        input_data=m4.data,
        expr='con11==1',
        output_left_data=False
    )
    
    日期: 2021-01-05 选出股票数量: 2
    ['002309.SZA', '603321.SHA']
    日期: 2021-01-05 买入: 002309.SZA
    日期: 2021-01-06 卖出2: 002309.SZA
    日期: 2021-01-06 选出股票数量: 1
    ['002284.SZA']
    日期: 2021-01-06 买入: 002284.SZA
    
    • 收益率-3.71%
    • 年化收益率-95.82%
    • 基准收益率3.96%
    • 阿尔法-1.0
    • 贝塔2.52
    • 夏普比率-9.25
    • 胜率0.0
    • 盈亏比0.0
    • 收益波动率34.0%
    • 信息比率-1.37
    • 最大回撤3.71%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-55a9248359fb4cb08205ea4878794988"}/bigcharts-data-end
    In [89]:
    m10.data.read()
    
    Out[89]:
    adjust_factor_1 close_1 close_2 date instrument open_0 volume_1 volume_2 ta_macd_dea(close_1/adjust_factor_1) ta_macd_dif(close_1/adjust_factor_1) con1 con2 con3 con4 con5 con6 con7 con8 con9 con10 con11 con12 my1 my2 my3 my4 yzd1 yzd2 yzd3 yzd4 yzd my buy_condition sell_condition
    180081 6.703219 46.453308 44.777504 2021-01-05 000723.SZA 46.453308 129610502 112488920 0.067509 0.063827 1 1 1 1 1 1 1 1 0 1 1 0 1 0 1 0 8 0 4 0 12 0 0 1
    181120 2.110157 9.917738 9.601214 2021-01-05 002160.SZA 9.938840 105774048 80518408 0.161524 0.163226 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 8 6 4 0 18 0 0 1
    181351 3.176340 21.281479 21.884983 2021-01-05 002238.SZA 21.154425 9723294 3537631 0.172108 0.120111 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 8 0 4 2 14 0 0 1
    181558 3.772222 21.426222 21.577110 2021-01-05 002309.SZA 21.124443 13194807 11151100 0.054321 0.068256 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 8 6 4 2 20 1 1 0
    182140 5.097065 21.764467 21.662527 2021-01-05 002506.SZA 21.815437 211584119 196910904 0.136100 0.108437 1 1 1 1 1 1 1 1 0 1 1 0 1 0 1 0 8 0 4 0 12 0 0 1
    189854 1.022794 8.898308 8.335772 2021-01-05 603321.SHA 8.877851 13219897 4621997 0.104847 0.110263 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 8 6 4 2 20 1 1 0

    其中2021-01-04的000723的ta_macd指标与通达信指标计算结果不一致,最离谱的是dea与dif的值的高低也有差别