forked from FinHackCN/xlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factors.py
executable file
·171 lines (151 loc) · 8.57 KB
/
factors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import os
import talib as ta
import pandas as pd
import alphalens
import numpy as np
from alphalens.utils import get_clean_factor_and_forward_returns
from alphalens.tears import create_full_tear_sheet
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
warnings.simplefilter(action='ignore', category=RuntimeWarning)
warnings.simplefilter(action='ignore', category=UserWarning)
def analysis(df_all,factor_name,periods=[10]):
df_all = df_all.replace([np.inf, -np.inf], np.nan)
df_all=df_all.fillna(0)
df_all=df_all[df_all.close>0]
df_all.reset_index(drop=True)
df_all['date']=pd.to_datetime(df_all['date'])
price = df_all.pivot_table(index='date', columns='symbol', values='close')
df_all.index=df_all['date']
price.index = pd.to_datetime(price.index)
assets = df_all.set_index([df_all.index,df_all['symbol']], drop=True,append=False, inplace=False)
assets=assets[~assets.index.duplicated()]
ret = get_clean_factor_and_forward_returns(assets[factor_name],price,periods=[10],quantiles=None,bins=10,max_loss=1,groupby=None)
sheet=alphalens.tears.create_full_tear_sheet(ret)
def get_finance(ts_code,factors,df_cal,basic,start,end):
df_price=AStock.getStockDailyPriceByCode(ts_code,db)
df_price=pd.merge(df_cal,df_price,on=['trade_date'],how='outer', validate="one_to_many")
df_balance=mysql.selectToDf("select ann_date as trade_date,fix_assets,cip,intan_assets,r_and_d from astock_finance_balancesheet where ts_code='"+ts_code+"' and report_type=1 order by trade_date asc",db)
df_daily_basic=mysql.selectToDf("select trade_date,total_mv from astock_price_daily_basic where ts_code='"+ts_code+"' order by trade_date asc",db)
df=pd.merge(df_price, df_balance, how='left', on='trade_date', copy=True, indicator=False)
df=pd.merge(df, df_daily_basic, how='left', on='trade_date', copy=True, indicator=False)
df=df.fillna(method='ffill')
df['ts_code']=ts_code
df['industry']=str(basic[basic['ts_code']==ts_code]['industry'].values[0])
df=df[df.trade_date>'20150101']
df=df[df.trade_date<'20220410']
df=df.reset_index(drop=True)
df['f1']=df['fix_assets'].astype('float')/df['total_mv'].astype('float')
df['f2']=df['cip'].astype('float')/df['total_mv'].astype('float')
df['f3']=df['intan_assets'].astype('float')/df['total_mv'].astype('float')
df['f4']=df['r_and_d'].astype('float')/df['total_mv'].astype('float')
df.rename(columns={'ts_code':'symbol','trade_date':'date'}, inplace = True)
df['date']=df['date'].map(lambda x: x[:4]+'-'+x[4:6]+'-'+x[6:8])
return df
def getTA(df):
## Overlap Studies Functions
df['UPPER'], df['MIDDLE'], df['LOWER'] = ta.BBANDS(df.close, timeperiod=90, nbdevup=2, nbdevdn=2, matype=0)
df['DEMA'] = ta.DEMA(df.close, 90)
df['EMA90'] = ta.EMA(df.close, 90)
df['HT-TRENDLINE'] = ta.HT_TRENDLINE(df.close)
df['MA90'] = ta.MA(df.close, 90)
df['MAMA'], df['FAMA'] = ta.MAMA(df.close)
df['MAVP'] = ta.MAVP(df.close, df.EMA90)
df['MIDPOINT'] = ta.MIDPOINT(df.close, 90)
df['MIDPRICE'] = ta.MIDPRICE(df.high, df.low, 90)
df['SAR'] = ta.SAR(df.high, df.low, acceleration=0, maximum=0)
df['SAREXT'] = ta.SAREXT(df.high, df.low, 0, 0, 0)
df['SMA'] = ta.SMA(df.close, 90)
df['T3'] = ta.T3(df.close, 90)
df['TEMA'] = ta.TEMA(df.close, 90)
df['TRIMA'] = ta.TRIMA(df.close, 90)
df['WMA'] = ta.WMA(df.close, 90)
## Momentum Indicator Funnctions
df['ADX'] = ta.ADX(df.high, df.low, df.close, 90)
df['ADXR'] = ta.ADXR(df.high, df.low, df.close, 90)
df['APO'] = ta.APO(df.close, 30, 90)
df['AROONDOWN'], df['ARRONUP'] = ta.AROON(df.high, df.low, 90)
df['AROONOSC'] = ta.AROONOSC(df.high, df.low, 90)
df['BOP'] = ta.BOP(df.open, df.high, df.low, df.close)
df['CCI'] = ta.CCI(df.high, df.low, df.close, 90)
df['CMO'] = ta.CMO(df.close, 90)
df['DX'] = ta.DX(df.high, df.low, df.close, 90)
df['MACD'], df['MACDSIGNAL'], df['MACDHIST'] = ta.MACD(df.close, fastperiod=12, slowperiod=26, signalperiod=9)
df['MACDX'], df['MACDSIGNALX'], df['MACDHISTX'] = ta.MACDEXT(df.close, fastperiod=12, slowperiod=26, signalperiod=9)
df['MACDFIX'], df['MACDSIGNALFIX'], df['MACDHISTFIX'] = ta.MACDFIX(df.close, 90)
df['MFI'] = ta.MFI(df.high, df.low, df.close, df.volume, 90)
df['MINUS-DI'] = ta.MINUS_DI(df.high, df.low, df.close, 90)
df['MINUS-DM'] = ta.MINUS_DM(df.high, df.low, 90)
df['MOM'] = ta.MOM(df.close, 90)
df['PLUS-DI'] = ta.PLUS_DI(df.high, df.low, df.close, 90)
df['PLUS-DM'] = ta.PLUS_DM(df.high, df.low, 90)
df['PPO'] = ta.PPO(df.close, 30, 90)
df['ROC'] = ta.ROC(df.close, 90)
df['ROCR'] = ta.ROCR(df.close, 90)
df['ROCR100'] = ta.ROCR100(df.close, 90)
df['RSI'] = ta.RSI(df.close,90)
df['SLOWK'], df['SLOWD'] = ta.STOCH(df.high, df.low, df.close)
df['FASTK'], df['FASTD'] = ta.STOCHF(df.high, df.low, df.close)
df['FASTK-RSI'], df['FASTD-RSI'] = ta.STOCHRSI(df.close, 90)
df['TRIX'] = ta.TRIX(df.close, 90)
df['ULTOSC'] = ta.ULTOSC(df.high, df.low, df.close)
df['WILLR'] = ta.WILLR(df.high, df.low, df.close, 90)
## Volume Indicator Functions
df['AD'] = ta.AD(df.high, df.low, df.close, df.volume)
df['ADOSC'] = ta.ADOSC(df.high, df.low, df.close, df.volume)
df['OBV'] = ta.OBV(df.close, df.volume)
## Volatility Indicator Functions
df['ATR'] = ta.ATR(df.high, df.low, df.close, 90)
df['NATR'] = ta.NATR(df.high, df.low, df.close, 90)
df['TRANGE'] = ta.TRANGE(df.high, df.low, df.close)
## Price Transform Functions
df['AVGPRICE'] = ta.AVGPRICE(df.open, df.high, df.low, df.close)
df['MEDPRICE'] = ta.MEDPRICE(df.high, df.low)
df['TYPPRICE'] = ta.TYPPRICE(df.high, df.low, df.close)
df['WCLPRICE'] = ta.WCLPRICE(df.high, df.low, df.close)
## Cycle Indicator Functions
df['HT-DCPERIOD'] = ta.HT_DCPERIOD(df.close)
df['HT-DCPHASE'] = ta.HT_DCPHASE(df.close)
df['INPHASE'], df['QUADRATURE'] = ta.HT_PHASOR(df.close)
df['SINE'] , df['LEADSINE'] = ta.HT_SINE(df.close)
df['HT-TRENDMODE'] = ta.HT_TRENDMODE(df.close)
## Beta
df['BETA'] = ta.BETA(df.high, df.low, 90)
df['CORREL'] = ta.CORREL(df.high, df.low, 90)
df['LINEARREG'] = ta.LINEARREG(df.close, 90)
df['LINEARREG-ANGLE'] = ta.LINEARREG_ANGLE(df.close, 90)
df['LINEARREG-INTERCEPT'] = ta.LINEARREG_INTERCEPT(df.close, 90)
df['LINEARREG-SLOPE'] = ta.LINEARREG_SLOPE(df.close, 90)
df['STDDEV'] = ta.STDDEV(df.close, 90, 1)
df['TSF'] = ta.TSF(df.close, 90)
df['VAR'] = ta.VAR(df.close, 90, 1)
## Math Transform Functions
df['ACOS'] = ta.ACOS(df.close)
df['ASIN'] = ta.ASIN(df.close)
df['ATAN'] = ta.ATAN(df.close)
df['CEIL'] = ta.CEIL(df.close)
df['COS'] = ta.COS(df.close)
df['COSH'] = ta.COSH(df.close)
df['EXP'] = ta.EXP(df.close)
df['FLOOR'] = ta.FLOOR(df.close)
df['LN'] = ta.LN(df.close) # Log
df['LOG10'] = ta.LOG10(df.close) # Log
df['SIN'] = ta.SIN(df.close)
df['SINH'] = ta.SINH(df.close)
df['SQRT'] = ta.SQRT(df.close)
df['TAN'] = ta.TAN(df.close)
df['TANH'] = ta.TANH(df.close)
## Math Operator Functions
df['ADD'] = ta.ADD(df.high, df.low)
df['DIV'] = ta.DIV(df.high, df.low)
df['MAX'] = ta.MAX(df.close, 90)
df['MAXINDEX'] = ta.MAXINDEX(df.close, 90)
df['MIN'] = ta.MIN(df.close, 90)
df['MININDEX'] = ta.MININDEX(df.close, 90)
df['MINIDX'], df['MAXIDX'] = ta.MINMAXINDEX(df.close, 90)
df['MULT'] = ta.MULT(df.high, df.low)
df['SUB'] = ta.SUB(df.high, df.low)
df['SUM'] = ta.SUM(df.close, 90)
df = df.replace([np.inf, -np.inf], np.nan)
df=df.fillna(0)
return df