-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactors.py
470 lines (350 loc) · 12.8 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# import basics
import datetime
import math
import time
from datetime import timedelta
import pandas as pd
import numpy as np
import statsmodels.api as sm
import matplotlib
from matplotlib import pyplot as plt
# import from sklearn
import sklearn
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
# import from joinquant
from jqlib.technical_analysis import *
from jqfactor import Factor, calc_factors
from jqfactor import get_factor_values
from jqfactor import neutralize
from jqfactor import winsorize
from jqfactor import standardlize
# some of the relevant key factors (defined in Joinquant)
factors = ['operating_revenue_growth_rate', 'total_asset_growth_rate', 'net_operate_cashflow_growth_rate',
'total_profit_growth_rate', 'net_profit_growth_rate', 'total_operating_revenue_ttm',
'operating_profit_ttm', 'gross_profit_ttm', 'EBIT',
'net_profit_ttm', 'market_cap', 'cash_flow_to_price_ratio',
'sales_to_price_ratio', 'net_profit_ratio', 'quick_ratio',
'current_ratio', 'operating_profit_ratio', 'SGI',
'roe_ttm', 'VOL10', 'VOL20', 'VOL60', 'VOL120',
'AR', 'BR', 'ARBR', 'VEMA5', 'DAVOL10', 'DAVOL5', 'Variance20',
'Variance60', 'Variance120', 'ATR6', 'ATR14', 'total_operating_revenue_per_share',
'eps_ttm', 'BIAS10', 'BIAS20', 'BIAS60', 'Volume1M',
'momentum', 'book_to_price_ratio', 'liquidity', 'earnings_yield',
'growth']
# parameters:
data_input_user =
start_date_user = "2019-01-01"
end_date_user = "2022-09-15"
# define log of the closing price:
class LOG_CLOSE(Factor):
name = 'log_close'
max_window = 1
dependencies = ['close']
def calc(self, data):
log_close = np.log(data['close'])
return log_close.mean()
# define EP
class EP(Factor):
name = 'ep'
max_window = 1
dependencies = ['net_profit_ttm', 'market_cap'] # net profit/market cap
def calc(self, data):
total_net_profit = data['net_profit_ttm']
market_value = data['market_cap']
EP = total_net_profit / market_value
return EP.mean()
# define BP
class BP(Factor):
name = "bp"
max_window = 1
dependencies = ['total_assets', 'total_liability', 'market_cap']
def calc(self, data):
net_asset = data['total_assets'] - data['total_liability']
bp = net_asset / data['market_cap']
return bp.mean()
# gross profit:
class GROSSPROFITABILITY(Factor):
name = 'gross_profitability'
max_window = 1
dependencies = ['total_operating_revenue', 'total_operating_cost', 'total_assets']
def calc(self, data):
total_operating_revenue = data['total_operating_revenue']
total_operating_cost = data['total_operating_cost']
total_assets = data['total_assets']
gross_profitability = (total_operating_revenue - total_operating_cost) / total_assets
return gross_profitability.mean()
# ROATTM
class ROATTM(Factor):
name = 'roa_ttm'
max_window = 1
dependencies = ['net_profit', 'net_profit_1', 'net_profit_2', 'net_profit_3',
'total_assets']
def calc(self, data):
net_profit_ttm = data['net_profit'] + data['net_profit_1'] + data['net_profit_2'] + data['net_profit_3']
result = net_profit_ttm / data['total_assets']
return result.mean()
# SP
class SP(Factor):
name = 'sp'
max_window = 1
dependencies = ['operating_revenue_ttm', 'market_cap']
def calc(self, data):
SP = data['operating_revenue_ttm'] / data['market_cap']
return SP.mean()
# increment in profit
class INCPROFITYOY(Factor):
name = 'profityoy'
max_window = 1
dependencies = ['inc_net_profit_year_on_year']
def calc(self, data):
yoy = data['inc_net_profit_year_on_year']
return yoy.mean()
# increment in revenue
class INCREVENUEYOY(Factor):
name = 'revenueyoy'
max_window = 1
dependencies = ['inc_revenue_year_on_year']
def calc(self, data):
yoy = data['inc_revenue_year_on_year']
return yoy.mean()
# increment in ROA
class ROA_GRO2(Factor):
name = 'roa_gro2'
max_window = 1
dependencies = ['roa_y', 'roa_y1']
def calc(self, data):
gro = (data['roa_y'] - data['roa_y1']) / data['roa_y1'] # calculate ROA
return gro.mean()
# increment in net profit:
class PROFIT_GRO(Factor):
name = 'profit_gro'
max_window = 1
dependencies = ['net_profit_y', 'net_profit_y1']
def calc(self, data):
gro = (data['net_profit_y'] - data['net_profit_y1']) / data['net_profit_y1']
return gro.mean()
# increment in sales
class SALES_GRO(Factor):
name = 'sales_gro'
max_window = 1
dependencies = ['operating_revenue', 'operating_revenue_y1']
def calc(self, data):
gro = (data['operating_revenue'] - data['operating_revenue_y1']) / data['operating_revenue_y1']
return gro.mean()
# net operational cash flow/net profit
class OPERATIONCASHRATIO_Q(Factor):
name = 'operationcashratio_q'
max_window = 1
dependencies = ['net_operate_cash_flow', 'net_profit']
def calc(self, data):
factor = data['net_operate_cash_flow'] / data['net_profit']
return factor.mean()
# net operational cash flow/TTM
class OPERATIONCASHRATIO_TTM(Factor):
name = 'operationcashratio_ttm'
max_window = 1
dependencies = ['net_operate_cash_flow_ttm', 'net_profit',
'net_profit_1', 'net_profit_2', 'net_profit_3']
def calc(self, data):
net_profit_ttm = data['net_profit'] + data['net_profit_1'] + data['net_profit_2'] + data['net_profit_3']
factor = data['net_operate_cash_flow_ttm'] / net_profit_ttm
return factor.mean()
# ROA
class ROA(Factor):
name = 'roa'
max_window = 1
dependencies = ['roa']
def calc(self, data):
roa = data['roa']
return roa.mean()
# ROE
class ROE(Factor):
name = 'roe'
max_window = 1
dependencies = ['roe']
def calc(self, data):
roe = data['roe']
return roe.mean()
# net profit
class NET_PRO(Factor):
name = 'net_pro'
max_window = 1
dependencies = ['net_profit']
def calc(self, data):
net_profit = data['net_profit']
return net_profit.mean()
# asset turnover rate
class ASSETTURNOVER_Q(Factor):
name = 'assetturnover_q'
max_window = 1
dependencies = ['operating_revenue', 'total_assets']
def calc(self, data):
turnover = data['operating_revenue'] / data['total_assets']
return turnover.mean()
# YTD
class PROFITMARGIN_Q(Factor):
name = 'profitmargin_q'
max_window = 1
dependencies = ['gross_profit_margin']
def calc(self, data):
profitm = data['gross_profit_margin']
return profitm.mean()
# total asset/net asset
class FINANCIAL_LEVERAGE(Factor):
name = 'financial_leverage'
max_window = 1
dependencies = ['total_assets', 'total_liability']
def calc(self, data):
net_asset = data['total_assets'] - data['total_liability']
leverage = data['total_assets'] / net_asset
return leverage.mean()
# leverage/net asset
class DEBTEQUITY_RATIO(Factor):
name = 'debtequity_ratio'
max_window = 1
dependencies = ['total_assets', 'total_liability', 'total_non_current_liability']
def calc(self, data):
net_asset = data['total_assets'] - data['total_liability']
leverage = data['total_non_current_liability'] / net_asset
return leverage.mean()
# yields in 180 days/360 days
class ALPHA_180(Factor):
name = 'alpha_180'
max_window = 181
dependencies = ['close']
def calc(self, data):
close = data['close'].pct_change()[1:]
index_close = self._get_extra_data(securities=['000001.XSHG'], fields=['close'])['close'].pct_change()[1:]
# regression
model = sm.OLS(close, sm.add_constant(index_close.values))
result = model.fit()
alpha = result.params[0]
return alpha.mean()
class ALPHA_360(Factor):
name = 'alpha_360'
max_window = 361
dependencies = ['close']
def calc(self, data):
close = data['close'].pct_change()[1:]
index_close = self._get_extra_data(securities=['000001.XSHG'], fields=['close'])['close'].pct_change()[1:]
# regression
model = sm.OLS(close, sm.add_constant(index_close.values))
result = model.fit()
alpha = result.params[0]
return alpha.mean()
class BETA_180(Factor):
name = 'beta_180'
max_window = 181
dependencies = ['close']
def calc(self, data):
close = data['close'].pct_change()[1:]
index_close = self._get_extra_data(securities=['000001.XSHG'], fields=['close'])['close'].pct_change()[1:]
# regression
model = sm.OLS(close, sm.add_constant(index_close.values))
result = model.fit()
beta = result.params[1]
return beta.mean()
class BETA_360(Factor):
name = 'beta_360'
max_window = 361
dependencies = ['close']
def calc(self, data):
close = data['close'].pct_change()[1:]
index_close = self._get_extra_data(securities=['000001.XSHG'], fields=['close'])['close'].pct_change()[1:]
# regression
model = sm.OLS(close, sm.add_constant(index_close.values))
result = model.fit()
beta = result.params[1]
return beta.mean()
# yields in the past 30,60,180,240 days
class RET_30(Factor):
name = 'ret_30'
max_window = 30
dependencies = ['close']
def calc(self, data):
close = data['close']
ret = close.iloc[-1, :] / close.iloc[0, :] - 1
return ret
class RET_60(Factor):
name = 'ret_60'
max_window = 60
dependencies = ['close']
def calc(self, data):
close = data['close']
ret = close.iloc[-1, :] / close.iloc[0, :] - 1
return ret
class RET_180(Factor):
name = 'ret_180'
max_window = 180
dependencies = ['close']
def calc(self, data):
close = data['close']
ret = close.iloc[-1, :] / close.iloc[0, :] - 1
return ret
class RET_360(Factor):
name = 'ret_360'
max_window = 360
dependencies = ['close']
def calc(self, data):
close = data['close']
ret = close.iloc[-1, :] / close.iloc[0, :] - 1
return ret
# turnover rate in the past 30,60,180,240 days
class TURN_RET30(Factor):
name = 'turn_ret30'
max_window = 31
dependencies = ['volume', 'circulating_cap', 'close']
def calc(self, data):
volume = data['volume'] / 10000
turn = volume / data['circulating_cap']
turn = turn[1:]
close = data['close'].pct_change()[1:]
turn_ret = close * turn
return turn_ret.mean()
class TURN_RET60(Factor):
name = 'turn_ret60'
max_window = 61
dependencies = ['volume', 'circulating_cap', 'close']
def calc(self, data):
volume = data['volume'] / 10000
turn = volume / data['circulating_cap']
turn = turn[1:]
close = data['close'].pct_change()[1:]
turn_ret = close * turn
return turn_ret.mean()
class TURN_RET120(Factor):
name = 'turn_ret120'
max_window = 121
dependencies = ['volume', 'circulating_cap', 'close']
def calc(self, data):
volume = data['volume'] / 10000
turn = volume / data['circulating_cap']
turn = turn[1:]
close = data['close'].pct_change()[1:]
turn_ret = close * turn
return turn_ret.mean()
class TURN_RET180(Factor):
name = 'turn_ret180'
max_window = 181
dependencies = ['volume', 'circulating_cap', 'close']
def calc(self, data):
volume = data['volume'] / 10000
turn = volume / data['circulating_cap']
turn = turn[1:]
close = data['close'].pct_change()[1:]
turn_ret = close * turn
return turn_ret.mean()
# return a panel of weights of these factors
stock_list = list(get_index_weights(data_input_user, date='a due date').index)
factor_data = get_factor_values(securities=stock_list,
factors=factors,
start_date=start_date_user, end_date=end_date_user)
df_factor = pd.DataFrame()
factor_name = list(factor_data.keys())
for name in factor_name:
df_factor = pd.concat([df_factor, factor_data[name]])
df_factor = df_factor.T
df_factor.columns = factor_name
# check if there are missing values
print(df_factor.isnull().sum())