-
Notifications
You must be signed in to change notification settings - Fork 11
/
factors_analysis.py
87 lines (68 loc) · 2.81 KB
/
factors_analysis.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
import json
import os
import sys, getopt
import datetime
sys.path.append(os.path.abspath('./efinance'))
# sys.path.append(r'../')
from stock_ai.trade_ratio_ml import trade_ratio_ml
from ultility.common_def import *
from ultility.common_func import *
from ultility.stock_codes_utility import *
def trade_ratio_k_mean(conf, stock_codes, trade_ratio_file, kmean_trade_ratio_file):
common_conf = conf['common']
path = common_conf['path']
folder = common_conf["folder"]
trade_conf = conf['trade']
path_in = os.path.join(path, folder['process_trade'])
path_out = os.path.join(path, folder['process_analyse'])
n_clusters = trade_conf['n_clusters']
trade_ratio = trade_ratio_ml(stock_codes, path_in, path_out, trade_ratio_file)
trade_ratio.kmean(n_clusters, kmean_trade_ratio_file)
def trade_kmean(conf):
trade_conf = conf['trade']
common_conf = conf['common']
path = common_conf['path']
folder = common_conf["folder"]
finance_conf = conf['finance']
# stock analyse
scu = stock_codes_utility(type_data = CONST_DEF.TYPE_STOCK)
stock_codes = scu.stock_codes_from_table()
csv_finance_factor = os.path.join(path, folder['finance_rank'], finance_conf['result_name'])
pd1 = pd.read_csv(csv_finance_factor, encoding='gbk')
kmnean_stock_num = trade_conf['kmnean_stock_num']
stock_codes = pd1.iloc[1:kmnean_stock_num, 0].apply(lambda x: x[1:])
trade_ratio_file = trade_conf['stock_trade_ratio_file']
kmean_trade_ratio_file = trade_conf['stock_kmean_trade_ratio_file']
trade_ratio_k_mean(conf, stock_codes, trade_ratio_file, kmean_trade_ratio_file)
#index analyse
scu = stock_codes_utility(type_data = CONST_DEF.TYPE_INDEX)
stock_codes = scu.stock_codes_from_table()
trade_ratio_file = trade_conf['index_trade_ratio_file']
kmean_trade_ratio_file = trade_conf['index_kmean_trade_ratio_file']
trade_ratio_k_mean(conf, stock_codes, trade_ratio_file, kmean_trade_ratio_file)
#block analyse
scu = stock_codes_utility()
stock_codes = scu.block_codes_from_eastmoney()
trade_ratio_file = trade_conf['block_trade_ratio_file']
kmean_trade_ratio_file = trade_conf['block_kmean_trade_ratio_file']
trade_ratio_k_mean(conf, stock_codes, trade_ratio_file, kmean_trade_ratio_file)
if __name__ == '__main__':
# default configure file name
filename = './conf/conf.json'
# default finance analysis
trade_flag = False
# tradeflag = True
try:
opts, args = getopt.getopt(sys.argv[1:], "f:p:t:", ["filename=", "path=", "tradeflag="])
except getopt.GetoptError:
print('test.py -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('python3 finance_stock_basic_proc.py -f conf.json')
elif opt in ("-f", "--filename"):
filename = arg
elif opt in ("-t", "--tradeflag"):
trade_flag = True
conf = common_func.read_config(filename)
trade_kmean(conf)