yufeng 3 years ago
parent
commit
573ffcf517

BIN
mix/5d_578A_dnn_seq.h5


BIN
mix/5d_578B_dnn_seq.h5


+ 103 - 0
mix/mix_predict_by_day_578A.py

@@ -0,0 +1,103 @@
1
+# -*- encoding:utf-8 -*-
2
+import numpy as np
3
+from keras.models import load_model
4
+import joblib
5
+
6
+
7
+def read_data(path):
8
+    day_lines = {}
9
+    with open(path) as f:
10
+        for line in f.readlines()[:]:
11
+            line = eval(line.strip())
12
+            date = str(line[-1][-1])
13
+            if date in day_lines:
14
+                day_lines[date].append(line)
15
+            else:
16
+                day_lines[date] = [line]
17
+    # print(len(day_lines['20191230']))
18
+    return day_lines
19
+
20
+
21
+def predict(file_path='', model_path='15min_dnn_seq', rows=18, cols=18):
22
+    day_lines = read_data(file_path)
23
+    print('数据读取完毕')
24
+
25
+    model=load_model(model_path + '.h5')
26
+    print('模型加载完毕')
27
+
28
+    items = sorted(day_lines.keys())
29
+    for key in items:
30
+        # print(day)
31
+        lines = day_lines[key]
32
+
33
+        up_num = 0
34
+        down_num = 0
35
+        size = len(lines[0])
36
+        x0 = 0
37
+        x1 = 0
38
+        x2 = 0
39
+        x3 = 0
40
+        x4 = 0
41
+
42
+        for line in lines:
43
+            train_x = np.array([line[:size - 1]])
44
+            train_x_a = train_x[:,:rows*cols]
45
+            train_x_a = train_x_a.reshape(train_x.shape[0], rows, cols, 1)
46
+            # train_x_b = train_x[:, 18*18:18*18+2*18]
47
+            # train_x_b = train_x_b.reshape(train_x.shape[0], 18, 2, 1)
48
+            train_x_c = train_x[:,rows*cols:]
49
+
50
+            result = model.predict([train_x_c, train_x_a])
51
+
52
+            ratio = 1
53
+            if train_x_c[0][-1] == 1:
54
+                ratio = 2
55
+            elif train_x_c[0][-2] == 1:
56
+                ratio = 1.6
57
+            elif train_x_c[0][-3] == 1:
58
+                ratio = 1.3
59
+
60
+            if result[0][0]> 0.5:
61
+                up_num = up_num + ratio
62
+            elif result[0][1] > 0.5:
63
+                up_num = up_num + 0.4*ratio
64
+            elif result[0][2] > 0.5:
65
+                down_num = down_num + 0.4*ratio
66
+            else:
67
+                down_num = down_num + ratio
68
+
69
+            maxx = max(result[0])
70
+            if maxx - result[0][0] == 0:
71
+                x0 = x0 + 1
72
+            if maxx - result[0][1] == 0:
73
+                x1 = x1 + 1
74
+            if maxx - result[0][2] == 0:
75
+                x2 = x2 + 1
76
+            if maxx - result[0][3] == 0:
77
+                x3 = x3 + 1
78
+
79
+        # print(key, int(up_num), int(down_num), (down_num*1.2 + 2)/(up_num*1.2 + 2), )
80
+        print(key, x0, x1, x2,x3, (down_num*1.5 + 2)/(up_num*1.2 + 2))
81
+
82
+
83
+import datetime
84
+if __name__ == '__main__':
85
+    today = datetime.datetime.now()
86
+    today = today
87
+    today =  today.strftime('%Y%m%d')
88
+    # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5')
89
+    # predict(file_path='D:\\data\\quantization\\stock9_18_20200220.log', model_path='18d_dnn_seq.h5')
90
+    # predict(file_path='D:\\data\\quantization\\stock9_18_2.log', model_path='18d_dnn_seq.h5')
91
+    # predict(file_path='D:\\data\\quantization\\stock16_18d_20200310.log', model_path='16_18d_mix_seq')
92
+    # predict(file_path='D:\\data\\quantization\\stock196_18d_20200326.log', model_path='196_18d_mix_6D_ma5_s_seq')
93
+
94
+
95
+    # predict(file_path='D:\\data\\quantization\\stock321_28d_5D_20200429.log', model_path='321_28d_mix_5D_ma5_s_seq_2', rows=28, cols=20)
96
+
97
+    predict(file_path='D:\\data\\quantization\\stock327_28d_' + today + '.log', model_path='327_28d_mix_5D_ma5_s_seq', rows=28, cols=20)
98
+
99
+
100
+
101
+    # predict(file_path='D:\\data\\quantization\\stock9_18_4.log', model_path='18d_dnn_seq.h5')
102
+    # predict(file_path='D:\\data\\quantization\\stock324_28d_3D_20200414_A.log', model_path='324_28d_mix_5D_ma5_s_seq', rows=28, cols=18)
103
+    # predict(file_path='D:\\data\\quantization\\stock324_28d_3D_20200414_A.log', model_path='603_30d_mix_5D_ma5_s_seq', rows=30, cols=19)

+ 94 - 0
mix/mix_predict_by_day_578B.py

@@ -0,0 +1,94 @@
1
+# -*- encoding:utf-8 -*-
2
+import numpy as np
3
+from keras.models import load_model
4
+import joblib
5
+
6
+
7
+def read_data(path):
8
+    day_lines = {}
9
+    with open(path) as f:
10
+        for line in f.readlines()[:]:
11
+            line = eval(line.strip())
12
+            date = str(line[-2][-1])
13
+            if date in day_lines:
14
+                day_lines[date].append(line)
15
+            else:
16
+                day_lines[date] = [line]
17
+    # print(len(day_lines['20191230']))
18
+    return day_lines
19
+
20
+
21
+def predict(file_path='', model_path='15min_dnn_seq', rows=18, cols=18):
22
+    day_lines = read_data(file_path)
23
+    print('数据读取完毕')
24
+
25
+    model=load_model(model_path + '.h5')
26
+    print('模型加载完毕')
27
+
28
+    items = sorted(day_lines.keys())
29
+    for key in items:
30
+        # print(day)
31
+        lines = day_lines[key]
32
+
33
+        up_num = 0
34
+        down_num = 0
35
+        size = len(lines[0])
36
+        x0 = 0
37
+        x1 = 0
38
+        x2 = 0
39
+        x3 = 0
40
+        x4 = 0
41
+
42
+        for line in lines:
43
+            train_x = np.array([line[:size - 2]])
44
+
45
+            result = model.predict([train_x])
46
+
47
+            ratio = 1
48
+            if train_x[0][-1] == 1:
49
+                ratio = 2
50
+            elif train_x[0][-2] == 1:
51
+                ratio = 1.6
52
+            elif train_x[0][-3] == 1:
53
+                ratio = 1.3
54
+
55
+            if result[0][0]> 0.5:
56
+                up_num = up_num + ratio
57
+            elif result[0][1] > 0.5:
58
+                up_num = up_num + 0.4*ratio
59
+            elif result[0][2] > 0.5:
60
+                down_num = down_num + ratio
61
+
62
+            maxx = max(result[0])
63
+            if maxx - result[0][0] == 0:
64
+                x0 = x0 + 1
65
+            elif maxx - result[0][1] == 0:
66
+                x1 = x1 + 1
67
+            else:
68
+                x2 = x2 + 1
69
+
70
+        # print(key, int(up_num), int(down_num), (down_num*1.2 + 2)/(up_num*1.2 + 2), )
71
+        print(key, x0, x1, x2, (down_num*1.5 + 2)/(up_num*1.2 + 2))
72
+
73
+
74
+import datetime
75
+if __name__ == '__main__':
76
+    today = datetime.datetime.now()
77
+    today = today
78
+    today =  '' #today.strftime('%Y%m%d')
79
+    # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5')
80
+    # predict(file_path='D:\\data\\quantization\\stock9_18_20200220.log', model_path='18d_dnn_seq.h5')
81
+    # predict(file_path='D:\\data\\quantization\\stock9_18_2.log', model_path='18d_dnn_seq.h5')
82
+    # predict(file_path='D:\\data\\quantization\\stock16_18d_20200310.log', model_path='16_18d_mix_seq')
83
+    # predict(file_path='D:\\data\\quantization\\stock196_18d_20200326.log', model_path='196_18d_mix_6D_ma5_s_seq')
84
+
85
+
86
+    predict(file_path='D:\\data\\quantization\\stock578A_12d_train3.log', model_path='5d_578A_dnn_seq', rows=28, cols=20)
87
+
88
+    # predict(file_path='D:\\data\\quantization\\stock578A_12d_' + today + '.log', model_path='5d_578A_dnn_seq', rows=28, cols=20)
89
+
90
+
91
+
92
+    # predict(file_path='D:\\data\\quantization\\stock9_18_4.log', model_path='18d_dnn_seq.h5')
93
+    # predict(file_path='D:\\data\\quantization\\stock324_28d_3D_20200414_A.log', model_path='324_28d_mix_5D_ma5_s_seq', rows=28, cols=18)
94
+    # predict(file_path='D:\\data\\quantization\\stock324_28d_3D_20200414_A.log', model_path='603_30d_mix_5D_ma5_s_seq', rows=30, cols=19)

+ 20 - 57
mix/mix_predict_everyday_600.py

@@ -35,16 +35,16 @@ def predict_today(file, day, model='10_18d', log=True):
35 35
 
36 36
     for line in lines:
37 37
         train_x = np.array([line[:size - 1]])
38
-        train_x_tmp = train_x[:,:30*19]
39
-        train_x_a = train_x_tmp.reshape(train_x.shape[0], 30, 19, 1)
40
-        # train_x_b = train_x_tmp.reshape(train_x.shape[0], 18, 24)
41
-        train_x_c = train_x[:,30*19:]
38
+        # train_x_tmp = train_x[:,:30*19]
39
+        # train_x_a = train_x_tmp.reshape(train_x.shape[0], 30, 19, 1)
40
+        # # train_x_b = train_x_tmp.reshape(train_x.shape[0], 18, 24)
41
+        # train_x_c = train_x[:,30*19:]
42 42
 
43
-        result = model.predict([train_x_c, train_x_a, ])
43
+        result = model.predict([train_x, ])
44 44
         # print(result, line[-1])
45 45
         stock = code_table.find_one({'ts_code':line[-1][0]})
46 46
 
47
-        if result[0][0] > 0.5 and stock['sw_industry'] in industry_list:
47
+        if result[0][0] > 0.85:
48 48
             if line[-1][0].startswith('688'):
49 49
                 continue
50 50
             # 去掉ST
@@ -66,27 +66,24 @@ def predict_today(file, day, model='10_18d', log=True):
66 66
 
67 67
             if stock['ts_code'] in zixuan_stock_list:
68 68
                 # print(line[-1], stock['name'], stock['sw_industry'], str(concept_detail_list), 'buy', k_table_list[0]['pct_chg'])
69
-                print(stock['ts_code'], stock['name'], '买入评级', k_table_list[0]['pct_chg'])
70
-                Z_list.append([stock['name'], stock['sw_industry'], k_table_list[0]['pct_chg']])
71
-            elif stock['ts_code'] in ROE_stock_list:
72
-                print(stock['ts_code'], stock['name'], '买入评级', k_table_list[0]['pct_chg'])
73
-                R_list.append([stock['name'], stock['sw_industry'], k_table_list[0]['pct_chg']])
69
+                print(stock['ts_code'], stock['name'], '买入评级', result[0][0])
70
+                Z_list.append([stock['name'], stock['sw_industry'], result[0][0]])
71
+            elif stock['ts_code'] in ROE_stock_list or stock['ts_code'] in zeng_stock_list:
72
+                print(stock['ts_code'], stock['name'], '买入评级', result[0][0])
73
+                R_list.append([stock['name'], stock['sw_industry'], result[0][0]])
74 74
             else:
75
-                O_list.append([stock['name'], stock['sw_industry'], k_table_list[0]['pct_chg']])
75
+                O_list.append([stock['name'], stock['sw_industry'], result[0][0]])
76 76
 
77 77
             if log is True:
78 78
                 with open('D:\\data\\quantization\\predict\\' + str(day) + '_mix.txt', mode='a', encoding="utf-8") as f:
79 79
                     f.write(str(line[-1]) + ' ' + stock['name'] + ' ' + stock['sw_industry'] + ' ' + str(concept_detail_list) + ' ' + str(result[0][0]) + '\n')
80 80
 
81
-        # elif result[0][1] > 0.5:
82
-        #     if stock['ts_code'] in holder_stock_list:
83
-        #         print(stock['ts_code'], stock['name'], '震荡评级')
84
-        # elif result[0][2] > 0.4:
85
-        #     if stock['ts_code'] in holder_stock_list:
86
-        #         print(stock['ts_code'], stock['name'], '赶紧卖出')
87
-        # else:
88
-        #     if stock['ts_code'] in holder_stock_list or stock['ts_code'] in ROE_stock_list:
89
-        #         print(stock['ts_code'], stock['name'], result[0],)
81
+        elif result[0][1] > 0.5:
82
+            if stock['ts_code'] in holder_stock_list:
83
+                print(stock['ts_code'], stock['name'], '震荡评级')
84
+        elif result[0][2] > 0.5:
85
+            if stock['ts_code'] in holder_stock_list:
86
+                print(stock['ts_code'], stock['name'], '赶紧卖出')
90 87
 
91 88
     # print(gainian_map)
92 89
     # print(hangye_map)
@@ -121,46 +118,12 @@ def predict_today(file, day, model='10_18d', log=True):
121 118
     print(O_list[:3])
122 119
 
123 120
 
124
-def _read_pfile_map(path):
125
-    s_list = []
126
-    with open(path, encoding='utf-8') as f:
127
-        for line in f.readlines()[:]:
128
-            s_list.append(line)
129
-    return s_list
130
-
131
-
132
-def join_two_day(a, b):
133
-    a_list = _read_pfile_map('D:\\data\\quantization\\predict\\' + str(a) + '.txt')
134
-    b_list = _read_pfile_map('D:\\data\\quantization\\predict\\dmi_' + str(b) + '.txt')
135
-    for a in a_list:
136
-        for b in b_list:
137
-            if a[2:11] == b[2:11]:
138
-                print(a)
139
-
140
-
141
-def check_everyday(day, today):
142
-    a_list = _read_pfile_map('D:\\data\\quantization\\predict\\' + str(day) + '.txt')
143
-    x = 0
144
-    for a in a_list:
145
-        print(a[:-1])
146
-        k_day_list = list(k_table.find({'code':a[2:11], 'tradeDate':{'$lte':int(today)}}).sort('tradeDate', pymongo.DESCENDING).limit(5))
147
-        if k_day_list is not None and len(k_day_list) > 0:
148
-            k_day = k_day_list[0]
149
-            k_day_0 = k_day_list[-1]
150
-            k_day_last = k_day_list[1]
151
-            if ((k_day_last['close'] - k_day_0['pre_close'])/k_day_0['pre_close']) < 0.2:
152
-                print(k_day['open'], k_day['close'], 100*(k_day['close'] - k_day_last['close'])/k_day_last['close'])
153
-                x = x + 100*(k_day['close'] - k_day_last['close'])/k_day_last['close']
154
-
155
-    print(x/len(a_list))
156
-
157
-
158 121
 if __name__ == '__main__':
159 122
     # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5')
160 123
     # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
161 124
     # multi_predict()
162 125
     # predict_today("D:\\data\\quantization\\stock405_30d_20200413.log", 20200413, model='405_30d_mix_5D_ma5_s_seq.h5', log=True)
163
-    # 模型A
164
-    predict_today("D:\\data\\quantization\\stock603_30d_20200415.log", 20200415, model='603_30d_mix_5D_ma5_s_seq.h5', log=True)
126
+    # 模型 下跌趋势,没大涨的不要买
127
+    predict_today("D:\\data\\quantization\\stock578A_5d_20200430.log", 20200430, model='5d_578A_dnn_seq.h5', log=True)
165 128
     # join_two_day(20200305, 20200305)
166 129
     # check_everyday(20200311, 20200312)

+ 2 - 2
stock/dnn_predict.py

@@ -31,7 +31,7 @@ def predict(file_path='', model_path='15min_dnn_seq.h5'):
31 31
     with open('dnn_predict_5d.txt', 'a') as f:
32 32
         for r in result:
33 33
             fact = test_y[i]
34
-            if r[0] > 0.7:
34
+            if r[0] > 0.8:
35 35
                 # f.write(str([lines[i][-2], lines[i][-1]]) + "\n")
36 36
                 # win_dnn.append([lines[i][-2], lines[i][-1]])
37 37
                 if fact[0] == 1:
@@ -74,5 +74,5 @@ def multi_predict():
74 74
 
75 75
 if __name__ == '__main__':
76 76
     # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5')
77
-    predict(file_path='D:\\data\\quantization\\stock571_12d_train1.log', model_path='5d_571_dnn_seq.h5')
77
+    predict(file_path='D:\\data\\quantization\\stock578A_12d_train3.log', model_path='5d_578A_dnn_seq.h5')
78 78
     # multi_predict()

+ 24 - 11
stock/dnn_train.py

@@ -15,11 +15,13 @@ early_stopping = EarlyStopping(monitor='accuracy', patience=5, verbose=2)
15 15
 def read_data(path):
16 16
     lines = []
17 17
     with open(path) as f:
18
-        # for x in range(30000):
19
-        # lines.append(eval(f.readline().strip()))
20 18
         for line in f.readlines()[:]:
21 19
             lines.append(eval(line.strip()))
22 20
 
21
+    # with open("D:\\data\\quantization\\stock578A_12d_train1.log") as f:
22
+    #     for line in f.readlines()[:]:
23
+    #         lines.append(eval(line.strip()))
24
+
23 25
     random.shuffle(lines)
24 26
     print('读取数据完毕')
25 27
 
@@ -73,15 +75,15 @@ def train(result_class=3, file_path="D:\\data\\quantization\\stock6.log", model_
73 75
 
74 76
     input_dim = train_x.shape[1]
75 77
     model = Sequential()
76
-    model.add(Dense(units=220+input_dim, input_dim=input_dim,  activation='relu'))
77
-    model.add(Dense(units=220+input_dim, activation='relu',kernel_regularizer=regularizers.l1(0.001)))
78
-    model.add(Dense(units=220+input_dim, activation='relu'))
78
+    model.add(Dense(units=320+input_dim, input_dim=input_dim,  activation='relu'))
79
+    model.add(Dense(units=320+input_dim, activation='relu',kernel_regularizer=regularizers.l1(0.001)))
80
+    model.add(Dense(units=320+input_dim, activation='relu'))
79 81
     model.add(Dropout(0.1))
80 82
     # model.add(Dense(units=220 + input_dim, activation='relu'))
81 83
     # model.add(Dropout(0.1))
82
-    model.add(Dense(units=220+input_dim, activation='selu'))
84
+    model.add(Dense(units=320+input_dim, activation='selu'))
83 85
     model.add(Dropout(0.1))
84
-    # model.add(Dense(units=220+input_dim, activation='selu'))
86
+    model.add(Dense(units=320+input_dim, activation='selu'))
85 87
     # model.add(Dropout(0.1))
86 88
     model.add(Dense(units=512, activation='relu'))
87 89
 
@@ -90,7 +92,7 @@ def train(result_class=3, file_path="D:\\data\\quantization\\stock6.log", model_
90 92
 
91 93
     print("Starting training ")
92 94
     # model.fit(train_x, train_y, batch_size=1024, epochs=400 + 4*int(len(train_x)/1000), shuffle=True)
93
-    model.fit(train_x, train_y, batch_size=4096, epochs=128, shuffle=True, callbacks=[early_stopping])
95
+    model.fit(train_x, train_y, batch_size=4096*2, epochs=128, shuffle=True, callbacks=[early_stopping])
94 96
     score = model.evaluate(test_x, test_y)
95 97
     print(score)
96 98
     print('Test score:', score[0])
@@ -114,13 +116,24 @@ def train(result_class=3, file_path="D:\\data\\quantization\\stock6.log", model_
114 116
 565 6d input_dim=26  
115 117
 566 6d input_dim=26  42,98,57  随机44,97,60
116 118
 567 2d input_dim=70  >0.9 30,100,51  随机44,97,60
117
-568 3d-3 input_dim=  >0.7 35,97,64
119
+
120
+568 3d-3 input_dim=  >0.7 35,97,64   
118 121
 569 4d-3 input_dim=  >0.7 33,100,52   >0.9 33,100.3,50
119
-570 5d-3 input_dim=  >0.9 32,102,42   
122
+570 5d-3 input_dim=  >0.9 32,102,42   随机45,99,56
120 123
 571 6d-3 >0.7 37,100,48
124
+572 5d-3 + macd+roc 37,101,46
125
+573 +index  32,101,44
126
+574 modify  35,101.9,43
127
+575 modify  34,101,45
128
+576 去掉index 35,101,47
129
+577 加上r0_ratio 34,100,48
130
+578 570修正 30,101,47 
131
+578A 38,101.7 44
132
+578B 4class 38,101.7 44
133
+574A >0.8 34,98,59
121 134
 '''
122 135
 if __name__ == '__main__':
123 136
     # train(input_dim=176, result_class=5, file_path="D:\\data\\quantization\\stock6_5.log", model_name='5d_dnn_seq.h5')
124
-    train(result_class=3, file_path="D:\\data\\quantization\\stock571_12d_train2.log", model_name='5d_571_dnn_seq.h5')
137
+    train(result_class=4, file_path="D:\\data\\quantization\\stock578B_12d_train2.log", model_name='5d_578B_dnn_seq.h5')
125 138
     # resample('D:\\data\\quantization\\stock8_14.log')
126 139
     # mul_train()