复制链接
克隆策略

    {"description":"实验创建于2017/8/26","graph":{"edges":[{"to_node_id":"-4862:features","from_node_id":"-4857:data"},{"to_node_id":"-6998:features","from_node_id":"-4857:data"},{"to_node_id":"-4862:instruments","from_node_id":"-4849:data"},{"to_node_id":"-281:instruments","from_node_id":"-4849:data"},{"to_node_id":"-768:instruments","from_node_id":"-4849:data"},{"to_node_id":"-40:instruments","from_node_id":"-4849:data"},{"to_node_id":"-6998:input_data","from_node_id":"-4862:data"},{"to_node_id":"-125:input_data","from_node_id":"-2687:data"},{"to_node_id":"-2687:input_data","from_node_id":"-6998:data"},{"to_node_id":"-118:data2","from_node_id":"-281:data"},{"to_node_id":"-768:options_data","from_node_id":"-110:sorted_data"},{"to_node_id":"-40:options_data","from_node_id":"-110:sorted_data"},{"to_node_id":"-110:input_ds","from_node_id":"-118:data"},{"to_node_id":"-118:data1","from_node_id":"-125:data"}],"nodes":[{"node_id":"-768","module_id":"BigQuantSpace.hftrade.hftrade-v2","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 context.test_data = 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 context.stock_count = 3\n # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]\n context.stock_weights = 1/context.stock_count\n #持仓周期\n context.hold_days = 7\n","type":"Literal","bound_global_parameter":null},{"name":"before_trading_start","value":"# 交易引擎:每个单位时间开盘前调用一次。\ndef bigquant_run(context, data):\n # 盘前处理,订阅行情等\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"handle_tick","value":"# 交易引擎:tick数据处理函数,每个tick执行一次\ndef bigquant_run(context, tick):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n \n #------------------------------------------止赢模块START--------------------------------------------\n date = data.current_dt.strftime('%Y-%m-%d')\n positions = {e.symbol: p.cost_basis for e, p in context.portfolio.positions.items()}\n # 新建当日止赢股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断\n current_stopwin_stock = [] \n if len(positions) > 0:\n for i in positions.keys():\n stock_cost = positions[i] \n stock_market_price = data.current(context.symbol(i), 'price') \n # 赚30%就止赢\n if (stock_market_price - stock_cost ) / stock_cost>= 0.25: \n context.order_target_percent(context.symbol(i),0) \n current_stopwin_stock.append(i)\n print('日期:',date,'股票:',i,'出现止盈状况')\n #-------------------------------------------止赢模块END---------------------------------------------\n if context.trading_day_index % context.hold_days != 0:\n return \n \n today = data.current_dt.strftime('%Y-%m-%d')\n \n # 获取当前持仓\n positions = {e.symbol: p.amount for e, p in context.portfolio.positions.items()}\n \n # 按日期过滤得到今日数据\n today_data = context.test_data[context.test_data.date == today]\n #今日需要买入的股票\n stocks_buy = today_data.instrument.iloc[0:context.stock_count].to_list()\n stocks_buy_name = today_data.name.iloc[0:context.stock_count].to_list() #打印显示用\n #print(today,\"买入股票池:\",stocks_buy,'',stocks_buy_name)\n #卖出\n for instrument in positions.keys():\n if instrument not in stocks_buy:\n context.order_target(context.symbol(instrument), 0)\n #print(today,\"卖出\",instrument)\n #买入\n cash_per_instrument = context.portfolio.portfolio_value * context.stock_weights\n print(cash_per_instrument)\n for instrument in stocks_buy:\n if instrument not in positions.keys():\n context.order_value(context.symbol(instrument), cash_per_instrument)\n #print(today,\"买入\",instrument) \n ","type":"Literal","bound_global_parameter":null},{"name":"handle_trade","value":"# 交易引擎:成交回报处理函数,每个成交发生时执行一次\ndef bigquant_run(context, trade):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"handle_order","value":"# 交易引擎:委托回报处理函数,每个委托变化时执行一次\ndef bigquant_run(context, order):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"after_trading","value":"# 交易引擎:盘后处理函数,每日盘后执行一次\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"capital_base","value":1000000,"type":"Literal","bound_global_parameter":null},{"name":"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":"before_start_days","value":"0","type":"Literal","bound_global_parameter":null},{"name":"volume_limit","value":1,"type":"Literal","bound_global_parameter":null},{"name":"order_price_field_buy","value":"twap_1","type":"Literal","bound_global_parameter":null},{"name":"order_price_field_sell","value":"twap_8","type":"Literal","bound_global_parameter":null},{"name":"benchmark","value":"000300.HIX","type":"Literal","bound_global_parameter":null},{"name":"plot_charts","value":"True","type":"Literal","bound_global_parameter":null},{"name":"disable_cache","value":"False","type":"Literal","bound_global_parameter":null},{"name":"replay_bdb","value":"False","type":"Literal","bound_global_parameter":null},{"name":"show_debug_info","value":"False","type":"Literal","bound_global_parameter":null},{"name":"backtest_only","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-768"},{"name":"options_data","node_id":"-768"},{"name":"history_ds","node_id":"-768"},{"name":"benchmark_ds","node_id":"-768"}],"output_ports":[{"name":"raw_perf","node_id":"-768"}],"cacheable":false,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-40","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 context.test_data = 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 context.stock_count = 3\n # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]\n context.stock_weights = 1/context.stock_count\n #持仓周期\n context.hold_days = 7\n","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n \n #------------------------------------------止赢模块START--------------------------------------------\n date = data.current_dt.strftime('%Y-%m-%d')\n positions = {e.symbol: p.cost_basis for e, p in context.portfolio.positions.items()}\n # 新建当日止赢股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断\n current_stopwin_stock = [] \n if len(positions) > 0:\n for i in positions.keys():\n stock_cost = positions[i] \n stock_market_price = data.current(context.symbol(i), 'price') \n # 赚30%就止赢\n if (stock_market_price - stock_cost ) / stock_cost>= 0.25: \n context.order_target_percent(context.symbol(i),0) \n current_stopwin_stock.append(i)\n print('日期:',date,'股票:',i,'出现止盈状况')\n #-------------------------------------------止赢模块END---------------------------------------------\n if context.trading_day_index % context.hold_days != 0:\n return \n \n today = data.current_dt.strftime('%Y-%m-%d')\n \n # 获取当前持仓\n positions = {e.symbol: p.amount for e, p in context.portfolio.positions.items()}\n \n # 按日期过滤得到今日数据\n today_data = context.test_data[context.test_data.date == today]\n #今日需要买入的股票\n stocks_buy = today_data.instrument.iloc[0:context.stock_count].to_list()\n stocks_buy_name = today_data.name.iloc[0:context.stock_count].to_list() #打印显示用\n #print(today,\"买入股票池:\",stocks_buy,'',stocks_buy_name)\n #卖出\n for instrument in positions.keys():\n if instrument not in stocks_buy:\n context.order_target(context.symbol(instrument), 0)\n #print(today,\"卖出\",instrument)\n #买入\n cash_per_instrument = context.portfolio.portfolio_value * context.stock_weights\n print(cash_per_instrument)\n for instrument in stocks_buy:\n if instrument not in positions.keys():\n context.order_value(context.symbol(instrument), cash_per_instrument)\n #print(today,\"买入\",instrument) \n ","type":"Literal","bound_global_parameter":null},{"name":"prepare","value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\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":"twap_1","type":"Literal","bound_global_parameter":null},{"name":"order_price_field_sell","value":"twap_8","type":"Literal","bound_global_parameter":null},{"name":"capital_base","value":"346937","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":"-40"},{"name":"options_data","node_id":"-40"},{"name":"history_ds","node_id":"-40"},{"name":"benchmark_ds","node_id":"-40"},{"name":"trading_calendar","node_id":"-40"}],"output_ports":[{"name":"raw_perf","node_id":"-40"}],"cacheable":false,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-4857","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"# #号开始的表示注释,注释需单独一行\n\nhs=sum(turn_0,5)\nhs1=shift(hs,5)\nmy=where(hs>hs1,1,0)\nbuy_condition=where(my>0,1,0)\nsell_condition=where(close_0>mean(close_0,60),1,0)\n\n\n\n\n","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-4857"}],"output_ports":[{"name":"data","node_id":"-4857"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-4849","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2022-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2022-09-06","type":"Literal","bound_global_parameter":null},{"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":"-4849"}],"output_ports":[{"name":"data","node_id":"-4849"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-4862","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":"120","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-4862"},{"name":"features","node_id":"-4862"}],"output_ports":[{"name":"data","node_id":"-4862"}],"cacheable":true,"seq_num":5,"comment":"","comment_collapsed":true},{"node_id":"-2687","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%3Atrue%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":"-2687"}],"output_ports":[{"name":"data","node_id":"-2687"},{"name":"left_data","node_id":"-2687"}],"cacheable":true,"seq_num":6,"comment":"","comment_collapsed":true},{"node_id":"-6998","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":"-6998"},{"name":"features","node_id":"-6998"}],"output_ports":[{"name":"data","node_id":"-6998"}],"cacheable":true,"seq_num":7,"comment":"","comment_collapsed":true},{"node_id":"-281","module_id":"BigQuantSpace.use_datasource.use_datasource-v1","parameters":[{"name":"datasource_id","value":"instruments_CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-281"},{"name":"features","node_id":"-281"}],"output_ports":[{"name":"data","node_id":"-281"}],"cacheable":true,"seq_num":8,"comment":"","comment_collapsed":true},{"node_id":"-110","module_id":"BigQuantSpace.sort.sort-v5","parameters":[{"name":"sort_by","value":"hs","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":"-110"},{"name":"sort_by_ds","node_id":"-110"}],"output_ports":[{"name":"sorted_data","node_id":"-110"}],"cacheable":true,"seq_num":9,"comment":"","comment_collapsed":true},{"node_id":"-118","module_id":"BigQuantSpace.join.join-v3","parameters":[{"name":"on","value":"date,instrument","type":"Literal","bound_global_parameter":null},{"name":"how","value":"left","type":"Literal","bound_global_parameter":null},{"name":"sort","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"data1","node_id":"-118"},{"name":"data2","node_id":"-118"}],"output_ports":[{"name":"data","node_id":"-118"}],"cacheable":true,"seq_num":10,"comment":"","comment_collapsed":true},{"node_id":"-125","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"my>0","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":"-125"}],"output_ports":[{"name":"data","node_id":"-125"},{"name":"left_data","node_id":"-125"}],"cacheable":true,"seq_num":11,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-768' Position='766.0714721679688,1184.3074035644531,200,200'/><node_position Node='-40' Position='21.395309448242188,1134.2052917480469,200,200'/><node_position Node='-4857' Position='565,-116,200,200'/><node_position Node='-4849' Position='-24,-118,200,200'/><node_position Node='-4862' Position='401,184,200,200'/><node_position Node='-2687' Position='425,533,200,200'/><node_position Node='-6998' Position='454,403,200,200'/><node_position Node='-281' Position='-47,509,200,200'/><node_position Node='-110' Position='1008,1061,200,200'/><node_position Node='-118' Position='1010,956,200,200'/><node_position Node='-125' Position='1019,784,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [4]:
    # 本代码由可视化策略环境自动生成 2022年9月20日 10:51
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    def m1_initialize_bigquant_run(context):
        # 加载预测数据
        context.test_data = 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只
        context.stock_count = 3
        # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]
        context.stock_weights = 1/context.stock_count
        #持仓周期
        context.hold_days = 7
    
    # 交易引擎:每个单位时间开盘前调用一次。
    def m1_before_trading_start_bigquant_run(context, data):
        # 盘前处理,订阅行情等
        pass
    
    # 交易引擎:tick数据处理函数,每个tick执行一次
    def m1_handle_tick_bigquant_run(context, tick):
        pass
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m1_handle_data_bigquant_run(context, data):
        
         #------------------------------------------止赢模块START--------------------------------------------
        date = data.current_dt.strftime('%Y-%m-%d')
        positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
        # 新建当日止赢股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
        current_stopwin_stock = [] 
        if len(positions) > 0:
            for i in positions.keys():
                stock_cost = positions[i] 
                stock_market_price = data.current(context.symbol(i), 'price') 
                # 赚30%就止赢
                if (stock_market_price - stock_cost ) / stock_cost>= 0.25:   
                    context.order_target_percent(context.symbol(i),0)     
                    current_stopwin_stock.append(i)
                    print('日期:',date,'股票:',i,'出现止盈状况')
        #-------------------------------------------止赢模块END---------------------------------------------
        if context.trading_day_index % context.hold_days != 0:
            return 
        
        today = data.current_dt.strftime('%Y-%m-%d')
        
        # 获取当前持仓
        positions = {e.symbol: p.amount for e, p in context.portfolio.positions.items()}
            
        # 按日期过滤得到今日数据
        today_data = context.test_data[context.test_data.date == today]
        #今日需要买入的股票
        stocks_buy = today_data.instrument.iloc[0:context.stock_count].to_list()
        stocks_buy_name = today_data.name.iloc[0:context.stock_count].to_list() #打印显示用
        #print(today,"买入股票池:",stocks_buy,'',stocks_buy_name)
        #卖出
        for instrument in positions.keys():
            if instrument not in stocks_buy:
                context.order_target(context.symbol(instrument), 0)
                #print(today,"卖出",instrument)
        #买入
        cash_per_instrument = context.portfolio.portfolio_value * context.stock_weights
        print(cash_per_instrument)
        for instrument in stocks_buy:
            if instrument not in positions.keys():
                context.order_value(context.symbol(instrument), cash_per_instrument)
                #print(today,"买入",instrument)          
        
    # 交易引擎:成交回报处理函数,每个成交发生时执行一次
    def m1_handle_trade_bigquant_run(context, trade):
        pass
    
    # 交易引擎:委托回报处理函数,每个委托变化时执行一次
    def m1_handle_order_bigquant_run(context, order):
        pass
    
    # 交易引擎:盘后处理函数,每日盘后执行一次
    def m1_after_trading_bigquant_run(context, data):
        pass
    
    # 回测引擎:初始化函数,只执行一次
    def m2_initialize_bigquant_run(context):
        # 加载预测数据
        context.test_data = 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只
        context.stock_count = 3
        # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]
        context.stock_weights = 1/context.stock_count
        #持仓周期
        context.hold_days = 7
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m2_handle_data_bigquant_run(context, data):
        
         #------------------------------------------止赢模块START--------------------------------------------
        date = data.current_dt.strftime('%Y-%m-%d')
        positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
        # 新建当日止赢股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
        current_stopwin_stock = [] 
        if len(positions) > 0:
            for i in positions.keys():
                stock_cost = positions[i] 
                stock_market_price = data.current(context.symbol(i), 'price') 
                # 赚30%就止赢
                if (stock_market_price - stock_cost ) / stock_cost>= 0.25:   
                    context.order_target_percent(context.symbol(i),0)     
                    current_stopwin_stock.append(i)
                    print('日期:',date,'股票:',i,'出现止盈状况')
        #-------------------------------------------止赢模块END---------------------------------------------
        if context.trading_day_index % context.hold_days != 0:
            return 
        
        today = data.current_dt.strftime('%Y-%m-%d')
        
        # 获取当前持仓
        positions = {e.symbol: p.amount for e, p in context.portfolio.positions.items()}
            
        # 按日期过滤得到今日数据
        today_data = context.test_data[context.test_data.date == today]
        #今日需要买入的股票
        stocks_buy = today_data.instrument.iloc[0:context.stock_count].to_list()
        stocks_buy_name = today_data.name.iloc[0:context.stock_count].to_list() #打印显示用
        #print(today,"买入股票池:",stocks_buy,'',stocks_buy_name)
        #卖出
        for instrument in positions.keys():
            if instrument not in stocks_buy:
                context.order_target(context.symbol(instrument), 0)
                #print(today,"卖出",instrument)
        #买入
        cash_per_instrument = context.portfolio.portfolio_value * context.stock_weights
        print(cash_per_instrument)
        for instrument in stocks_buy:
            if instrument not in positions.keys():
                context.order_value(context.symbol(instrument), cash_per_instrument)
                #print(today,"买入",instrument)          
        
    # 回测引擎:准备数据,只执行一次
    def m2_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m2_before_trading_start_bigquant_run(context, data):
        pass
    
    
    m3 = M.input_features.v1(
        features="""# #号开始的表示注释,注释需单独一行
    
    hs=sum(turn_0,5)
    hs1=shift(hs,5)
    my=where(hs>hs1,1,0)
    buy_condition=where(my>0,1,0)
    sell_condition=where(close_0>mean(close_0,60),1,0)
    
    
    
    
    """
    )
    
    m4 = M.instruments.v2(
        start_date='2022-01-01',
        end_date='2022-09-06',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m5 = M.general_feature_extractor.v7(
        instruments=m4.data,
        features=m3.data,
        start_date='',
        end_date='',
        before_start_days=120
    )
    
    m7 = M.derived_feature_extractor.v3(
        input_data=m5.data,
        features=m3.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.filter.v3(
        input_data=m6.data,
        expr='my>0',
        output_left_data=False
    )
    
    m8 = M.use_datasource.v1(
        instruments=m4.data,
        datasource_id='instruments_CN_STOCK_A',
        start_date='',
        end_date=''
    )
    
    m10 = M.join.v3(
        data1=m11.data,
        data2=m8.data,
        on='date,instrument',
        how='left',
        sort=False
    )
    
    m9 = M.sort.v5(
        input_ds=m10.data,
        sort_by='hs',
        group_by='date',
        keep_columns='--',
        ascending=False
    )
    
    m1 = M.hftrade.v2(
        instruments=m4.data,
        options_data=m9.sorted_data,
        start_date='',
        end_date='',
        initialize=m1_initialize_bigquant_run,
        before_trading_start=m1_before_trading_start_bigquant_run,
        handle_tick=m1_handle_tick_bigquant_run,
        handle_data=m1_handle_data_bigquant_run,
        handle_trade=m1_handle_trade_bigquant_run,
        handle_order=m1_handle_order_bigquant_run,
        after_trading=m1_after_trading_bigquant_run,
        capital_base=1000000,
        frequency='daily',
        price_type='真实价格',
        product_type='股票',
        before_start_days='0',
        volume_limit=1,
        order_price_field_buy='twap_1',
        order_price_field_sell='twap_8',
        benchmark='000300.HIX',
        plot_charts=True,
        disable_cache=False,
        replay_bdb=False,
        show_debug_info=False,
        backtest_only=False
    )
    
    m2 = M.trade.v4(
        instruments=m4.data,
        options_data=m9.sorted_data,
        start_date='',
        end_date='',
        initialize=m2_initialize_bigquant_run,
        handle_data=m2_handle_data_bigquant_run,
        prepare=m2_prepare_bigquant_run,
        before_trading_start=m2_before_trading_start_bigquant_run,
        volume_limit=0,
        order_price_field_buy='twap_1',
        order_price_field_sell='twap_8',
        capital_base=346937,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='真实价格',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
    333333.3333333333
    2022-09-20 10:51:03.550750 strategy strategy exception:Traceback (most recent call last):
      File "bigtrader/strategy/engine.py", line 713, in bigtrader2.bigtrader.strategy.engine.StrategyEngine._call_strategy_func
      File "bigtrader/strategy/strategy_base.py", line 2244, in bigtrader2.bigtrader.strategy.strategy_base.StrategyBase.call_handle_data
      File "<ipython-input-4-5715c2018ab3>", line 34, in m1_handle_data_bigquant_run
        positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
      File "<ipython-input-4-5715c2018ab3>", line 34, in <dictcomp>
        positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
    AttributeError: 'str' object has no attribute 'symbol'
     
    
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-4-5715c2018ab3> in <module>
        233 )
        234 
    --> 235 m1 = M.hftrade.v2(
        236     instruments=m4.data,
        237     options_data=m9.sorted_data,
    
    <ipython-input-4-5715c2018ab3> in m1_handle_data_bigquant_run(context, data)
         32      #------------------------------------------止赢模块START--------------------------------------------
         33     date = data.current_dt.strftime('%Y-%m-%d')
    ---> 34     positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
         35     # 新建当日止赢股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
         36     current_stopwin_stock = []
    
    <ipython-input-4-5715c2018ab3> in <dictcomp>(.0)
         32      #------------------------------------------止赢模块START--------------------------------------------
         33     date = data.current_dt.strftime('%Y-%m-%d')
    ---> 34     positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
         35     # 新建当日止赢股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
         36     current_stopwin_stock = []
    
    AttributeError: 'str' object has no attribute 'symbol'