日内因子:开盘缺口探索

策略分享
标签: #<Tag:0x00007f48f5669f00>

(sszy) #1

本文探索股价日内模式中蕴藏的一种选股因子:开盘缺口。

股价的日内走势可能蕴藏着一些有用的信息,特别是开盘和收盘的那几分钟,尤其可能潜藏着一些“私有信息”。比如,由于隔夜时段的交易暂停,每个交易日开盘后,市场累积的大量私有信息,将通过交易迅速得到释放,知情交易概率在日内呈现快速下降的态势。

开盘缺口因子就致力于抓住上一日收盘和本日开盘之间信息差距。如果开盘价远高于前一日收盘价(跳空高开),说明说明市场情绪激动,股票可能会大幅上涨(突破缺口)或者也会逐步下跌(缺口填补)。本文主要探索上一日收盘价和本日开盘价的差距(close_1/open)是否会造成一定的收益差距(ret_0)。

首先抽取特征,然后创建下列衍生因子:

  • alpha:open/close_1,alpha越大,说明本日开盘价高出上一日收盘价幅度越大
  • rank(alpha):对于alpha值进行百分比升序排列
  • ret0:shift(close,-2)/shift(open,-1)-1,下一日收益

接着进行股票过滤:

  • 过滤掉退市和ST的股票
  • 过滤掉出现涨跌停的股票

最后进行alpha因子的分组收益分析:
对alpha排序后,将其分为5组,计算每一组股票的平均累计收益率,画出累计收益图以分析不同大小alpha对于收益率的影响。

def m6_run_bigquant_run(input_1, input_2):
    # 示例代码如下。在这里编写您的代码
    df = input_1.read_df()
    factor = input_2.read_pickle()[0]
    df['group'] = pd.cut(df[factor],bins=[0.0,0.02,0.4,0.6,0.98,1.0],labels=[1,2,3,4,5])
    grouped_processed_df = df.groupby(['date','group'])['ret0'].agg(np.mean).fillna(0).reset_index()
    results = grouped_processed_df.groupby('group').apply(lambda df: np.cumprod(1+df.set_index('date')['ret0'])).T
    data_2 = DataSource.write_pickle([factor])
    data_1 = DataSource.write_df(results)

    return Outputs(data_1=data_1,data_2=data_2)

# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
def m6_post_run_bigquant_run(outputs):
    results = outputs.data_1.read()
    new_data = pd.DataFrame()
    for i in range(1,6):
        new_data[i] = results[i]
    factor = outputs.data_2.read_pickle()[0]
    T.plot(new_data, 
           options={
               'legend':{'enabled': True},
               'chart': {'type': 'line'},
               'title': {'text': factor+' :因子分组收益'}})
    return outputs

在2015-2017年时间段进行回测,发现第五组有显著的增加收益效果。

具体代码如下,可以在此基础上进一步探索:

克隆策略

    {"Description":"实验创建于2019/7/31","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-105:instruments","SourceOutputPortId":"-92:data"},{"DestinationInputPortId":"-1189:instruments","SourceOutputPortId":"-92:data"},{"DestinationInputPortId":"-105:features","SourceOutputPortId":"-100:data"},{"DestinationInputPortId":"-1200:data1","SourceOutputPortId":"-105:data"},{"DestinationInputPortId":"-277:input_2","SourceOutputPortId":"-473:data"},{"DestinationInputPortId":"-2559:features","SourceOutputPortId":"-473:data"},{"DestinationInputPortId":"-2290:features","SourceOutputPortId":"-124:data"},{"DestinationInputPortId":"-1169:input_data","SourceOutputPortId":"-2290:data"},{"DestinationInputPortId":"-2559:input_data","SourceOutputPortId":"-70:data"},{"DestinationInputPortId":"-277:input_1","SourceOutputPortId":"-2559:data"},{"DestinationInputPortId":"-1381:input_data","SourceOutputPortId":"-1169:data"},{"DestinationInputPortId":"-1200:data2","SourceOutputPortId":"-1189:data"},{"DestinationInputPortId":"-1189:features","SourceOutputPortId":"-1195:data"},{"DestinationInputPortId":"-2290:input_data","SourceOutputPortId":"-1200:data"},{"DestinationInputPortId":"-70:input_data","SourceOutputPortId":"-1381:data"}],"ModuleNodes":[{"Id":"-92","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2015-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2017-04-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-92"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-92","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-100","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\ninstrument\ndate\nopen\nclose\nvolume\namount\nadjust_factor\nprice_limit_status_0\nclose_1","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-100"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-100","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-105","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1d_CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-105"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-105"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-105","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-473","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nrank(alpha)","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-473"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-473","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":14,"Comment":"","CommentCollapsed":true},{"Id":"-124","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nret0=shift(close,-2)/shift(open,-1)-1\navg_price = amount/volume*adjust_factor\nalpha = open/close_1","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-124"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-124","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":11,"Comment":"衍生特征","CommentCollapsed":false},{"Id":"-2290","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-2290"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-2290"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-2290","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-70","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-70"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-70","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"Comment":"","CommentCollapsed":true},{"Id":"-277","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2):\n # 示例代码如下。在这里编写您的代码\n df = input_1.read_df()\n factor = input_2.read_pickle()[0]\n df['group'] = pd.cut(df[factor],bins=[0.0,0.02,0.4,0.6,0.98,1.0],labels=[1,2,3,4,5])\n grouped_processed_df = df.groupby(['date','group'])['ret0'].agg(np.mean).fillna(0).reset_index()\n results = grouped_processed_df.groupby('group').apply(lambda df: np.cumprod(1+df.set_index('date')['ret0'])).T\n data_2 = DataSource.write_pickle([factor])\n data_1 = DataSource.write_df(results)\n\n return Outputs(data_1=data_1,data_2=data_2)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n results = outputs.data_1.read()\n new_data = pd.DataFrame()\n for i in range(1,6):\n new_data[i] = results[i]\n factor = outputs.data_2.read_pickle()[0]\n T.plot(new_data, \n options={\n 'legend':{'enabled': True},\n 'chart': {'type': 'line'},\n 'title': {'text': factor+' :因子分组收益'}})\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"input_1,input_2","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"data_1,data_2","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-277"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-277"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-277"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-277","OutputType":null},{"Name":"data_2","NodeId":"-277","OutputType":null},{"Name":"data_3","NodeId":"-277","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"Comment":"因子分组和分组收益","CommentCollapsed":false},{"Id":"-2559","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-2559"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-2559"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-2559","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":9,"Comment":"","CommentCollapsed":true},{"Id":"-1169","ModuleId":"BigQuantSpace.chinaa_stock_filter.chinaa_stock_filter-v1","ModuleParameters":[{"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","ValueType":"Literal","LinkedGlobalParameter":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%5D%7D","ValueType":"Literal","LinkedGlobalParameter":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","ValueType":"Literal","LinkedGlobalParameter":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*ST%22%2C%22displayValue%22%3A%22*ST%22%2C%22selected%22%3Afalse%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%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%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","ValueType":"Literal","LinkedGlobalParameter":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","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_left_data","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-1169"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1169","OutputType":null},{"Name":"left_data","NodeId":"-1169","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"Comment":"","CommentCollapsed":true},{"Id":"-1189","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":90,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-1189"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-1189"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1189","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":13,"Comment":"","CommentCollapsed":true},{"Id":"-1195","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nprice_limit_status_0\nclose_1","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-1195"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1195","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"Comment":"","CommentCollapsed":true},{"Id":"-1200","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date,instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how","Value":"inner","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sort","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data1","NodeId":"-1200"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"-1200"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1200","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":16,"Comment":"","CommentCollapsed":true},{"Id":"-1381","ModuleId":"BigQuantSpace.filter.filter-v3","ModuleParameters":[{"Name":"expr","Value":"price_limit_status_0==2","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_left_data","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-1381"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1381","OutputType":null},{"Name":"left_data","NodeId":"-1381","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":10,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='-92' Position='1240,307,200,200'/><NodePosition Node='-100' Position='863,310,200,200'/><NodePosition Node='-105' Position='1037,457,200,200'/><NodePosition Node='-473' Position='1561,962,200,200'/><NodePosition Node='-124' Position='1551,570,200,200'/><NodePosition Node='-2290' Position='1229,683,200,200'/><NodePosition Node='-70' Position='1236,968,200,200'/><NodePosition Node='-277' Position='1432,1171,200,200'/><NodePosition Node='-2559' Position='1367,1067,200,200'/><NodePosition Node='-1169' Position='1230,783,200,200'/><NodePosition Node='-1189' Position='1431,466,200,200'/><NodePosition Node='-1195' Position='1585,306,200,200'/><NodePosition Node='-1200' Position='1220,575,200,200'/><NodePosition Node='-1381' Position='1228,872,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":true}
    In [15]:
    # 本代码由可视化策略环境自动生成 2020年6月15日 14:28
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m6_run_bigquant_run(input_1, input_2):
        # 示例代码如下。在这里编写您的代码
        df = input_1.read_df()
        factor = input_2.read_pickle()[0]
        df['group'] = pd.cut(df[factor],bins=[0.0,0.02,0.4,0.6,0.98,1.0],labels=[1,2,3,4,5])
        grouped_processed_df = df.groupby(['date','group'])['ret0'].agg(np.mean).fillna(0).reset_index()
        results = grouped_processed_df.groupby('group').apply(lambda df: np.cumprod(1+df.set_index('date')['ret0'])).T
        data_2 = DataSource.write_pickle([factor])
        data_1 = DataSource.write_df(results)
    
        return Outputs(data_1=data_1,data_2=data_2)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m6_post_run_bigquant_run(outputs):
        results = outputs.data_1.read()
        new_data = pd.DataFrame()
        for i in range(1,6):
            new_data[i] = results[i]
        factor = outputs.data_2.read_pickle()[0]
        T.plot(new_data, 
               options={
                   'legend':{'enabled': True},
                   'chart': {'type': 'line'},
                   'title': {'text': factor+' :因子分组收益'}})
        return outputs
    
    
    m1 = M.instruments.v2(
        start_date='2015-01-01',
        end_date='2017-04-01',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m2 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    instrument
    date
    open
    close
    volume
    amount
    adjust_factor
    price_limit_status_0
    close_1"""
    )
    
    m3 = M.use_datasource.v1(
        instruments=m1.data,
        features=m2.data,
        datasource_id='bar1d_CN_STOCK_A',
        start_date='',
        end_date=''
    )
    
    m14 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    rank(alpha)"""
    )
    
    m11 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    ret0=shift(close,-2)/shift(open,-1)-1
    avg_price = amount/volume*adjust_factor
    alpha = open/close_1"""
    )
    
    m15 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    price_limit_status_0
    close_1"""
    )
    
    m13 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m15.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m16 = M.join.v3(
        data1=m3.data,
        data2=m13.data,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    m4 = M.derived_feature_extractor.v3(
        input_data=m16.data,
        features=m11.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m8 = M.chinaa_stock_filter.v1(
        input_data=m4.data,
        index_constituent_cond=['全部'],
        board_cond=['全部'],
        industry_cond=['全部'],
        st_cond=['正常'],
        delist_cond=['全部'],
        output_left_data=False
    )
    
    m10 = M.filter.v3(
        input_data=m8.data,
        expr='price_limit_status_0==2',
        output_left_data=False
    )
    
    m5 = M.dropnan.v1(
        input_data=m10.data
    )
    
    m9 = M.derived_feature_extractor.v3(
        input_data=m5.data,
        features=m14.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m6 = M.cached.v3(
        input_1=m9.data,
        input_2=m14.data,
        run=m6_run_bigquant_run,
        post_run=m6_post_run_bigquant_run,
        input_ports='input_1,input_2',
        params='{}',
        output_ports='data_1,data_2'
    )
    

    (momox) #2

    请问老师,怎么把第五组因子,写到策略里面?您这里是对因子分析,我想直接用到交易策略里面


    (cash01) #3

    我的理解是 你可以直接从每天收盘后的数据里按alpha值排序后取第5组的股票,直接作为第二天交易,当然这边每组600多股票太多了,可以过滤后,结合你其他策略,再去按排序筛选1-5个股票。


    (momox) #4

    感谢回答,你说的我看明白了。
    感觉要自己写代码,做排序,然后再筛选,还要自己写交易代码吧,没法用可视化的方法来实现这个策略吗?
    代码不太熟,不知道能不能通过可视化的方法,快速建立这个交易策略?


    (cash01) #5

    除了分组筛选需要自定模块(可以参照上面的代码),别的都可以用标准模块,建议你先看看模板策略


    (momox) #6

    好的,谢谢老师,我尝试一下


    (cash01) #7

    我不是老师哈。。。。只是路过,而且问题也不复杂