Demo entry 6365458

代码高亮

   

Submitted by anonymous on May 19, 2017 at 12:34
Language: Python 3. Code size: 6.7 kB.

import datetime
import pandas as pd
import numpy as np

# 获取所有交易日日期
trade_days = jqdata.get_trade_days()
trade_days = [date.strftime("%Y-%m-%d") for date in trade_days]

# 获取所有股票
stocks = get_all_securities(['stock']).index.tolist() #获取所有股票

# n日涨幅
roc_n = 5
# 波动率天数
fluidity_n = 5


'''
计算动量因子 前两个月的收益累计
'''
def get_momentum(stock,tdate):
    df_one = get_price(stock,None,tdate,'20d',['close'],skip_paused=None,count=3)
    momentum = 0
    momentum = (df_one.close[-1]/df_one.close[-2]-1.0)+(df_one.close[-2]/df_one.close[-3]-1.0)
    
    return momentum

'''
是否除以市值权重呢?
'''
def get_ILLIQ(stock,tdate):
    df_one = get_price(stock,None,tdate,'1d',['close','volume'],skip_paused=True,count=fluidity_n+1)
    df_one['roc'] = abs(df_one.close/df_one.close.shift(1)-1.0)
    df_one['ILLIQ'] = df_one.roc/df_one.volume    
    df_one = df_one.dropna()
    if len(df_one)==0:
        return 0
    ILLIQ = df_one.ILLIQ.values.mean()
    
    return ILLIQ

# 剔除 少于2日 或前两日一字涨跌停的股票
def get_Roc(stock,tdate,edate):
    df_one = get_price(stock,tdate,edate,'1d',['close','high','low'],skip_paused=True)
    if len(df_one)<2:
        #print(stock,tdate,edate,'中间停牌')
        return 0.0
    if (df_one.high[0]==df_one.low[0]) or (df_one.high[1]==df_one.low[1]):
        #print(stock,tdate,edate,'前两天一字涨跌停')
        return 0.0
    roc = df_one.close[-1]/df_one.close[0]-1.0
    
return roc

array_count = 50 #算几期因子表现
date = '2015-12-30'

tdate = date #因子计算日期

datelist = [] #记入每期因子的日期
one_roc_array = np.zeros(array_count) #五分位组 第一组
two_roc_array = np.zeros(array_count)
three_roc_array = np.zeros(array_count)
four_roc_array = np.zeros(array_count)
five_roc_array = np.zeros(array_count)

yy = 0
for times in range(0,array_count):
    
    edate = trade_days[trade_days.index(tdate)+roc_n] #未来收益日期
    # 获取股票 剔除对应日期的新股
    today_datetime = datetime.datetime.strptime(tdate,"%Y-%m-%d")
    stocks = get_all_securities(['stock'])
    stocks = stocks[(today_datetime- stocks.start_date) > datetime.timedelta(60)].index #剔除对应日期的新股
    
    # 开始日期 投资活动产生的现金流量净额
    q = query(
        cash_flow.code,
        cash_flow.net_invest_cash_flow
    ).filter(
        cash_flow.code.in_(stocks)
    )
    dfgb_one_periods = get_fundamentals(q,tdate)
    dfgb_one_periods = dfgb_one_periods.dropna()     #除nan数据
    stocks = dfgb_one_periods.code.values        #更新股票池    
    
    # 因子列
    momentum_array = np.zeros(len(dfgb_one_periods))
    ILLIQ_array = np.zeros(len(dfgb_one_periods))
    Roc_array = np.zeros(len(dfgb_one_periods))
    # 遍历所有股票 算因子和未来收益
    ii = 0
    for stock in stocks:
        momentum = get_momentum(stock,tdate)
        ILLIQ = get_ILLIQ(stock,tdate)
        Roc = get_Roc(stock,tdate,edate)

        
        momentum_array[ii] = momentum
        ILLIQ_array[ii] = ILLIQ
        Roc_array[ii] = Roc
        ii+=1
    
    dfgb_one_periods['momentum'] = momentum_array # 动量因子
    dfgb_one_periods['ILLIQ'] = ILLIQ_array # 流动性因子
    dfgb_one_periods['Roc'] = Roc_array #未来五天收益率
    
    # 删除无用数据
    dfgb_one_periods = dfgb_one_periods[dfgb_one_periods.ILLIQ!=0] #ILLIQ 无数据的
    dfgb_one_periods = dfgb_one_periods[dfgb_one_periods.Roc!=0] #删掉停牌或一字涨停股
    dfgb_one_periods = dfgb_one_periods.dropna()
    
    scores = np.array(range(len(dfgb_one_periods)))
    #排序 打分
    dfgb_one_periods = dfgb_one_periods.sort(columns='momentum',ascending=False) #降序
    dfgb_one_periods['s1'] = scores
    dfgb_one_periods = dfgb_one_periods.sort(columns='net_invest_cash_flow') #升序
    dfgb_one_periods['s2'] = scores
    dfgb_one_periods = dfgb_one_periods.sort(columns='ILLIQ') #升序
    dfgb_one_periods['s3'] = scores
    
    dfgb_one_periods['scores'] = dfgb_one_periods.s1.values*0.00608+dfgb_one_periods.s2.values*0.00294+\
    dfgb_one_periods.s2.values*0.00533
    dfgb_one_periods = dfgb_one_periods.sort(columns='scores') #升序
    
    print(tdate,edate,len(dfgb_one_periods))
    
    datelist.append(tdate) #记入当期因子计算日期
    tdate = edate #下次因子计算日期
    
    # 记入当期因子五分位未来收益表现
    Count = int(len(dfgb_one_periods)/5)
    one_roc_array[yy] = dfgb_one_periods.Roc.values[:Count].mean()
    two_roc_array[yy] = dfgb_one_periods.Roc.values[Count:Count*2].mean()
    three_roc_array[yy] = dfgb_one_periods.Roc.values[Count*2:Count*3].mean()
    four_roc_array[yy] = dfgb_one_periods.Roc.values[Count*3:Count*4].mean()
    five_roc_array[yy] = dfgb_one_periods.Roc.values[-Count:].mean()  
yy+=1


def gt_roc(roc_array):
    Count = len(roc_array)
    roc_gt = np.ones(Count)
    roc_gt[0] = roc_array[0]+1
    for xx in range(1,Count):
        roc_gt[xx] = roc_gt[xx-1]*(roc_array[xx]+1)
    return roc_gt
# 大盘指数的roc计算
sdate = '2015-12-30'
edate = '2017-01-09'
print(sdate,edate)
benchmark_close = get_price('000300.XSHG',sdate,edate,'5d',['close'])
dates = benchmark_close.index.values
benchmark_close['roc']= benchmark_close.close/benchmark_close.close.shift(1)-1
benchmark_close['roc'][0]=0
benchmark = benchmark_close.roc.values[1:] #大盘每期roc


one_roc_gt = gt_roc(one_roc_array) #第一组累计roc
two_roc_gt = gt_roc(two_roc_array) #第二组累计roc
three_roc_gt = gt_roc(three_roc_array) #第三组累计roc
four_roc_gt = gt_roc(four_roc_array) #第四组累计roc
five_roc_gt = gt_roc(five_roc_array) #第五组累计roc
benchmark_roc_gt = gt_roc(benchmark) #大盘累计净值

one_alpha_roc = one_roc_array-benchmark
two_alpha_roc = two_roc_array-benchmark
three_alpha_roc = three_roc_array-benchmark
four_alpha_roc = four_roc_array-benchmark
five_alpha_roc =five_roc_array-benchmark

one_alpha_gt = gt_roc(one_alpha_roc) #第一组累计alpha
two_alpha_gt = gt_roc(two_alpha_roc) #第二组累计alpha
three_alpha_gt = gt_roc(three_alpha_roc) #第三组累计alpha
four_alpha_gt = gt_roc(four_alpha_roc) #第四组累计alpha
five_alpha_gt = gt_roc(five_alpha_roc) #第五组累计alpha

dif_gt = gt_roc(one_roc_array-five_roc_array) #最好减掉最差组

plt.figure(figsize=(15,6)) #设置图片大小
plt.grid()#开启网格
plt.suptitle('最好分位组-最差分位组',fontsize=15)#标题设置
plt.xlabel('日期',fontsize=15)
plt.ylabel('净值',fontsize=15)
plt.xlim(0,len(five_roc_array))#坐标显示范围

X_index = range(len(five_roc_array))

#plt.plot(X_index,gt_roc(benchmark),'b-',linewidth=1.3,label='benchmark')
#plt.plot(X_index,five_alpha_gt,'r-',linewidth=1.3,label='5-benchmark')
plt.plot(X_index,gt_roc(five_roc_array-one_roc_array),'y-',linewidth=1.3,label='diff(5-1)')

#显示图例在上 右
legend(loc='upper left',fontsize=12,handlelength=3.5) 

#设置刻度
locator_index = range(0,len(one_roc_gt),5)
locator_name = map(lambda a: datelist[a], locator_index)
locator_name = [i[:7] for i in locator_name]
plt.xticks(locator_index,locator_name, rotation=0,fontsize=12)

plt.show()

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).