# Demo entry 6627433

py

Submitted by Zhao on Jun 29, 2017 at 03:37
Language: Python 3. Code size: 3.6 kB.

```''' 投资学实验报告选股代码
时间：2017-06-28
姓名：赵丹                                  '''
#--------------------选取沪深300股----------------------------
import pandas as pd
stocks = get_index_stocks('000300.XSHG')
date=[]
for stock in stocks:
info=get_security_info(stock)
date.append(info.start_date)
pool=pd.DataFrame({"start_date":date})
#%% -----------------剔除上市不满1个月的股票-----------------------------
dateStr='2017-05-20'
date0=datetime.datetime.strptime(dateStr, "%Y-%m-%d") #后面代表字符串格式
x=[]
for i in range(0,len(pool)):
x.append(datetime.datetime.strptime(str(pool.start_date[i]), "%Y-%m-%d"))
pool=pd.DataFrame({"start_date":x})
pool.index=stocks
gap=pool.start_date-date0
stock_pool=gap[gap<0]              #获取上市时间大于1个月

#%% --------------------定义指标函数-----------------------------
#定义mf指标计算函数
import datetime
def ForMf(df):
df_delay1 = df['close'].shift(1)
df['MF']=df['volume']*df['close']*abs(df['close']-df_delay1)/(df['close']-df_delay1)
return(df)
#定义IC指标计算函数
def ForIc(df):
df['IC']=df['MF']/df['money']
return(df)
#定义MFP指标计算函数
def ForMfp(df):
df['MFP']=df['MF']/df['circulating_market_cap']
return df

#%%--------------------运行：计算每只股票的指标------------------------
mf=[]
ic=[]
mfp=[]
for code in stock_pool.index:
df=get_price(code,start_date='2017-01-01',end_date='2017-06-20'                 ,frequency='1d',fields=['close','volume','money'])
if (df['close'].isnull().sum())/len(df['close'])<0.3: #剔除停牌太久的股票
q = query(valuation.circulating_market_cap).filter(valuation.code == code)
vcmc=pd.DataFrame({"circulating_market_cap":[]})
for i in range(0,len(df)):                #获取该股票的流通市值
factor=get_fundamentals(q, str(df.index[i]))
if factor.index.dtype=='object':        #若流通市值为空值，用nan代替
factor=pd.DataFrame({"circulating_market_cap":[nan]})
vcmc=pd.concat([vcmc,factor])
vcmc.index=df.index
df=pd.concat([df,vcmc],axis=2) #df为代号，收盘，开盘，交易额，流通市值
#计算指标
df1=ForMf(df)  #计算mf指标
df2=ForIc(df1)  #计算IC指标
df3=ForMfp(df2) #计算mfp指标
#print(df3)  #打印某只股票的各指标值
mf.append(df3['MF'].sum())
ic.append(df3['IC'].sum())
mfp.append(df3['MFP'].sum())
result=pd.concat([pd.DataFrame(stock_pool.index),pd.DataFrame(mf),                  pd.DataFrame(ic),pd.DataFrame(mfp)],axis=1)
result.columns = ['code','mf', 'ic','mfp']

#----------------------剔除指标确实以及信息含量小于10%的股票--------------------
df4=result.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)#剔除指标缺失的股票
df4=df4[df4.ic>=0.1]              #剔除信息含量小于10%的股票
df4.index=df4.code

#-----------------------指标打分--------------------
import numpy as np
df4['mf_sort']=df4['mf'].rank(ascending=0)
df4['ic_sort']=df4['ic'].rank(ascending=0)
df4['mfp_sort']=df4['mfp'].rank(ascending=0)
for i in range(4,7):
Rank=df4.iloc[:,i]/len(df4)
r=Rank*100
score=np.ceil(r)
df4=pd.concat([df4,score],axis=2)
df4.columns = ['code','mf', 'ic','mfp','r1', 'r2','r3','mf_s', 'ic_s','mfp_s']
#求和排序
df5=df4.iloc[:,7:10]
df4['Col_sum'] = df5.apply(lambda x: x.sum(),axis=1)
df6=df4['Col_sum'].order()
print('相关指标')
print(df4)
print('\n---------------------指标打分的股票排序----------------------------------\n')
print(df6)
#-------------------------打印目标股票池-------------------------
print('\n-------------------------------------------------------\n')
print('最终的股票组合：\n')