计算结果展示
计算过程
接下来就写一个循环去计算每个因子,并且计算收益,最后将数据存储:
def calculate_returns(excel_columns,stock_name,data):
#创建新的一行
excel=pd.DataFrame([[np.nan]*118],columns=excel_columns).iloc[0]
#将股票信息与收益放入表格
for x in['日期','股票代码','股票名称','股票类型']:
excel[x]=stock_name[x]
excel['returns']=(data.iloc[-1]['closePrice']-data.iloc[-2]['openPrice'])/data.iloc[-2]['openPrice']*100
#计算各因子值
for x,y in yinzi_name().items():
excel[x]=eval(x)(data.iloc[-(2+y):-2].reset_index(drop = True))
#写入文件
pd.DataFrame(columns=excel_columns).append(excel).to_csv(open('因子数据.csv','a'),index=False,header=0)
各因子计算
接下来,就是写计算各因子的模块了:
根据公式,写好的因子函数如下:
import numpy as np
import pandas as pd
from scipy import stats
from sklearn import preprocessing
from function import*
def alpha001(data, dependencies=['closePrice','openPrice','turnoverVol'], max_window=7):
# (-1*CORR(RANK(DELTA(LOG(VOLUME),1)),RANK(((CLOSE-OPEN)/OPEN)),6)
rank_sizenl = np.log(data['turnoverVol']).diff(1).rank(axis=0, pct=True)
rank_ret = (data['closePrice'] / data['openPrice']) .rank(axis=0, pct=True)
rel = rank_sizenl.rolling(window=6,min_periods=6).corr(rank_ret).iloc[-1] * (-1)
return rel
def alpha002(data, dependencies=['closePrice','lowestPrice','highestPrice'], max_window=2):
# -1*delta(((close-low)-(high-close))/(high-low),1)
win_ratio = (2*data['closePrice']-data['lowestPrice']-data['highestPrice'])/(data['highestPrice']-data['lowestPrice'])
return win_ratio.diff(1).iloc[-1] * (-1)
def alpha003(data, dependencies=['closePrice','lowestPrice','highestPrice'], max_window=6):
# -1*SUM((CLOSE=DELAY(CLOSE,1)?0:CLOSE-(CLOSE>DELAY(CLOSE,1)?MIN(LOW,DELAY(CLOSE,1)):MAX(HIGH,DELAY(CLOSE,1)))),6)
# \u8fd9\u91ccSUM\u5e94\u8be5\u4e3aTSSUM
alpha = data['closePrice']
condition2 = data['closePrice'].diff(periods=1) > 0.0
condition3 = data['closePrice'].diff(periods=1) < 0.0
alpha[condition2] = data['closePrice'][condition2] - np.minimum(data['closePrice'][condition2].shift(1).replace(np.NaN,10000), data['lowestPrice'][condition2])
alpha[condition3] = data['closePrice'][condition3] - np.maximum(data['closePrice'][condition3].shift(1).replace(np.NaN,0), data['highestPrice'][condition3])
return alpha.sum(axis=0) * (-1)
def alpha004(data, dependencies=['closePrice','turnoverVol'], max_window=20):
# (((SUM(CLOSE,8)/8)+STD(CLOSE,8))<(SUM(CLOSE,2)/2))
# ?-1:(SUM(CLOSE,2)/2<(SUM(CLOSE,8)/8-STD(CLOSE,8))
# ?1:(1<=(VOLUME/MEAN(VOLUME,20))
# ?1:-1))
#STD(CLOSE,8):过去8天的收盘价的标准差;VOLUME:成交量;MEAN(VOLUME,20);过去20天的均值
if MEAN(data['closePrice'],8)+STD(data['closePrice'],8)<MEAN(data['closePrice'],2):return -1
elif MEAN(data['closePrice'],2)<MEAN(data['closePrice'],8)-STD(data['closePrice'],8):return 1
elif 1<=data['turnoverVol'].iloc[19]/MEAN(data['turnoverVol'],20):return 1
else: return -1
def alpha005(data, dependencies=['turnoverVol', 'highestPrice'], max_window=13):
# -1*TSMAX(CORR(TSRANK(VOLUME,5),TSRANK(HIGH,5),5),3)
ts_volume = data['turnoverVol'].rolling(window=5,min_periods=5).apply(lambda x: stats.rankdata(x)[-1]/5.0)
ts_high = data['highestPrice'].rolling(window=5,min_periods=5).apply(lambda x: stats.rankdata(x)[-1]/5.0)
corr_ts = ts_volume.rolling(window=5, min_periods=5).corr(ts_high)
alpha = corr_ts.iloc[-3:].max(axis=0) * (-1)
return alpha
def alpha006(data, dependencies=['openPrice', 'highestPrice'], max_window=5):
# -1*RANK(SIGN(DELTA(OPEN*0.85+HIGH*0.15,4)))
# \u6ce8:\u53d6\u503c\u6392\u5e8f\u6709\u968f\u673a\u6027
return sorted(np.sign(DELTA(data['openPrice']*0.85+data['highestPrice']*0.15,4)))[0]*-1
def alpha007(data, dependencies=['turnoverVol', 'turnoverValue', 'closePrice'], max_window=4):
# (RANK(MAX(VWAP-CLOSE,3))+RANK(MIN(VWAP-CLOSE,3)))*RANK(DELTA(VOLUME,3))
# \u611f\u89c9MAX\u5e94\u8be5\u4e3aTSMAX
vwap = data['turnoverValue'] / data['turnoverVol']
part1 = (vwap - data['closePrice']).rolling(window=3,min_periods=3).max().rank(axis=0, pct=True)
part2 = (vwap - data['closePrice']).rolling(window=3,min_periods=3).min().rank(axis=0, pct=True)
part3 = data['turnoverVol'].diff(3).rank(axis=0, pct=True).iloc[-1]
alpha = (part1 + part2) * part3
return alpha.iloc[-1]
def alpha008(data, dependencies=['turnoverVol', 'turnoverValue', 'highestPrice', 'lowestPrice'], max_window=5):
# -1*RANK(DELTA((HIGH+LOW)/10+VWAP*0.8,4))
# \u53d7\u80a1\u4ef7\u5355\u4ef7\u5f71\u54cd,\u53cd\u8f6c
vwap = data['turnoverValue'] / data['turnoverVol']
ma_price = data['highestPrice']*0.1 + data['lowestPrice']*0.1 + vwap*0.8
alpha = ma_price.diff(4).iloc[-1] * (-1)
return alpha
def alpha009(data, dependencies=['highestPrice', 'lowestPrice', 'turnoverVol'], max_window=8):
# SMA(((HIGH+LOW)/2-(DELAY(HIGH,1)+DELAY(LOW,1))/2)*(HIGH-LOW)/VOLUME,7,2)
part1 = (data['highestPrice']+data['lowestPrice'])*0.5-(data['highestPrice'].shift(1)+data['lowestPrice'].shift(1))*0.5
part2 = part1 * (data['highestPrice']-data['lowestPrice']) / data['turnoverVol']
alpha = part2.ewm(adjust=False, alpha=float(2)/7, min_periods=0, ignore_na=False).mean().iloc[-1]
return alpha
def alpha010(data, dependencies=['closePrice'], max_window=25):
# RANK(MAX(((RET<0)?STD(RET,20):CLOSE)^2,5))
# \u6ca1\u6cd5\u89e3\u91ca,\u611f\u89c9MAX\u5e94\u8be5\u4e3aTSMAX
ret = data['closePrice'].pct_change(periods=1)
part1 = ret.rolling(window=20, min_periods=20).std()
condition = ret >= 0.0
part1[condition] = data['closePrice'][condition]
alpha = (part1 ** 2).rolling(window=5,min_periods=5).max().rank(axis=0, pct=True)
return alpha.iloc[-1]
def alpha011(data, dependencies=['closePrice','lowestPrice','highestPrice','turnoverVol'], max_window=6):
# SUM(((CLOSE-LOW)-(HIGH-CLOSE))./(HIGH-LOW).*VOLUME,6)
# \u8fd16\u5929\u83b7\u5229\u76d8\u6bd4\u4f8b
return ((2*data['closePrice']-data['lowestPrice']-data['highestPrice'])/(data['highestPrice']-data['lowestPrice'])*data['turnoverVol']).sum(axis=0) * (-1)
def alpha012(data, dependencies=['openPrice','closePrice','turnoverVol', 'turnoverValue'], max_window=10):
# RANK(OPEN-MA(VWAP,10))*RANK(ABS(CLOSE-VWAP))*(-1)
vwap = data['turnoverValue'] / data['turnoverVol']
part1 = (data['openPrice']-vwap.rolling(window=10,center=False).mean()).rank(axis=0, pct=True).iloc[-1]
part2 = abs(data['closePrice']-vwap).rank(axis=0, pct=True).iloc[-1]
alpha = part1 * part2 * (-1)
return alpha
def alpha013(data, dependencies=['highestPrice','lowestPrice','turnoverVol', 'turnoverValue'], max_window=1):
# ((HIGH*LOW)^0.5)-VWAP
# \u8981\u6ce8\u610fVWAP/price\u662f\u5426\u590d\u6743
vwap = data['turnoverValue'] / data['turnoverVol']
alpha = np.sqrt(data['highestPrice'] * data['lowestPrice']) - vwap
return alpha.iloc[-1]
def alpha014(data, dependencies=['closePrice'], max_window=6):
# CLOSE-DELAY(CLOSE,5)
# \u4e0e\u80a1\u4ef7\u76f8\u5173\uff0c\u5229\u597d\u8305\u53f0
return data['closePrice'].diff(5).iloc[-1]
def alpha015(data, dependencies=['openPrice', 'closePrice'], max_window=2):
# OPEN/DELAY(CLOSE,1)-1
# \u8df3\u7a7a\u9ad8\u5f00/\u4f4e\u5f00
return (data['openPrice']/data['closePrice'].shift(1)-1.0).iloc[-1]
def alpha016(data, dependencies=['turnoverVol', 'turnoverValue'], max_window=10):
# (-1*TSMAX(RANK(CORR(RANK(VOLUME),RANK(VWAP),5)),5))
# \u611f\u89c9\u5176\u4e2d\u6709\u4e2aTSRANK
vwap = data['turnoverValue'] / data['turnoverVol']
corr_vol_vwap = data['turnoverVol'].rank(axis=0, pct=True).rolling(window=5,min_periods=5).corr(vwap.rank(axis=0, pct=True))
alpha = corr_vol_vwap.rolling(window=5,min_periods=5).apply(lambda x: stats.rankdata(x)[-1]/5.0)
alpha = alpha.iloc[-5:].max(axis=0) * (-1)
return alpha
def alpha017(data, dependencies=['closePrice', 'turnoverVol', 'turnoverValue'], max_window=16):
# RANK(VWAP-MAX(VWAP,15))^DELTA(CLOSE,5)
vwap = data['turnoverValue'] / data['turnoverVol']
delta_price = data['closePrice'].diff(5).iloc[-1]
alpha = (vwap-vwap.rolling(window=15,min_periods=15).max()).rank(axis=0, pct=True).iloc[-1] ** delta_price
return alpha
def alpha018(data, dependencies=['closePrice'], max_window=6):
# CLOSE/DELAY(CLOSE,5)
# \u8fd15\u65e5\u6da8\u5e45, REVS5
return (data['closePrice'] / data['closePrice'].shift(5)).iloc[-1]
def alpha019(data, dependencies=['closePrice'], max_window=6):
# (CLOSE<DELAY(CLOSE,5)?(CLOSE/DELAY(CLOSE,5)-1):(CLOSE=DELAY(CLOSE,5)?0:(1-DELAY(CLOSE,5)/CLOSE)))
# \u7c7b\u4f3c\u4e8e\u8fd1\u4e94\u65e5\u6da8\u5e45
condition1 = data['closePrice'] <= data['closePrice'].shift(5)
alpha = data['closePrice']
alpha[condition1] = data['closePrice'].pct_change(periods=5)[condition1]
alpha[~condition1] = -data['closePrice'].pct_change(periods=5)[~condition1]
return alpha.iloc[-1]
def alpha020(data, dependencies=['closePrice'], max_window=7):
# (CLOSE/DELAY(CLOSE,6)-1)*100
# \u8fd16\u65e5\u6da8\u5e45
return (data['closePrice'].pct_change(periods=6) * 100.0).iloc[-1]
def alpha021(data, dependencies=['closePrice'], max_window=12):
# REGBETA(MEAN(CLOSE,6),SEQUENCE(6))
a=[MEAN(list(data['closePrice'])[:-x],6) for x in range(1,7)]
return REGBETA(a,SEQUENCE(6),6)
def alpha022(data, dependencies=['closePrice'], max_window=21):
# SMEAN((CLOSE/MEAN(CLOSE,6)-1-DELAY(CLOSE/MEAN(CLOSE,6)-1,3)),12,1)
# \u731cSMEAN\u662fSMA
ratio = data['closePrice'] / data['closePrice'].rolling(window=6,min_periods=6).mean() - 1.0
alpha = ratio.diff(3).ewm(adjust=False, alpha=float(1)/12, min_periods=12, ignore_na=False).mean().iloc[-1]
return alpha
def alpha023(data, dependencies=['closePrice'], max_window=40):
# SMA((CLOSE>DELAY(CLOSE,1)?STD(CLOSE,20):0),20,1) /
# (SMA((CLOSE>DELAY(CLOSE,1)?STD(CLOSE,20):0),20,1)+SMA((CLOSE<=DELAY(CLOSE,1)?STD(CLOSE,20):0),20,1))
# *100
prc_std = data['closePrice'].rolling(window=20, min_periods=20).std()
condition1 = data['closePrice'] > data['closePrice'].shift(1)
part1 = prc_std.copy(deep=True)
part2 = prc_std.copy(deep=True)
part1[~condition1] = 0.0
part2[condition1] = 0.0
alpha = part1.ewm(adjust=False, alpha=float(1)/20, min_periods=20, ignore_na=False).mean() / (part1.ewm(adjust=False, alpha=float(1)/20, min_periods=20, ignore_na=False).mean() + part2.ewm(adjust=False, alpha=float(1)/20, min_periods=20, ignore_na=False).mean()) * 100
return alpha.iloc[-1]
def alpha024(data, dependencies=['closePrice'], max_window=10):
# SMA(CLOSE-DELAY(CLOSE,5),5,1)
return data['closePrice'].diff(5).ewm(adjust=False, alpha=float(1)/5, min_periods=5, ignore_na=False).mean().iloc[-1]
def alpha025(data, dependencies=['closePrice', 'turnoverVol'], max_window=251):
# (-1*RANK(DELTA(CLOSE,7)*(1-RANK(DECAYLINEAR(VOLUME/MEAN(VOLUME,20),9)))))*(1+RANK(SUM(RET,250)))
w = np.array(range(1, 10))
ret = data['closePrice'].pct_change(periods=1)
part1 = data['closePrice'].diff(7)
part2 = data['turnoverVol']/(data['turnoverVol'].rolling(window=20,min_periods=20).mean())
part2 = 1.0 - part2.rolling(window=9, min_periods=9).apply(lambda x: np.dot(x, w)).rank(axis=0, pct=True)
part3 = 1.0 + ret.rolling(window=250, min_periods=250).sum().rank(axis=0, pct=True)
alpha = (-1.0) * (part1 * part2).rank(axis=0, pct=True) * part3
return alpha.iloc[-1]
def alpha026(data, dependencies=['closePrice', 'turnoverValue', 'turnoverVol'], max_window=235):
# (SUM(CLOSE,7)/7-CLOSE+CORR(VWAP,DELAY(CLOSE,5),230))
vwap = data['turnoverValue'] / data['turnoverVol']
part1 = data['closePrice'].rolling(window=7, min_periods=7).mean() - data['closePrice']
part2 = vwap.rolling(window=230, min_periods=230).corr(data['closePrice'].shift(5))
return (part1 + part2).iloc[-1]
def alpha027(data, dependencies=['closePrice'], max_window=18):
# WMA((CLOSE-DELTA(CLOSE,3))/DELAY(CLOSE,3)*100+(CLOSE-DELAY(CLOSE,6))/DELAY(CLOSE,6)*100,12)
part1 = data['closePrice'].pct_change(periods=3) * 100.0 + data['closePrice'].pct_change(periods=6) * 100.0
# w = preprocessing.normalize(np.array([i for i in range(1, 13)]),norm='l1',axis=1).reshape(-1)
w=np.array(range(1,13))
alpha = part1.rolling(window=12, min_periods=12).apply(lambda x: np.dot(x, w))
return alpha.iloc[-1]
def alpha028(data, dependencies=['KDJ_J'], max_window=13):
# 3*SMA((CLOSE-TSMIN(LOW,9))/(TSMAX(HIGH,9)-TSMIN(LOW,9))*100,3,1)
# -2*SMA(SMA((CLOSE-TSMIN(LOW,9))/( TSMAX(HIGH,9)-TSMIN(LOW,9))*100,3,1),3,1)
# \u5c31\u662fKDJ_J
part1 =data['closePrice']- data['closePrice'].rolling(window=9, min_periods=9).min()
part2=data['highestPrice'].rolling(window=9, min_periods=9).max()-data['lowestPrice'].rolling(window=9, min_periods=9).min()
part3= 3*SMA(list(part1/part2*100)[-3:],3,1)
part4=[SMA(list(part1/part2*100)[-5:-2],3,1),SMA(list(part1/part2*100)[-4:-1],3,1),SMA(list(part1/part2*100)[-3:],3,1)]
part5=part3-2*SMA(part4,3,1)
return part5
def alpha029(data, dependencies=['closePrice', 'turnoverVol'], max_window=7):
# (CLOSE-DELAY(CLOSE,6))/DELAY(CLOSE,6)*VOLUME
# \u83b7\u5229\u6210\u4ea4\u91cf
return (data['closePrice'].pct_change(periods=6)*data['turnoverVol']).iloc[-1]
def alpha030(data, dependencies=['closePrice', 'PB', 'MktValue'], max_window=81):
# WMA((REGRESI(RET,MKT,SMB,HML,60))^2,20)
# \u5373\u7279\u8d28\u6027\u6536\u76ca
# MKT \u4e3a\u5e02\u503c\u52a0\u6743\u7684\u5e02\u573a\u5e73\u5747\u6536\u76ca\u7387\uff0c
# SMB \u4e3a\u5e02\u503c\u6700\u5c0f\u768430%\u7684\u80a1\u7968\u7684\u5e73\u5747\u6536\u76ca\u51cf\u53bb\u5e02\u503c\u6700\u5927\u768430%\u7684\u80a1\u7968\u7684\u5e73\u5747\u6536\u76ca\uff0c
# HML \u4e3aPB\u6700\u9ad8\u768430%\u7684\u80a1\u7968\u7684\u5e73\u5747\u6536\u76ca\u51cf\u53bbPB\u6700\u4f4e\u768430%\u7684\u80a1\u7968\u7684\u5e73\u5747\u6536\u76ca
ret = data['closePrice'].pct_change(periods=1).fillna(0.0)
mkt_ret = (ret * data['MktValue']).sum(axis=1) / data['MktValue'].sum(axis=1)
me30 = (data['MktValue'].T <= data['MktValue'].quantile(0.3, axis=1)).T
me70 = (data['MktValue'].T >= data['MktValue'].quantile(0.7, axis=1)).T
pb30 = (data['PB'].T <= data['PB'].quantile(0.3, axis=1)).T
pb70 = (data['PB'].T >= data['PB'].quantile(0.7, axis=1)).T
smb_ret = ret[me30].mean(axis=1, skipna=True) - ret[me70].mean(axis=1, skipna=True)
hml_ret = ret[pb70].mean(axis=1, skipna=True) - ret[pb30].mean(axis=1, skipna=True)
xs = pd.concat([mkt_ret, smb_ret, hml_ret], axis=1)
idxs = pd.Series(data=range(len(data['closePrice'].index)), index=data['closePrice'].index)
def multi_var_linregress(idx, y, xs):
X = xs.iloc[idx]
Y = y.iloc[idx]
X = sm.add_constant(X)
try:
res = np.array(sm.OLS(Y, X).fit().resid)
except Exception as e:
return np.nan
return res[-1]
# print(xs.tail(5), ret.tail(5))
residual = [idxs.rolling(window=60, min_periods=60).apply(lambda x: multi_var_linregress(x, ret[col], xs)) for col in ret.columns]
residual = pd.concat(residual, axis=1)
residual.columns = ret.columns
w = preprocessing.normalize(np.array([i for i in range(1, 21)]), norm='l1', axis=1).reshape(-1)
alpha = (residual ** 2).rolling(window=20, min_periods=20).apply(lambda x: np.dot(x, w))
return alpha.iloc[-1]
def alpha031(data, dependencies=['closePrice'], max_window=12):
# (CLOSE-MEAN(CLOSE,12))/MEAN(CLOSE,12)*100
return ((data['closePrice']/data['closePrice'].rolling(window=12,min_periods=12).mean()-1.0)*100).iloc[-1]
def alpha032(data, dependencies=['highestPrice', 'turnoverVol'], max_window=6):
# (-1*SUM(RANK(CORR(RANK(HIGH),RANK(VOLUME),3)),3))
# \u91cf\u4ef7\u9f50\u5347/\u53cd\u8f6c
part1 = data['highestPrice'].rank(axis=0, pct=True).rolling(window=3, min_periods=3).corr(data['turnoverVol'].rank(axis=0, pct=True))
alpha = part1.rank(axis=0, pct=True).iloc[-3:].sum(axis=0) * (-1)
return alpha
def alpha033(data, dependencies=['lowestPrice', 'closePrice', 'turnoverVol'], max_window=241):
# (-1*TSMIN(LOW,5)+DELAY(TSMIN(LOW,5),5))*RANK((SUM(RET,240)-SUM(RET,20))/220)*TSRANK(VOLUME,5)
part1 = data['lowestPrice'].rolling(window=5, min_periods=5).min().diff(5) * (-1)
ret = data['closePrice'].pct_change(periods=1)
part2 = ((ret.rolling(window=240, min_periods=240).sum() - ret.rolling(window=20, min_periods=20).sum()) / 220).rank(axis=0, pct=True)
part3 = data['turnoverVol'].iloc[-5:].rank(axis=0, pct=True)
alpha = part1.iloc[-1] * part2.iloc[-1] * part3.iloc[-1]
return alpha
def alpha034(data, dependencies=['closePrice'], max_window=12):
# MEAN(CLOSE,12)/CLOSE
return (data['closePrice'].rolling(window=12, min_periods=12).mean() / data['closePrice']).iloc[-1]
def alpha035(data, dependencies=['openPrice', 'closePrice', 'turnoverVol'], max_window=24):
# (MIN(RANK(DECAYLINEAR(DELTA(OPEN,1),15)),RANK(DECAYLINEAR(CORR(VOLUME,OPEN*0.65+CLOSE*0.35,17),7)))*-1)
# \u731c\u540e\u4e00\u9879OPEN\u4e3aCLOSE
w7 =np.array(range(1, 8)).reshape(-1)
w15 = np.array(range(1, 16)).reshape(-1)
part1 = data['openPrice'].diff(periods=1).rolling(window=15, min_periods=15).apply(lambda x: np.dot(x, w15)).rank(axis=0, pct=True)
part2 = (data['openPrice']*0.65+data['closePrice']*0.35).rolling(window=17, min_periods=17).corr(data['turnoverVol']).rolling(window=7, min_periods=7).apply(lambda x: np.dot(x, w7)).rank(axis=0, pct=True)
alpha = np.minimum(part1, part2).iloc[-1] * (-1)
return alpha
def alpha036(data, dependencies=['turnoverValue', 'turnoverVol'], max_window=9):
# RANK(SUM(CORR(RANK(VOLUME),RANK(VWAP),6),2))
# \u91cf\u4ef7\u9f50\u5347, TSSUM
vwap = data['turnoverValue'] / data['turnoverVol']
part1 = data['turnoverVol'].rank(axis=0, pct=True).rolling(window=6,min_periods=6).corr(vwap.rank(axis=0, pct=True))
alpha = part1.rolling(window=2, min_periods=2).sum().rank(axis=0, pct=True).iloc[-1]
return alpha
def alpha037(data, dependencies=['openPrice', 'closePrice'], max_window=16):
# (-1*RANK(SUM(OPEN,5)*SUM(RET,5)-DELAY(SUM(OPEN,5)*SUM(RET,5),10)))
part1 = data['openPrice'].rolling(window=5, min_periods=5).sum() * (data['closePrice'].pct_change(periods=1).rolling(window=5, min_periods=5).sum())
alpha = part1.diff(periods=10).iloc[-1] * (-1)
return alpha
def alpha038(data, dependencies=['highestPrice'], max_window=20):
# ((SUM(HIGH,20)/20)<HIGH)?(-1*DELTA(HIGH,2)):0
# \u4e0e\u80a1\u4ef7\u76f8\u5173\uff0c\u5229\u597d\u8305\u53f0
condition = data['highestPrice'].rolling(window=20, min_periods=20).mean() < data['highestPrice']
alpha = data['highestPrice'].diff(periods=2) * (-1)
alpha[~condition] = 0.0
return alpha.iloc[-1]
def alpha039(data, dependencies=['closePrice', 'openPrice', 'turnoverValue', 'turnoverVol'], max_window=243):
# (RANK(DECAYLINEAR(DELTA(CLOSE,2),8))-RANK(DECAYLINEAR(CORR(VWAP*0.3+OPEN*0.7,SUM(MEAN(VOLUME,180),37),14),12)))*-1
w8 =np.array(range(1, 9)).reshape(-1)
w12 = np.array(range(1, 13)).reshape(-1)
parta = data['turnoverValue'] / data['turnoverVol'] * 0.3 + data['openPrice'] * 0.7
partb = data['turnoverVol'].rolling(window=180, min_periods=180).mean().rolling(window=37, min_periods=37).sum()
part1 = data['closePrice'].diff(periods=2).rolling(window=8, min_periods=8).apply(lambda x: np.dot(x, w8)).rank(axis=0,pct=True)
part2 = parta.rolling(window=14, min_periods=14).corr(partb).rolling(window=12, min_periods=12).apply(lambda x: np.dot(x, w12)).rank(axis=0, pct=True)
return (part1 - part2).iloc[-1] * (-1)
def alpha040(data, dependencies=['VR','turnoverVol'], max_window=27):
# SUM(CLOSE>DELAY(CLOSE,1)?VOLUME:0,26)/SUM(CLOSE<=DELAY(CLOSE,1)?VOLUME:0,26)*100
# \u5373VR\u6280\u672f\u6307\u6807
part1=((data['closePrice'].diff(periods=1)>0)*data['turnoverVol']).sum()
part2=((data['closePrice'].diff(periods=1)<=0)*data['turnoverVol']).sum()
return part1/part2*100
def alpha041(data, dependencies=['turnoverValue', 'turnoverVol'], max_window=9):
# RANK(MAX(DELTA(VWAP,3),5))*-1
return (data['turnoverValue'] / data['turnoverVol']).diff(periods=3).rolling(window=5, min_periods=5).max().rank(axis=0, pct=True).iloc[-1] * (-1)
def alpha042(data, dependencies=['highestPrice', 'turnoverVol'], max_window=10):
# (-1*RANK(STD(HIGH,10)))*CORR(HIGH,VOLUME,10)
# \u4ef7\u7a33/\u91cf\u4ef7\u9f50\u5347
part1 = data['highestPrice'].rolling(window=10,min_periods=10).std().rank(axis=0,pct=True) * (-1)
part2 = data['highestPrice'].rolling(window=10,min_periods=10).corr(data['turnoverVol'])
return