import pickle

begin = '2017-01-01'
end = '2021-02-28'

# 读取李加虎因子表现汇总 # user = '******','yyzhao:304,220,176','JiahuLi:191,189','Alex:195,194','cshu:97,'
mine_summary = query_data.get_alphafactors_info(user='******')
print(len(mine_summary))
# 提取因子名
fac_name = [i['factor_name'] for i in mine_summary]
print(len(fac_name))
# 提取因子ic的正负
ic = [i['perf'][list(i['perf'].keys())[0]]['ic-mean'] for i in mine_summary]
print(len(ic))
ic_sign = [
    uc.sign(i['perf'][list(i['perf'].keys())[0]]['ic-mean'])
    for i in mine_summary
]
print(len(ic_sign))
# 提取因子值
factor_value = fetch.fetch_factor(begin,
                                  end,
                                  fields=fac_name,
                                  standard='clean1_alla',
                                  codes=None,
                                  df=False)
# 有些因子数据不足1009个,比较奇怪
factor_value = {
    k: v
    for k, v in factor_value.items() if len(v) == len(
        query_data.get_trade_days('d', from_trade_day=begin, to_trade_day=end))
示例#2
0
# 读取因子数据
factor_value = fetch.fetch_factor(begin,
                                  end,
                                  fields=name_list,
                                  standard='clean1_alla',
                                  codes=None,
                                  df=False)
mine_summary = query_data.get_alphafactors_info(user='******')
# 调整正负
factor_value_adj = {}
for summa in mine_summary:
    if summa['factor_name'] in list(factor_value.keys()):
        if 'IC' in list(summa['perf']['1_d'].keys()):
            factor_value_adj[summa['factor_name']] = factor_value[
                summa['factor_name']] * uc.sign(summa['perf']['1_d']['IC'])
        else:
            factor_value_adj[summa['factor_name']] = factor_value[
                summa['factor_name']] * uc.sign(
                    summa['perf']['1_d']['ic-mean'])

# 建立股票在未来n日的涨跌标签
oc_data = fetch_data.fetch(begin, end, ['stock_adjopen', 'stock_adjclose'])
ud_tag = uc.ts_delay(oc_data['stock_adjclose'], -pred_window) / uc.ts_delay(
    oc_data['stock_adjopen'], -1) - 1  # 以第二日的开盘价买入
ud_tag = ud_tag.mask(ud_tag > 0, 1)
ud_tag = ud_tag.mask(ud_tag < 0, 0)

# 股票因子值的reshape
new_f = {}
for k, v in factor_value_adj.items():
vp_name = pd.read_pickle(name_pat + 'price_volume/cluster_name.pkl')
# factor_name = dict(hfmf_name, **hfvp_name, **mf_name, **vp_name)  # 记得修改
factor_name = hfvp_name  # 记得修改
name_list = []
for na in factor_name.values():
    name_list.extend(na)

# 读取因子数据
factor_value = fetch.fetch_factor(begin, end, fields=name_list, standard='clean1_alla', codes=None, df=False)
mine_summary = query_data.get_alphafactors_info(user='******')
# 调整正负
factor_value_adj = {}
for summa in mine_summary:
    if summa['factor_name'] in list(factor_value.keys()):
        if 'IC' in list(summa['perf']['1_d'].keys()):
            factor_value_adj[summa['factor_name']] = factor_value[summa['factor_name']] * uc.sign(summa['perf']['1_d']['IC'])
        else:
            factor_value_adj[summa['factor_name']] = factor_value[summa['factor_name']] * uc.sign(summa['perf']['1_d']['ic-mean'])


# 建立股票在未来n日的涨跌标签
oc_data = fetch_data.fetch(begin, end, ['stock_adjopen', 'stock_adjclose'])
ud_tag = uc.ts_delay(oc_data['stock_adjclose'], -pred_window) / uc.ts_delay(oc_data['stock_adjopen'], -1) - 1  # 以第二日的开盘价买入
ud_tag = ud_tag.mask(ud_tag > 0, 1)
ud_tag = ud_tag.mask(ud_tag < 0, 0)


# 股票因子值的reshape
new_f = {}
for k, v in factor_value_adj.items():
    new_v = pd.DataFrame(v.stack())
示例#4
0
# 删除因子数据不足的因子
factor_value = {
    k: v
    for k, v in factor_value.items() if len(v) == len(
        query_data.get_trade_days('d', from_trade_day=begin, to_trade_day=end))
}
f = open(data_pat + '/fac_expand/other_fac_20170101-20210228.pkl', 'wb')
pickle.dump(factor_value, f, -1)
f.close()

# 调整因子的正负
factor_value_adj = {}
for fac in factor_value.keys():
    fac_info = query_data.get_alphafactors_info(factor_name=fac)[0]
    if fac_info['user'] in ['wangfeng', 'JiahuLi', 'Alex']:
        factor_value_adj[fac] = factor_value[fac] * uc.sign(
            fac_info['perf'][list(fac_info['perf'].keys())[0]]['ic-mean'])
    if fac_info['user'] == 'yyzhao':
        factor_value_adj[fac] = factor_value[fac] * uc.sign(
            fac_info['perf'][list(fac_info['perf'].keys())[0]]['IC'])
    if fac_info['user'] == 'cshu':  # 舒畅的因子正负调整?
        factor_value_adj[fac] = factor_value[fac] * fac_info['perf_summary'][
            '2018-03-09__2021-03-08']['1_d']['sign']

# 拆解
new_name = list(factor_value_adj.keys())
factor_value_adj1 = {k: factor_value_adj[k] for k in new_name[0:100]}
factor_value_adj2 = {k: factor_value_adj[k] for k in new_name[100:200]}
factor_value_adj3 = {k: factor_value_adj[k] for k in new_name[200:300]}
factor_value_adj4 = {k: factor_value_adj[k] for k in new_name[300:400]}
factor_value_adj5 = {k: factor_value_adj[k] for k in new_name[400:500]}
factor_value_adj6 = {k: factor_value_adj[k] for k in new_name[500:537]}
begin = '2017-01-01'
end = '2021-02-28'

# 读取赵艺阳因子表现汇总 # user = '******','yyzhao:304,220,176','JiahuLi:191,189','Alex:195,194','cshu:97,'
mine_summary = query_data.get_alphafactors_info(user='******')
print(len(mine_summary))
# 提取因子名
fac_name = [i['factor_name'] for i in mine_summary]
print(len(fac_name))
# 提取因子类别
fac_type = {i['factor_name']: i['data_sector'] for i in mine_summary}
print(len(fac_type))
# 提取因子ic的正负
ic_sign = [
    uc.sign(i['perf'][list(i['perf'].keys())[0]]['IC']) for i in mine_summary
]
print(len(ic_sign))

# 提取因子值
factor_value = fetch.fetch_factor(begin,
                                  end,
                                  fields=fac_name,
                                  standard='clean1_alla',
                                  codes=None,
                                  df=False)
# 有些因子数据不足1009个,比较奇怪
factor_value = {
    k: v
    for k, v in factor_value.items() if len(v) == len(
        query_data.get_trade_days('d', from_trade_day=begin, to_trade_day=end))