-
Notifications
You must be signed in to change notification settings - Fork 4
/
run.py
315 lines (247 loc) · 10.2 KB
/
run.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
"""
run modules
major modules:
Factor Generation:
- 'gen': factor geneartor
- config in 'factor_generation/raw_factor/config.py'
Backtest:
- 'backtest_factor': factor backtest (continuous values on each cross section)
- config in 'backtest/configuration/config.py'
- 'backtest_signal': signal backtest (0, 1, -1 on each cross section)
- config in 'backtest/configuration/config.py'
Factor Combination
- 'comb': factor combination (ML)
- config in 'factor_combination/configuration/config.py'
Portfolio Optimization
- 'opt_fac_ret': generate risk factor returns
- config in 'portfolio_optimizer/config.py'
- 'opt_cov_est': covariance estimation
- config in 'portfolio_optimizer/config.py'
- 'opt_weight': optimize weight
- config in 'portfolio_optimizer/config.py'
Graph Clustering
- 'cluster_train': train graph clustering
- config in 'graph_cluster/config.py'
other modules:
- 'pairs': run pairs factor generation
- config in 'factor_generation/raw_factor/pairs_modified.py'
- 'gen_risk': risk factor generator
- config in 'factor_generation/raw_factor/style_factor_config.py'
"""
# load packages
import sys
import argparse
# =========================
# ------- module ----------
# =========================
# ----- factor gen -------
def generator_factor():
""" run factor generation """
from src.factor_generation.raw_factor.FactorGenerator import FactorGenerator
fg = FactorGenerator()
fg.run()
# ------ backtest --------
def backtest_factor():
""" run factor backtest """
from src.backtest.bin.batch_factor_test import run
run()
def backtest_signal():
""" run signal backtest """
from src.backtest.bin.batch_signal_test import run
run()
# ------ factor combination ------
def factor_combination():
""" run ml on factors """
from src.factor_combination.bins.ModelTrain import run
run()
# ----- portfolio optimization ------
def portfolio_optimization_fac_ret():
""" run factor return generation """
from src.portfolio_optimization.FactorReturnGenerator import FactorReturnGenerator
# accept arguments for meta control
parser = argparse.ArgumentParser(description='portfolio optimization factor return estimation config')
parser.add_argument('--start_date', default=None, help='start date to estimate returns')
parser.add_argument('--end_date', default=None, help='end date to estimate returns')
parser.add_argument('--use_dynamic_ind', type=bool, default=None, help='whether to use dynamic industry')
parser.add_argument('--dynamic_ind_name', default=None, help='name of the dynamic industry')
args, _ = parser.parse_known_args()
# init
loading_process = FactorReturnGenerator()
# change config
if not args.start_date is None:
loading_process.start_date = args.start_date
if not args.end_date is None:
loading_process.end_date = args.end_date
if not args.use_dynamic_ind is None:
loading_process.use_dynamic_ind = args.use_dynamic_ind
if not args.dynamic_ind_name is None:
loading_process.dynamic_ind_name = args.dynamic_ind_name
# run
loading_process.start_cal_return_process()
def portfolio_optimization_cov_est():
""" run covariance estimation """
from src.portfolio_optimization.CovMatrixEstimator import CovMatrixEstimator
# accept arguments for meta control
parser = argparse.ArgumentParser(description='portfolio optimization covariance estimation config')
parser.add_argument('--start_date', default=None, help='start date to estimate covariance')
parser.add_argument('--end_date', default=None, help='end date to estimate covariance')
parser.add_argument('--use_dynamic_ind', type=bool, default=None, help='whether to use dynamic industry')
parser.add_argument('--dynamic_ind_name', default=None, help='name of the dynamic industry')
args, _ = parser.parse_known_args()
# init
calculating_process = CovMatrixEstimator()
# change config
if not args.start_date is None:
calculating_process.start_date = args.start_date
if not args.end_date is None:
calculating_process.end_date = args.end_date
if not args.use_dynamic_ind is None:
calculating_process.use_dynamic_ind = args.use_dynamic_ind
if not args.dynamic_ind_name is None:
calculating_process.dynamic_ind_name = args.dynamic_ind_name
# run
calculating_process.start_cal_cov_process()
def portfolio_optimization_weight():
""" adjust weight """
from src.portfolio_optimization.WeightOptimizer import WeightOptimizer
# accept arguments for meta control
parser = argparse.ArgumentParser(description='portfolio optimization weight config')
parser.add_argument('--start_date', default=None, help='start date to adjust weight')
parser.add_argument('--end_date', default=None, help='end date to adjust weight')
parser.add_argument('--use_dynamic_ind', type=bool,
default=None, help='whether to use dynamic industry')
parser.add_argument('--dynamic_ind_name', default=None,
help='name of the dynamic industry')
parser.add_argument('--ind_low_limit', default=None, type=float, help='excess weight minimum industry exposure')
parser.add_argument('--ind_high_limit', default=None, type=float, help='excess weight maximum industry exposure')
args, _ = parser.parse_known_args()
# init
calculating_process = WeightOptimizer()
# change config
if not args.start_date is None:
calculating_process.start_date = args.start_date
if not args.end_date is None:
calculating_process.end_date = args.end_date
if not args.use_dynamic_ind is None:
calculating_process.use_dynamic_ind = args.use_dynamic_ind
if not args.dynamic_ind_name is None:
calculating_process.dynamic_ind_name = args.dynamic_ind_name
if not args.ind_low_limit is None:
calculating_process.ind_low_limit = args.ind_low_limit
if not args.ind_high_limit is None:
calculating_process.ind_high_limit = args.ind_high_limit
# run
calculating_process.start_weight_optimize_process()
# ----- graph clustering ------
def graph_clustering_train():
""" train graph clusters """
from src.graph_cluster.IndustryTrainer import IndustryTrainer, MultiIndustryTrainer
parser = argparse.ArgumentParser(description="graph clustering config")
# single
parser.add_argument('--graph_type', default=None, choices=['AG', 'MST', 'PMFG'], help='type of graph')
parser.add_argument('--num_clusters', type=int, default=None, help='number of clusters')
parser.add_argument('--clustering_type', default=None,
choices=['single_linkage', 'spectral', 'node2vec', 'sub2vec'], help='type of clustering')
parser.add_argument('--filter_mode', default=None, type=int, choices=[0, 1, 2], help='filter noise mode')
# multi
parser.add_argument('--use_multi', type=bool, default=False, choices=[True, False],
help='whether to train multiple labels at a time')
parser.add_argument('--multi_num_clusters',
type=int,
nargs='+',
default=[5, 10, 20, 30, 40, 60],
help='multiple num clusters'
)
parser.add_argument('--multi_clustering_type',
type=str,
nargs='+',
default=['spectral', 'node2vec', 'sub2vec'],
help='multiple clustering type'
)
args, _ = parser.parse_known_args()
if not args.use_multi:
industry_trainer = IndustryTrainer(
graph_type=args.graph_type,
num_clusters=args.num_clusters,
clustering_type=args.clustering_type,
filter_mode=args.filter_mode
)
industry_trainer.run()
else:
industry_trainer = MultiIndustryTrainer(
graph_type=args.graph_type,
num_clusters=args.multi_num_clusters,
clustering_type=args.multi_clustering_type,
filter_mode=args.filter_mode
)
industry_trainer.run()
# =======================
# ------ others ---------
# =======================
def run_pairs():
"""
run pairs factor generation
"""
from src.factor_generation.raw_factor.pairs_modified import run
run()
def run_risk_factor_gen():
"""
generate risk factors
"""
from src.factor_generation.raw_factor.StyleFactorGenerator import StyleFactorGenerator
loading_process = StyleFactorGenerator()
loading_process.start_loading_data_process()
# =======================
# ------ main -----------
# =======================
def main(targets):
"""
run modules
"""
# --------- main modules ----------
# factor generator
if 'gen' in targets:
generator_factor()
# backtest
elif 'backtest_factor' in targets:
backtest_factor()
elif 'backtest_signal' in targets:
backtest_signal()
# ml factor combination
elif 'comb' in targets:
factor_combination()
# Markowitz portfolio optimization
elif 'opt_cov_est' in targets:
portfolio_optimization_cov_est()
elif 'opt_fac_ret' in targets:
portfolio_optimization_fac_ret()
elif 'opt_weight' in targets:
portfolio_optimization_weight()
# graph clustering
elif 'cluster_train' in targets:
graph_clustering_train()
# ---------- side modules ------------
# pairs
elif 'pairs' in targets:
run_pairs()
elif 'gen_risk' in targets:
run_risk_factor_gen()
else:
raise NotImplementedError(
'Target not Found / Module not Defined. Please pick from the following modes: \n' +
'\t{}\n\t{}\n\t{}\n\t{}\n\t{}\n\t{}\n\t{}\n\t{}\n\t{}\n'.format(
'gen',
'backtest_factor',
'backtest_signal',
'comb',
'opt_cov_est',
'opt_fac_ret',
'opt_weight',
'cluster_train',
'pairs',
'gen_risk'
)
)
if __name__ == '__main__':
targets = sys.argv[1:]
main(targets)