复制链接
克隆策略

    {"description":"实验创建于2022/5/23","graph":{"edges":[{"to_node_id":"-681:instruments","from_node_id":"-672:data"},{"to_node_id":"-1409:input_1","from_node_id":"-672:data"},{"to_node_id":"-32:instruments","from_node_id":"-672:data"},{"to_node_id":"-703:data2","from_node_id":"-681:data"},{"to_node_id":"-681:features","from_node_id":"-687:data"},{"to_node_id":"-715:input_data","from_node_id":"-703:data"},{"to_node_id":"-2421:input_data","from_node_id":"-715:data"},{"to_node_id":"-703:data1","from_node_id":"-1409:data_1"},{"to_node_id":"-1409:input_2","from_node_id":"-1417:data"},{"to_node_id":"-1947:sort_by_ds","from_node_id":"-1417:data"},{"to_node_id":"-1947:input_ds","from_node_id":"-1422:data"},{"to_node_id":"-32:options_data","from_node_id":"-1947:sorted_data"},{"to_node_id":"-1422:input_data","from_node_id":"-2421:data"}],"nodes":[{"node_id":"-672","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2010-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2022-05-30","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":"-672"}],"output_ports":[{"name":"data","node_id":"-672"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-681","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":90,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-681"},{"name":"features","node_id":"-681"}],"output_ports":[{"name":"data","node_id":"-681"}],"cacheable":true,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-687","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nclose_0\n","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-687"}],"output_ports":[{"name":"data","node_id":"-687"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-703","module_id":"BigQuantSpace.join.join-v3","parameters":[{"name":"on","value":"date,instrument","type":"Literal","bound_global_parameter":null},{"name":"how","value":"right","type":"Literal","bound_global_parameter":null},{"name":"sort","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"data1","node_id":"-703"},{"name":"data2","node_id":"-703"}],"output_ports":[{"name":"data","node_id":"-703"}],"cacheable":true,"seq_num":6,"comment":"","comment_collapsed":true},{"node_id":"-715","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%3Atrue%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%3Afalse%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%3Afalse%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%2C%7B%22value%22%3A%22%E5%8C%97%E4%BA%A4%E6%89%80%22%2C%22displayValue%22%3A%22%E5%8C%97%E4%BA%A4%E6%89%80%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%3Afalse%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Atrue%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%3Afalse%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":"-715"}],"output_ports":[{"name":"data","node_id":"-715"},{"name":"left_data","node_id":"-715"}],"cacheable":true,"seq_num":8,"comment":"","comment_collapsed":true},{"node_id":"-1409","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n # 输入代码列表\n instruments = input_1.read()['instruments']\n # 输入开始日期\n start_date = input_1.read()['start_date']\n # 向前取90天\n start_date = pd.to_datetime(start_date) + datetime.timedelta(days=-90)\n start_date = start_date.strftime('%Y-%m-%d')\n # 输入结束日期\n end_date = input_1.read()['end_date']\n # 输入特征列表\n features = input_2.read()\n \n # 提取专利数据\n patents = DataSource('patent_CN_STOCK_A').read(instruments=instruments, start_date=start_date, end_date=end_date)\n patents = patents[features + ['date','instrument']]\n # 提取交易日历\n trade_days = DataSource(\"trading_days\").read(start_date=start_date, end_date=end_date)\n trade_days = trade_days[trade_days.country_code=='CN']\n \n # 将月频数据转换为日频数据\n def cpt_daily(x):\n df = pd.merge(x, trade_days, on='date', how='outer')\n df = df.sort_values('date').fillna(method='ffill')\n return df\n df = patents.groupby('instrument').apply(cpt_daily).reset_index(drop=True)\n \n data_1 = DataSource.write_df(df)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-1409"},{"name":"input_2","node_id":"-1409"},{"name":"input_3","node_id":"-1409"}],"output_ports":[{"name":"data_1","node_id":"-1409"},{"name":"data_2","node_id":"-1409"},{"name":"data_3","node_id":"-1409"}],"cacheable":true,"seq_num":7,"comment":"1.提取专利数据\n2.转换为日频数据","comment_collapsed":false},{"node_id":"-1417","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nTG1_V011 ","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-1417"}],"output_ports":[{"name":"data","node_id":"-1417"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-1422","module_id":"BigQuantSpace.dropnan.dropnan-v2","parameters":[],"input_ports":[{"name":"input_data","node_id":"-1422"},{"name":"features","node_id":"-1422"}],"output_ports":[{"name":"data","node_id":"-1422"}],"cacheable":true,"seq_num":5,"comment":"","comment_collapsed":true},{"node_id":"-32","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 # 加载股票指标数据,数据继承自m6模块\n context.indicator_data = context.options['data'].read_df()\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0001, sell_cost=0.001, min_cost=5))\n # 设置股票数量\n context.stock_num = 30\n # 最大持仓占比\n context.max_positions = 0.9\n # 设置一个 symbols 用于存放历史周期上的股票池\n context.symbols = pd.Series()\n context.extension['index'] = 0\n \n # 月末尾盘执行选股,第二天买入\n schedule_function(func=month_handle_data,\n date_rule=date_rules.month_end(),\n time_rule=time_rules.market_close())\n \n \n","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"#每月初调用一次主处理函数,在本函数进行目标股票获取并进行买卖\ndef month_handle_data(context, data):\n from datetime import timedelta\n factor = m4.data.read()[0]\n \n # 获取当前日期\n date = data.current_dt.strftime(\"%Y-%m-%d\")\n # 提取当日的数据\n cur_data = context.indicator_data[context.indicator_data.date==date]\n # 股票池\n symbols =list(cur_data.instrument)[:context.stock_num]\n # 持仓股\n holdings = [equity.symbol for equity in context.portfolio.positions]\n # 待卖出的股票:当前持仓不在股票池即为卖出\n stocks_to_sell = [x for x in holdings if x not in symbols]\n \n # 计算股票池大小和选股重复率\n len_stks = len(cur_data[cur_data[factor] > 0])\n context.extension['index'] += 1\n context.symbols[str(context.extension['index'])] = symbols\n if context.extension['index'] > 1 and len(symbols) > 0: \n # 提取上一日的股票池\n last_symbols = context.symbols[str(context.extension['index']-1)]\n # 计算两期股票池的交集\n dup_len = len(list(set(last_symbols).intersection(set(symbols))))\n dup_ratio = dup_len/len(symbols)\n # print(date,\"买入股票和持仓股的重复率:\",round(dup_ratio*100,2),\",股票池大小:\",len_stks)\n \n # 发单:卖出\n for stock in stocks_to_sell:\n code = context.symbol(stock)\n # 如果股票停牌,则无法成交\n if data.can_trade(code):\n context.order_target_percent(code, 0)\n \n # 如果当天没有买入的股票,则直接返回:\n if len(symbols) == 0:\n return\n \n # 确定权重\n weight = context.max_positions / len(symbols)\n \n # 发单:买入\n for stock in symbols:\n code = context.symbol(stock)\n if data.can_trade(code):\n context.order_target_percent(code,weight)\n \n \ndef bigquant_run(context, data):\n pass","type":"Literal","bound_global_parameter":null},{"name":"prepare","value":"\n\n# 回测引擎:准备数据,只执行一次\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.025,"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":1000000,"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":"-32"},{"name":"options_data","node_id":"-32"},{"name":"history_ds","node_id":"-32"},{"name":"benchmark_ds","node_id":"-32"},{"name":"trading_calendar","node_id":"-32"}],"output_ports":[{"name":"raw_perf","node_id":"-32"}],"cacheable":false,"seq_num":9,"comment":"回测","comment_collapsed":true},{"node_id":"-1947","module_id":"BigQuantSpace.sort.sort-v5","parameters":[{"name":"sort_by","value":"--","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":"-1947"},{"name":"sort_by_ds","node_id":"-1947"}],"output_ports":[{"name":"sorted_data","node_id":"-1947"}],"cacheable":true,"seq_num":10,"comment":"","comment_collapsed":true},{"node_id":"-2421","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"TG1_V011 > 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":"-2421"}],"output_ports":[{"name":"data","node_id":"-2421"},{"name":"left_data","node_id":"-2421"}],"cacheable":true,"seq_num":11,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-672' Position='155,23,200,200'/><node_position Node='-681' Position='360,241,200,200'/><node_position Node='-687' Position='413,105,200,200'/><node_position Node='-703' Position='138,383,200,200'/><node_position Node='-715' Position='137,480,200,200'/><node_position Node='-1409' Position='-35,216,200,200'/><node_position Node='-1417' Position='-142,90,200,200'/><node_position Node='-1422' Position='136,671,200,200'/><node_position Node='-32' Position='32,857,200,200'/><node_position Node='-1947' Position='132,759,200,200'/><node_position Node='-2421' Position='137,577,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [2]:
    # 本代码由可视化策略环境自动生成 2022年6月2日 15:06
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m7_run_bigquant_run(input_1, input_2, input_3):
        # 输入代码列表
        instruments = input_1.read()['instruments']
        # 输入开始日期
        start_date = input_1.read()['start_date']
        # 向前取90天
        start_date = pd.to_datetime(start_date) + datetime.timedelta(days=-90)
        start_date = start_date.strftime('%Y-%m-%d')
        # 输入结束日期
        end_date = input_1.read()['end_date']
        # 输入特征列表
        features = input_2.read()
        
        # 提取专利数据
        patents = DataSource('patent_CN_STOCK_A').read(instruments=instruments, start_date=start_date, end_date=end_date)
        patents = patents[features + ['date','instrument']]
        # 提取交易日历
        trade_days = DataSource("trading_days").read(start_date=start_date, end_date=end_date)
        trade_days = trade_days[trade_days.country_code=='CN']
        
        # 将月频数据转换为日频数据
        def cpt_daily(x):
            df = pd.merge(x, trade_days, on='date', how='outer')
            df = df.sort_values('date').fillna(method='ffill')
            return df
        df = patents.groupby('instrument').apply(cpt_daily).reset_index(drop=True)
        
        data_1 = DataSource.write_df(df)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m7_post_run_bigquant_run(outputs):
        return outputs
    
    # 回测引擎:初始化函数,只执行一次
    def m9_initialize_bigquant_run(context):   
        # 加载股票指标数据,数据继承自m6模块
        context.indicator_data = context.options['data'].read_df()
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0001, sell_cost=0.001, min_cost=5))
        # 设置股票数量
        context.stock_num = 30
        # 最大持仓占比
        context.max_positions = 0.9
        # 设置一个 symbols 用于存放历史周期上的股票池
        context.symbols = pd.Series()
        context.extension['index'] = 0
        
        # 月末尾盘执行选股,第二天买入
        schedule_function(func=month_handle_data,
            date_rule=date_rules.month_end(),
            time_rule=time_rules.market_close())
        
        
    
    #每月初调用一次主处理函数,在本函数进行目标股票获取并进行买卖
    def month_handle_data(context, data):
        from datetime import timedelta
        factor = m4.data.read()[0]
        
        # 获取当前日期
        date = data.current_dt.strftime("%Y-%m-%d")
        # 提取当日的数据
        cur_data = context.indicator_data[context.indicator_data.date==date]
        # 股票池
        symbols =list(cur_data.instrument)[:context.stock_num]
        # 持仓股
        holdings = [equity.symbol for equity in context.portfolio.positions]
        # 待卖出的股票:当前持仓不在股票池即为卖出
        stocks_to_sell = [x for x in holdings if x not in symbols]
        
        # 计算股票池大小和选股重复率
        len_stks = len(cur_data[cur_data[factor] > 0])
        context.extension['index'] += 1
        context.symbols[str(context.extension['index'])] = symbols
        if context.extension['index'] > 1 and len(symbols) > 0:  
            # 提取上一日的股票池
            last_symbols = context.symbols[str(context.extension['index']-1)]
            # 计算两期股票池的交集
            dup_len = len(list(set(last_symbols).intersection(set(symbols))))
            dup_ratio = dup_len/len(symbols)
            # print(date,"买入股票和持仓股的重复率:",round(dup_ratio*100,2),",股票池大小:",len_stks)
        
        # 发单:卖出
        for stock in stocks_to_sell:
            code = context.symbol(stock)
            # 如果股票停牌,则无法成交
            if data.can_trade(code):
                context.order_target_percent(code, 0)
        
        # 如果当天没有买入的股票,则直接返回:
        if len(symbols) == 0:
            return
        
        # 确定权重
        weight = context.max_positions / len(symbols)
        
        # 发单:买入
        for stock in symbols:
            code = context.symbol(stock)
            if data.can_trade(code):
                context.order_target_percent(code,weight)
            
            
    def m9_handle_data_bigquant_run(context, data):
        pass
    
    
    # 回测引擎:准备数据,只执行一次
    def m9_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m9_before_trading_start_bigquant_run(context, data):
        pass
    
    
    m1 = M.instruments.v2(
        start_date='2010-01-01',
        end_date='2022-05-30',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m3 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    close_0
    """
    )
    
    m2 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m3.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m4 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    TG1_V011 """
    )
    
    m7 = M.cached.v3(
        input_1=m1.data,
        input_2=m4.data,
        run=m7_run_bigquant_run,
        post_run=m7_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m6 = M.join.v3(
        data1=m7.data_1,
        data2=m2.data,
        on='date,instrument',
        how='right',
        sort=False
    )
    
    m8 = M.chinaa_stock_filter.v1(
        input_data=m6.data,
        index_constituent_cond=['全部'],
        board_cond=['全部'],
        industry_cond=['公用事业', '农林牧渔', '化工', '医药生物', '国防军工', '家用电器', '建筑材料/建筑建材', '建筑装饰', '有色金属', '机械设备', '汽车/交运设备', '电子', '电气设备', '纺织服装', '计算机', '轻工制造', '通信', '采掘', '钢铁', '食品饮料'],
        st_cond=['正常'],
        delist_cond=['全部'],
        output_left_data=False
    )
    
    m11 = M.filter.v3(
        input_data=m8.data,
        expr='TG1_V011  > 0',
        output_left_data=False
    )
    
    m5 = M.dropnan.v2(
        input_data=m11.data
    )
    
    m10 = M.sort.v5(
        input_ds=m5.data,
        sort_by_ds=m4.data,
        sort_by='--',
        group_by='date',
        keep_columns='--',
        ascending=False
    )
    
    m9 = M.trade.v4(
        instruments=m1.data,
        options_data=m10.sorted_data,
        start_date='',
        end_date='',
        initialize=m9_initialize_bigquant_run,
        handle_data=m9_handle_data_bigquant_run,
        prepare=m9_prepare_bigquant_run,
        before_trading_start=m9_before_trading_start_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=1000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
    • 收益率308.64%
    • 年化收益率12.5%
    • 基准收益率12.68%
    • 阿尔法0.12
    • 贝塔0.88
    • 夏普比率0.48
    • 胜率0.64
    • 盈亏比1.32
    • 收益波动率24.5%
    • 信息比率0.05
    • 最大回撤46.11%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-6dbdab41ca7343e6af95df73e0f61a28"}/bigcharts-data-end