# -*- coding: utf-8 -*-
"""
Created on Tue Jan 23 09:56:58 2018
@author: Administrator
"""
import numpy as np
import pandas as pd
import alphalens
import matplotlib.pyplot as plt
import os
import datetime
import time
import math
df = pd.read_csv('NewGuzhiYinziInitialData.csv')
df.drop(df.columns[[0]],axis=1,inplace = True)
fg = pd.read_csv('NewGuzhiYinziDirection.csv')
fg.drop(fg.columns[[0]],axis=1,inplace = True)
fg.iloc[:] = 1
need_test_factor_name = df.columns[6:-2]
need_test_factor_direction = fg.iloc[:,0].values
needtesttime = pd.unique(df.loc[:,'tdate'])
all_single_df_fenshu = []
for itest in needtesttime :
#itest = needtesttime[386]
print(itest)
single_df_fenshu = df.loc[df.tdate == itest,['tdate','security_code','stockname','securityWeight','week_return','stock_end_hangye']]
# 把因子值转化为排序值
for jyest in list(range(len(need_test_factor_name))) :
# print(jyest)
# jyest = 0
single_df = df.loc[df.tdate == itest,:]
single_df.insert(0,'judge_trade',single_df['judge_zhangdie'] + single_df['judge_tingpai'])
# single_df['judge_trade'] = single_df[:,'judge_zhangdie'] + single_df[:,'judge_tingpai']
xx = single_df.sort_values(['stock_end_hangye','judge_trade',need_test_factor_name[jyest]],ascending = [1,1,need_test_factor_direction[jyest]])
xx = xx[(xx['stock_end_hangye']>0) & (xx['stock_end_hangye']<12)]
def ListGivingFenshu(ssff,ssgg):
ssfff = ssff[ssgg==0]
aaff = list(range(1,np.sum(ssfff == ssfff)+1,1)) + list(10000*np.ones(np.sum(ssfff != ssfff)+np.sum(ssgg>=1),dtype = 'int'))
return aaff
yy1 = xx.groupby('stock_end_hangye').apply(lambda subf:ListGivingFenshu(subf[need_test_factor_name[jyest]],subf['judge_trade']))
zz1 = []
for i in list(range(1,12)):
zz1.extend(yy1[i])
zz2 = pd.Series(zz1,index = xx.index)
single_df_fenshu[need_test_factor_name[jyest]] = zz2
all_single_df_fenshu.append(single_df_fenshu)
all_end_df_fenshu = pd.concat(all_single_df_fenshu,ignore_index = True)
all_all_zz = []
for jjtest in list(range(len(need_test_factor_name))):
print(jjtest)
all_zz = []
for iitest in needtesttime[:-1]:
# print(iitest)
single_df_fenshu = all_end_df_fenshu.loc[all_end_df_fenshu.tdate == iitest,:]
# 剔除没有数据的股票,并计算所有因子的 IC 值:当前排序分数与下期收益的相关系数
single_factor_fenshu = single_df_fenshu.loc[:,['tdate', 'security_code','week_return','stock_end_hangye',need_test_factor_name[jjtest]]]
single_factor_fenshu = single_factor_fenshu.sort_values(['stock_end_hangye',single_factor_fenshu.columns[-1]])
single_factor_fenshu = single_factor_fenshu[(single_factor_fenshu['stock_end_hangye']>0) & (single_factor_fenshu['stock_end_hangye']<12)]
def ListGivingCorr(ddgg,ddhh):
endddgg = ddgg[ddhh.values<10000]
endddhh = ddhh[ddhh.values<10000]
# return endddgg.corr(endddhh)
#拆分为 10 档,每档内部取 平均收益率 为该档的收益
if len(endddgg) < 10:
return endddgg.corr(endddhh)
else:
fendangjiange = math.floor(len(endddgg)/10)
fendangleave = len(endddgg) - 10 * fendangjiange
fendangend = fendangjiange*np.ones(10) + np.append(np.zeros(10-fendangleave),np.ones(fendangleave))
fendang_shouyi = []
fendang_local = []
for kkii in list(range(len(fendangend))):
fendang_shouyi.append(np.mean(endddgg.iloc[int(sum(fendangend[:kkii])):int(sum(fendangend[:kkii+1]))]))
fendang_local.append(kkii + 1)
fendang_shouyi_series = pd.Series(fendang_shouyi)
fendang_local_series = pd.Series(fendang_local)
return fendang_shouyi_series.corr(fendang_local_series)
zz = single_factor_fenshu.groupby('stock_end_hangye').apply(lambda subf:ListGivingCorr(subf['week_return'],subf[single_factor_fenshu.columns[-1]]))
zz.fillna(0,inplace = True)
if len(zz)>11:
break
all_zz.append(zz.values)
all_all_zz.append(np.column_stack((all_zz)))
aaa_all_all_zz = np.vstack(all_all_zz)
#plt.plot(all_all_zz[0,:])
# 先测试 第 1 个行业,根据IC—IR计算的权重,来合成全新的权重
TestZhidinghangye = 11
aaa_aaa_single_zz = aaa_all_all_zz[list(range(TestZhidinghangye - 1,aaa_all_all_zz.shape[0],11)),:]
SlideSize = 200 #计算IC——IR所需要的窗口期
aaa_all_direction = []
aaa_all_quanzhong = []
for TTDay in list(range(200,aaa_aaa_single_zz.shape[1]+1)): # 首次计算
ss_compute_data = np.mean(aaa_aaa_single_zz[:,(TTDay - SlideSize):TTDay],1)/np.std(aaa_aaa_single_zz[:,(TTDay - SlideSize):TTDay],1)
ss_compute_data_quanzhong = abs(ss_compute_data)/sum(abs(ss_compute_data))
ss_compute_data_drect = np.zeros(len(ss_compute_data))
ss_compute_data_drect[ss_compute_data>0] = 1
aaa_all_direction.append(ss_compute_data_drect)
aaa_all_quanzhong.append(ss_compute_data_quanzhong)
aaa_all_direction = np.vstack((aaa_all_direction))
aaa_all_quanzhong = np.vstack((aaa_all_quanzhong))
aaa_all_recordtime = needtesttime[200:]
AAAA_Record_result = []
for TTiday in list(range(len(aaa_all_recordtime)-1)):
print(aaa_all_recordtime[TTiday])
TTtesttime = aaa_all_recordtime[TTiday]
# TTtestdirect = aaa_all_direction[TTiday,:]
# TTtestquanzhong = aaa_all_quanzhong[TTiday,:]
TTtestdirect = aaa_all_direction[0,:]
TTtestquanzhong = aaa_all_quanzhong[0,:]
# TTtestquanzhong = [1, 0, 0, 0, 0, 0, 0, 0, 0]
single_hangye_df_fenshu = df.loc[(df.tdate == TTtesttime) & (df.stock_end_hangye == TestZhidinghangye),['tdate','security_code','stockname','securityWeight','week_return','stock_end_hangye']]
# 把因子值转化为排序值
for jyest in list(range(len(need_test_factor_name))) :
# print(jyest)
# jyest = 0
single_hangye_df = df.loc[(df.tdate == TTtesttime) & (df.stock_end_hangye == TestZhidinghangye),:]
single_hangye_df.insert(0,'judge_trade',single_hangye_df['judge_zhangdie'] + single_hangye_df['judge_tingpai'])
# single_df['judge_trade'] = single_df[:,'judge_zhangdie'] + single_df[:,'judge_tingpai']
xx = single_hangye_df.sort_values(['stock_end_hangye','judge_trade',need_test_factor_name[jyest]],ascending = [1,1,int(TTtestdirect[jyest])])
xx = xx[(xx['stock_end_hangye']>0) & (xx['stock_end_hangye']<12)]
def ListGivingFenshuHH(ssff,ssgg):
ssfff = ssff[ssgg==0]
sssfff = ssfff[ssfff == ssfff]
if len(sssfff) < 10 :
sssfff_fenshu = list(range(len(sssfff),0,-1))
else:
fendangjiange = math.floor(len(sssfff)/10)
fendangleave = len(sssfff) - 10 * fendangjiange
fendangend = fendangjiange*np.ones(10) + np.append(np.zeros(10-fendangleave),np.ones(fendangleave))
sssfff_fenshu = np.zeros(len(sssfff))
for kkii in list(range(len(fendangend))):
sssfff_fenshu[int(sum(fendangend[:kkii])):int(sum(fendangend[:kkii+1]))] = 10 - kkii
aaff = list(sssfff_fenshu) + list(np.zeros(np.sum(ssfff != ssfff))) + list((-10000)*np.ones(np.sum(ssgg>=1)))
return aaff
yy1 = xx.groupby('stock_end_hangye').apply(lambda s