yufeng 4 years ago
parent
commit
23362693b3

mix/216_18d_mix_6D_ma5_s_seq.h5 → mix/216_18d_mix_5D_ma5_s_seq.h5


mix/321_28d_mix_6D_ma5_s_seq.h5 → mix/321_28d_mix_5D_ma5_s_seq.h5


+ 1 - 1
mix/mix_predict_190.py

@@ -28,7 +28,7 @@ def _score(fact, line):
28 28
         up_right = up_right + 1.04
29 29
     elif fact[1] == 1:
30 30
         up_right = up_right + 1
31
-    elif fact[1] == 2:
31
+    elif fact[2] == 1:
32 32
         up_right = up_right + 0.96
33 33
         up_error = up_error + 0.3
34 34
     else:

+ 15 - 15
mix/mix_predict_200.py

@@ -18,30 +18,31 @@ def read_data(path):
18 18
 
19 19
 
20 20
 def _score(fact, line):
21
+    with open('dnn_predict_dmi_18d.txt', 'a') as f:
22
+        f.write(str([line[-2], line[-1]]) + "\n")
23
+
21 24
     up_right = 0
22 25
     up_error = 0
23 26
 
24 27
     if fact[0] == 1:
25 28
         up_right = up_right + 1.04
26 29
     elif fact[1] == 1:
27
-        up_right = up_right + 1
28
-    elif fact[1] == 2:
29
-        up_right = up_right + 0.96
30 30
         up_error = up_error + 0.3
31
+        up_right = up_right + 0.98
31 32
     else:
32 33
         up_error = up_error + 1
33
-        up_right = up_right + 0.90
34
+        up_right = up_right + 0.94
34 35
     return up_right,up_error
35 36
 
36 37
 
37
-def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1, row=18, col=20):
38
+def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1, row=18, col=18):
38 39
     test_x,test_y,lines=read_data(file_path)
39 40
 
40
-    test_x_a = test_x[:,:row*col]
41
-    test_x_a = test_x_a.reshape(test_x.shape[0], row, col, 1)
41
+    test_x_a = test_x[:,:18*20]
42
+    test_x_a = test_x_a.reshape(test_x.shape[0], 18, 20, 1)
42 43
     # test_x_b = test_x[:, 9*26:9*26+9*26]
43 44
     # test_x_b = test_x_b.reshape(test_x.shape[0], 9, 26, 1)
44
-    test_x_c = test_x[:,row*col:]
45
+    test_x_c = test_x[:,18*20:]
45 46
 
46 47
     model=load_model(model_path)
47 48
     score = model.evaluate([test_x_c, test_x_a,], test_y)
@@ -54,7 +55,7 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1, row=18, col=20)
54 55
     down_error = 0
55 56
     down_right = 0
56 57
     i = 0
57
-    result = model.predict([test_x_c, test_x_a])
58
+    result = model.predict([test_x_c, test_x_a,])
58 59
     win_dnn = []
59 60
     for r in result:
60 61
         fact = test_y[i]
@@ -63,7 +64,7 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1, row=18, col=20)
63 64
             if r[0] > 0.5 or r[1] > 0.5:
64 65
                 pass
65 66
         else:
66
-            if r[0] > 0.7:
67
+            if r[0] > 0.6:
67 68
                 tmp_right,tmp_error = _score(fact, lines[i])
68 69
                 up_right = tmp_right + up_right
69 70
                 up_error = tmp_error + up_error
@@ -71,12 +72,12 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1, row=18, col=20)
71 72
             elif r[2] > 0.6:
72 73
                 if fact[0] == 1:
73 74
                     down_error = down_error + 1
74
-                    down_right = down_right + 1.1
75
+                    down_right = down_right + 1.04
75 76
                 elif fact[1] == 1:
76
-                    down_error = down_error + 0.2
77
+                    down_error = down_error + 0.3
77 78
                     down_right = down_right + 0.99
78 79
                 else:
79
-                    down_right = down_right + 0.88
80
+                    down_right = down_right + 0.92
80 81
                 down_num = down_num + 1
81 82
 
82 83
         i = i + 1
@@ -90,8 +91,7 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1, row=18, col=20)
90 91
 
91 92
 if __name__ == '__main__':
92 93
     # predict(file_path='D:\\data\\quantization\\stock181_18d_test.log', model_path='181_18d_mix_6D_ma5_s_seq.h5')
93
-    predict(file_path='D:\\data\\quantization\\stock217_18d_train1.log', model_path='217_18d_mix_5D_ma5_s_seq.h5', row=18, col=18)
94
-    # predict(file_path='D:\\data\\quantization\\stock321_28d_train1.log', model_path='321_28d_mix_6D_ma5_s_seq.h5', row=28, col=20)
94
+    predict(file_path='D:\\data\\quantization\\stock216_18d_train1.log', model_path='216_18d_mix_5D_ma5_s_seq.h5', row=18, col=18)
95 95
     # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
96 96
     # multi_predict(model='15_18d')
97 97
     # predict_today(20200229, model='11_18d')

+ 17 - 16
mix/mix_predict_180.py

@@ -18,34 +18,34 @@ def read_data(path):
18 18
 
19 19
 
20 20
 def _score(fact, line):
21
-    with open('dnn_predict_dmi_18d.txt', 'a') as f:
22
-        f.write(str([line[-2], line[-1]]) + "\n")
23
-
24 21
     up_right = 0
25 22
     up_error = 0
26 23
 
27 24
     if fact[0] == 1:
28
-        up_right = up_right + 1.1
25
+        up_right = up_right + 1.04
29 26
     elif fact[1] == 1:
30
-        up_right = up_right + 0.99
31
-        up_error = up_error + 0.2
27
+        up_right = up_right + 1
28
+        up_error = up_error + 0.1
29
+    elif fact[2] == 1:
30
+        up_right = up_right + 0.96
31
+        up_error = up_error + 0.5
32 32
     else:
33 33
         up_error = up_error + 1
34 34
         up_right = up_right + 0.90
35 35
     return up_right,up_error
36 36
 
37 37
 
38
-def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
38
+def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1, row=18, col=20):
39 39
     test_x,test_y,lines=read_data(file_path)
40 40
 
41
-    test_x_a = test_x[:,:18*18]
42
-    test_x_a = test_x_a.reshape(test_x.shape[0], 18, 18, 1)
43
-    # test_x_b = test_x[:, 9*26:9*26+9*26]
44
-    # test_x_b = test_x_b.reshape(test_x.shape[0], 9, 26, 1)
45
-    test_x_c = test_x[:,18*18:]
41
+    test_x_a = test_x[:,:row*col]
42
+    test_x_a = test_x_a.reshape(test_x.shape[0], row, col, 1)
43
+    # test_x_b = test_x[:, row*col:row*col+18*2]
44
+    # test_x_b = test_x_b.reshape(test_x.shape[0], 18, 2, 1)
45
+    test_x_c = test_x[:,row*col:]
46 46
 
47 47
     model=load_model(model_path)
48
-    score = model.evaluate([test_x_c, test_x_a,], test_y)
48
+    score = model.evaluate([test_x_c, test_x_a, ], test_y)
49 49
     print('MIX', score)
50 50
 
51 51
     up_num = 0
@@ -55,7 +55,7 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
55 55
     down_error = 0
56 56
     down_right = 0
57 57
     i = 0
58
-    result = model.predict([test_x_c, test_x_a,])
58
+    result = model.predict([test_x_c, test_x_a, ])
59 59
     win_dnn = []
60 60
     for r in result:
61 61
         fact = test_y[i]
@@ -64,7 +64,7 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
64 64
             if r[0] > 0.5 or r[1] > 0.5:
65 65
                 pass
66 66
         else:
67
-            if r[0] > 0.8:
67
+            if r[0] > 0.7:
68 68
                 tmp_right,tmp_error = _score(fact, lines[i])
69 69
                 up_right = tmp_right + up_right
70 70
                 up_error = tmp_error + up_error
@@ -91,7 +91,8 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
91 91
 
92 92
 if __name__ == '__main__':
93 93
     # predict(file_path='D:\\data\\quantization\\stock181_18d_test.log', model_path='181_18d_mix_6D_ma5_s_seq.h5')
94
-    predict(file_path='D:\\data\\quantization\\stock186C_18d_train1.log', model_path='186C_18d_mix_6D_ma5_s_seq.h5')
94
+    # predict(file_path='D:\\data\\quantization\\stock217_18d_train1.log', model_path='218_18d_mix_5D_ma5_s_seq.h5', row=18, col=18)
95
+    predict(file_path='D:\\data\\quantization\\stock321_28d_test.log', model_path='321_28d_mix_5D_ma5_s_seq.h5', row=28, col=20)
95 96
     # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
96 97
     # multi_predict(model='15_18d')
97 98
     # predict_today(20200229, model='11_18d')

+ 96 - 0
mix/mix_predict_400.py

@@ -0,0 +1,96 @@
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
+    lines = []
9
+    with open(path) as f:
10
+        for line in f.readlines()[:]:
11
+            line = eval(line.strip())
12
+            lines.append(line)
13
+
14
+    size = len(lines[0])
15
+    train_x=[s[:size - 2] for s in lines]
16
+    train_y=[s[size-1] for s in lines]
17
+    return np.array(train_x, dtype=np.float32),np.array(train_y, dtype=np.float32),lines
18
+
19
+
20
+def _score(fact, line):
21
+    up_right = 0
22
+    up_error = 0
23
+
24
+    if fact[0] == 1:
25
+        up_right = up_right + 1.04
26
+    elif fact[1] == 1:
27
+        up_error = up_error + 0.3
28
+        up_right = up_right + 0.98
29
+    else:
30
+        up_error = up_error + 1
31
+        up_right = up_right + 0.94
32
+    return up_right,up_error
33
+
34
+
35
+def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1, row=18, col=20):
36
+    test_x,test_y,lines=read_data(file_path)
37
+
38
+    test_x_a = test_x[:,:row*col]
39
+    test_x_a = test_x_a.reshape(test_x.shape[0], row, col, 1)
40
+    # test_x_b = test_x[:, row*col:row*col+18*2]
41
+    # test_x_b = test_x_b.reshape(test_x.shape[0], 18, 2, 1)
42
+    test_x_c = test_x[:,row*col:]
43
+
44
+    model=load_model(model_path)
45
+    score = model.evaluate([test_x_c, test_x_a, ], test_y)
46
+    print('MIX', score)
47
+
48
+    up_num = 0
49
+    up_error = 0
50
+    up_right = 0
51
+    down_num = 0
52
+    down_error = 0
53
+    down_right = 0
54
+    i = 0
55
+    result = model.predict([test_x_c, test_x_a, ])
56
+    win_dnn = []
57
+    for r in result:
58
+        fact = test_y[i]
59
+
60
+        if idx in [-2]:
61
+            if r[0] > 0.5 or r[1] > 0.5:
62
+                pass
63
+        else:
64
+            if r[0] > 0.7:
65
+                tmp_right,tmp_error = _score(fact, lines[i])
66
+                up_right = tmp_right + up_right
67
+                up_error = tmp_error + up_error
68
+                up_num = up_num + 1
69
+            elif r[2] > 0.6:
70
+                if fact[0] == 1:
71
+                    down_error = down_error + 1
72
+                    down_right = down_right + 1.04
73
+                elif fact[1] == 1:
74
+                    down_error = down_error + 0.3
75
+                    down_right = down_right + 0.98
76
+                else:
77
+                    down_right = down_right + 0.92
78
+                down_num = down_num + 1
79
+
80
+        i = i + 1
81
+    if up_num == 0:
82
+        up_num = 1
83
+    if down_num == 0:
84
+        down_num = 1
85
+    print('MIX', up_right, up_num, up_right/up_num, up_error/up_num, down_right/down_num, down_error/down_num)
86
+    return win_dnn,up_right/up_num,down_right/down_num
87
+
88
+
89
+if __name__ == '__main__':
90
+    # predict(file_path='D:\\data\\quantization\\stock181_18d_test.log', model_path='181_18d_mix_6D_ma5_s_seq.h5')
91
+    # predict(file_path='D:\\data\\quantization\\stock217_18d_train1.log', model_path='218_18d_mix_5D_ma5_s_seq.h5', row=18, col=18)
92
+    # predict(file_path='D:\\data\\quantization\\stock400_18d_train1.log', model_path='400_18d_mix_5D_ma5_s_seq.h5', row=18, col=18)
93
+    predict(file_path='D:\\data\\quantization\\stock403_30d_train1.log', model_path='403_30d_mix_5D_ma5_s_seq_2.h5', row=30, col=20)
94
+    # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
95
+    # multi_predict(model='15_18d')
96
+    # predict_today(20200229, model='11_18d')

+ 100 - 0
mix/mix_predict_500.py

@@ -0,0 +1,100 @@
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
+    lines = []
9
+    with open(path) as f:
10
+        for line in f.readlines()[:]:
11
+            line = eval(line.strip())
12
+            lines.append(line)
13
+
14
+    size = len(lines[0])
15
+    train_x=[s[:size - 2] for s in lines]
16
+    train_y=[s[size-1] for s in lines]
17
+    return np.array(train_x, dtype=np.float32),np.array(train_y, dtype=np.float32),lines
18
+
19
+
20
+def _score(fact, line):
21
+    up_right = 0
22
+    up_error = 0
23
+
24
+    if fact[0] == 1:
25
+        up_right = up_right + 1.12
26
+    elif fact[1] == 1:
27
+        up_right = up_right + 1.04
28
+    elif fact[2] == 1:
29
+        up_error = up_error + 0.3
30
+        up_right = up_right + 0.98
31
+    else:
32
+        up_error = up_error + 1
33
+        up_right = up_right + 0.94
34
+    return up_right,up_error
35
+
36
+
37
+def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1, row=18, col=20):
38
+    test_x,test_y,lines=read_data(file_path)
39
+
40
+    test_x_a = test_x[:,:row*col]
41
+    test_x_a = test_x_a.reshape(test_x.shape[0], row, col, 1)
42
+    # test_x_b = test_x[:, row*col:row*col+18*2]
43
+    # test_x_b = test_x_b.reshape(test_x.shape[0], 18, 2, 1)
44
+    test_x_c = test_x[:,row*col:]
45
+
46
+    model=load_model(model_path)
47
+    score = model.evaluate([test_x_c, test_x_a, ], test_y)
48
+    print('MIX', score)
49
+
50
+    up_num = 0
51
+    up_error = 0
52
+    up_right = 0
53
+    down_num = 0
54
+    down_error = 0
55
+    down_right = 0
56
+    i = 0
57
+    result = model.predict([test_x_c, test_x_a, ])
58
+    win_dnn = []
59
+    for r in result:
60
+        fact = test_y[i]
61
+
62
+        if idx in [-2]:
63
+            if r[0] > 0.5 or r[1] > 0.5:
64
+                pass
65
+        else:
66
+            if r[0] > 0.6 :
67
+                tmp_right,tmp_error = _score(fact, lines[i])
68
+                up_right = tmp_right + up_right
69
+                up_error = tmp_error + up_error
70
+                up_num = up_num + 1
71
+            elif r[2] > 0.6 or r[3] > 0.6:
72
+                if fact[0] == 1:
73
+                    down_error = down_error + 1
74
+                    down_right = down_right + 1.14
75
+                elif fact[1] == 1:
76
+                    down_error = down_error + 0.5
77
+                    down_right = down_right + 1.04
78
+                elif fact[2] == 1:
79
+                    down_right = down_right + 0.98
80
+                else:
81
+                    down_right = down_right + 0.92
82
+                down_num = down_num + 1
83
+
84
+        i = i + 1
85
+    if up_num == 0:
86
+        up_num = 1
87
+    if down_num == 0:
88
+        down_num = 1
89
+    print('MIX', up_right, up_num, up_right/up_num, up_error/up_num, down_right/down_num, down_error/down_num)
90
+    return win_dnn,up_right/up_num,down_right/down_num
91
+
92
+
93
+if __name__ == '__main__':
94
+    # predict(file_path='D:\\data\\quantization\\stock181_18d_test.log', model_path='181_18d_mix_6D_ma5_s_seq.h5')
95
+    # predict(file_path='D:\\data\\quantization\\stock217_18d_train1.log', model_path='218_18d_mix_5D_ma5_s_seq.h5', row=18, col=18)
96
+    # predict(file_path='D:\\data\\quantization\\stock400_18d_train1.log', model_path='400_18d_mix_5D_ma5_s_seq.h5', row=18, col=18)
97
+    predict(file_path='D:\\data\\quantization\\stock501_28d_train1.log', model_path='501_28d_mix_5D_ma5_s_seq.h5', row=28, col=19)
98
+    # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
99
+    # multi_predict(model='15_18d')
100
+    # predict_today(20200229, model='11_18d')

+ 8 - 4
mix/mix_predict_by_day_190.py

@@ -51,14 +51,18 @@ def predict(file_path='', model_path='15min_dnn_seq', rows=18, cols=18):
51 51
 
52 52
             ratio = 1
53 53
             if train_x_c[0][-1] == 1:
54
-                ratio = 1.2
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
55 59
 
56 60
             if result[0][0]> 0.5:
57 61
                 up_num = up_num + ratio
58 62
             elif result[0][1] > 0.5:
59
-                up_num = up_num + 0.3
63
+                up_num = up_num + 0.5*ratio
60 64
             elif result[0][2] > 0.5:
61
-                down_num = down_num + 0.3
65
+                down_num = down_num + 0.5*ratio
62 66
             else:
63 67
                 down_num = down_num + ratio
64 68
 
@@ -82,5 +86,5 @@ if __name__ == '__main__':
82 86
     # predict(file_path='D:\\data\\quantization\\stock9_18_2.log', model_path='18d_dnn_seq.h5')
83 87
     # predict(file_path='D:\\data\\quantization\\stock16_18d_20200310.log', model_path='16_18d_mix_seq')
84 88
     # predict(file_path='D:\\data\\quantization\\stock196_18d_20200326.log', model_path='196_18d_mix_6D_ma5_s_seq')
85
-    predict(file_path='D:\\data\\quantization\\stock321_28d_5D_20200327.log', model_path='321_28d_mix_6D_ma5_s_seq', rows=28, cols=20)
89
+    predict(file_path='D:\\data\\quantization\\stock321_28d_5D_20200403.log', model_path='321_28d_mix_5D_ma5_s_seq', rows=28, cols=20)
86 90
     # predict(file_path='D:\\data\\quantization\\stock9_18_4.log', model_path='18d_dnn_seq.h5')

+ 29 - 28
mix/mix_predict_everyday_200.py

@@ -5,21 +5,22 @@ import random
5 5
 
6 6
 
7 7
 holder_stock_list = [
8
-                        # 医疗
9
-                        '000150.SZ', '300300.SZ', '603990.SH', '300759.SZ', '300347.SZ', '300003.SZ', '300253.SZ',
10
-                        # 5G
11
-                        '300698.SZ', '600498.SH', '300310.SZ', '600353.SH', '603912.SH', '603220.SH', '300602.SZ', '600260.SH',
12
-                        # 车联网
13
-                        '002369.SZ', '002920.SZ', '300020.SZ', '002373.SZ', '002869.SZ',
14
-                        # 工业互联网
15
-                        '002184.SZ', '002364.SZ','300310.SZ', '300670.SZ', '300166.SZ', '002169.SZ', '002380.SZ',
16
-                        # 特高压
17
-                        '300341.SZ', '300670.SZ', '300018.SZ', '600268.SH', '002879.SZ',
18
-                        # 基础建设
19
-                        '300041.SZ', '603568.SH', '000967.SZ', '603018.SH',
20
-                        # 华为
21
-                        '300687.SZ','002316.SZ','300339.SZ','300378.SZ','300020.SZ','300634.SZ','002570.SZ',
22
-                        '600801.SH', '300113.SZ','002555.SZ', '002174.SZ',
8
+        # 医疗
9
+        '000150.SZ', '300300.SZ', '603990.SH', '300759.SZ', '300347.SZ', '300003.SZ', '300253.SZ','002421.SZ','300168.SZ','002432.SZ',
10
+        # 5G
11
+        '300698.SZ', '600498.SH', '300310.SZ', '600353.SH', '603912.SH', '603220.SH', '300602.SZ', '600260.SH', '002463.SZ','300738.SZ',
12
+        # 车联网
13
+        '002369.SZ', '002920.SZ', '300020.SZ', '002373.SZ', '002869.SZ','300098.SZ','300048.SZ','002401.SZ',
14
+        # 工业互联网
15
+        '002184.SZ', '002364.SZ','300310.SZ', '300670.SZ', '300166.SZ', '002169.SZ', '002380.SZ','002421.SZ',
16
+        # 特高压
17
+        '300341.SZ', '300670.SZ', '300018.SZ', '600268.SH', '002879.SZ','002028.SZ',
18
+        # 基础建设
19
+        '300041.SZ', '603568.SH', '000967.SZ', '603018.SH','002062.SZ',
20
+        # 华为
21
+        '300687.SZ','002316.SZ','300339.SZ','300378.SZ','300020.SZ','300634.SZ','002570.SZ',
22
+        '600801.SH', '300113.SZ','002555.SZ', '002174.SZ','600585.SH','600276.SH','002415.SZ','000651.SZ',
23
+        '300074.SZ'
23 24
 ]
24 25
 
25 26
 ROE_stock_list =   [                      # ROE
@@ -99,10 +100,10 @@ def predict_today(file, day, model='10_18d', log=True):
99 100
 
100 101
     for line in lines:
101 102
         train_x = np.array([line[:size - 1]])
102
-        train_x_tmp = train_x[:,:18*20]
103
-        train_x_a = train_x_tmp.reshape(train_x.shape[0], 18, 20, 1)
103
+        train_x_tmp = train_x[:,:30*19]
104
+        train_x_a = train_x_tmp.reshape(train_x.shape[0], 30, 19, 1)
104 105
         # train_x_b = train_x_tmp.reshape(train_x.shape[0], 18, 24)
105
-        train_x_c = train_x[:,18*20:]
106
+        train_x_c = train_x[:,30*19:]
106 107
 
107 108
         result = model.predict([train_x_c, train_x_a, ])
108 109
         # print(result, line[-1])
@@ -162,15 +163,15 @@ def predict_today(file, day, model='10_18d', log=True):
162 163
                 with open('D:\\data\\quantization\\predict\\' + str(day) + '_mix.txt', mode='a', encoding="utf-8") as f:
163 164
                     f.write(str(line[-1]) + ' ' + stock['name'] + ' ' + stock['sw_industry'] + ' ' + str(concept_detail_list) + ' buy' + '\n')
164 165
 
165
-        elif result[0][1] > 0.5:
166
-            if stock['ts_code'] in holder_stock_list or stock['ts_code'] in ROE_stock_list:
167
-                print(stock['ts_code'], stock['name'], '震荡评级')
168
-        elif result[0][2] > 0.4:
169
-            if stock['ts_code'] in holder_stock_list or stock['ts_code'] in ROE_stock_list:
170
-                print(stock['ts_code'], stock['name'], '赶紧卖出')
171
-        else:
172
-            if stock['ts_code'] in holder_stock_list or stock['ts_code'] in ROE_stock_list:
173
-                print(stock['ts_code'], stock['name'], result[0],)
166
+        # elif result[0][1] > 0.5:
167
+        #     if stock['ts_code'] in holder_stock_list:
168
+        #         print(stock['ts_code'], stock['name'], '震荡评级')
169
+        # elif result[0][2] > 0.4:
170
+        #     if stock['ts_code'] in holder_stock_list:
171
+        #         print(stock['ts_code'], stock['name'], '赶紧卖出')
172
+        # else:
173
+        #     if stock['ts_code'] in holder_stock_list or stock['ts_code'] in ROE_stock_list:
174
+        #         print(stock['ts_code'], stock['name'], result[0],)
174 175
 
175 176
     # print(gainian_map)
176 177
     # print(hangye_map)
@@ -241,6 +242,6 @@ if __name__ == '__main__':
241 242
     # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5')
242 243
     # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
243 244
     # multi_predict()
244
-    predict_today("D:\\data\\quantization\\stock216_18d_20200327.log", 20200327, model='216_18d_mix_6D_ma5_s_seq.h5', log=True)
245
+    predict_today("D:\\data\\quantization\\stock405_30d_20200403.log", 20200403, model='405_30d_mix_5D_ma5_s_seq.h5', log=True)
245 246
     # join_two_day(20200305, 20200305)
246 247
     # check_everyday(20200311, 20200312)

+ 146 - 0
mix/mix_predict_everyday_500.py

@@ -0,0 +1,146 @@
1
+# -*- encoding:utf-8 -*-
2
+import numpy as np
3
+from keras.models import load_model
4
+import random
5
+
6
+
7
+zixuan_stock_list = [
8
+                        # 医疗
9
+                        '000150.SZ', '300300.SZ', '603990.SH', '300759.SZ', '300347.SZ', '300003.SZ', '300253.SZ','002421.SZ','300168.SZ','002432.SZ',
10
+                        # 5G
11
+                        '300698.SZ', '600498.SH', '300310.SZ', '600353.SH', '603912.SH', '603220.SH', '300602.SZ', '600260.SH', '002463.SZ','300738.SZ',
12
+                        # 车联网
13
+                        '002369.SZ', '002920.SZ', '300020.SZ', '002373.SZ', '002869.SZ','300098.SZ','300048.SZ','002401.SZ',
14
+                        # 工业互联网
15
+                        '002184.SZ', '002364.SZ','300310.SZ', '300670.SZ', '300166.SZ', '002169.SZ', '002380.SZ','002421.SZ',
16
+                        # 特高压
17
+                        '300341.SZ', '300670.SZ', '300018.SZ', '600268.SH', '002879.SZ','002028.SZ',
18
+                        # 基础建设
19
+                        '300041.SZ', '603568.SH', '000967.SZ', '603018.SH','002062.SZ',
20
+                        # 华为
21
+                        '300687.SZ','002316.SZ','300339.SZ','300378.SZ','300020.SZ','300634.SZ','002570.SZ',
22
+                        '600801.SH', '300113.SZ','002555.SZ', '002174.SZ','600585.SH','600276.SH','002415.SZ','000651.SZ',
23
+                        '300074.SZ'
24
+]
25
+
26
+ROE_stock_list =   [                      # ROE
27
+'002976.SZ', '002847.SZ', '002597.SZ', '300686.SZ', '000708.SZ', '603948.SH', '600507.SH', '300401.SZ', '002714.SZ', '600732.SH', '300033.SZ', '300822.SZ', '300821.SZ',
28
+'002458.SZ', '000708.SZ', '600732.SH', '603719.SH', '300821.SZ', '300800.SZ', '300816.SZ', '300812.SZ', '603195.SH', '300815.SZ', '603053.SH', '603551.SH', '002975.SZ',
29
+'603949.SH', '002970.SZ', '300809.SZ', '002968.SZ', '300559.SZ', '002512.SZ', '300783.SZ', '300003.SZ', '603489.SH', '300564.SZ', '600802.SH', '002600.SZ',
30
+'000933.SZ', '601918.SH', '000651.SZ', '002916.SZ', '000568.SZ', '000717.SZ', '600452.SH', '603589.SH', '600690.SH', '603886.SH', '300117.SZ', '000858.SZ', '002102.SZ',
31
+'300136.SZ', '600801.SH', '600436.SH', '300401.SZ', '002190.SZ', '300122.SZ', '002299.SZ', '603610.SH', '002963.SZ', '600486.SH', '300601.SZ', '300682.SZ', '300771.SZ',
32
+'000868.SZ', '002607.SZ', '603068.SH', '603508.SH', '603658.SH', '300571.SZ', '603868.SH', '600768.SH', '300760.SZ', '002901.SZ', '603638.SH', '601100.SH', '002032.SZ',
33
+'600083.SH', '600507.SH', '603288.SH', '002304.SZ', '000963.SZ', '300572.SZ', '000885.SZ', '600995.SH', '300080.SZ', '601888.SH', '000048.SZ', '000333.SZ', '300529.SZ',
34
+'000537.SZ', '002869.SZ', '600217.SH', '000526.SZ', '600887.SH', '002161.SZ', '600267.SH', '600668.SH', '600052.SH', '002379.SZ', '603369.SH', '601360.SH', '002833.SZ',
35
+'002035.SZ', '600031.SH', '600678.SH', '600398.SH', '600587.SH', '600763.SH', '002016.SZ', '603816.SH', '000031.SZ', '002555.SZ', '603983.SH', '002746.SZ', '603899.SH',
36
+'300595.SZ', '300632.SZ', '600809.SH', '002507.SZ', '300198.SZ', '600779.SH', '603568.SH', '300638.SZ', '002011.SZ', '603517.SH', '000661.SZ', '300630.SZ', '000895.SZ',
37
+'002841.SZ', '300602.SZ', '300418.SZ', '603737.SH', '002755.SZ', '002803.SZ', '002182.SZ', '600132.SH', '300725.SZ', '600346.SH', '300015.SZ', '300014.SZ', '300628.SZ',
38
+'000789.SZ', '600368.SH', '300776.SZ', '600570.SH', '000509.SZ', '600338.SH', '300770.SZ', '600309.SH', '000596.SZ', '300702.SZ', '002271.SZ', '300782.SZ', '300577.SZ',
39
+'603505.SH', '603160.SH', '300761.SZ', '603327.SH', '002458.SZ', '300146.SZ', '002463.SZ', '300417.SZ', '600566.SH', '002372.SZ', '600585.SH', '000848.SZ', '600519.SH',
40
+'000672.SZ', '300357.SZ', '002234.SZ', '603444.SH', '300236.SZ', '603360.SH', '002677.SZ', '300487.SZ', '600319.SH', '002415.SZ', '000403.SZ', '600340.SH', '601318.SH',
41
+
42
+]
43
+
44
+holder_stock_list = [
45
+'600498.SH', '002223.SZ', '300136.SZ', '300559.SZ',
46
+    '600496.SH', '300682.SZ'
47
+]
48
+
49
+import pymongo
50
+from util.mongodb import get_mongo_table_instance
51
+code_table = get_mongo_table_instance('tushare_code')
52
+k_table = get_mongo_table_instance('stock_day_k')
53
+stock_concept_table = get_mongo_table_instance('tushare_concept_detail')
54
+all_concept_code_list = list(get_mongo_table_instance('tushare_concept').find({}))
55
+
56
+
57
+industry = ['家用电器', '元器件', 'IT设备', '汽车服务',
58
+            '汽车配件', '软件服务',
59
+            '互联网', '纺织',
60
+            '塑料', '半导体',]
61
+
62
+gainian_map = {}
63
+hangye_map = {}
64
+
65
+Z_list = []  # 自选
66
+R_list = []  #  ROE
67
+O_list = []  # 其他
68
+
69
+def predict_today(file, day, model='10_18d', log=True):
70
+    lines = []
71
+    with open(file) as f:
72
+        for line in f.readlines()[:]:
73
+            line = eval(line.strip())
74
+            lines.append(line)
75
+
76
+    size = len(lines[0])
77
+
78
+    model=load_model(model)
79
+
80
+    for line in lines:
81
+        train_x = np.array([line[:size - 1]])
82
+        train_x_tmp = train_x[:,:28*20]
83
+        train_x_a = train_x_tmp.reshape(train_x.shape[0], 28, 20, 1)
84
+        # train_x_b = train_x_tmp.reshape(train_x.shape[0], 18, 24)
85
+        train_x_c = train_x[:,28*20:]
86
+
87
+        result = model.predict([train_x_c, train_x_a, ])
88
+        # print(result, line[-1])
89
+        stock = code_table.find_one({'ts_code':line[-1][0]})
90
+
91
+        if result[0][0] > 0.6:
92
+            pass
93
+
94
+        elif result[0][1] > 0.5:
95
+            pass
96
+        elif result[0][2] > 0.5 or result[0][3] > 0.5:
97
+            if stock['ts_code'] in holder_stock_list or stock['ts_code'] in zixuan_stock_list:
98
+                print(stock['ts_code'], stock['name'], '赶紧卖出')
99
+        else:
100
+            pass
101
+
102
+    # print(gainian_map)
103
+    # print(hangye_map)
104
+
105
+
106
+def _read_pfile_map(path):
107
+    s_list = []
108
+    with open(path, encoding='utf-8') as f:
109
+        for line in f.readlines()[:]:
110
+            s_list.append(line)
111
+    return s_list
112
+
113
+
114
+def join_two_day(a, b):
115
+    a_list = _read_pfile_map('D:\\data\\quantization\\predict\\' + str(a) + '.txt')
116
+    b_list = _read_pfile_map('D:\\data\\quantization\\predict\\dmi_' + str(b) + '.txt')
117
+    for a in a_list:
118
+        for b in b_list:
119
+            if a[2:11] == b[2:11]:
120
+                print(a)
121
+
122
+
123
+def check_everyday(day, today):
124
+    a_list = _read_pfile_map('D:\\data\\quantization\\predict\\' + str(day) + '.txt')
125
+    x = 0
126
+    for a in a_list:
127
+        print(a[:-1])
128
+        k_day_list = list(k_table.find({'code':a[2:11], 'tradeDate':{'$lte':int(today)}}).sort('tradeDate', pymongo.DESCENDING).limit(5))
129
+        if k_day_list is not None and len(k_day_list) > 0:
130
+            k_day = k_day_list[0]
131
+            k_day_0 = k_day_list[-1]
132
+            k_day_last = k_day_list[1]
133
+            if ((k_day_last['close'] - k_day_0['pre_close'])/k_day_0['pre_close']) < 0.2:
134
+                print(k_day['open'], k_day['close'], 100*(k_day['close'] - k_day_last['close'])/k_day_last['close'])
135
+                x = x + 100*(k_day['close'] - k_day_last['close'])/k_day_last['close']
136
+
137
+    print(x/len(a_list))
138
+
139
+
140
+if __name__ == '__main__':
141
+    # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5')
142
+    # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
143
+    # multi_predict()
144
+    predict_today("D:\\data\\quantization\\stock500_28d_20200403.log", 20200403, model='500_28d_mix_5D_ma5_s_seq.h5', log=True)
145
+    # join_two_day(20200305, 20200305)
146
+    # check_everyday(20200311, 20200312)

+ 17 - 15
mix/mix_train_200.py

@@ -18,10 +18,10 @@ from keras.callbacks import EarlyStopping
18 18
 
19 19
 early_stopping = EarlyStopping(monitor='accuracy', patience=5, verbose=2)
20 20
 
21
-epochs= 60
21
+epochs= 42
22 22
 size = 420000 #18W 60W
23 23
 file_path = 'D:\\data\\quantization\\stock217_18d_train2.log'
24
-model_path = '217_18d_mix_5D_ma5_s_seq.h5'
24
+model_path = '218_18d_mix_5D_ma5_s_seq.h5'
25 25
 file_path1='D:\\data\\quantization\\stock217_18d_test.log'
26 26
 '''
27 27
 ROC
@@ -44,8 +44,10 @@ beta+2+5                                           52,96,38
44 44
 
45 45
 15 流通市值>30                                     54,97,33
46 46
                                                    
47
-16 流通市值>30 + 5d                                59,98,26
48
-17 roc放后面, 18*18                                57,97,29
47
+16 流通市值>30 + 5d                                59,97,53     
48
+17 roc放后面, 18*18                                58,97,31
49
+18 roc放外面,18*18,两个cnn                       59,97,28
50
+19 窗口数180                                   
49 51
 '''
50 52
 
51 53
 def read_data(path, path1=file_path1):
@@ -85,9 +87,9 @@ train_x,train_y,test_x,test_y=read_data(file_path)
85 87
 
86 88
 train_x_a = train_x[:,:18*18]
87 89
 train_x_a = train_x_a.reshape(train_x.shape[0], 18, 18, 1)
88
-# train_x_b = train_x[:, 9*26:18*26]
89
-# train_x_b = train_x_b.reshape(train_x.shape[0], 9, 26, 1)
90
-train_x_c = train_x[:,18*18:]
90
+train_x_b = train_x[:, 18*18:18*18 + 2*18]
91
+train_x_b = train_x_b.reshape(train_x.shape[0], 18, 2, 1)
92
+train_x_c = train_x[:,18*18 + 2*18:]
91 93
 
92 94
 
93 95
 def create_mlp(dim, regress=False):
@@ -155,11 +157,11 @@ cnn_0 = create_cnn(18, 18, 1, kernel_size=(6, 20), size=96, regress=False, outpu
155 157
 # cnn_0 = create_cnn(18, 20, 1, kernel_size=(9, 9), size=90, regress=False, output=96)         # 28 97 53
156 158
 # cnn_0 = create_cnn(18, 20, 1, kernel_size=(3, 20), size=90, regress=False, output=96)
157 159
 # cnn_1 = create_cnn(18, 20, 1, kernel_size=(18, 10), size=80, regress=False, output=96)
158
-# cnn_1 = create_cnn(9, 26, 1, kernel_size=(2, 14), size=36, regress=False, output=64)
160
+cnn_1 = create_cnn(18, 2, 1, kernel_size=(6, 2), size=24, regress=False, output=24)
159 161
 
160 162
 # create the input to our final set of layers as the *output* of both
161 163
 # the MLP and CNN
162
-combinedInput = concatenate([mlp.output, cnn_0.output, ])
164
+combinedInput = concatenate([mlp.output, cnn_0.output, cnn_1.output])
163 165
 
164 166
 # our final FC layer head will have two dense layers, the final one
165 167
 # being our regression head
@@ -176,7 +178,7 @@ x = Dense(3, activation="softmax")(x)
176 178
 # our final model will accept categorical/numerical data on the MLP
177 179
 # input and images on the CNN input, outputting a single value (the
178 180
 # predicted price of the house)
179
-model = Model(inputs=[mlp.input, cnn_0.input, ], outputs=x)
181
+model = Model(inputs=[mlp.input, cnn_0.input, cnn_1.input], outputs=x)
180 182
 
181 183
 
182 184
 print("Starting training ")
@@ -191,7 +193,7 @@ model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=['accuracy
191 193
 # train the model
192 194
 print("[INFO] training model...")
193 195
 model.fit(
194
-    [train_x_c, train_x_a, ], train_y,
196
+    [train_x_c, train_x_a, train_x_b], train_y,
195 197
     # validation_data=([testAttrX, testImagesX], testY),
196 198
     # epochs=int(3*train_x_a.shape[0]/1300),
197 199
     epochs=epochs,
@@ -203,13 +205,13 @@ model.save(model_path)
203 205
 
204 206
 test_x_a = test_x[:,:18*18]
205 207
 test_x_a = test_x_a.reshape(test_x.shape[0], 18, 18, 1)
206
-# test_x_b = test_x[:, 9*26:9*26+9*26]
207
-# test_x_b = test_x_b.reshape(test_x.shape[0], 9, 26, 1)
208
-test_x_c = test_x[:,18*18:]
208
+test_x_b = test_x[:, 18*18:18*18+18*2]
209
+test_x_b = test_x_b.reshape(test_x.shape[0], 18, 2, 1)
210
+test_x_c = test_x[:,18*18 + 18*2:]
209 211
 
210 212
 # make predictions on the testing data
211 213
 print("[INFO] predicting house prices...")
212
-score  = model.evaluate([test_x_c, test_x_a,], test_y)
214
+score  = model.evaluate([test_x_c, test_x_a, test_x_b], test_y)
213 215
 
214 216
 print(score)
215 217
 print('Test score:', score[0])

+ 6 - 6
mix/mix_train_300.py

@@ -18,10 +18,10 @@ from keras.callbacks import EarlyStopping
18 18
 
19 19
 early_stopping = EarlyStopping(monitor='accuracy', patience=5, verbose=2)
20 20
 
21
-epochs= 88
22
-size = 410000 #18W 60W
21
+epochs= 108
22
+size = 580000 #共68W
23 23
 file_path = 'D:\\data\\quantization\\stock321_28d_train2.log'
24
-model_path = '321_28d_mix_6D_ma5_s_seq.h5'
24
+model_path = '321_28d_mix_5D_ma5_s_seq_2.h5'
25 25
 file_path1='D:\\data\\quantization\\stock321_28d_test.log'
26 26
 row = 28
27 27
 col = 20
@@ -36,7 +36,7 @@ col = 20
36 36
 11 DMI     28*20          37,101,16
37 37
 12 MACD    28*19           
38 38
 28d+ma5+5+流通市值>10
39
-21 DMI     28*20          43,101,11  非常好
39
+21 DMI     28*20          43,102,9  非常好 
40 40
  
41 41
        
42 42
 30d+close
@@ -52,12 +52,12 @@ col = 20
52 52
 def read_data(path, path1=file_path1):
53 53
     lines = []
54 54
     with open(path) as f:
55
-        for x in range(size): #610000
55
+        for x in range(size): #680000
56 56
             line = eval(f.readline().strip())
57 57
             lines.append(line)
58 58
 
59 59
     with open(path1) as f:
60
-        for x in range(40000):
60
+        for x in range(60000): #6w
61 61
             line = eval(f.readline().strip())
62 62
             lines.append(line)
63 63
 

+ 208 - 0
mix/mix_train_400.py

@@ -0,0 +1,208 @@
1
+import keras
2
+# -*- encoding:utf-8 -*-
3
+import numpy as np
4
+from keras.models import Sequential
5
+# 优化方法选用Adam(其实可选项有很多,如SGD)
6
+from keras.optimizers import Adam
7
+import random
8
+from keras.models import load_model
9
+from imblearn.over_sampling import RandomOverSampler
10
+from keras.utils import np_utils
11
+# 用于模型初始化,Conv2D模型初始化、Activation激活函数,MaxPooling2D是池化层
12
+# Flatten作用是将多位输入进行一维化
13
+# Dense是全连接层
14
+from keras.layers import Conv2D, Activation, MaxPool2D, Flatten, Dense,Dropout,Input,MaxPooling2D,BatchNormalization,concatenate
15
+from keras import regularizers
16
+from keras.models import Model
17
+from keras.callbacks import EarlyStopping
18
+
19
+early_stopping = EarlyStopping(monitor='accuracy', patience=5, verbose=2)
20
+
21
+epochs= 40
22
+size = 440000 #共68W
23
+file_path = 'D:\\data\\quantization\\stock403_30d_train2.log'
24
+model_path = '403_30d_mix_5D_ma5_s_seq_2.h5'
25
+file_path1='D:\\data\\quantization\\stock403_30d_test.log'
26
+row = 30
27
+col = 20
28
+'''
29
+0    roc 涨幅int表示  18*18             59,97,46                                       
30
+1    dmi              24*20             59,98,41
31
+2    roc              24*18             56,98,41
32
+3    dmi              30*20             55,98,40       
33
+3B   dmi    9
34
+3A   dmi              30*20             53,97,44
35
+4    roc              30*18             63,98,40
36
+5    macd             30*19             64,98,39       !
37
+5_1   macd   9                          62,98,41
38
+5_2   macd   12                         58,98,43
39
+9    rsi              30*17             50,97,43
40
+
41
+6    dmi+大盘形态     30*20             52,98,39
42
+7    roc+大盘形态     30*18             59,98,40
43
+8    macd+大盘形态    30*19             
44
+'''
45
+
46
+def read_data(path, path1=file_path1):
47
+    lines = []
48
+    with open(path) as f:
49
+        for x in range(size): #680000
50
+            line = eval(f.readline().strip())
51
+            lines.append(line)
52
+
53
+    with open(path1) as f:
54
+        for x in range(30000): #6w
55
+            line = eval(f.readline().strip())
56
+            lines.append(line)
57
+
58
+    random.shuffle(lines)
59
+    print('读取数据完毕')
60
+
61
+    d=int(0.85*len(lines))
62
+    length = len(lines[0])
63
+
64
+    train_x=[s[:length - 2] for s in lines[0:d]]
65
+    train_y=[s[-1] for s in lines[0:d]]
66
+    test_x=[s[:length - 2] for s in lines[d:]]
67
+    test_y=[s[-1] for s in lines[d:]]
68
+
69
+    print('转换数据完毕')
70
+
71
+    ros = RandomOverSampler(random_state=0)
72
+    X_resampled, y_resampled = ros.fit_sample(np.array(train_x, dtype=np.float32), np.array(train_y, dtype=np.float32))
73
+
74
+    print('数据重采样完毕')
75
+
76
+    return X_resampled,y_resampled,np.array(test_x, dtype=np.float32),np.array(test_y, dtype=np.float32)
77
+
78
+
79
+train_x,train_y,test_x,test_y=read_data(file_path)
80
+
81
+train_x_a = train_x[:,:row*col]
82
+train_x_a = train_x_a.reshape(train_x.shape[0], row, col, 1)
83
+# train_x_b = train_x[:, 9*26:18*26]
84
+# train_x_b = train_x_b.reshape(train_x.shape[0], 9, 26, 1)
85
+train_x_c = train_x[:,row*col:]
86
+
87
+
88
+def create_mlp(dim, regress=False):
89
+    # define our MLP network
90
+    model = Sequential()
91
+    model.add(Dense(256, input_dim=dim, activation="relu"))
92
+    model.add(Dropout(0.2))
93
+    model.add(Dense(256, activation="relu"))
94
+    model.add(Dense(256, activation="relu"))
95
+    model.add(Dense(128, activation="relu"))
96
+
97
+    # check to see if the regression node should be added
98
+    if regress:
99
+        model.add(Dense(1, activation="linear"))
100
+
101
+    # return our model
102
+    return model
103
+
104
+
105
+def create_cnn(width, height, depth, size=48, kernel_size=(5, 6), regress=False, output=24):
106
+    # initialize the input shape and channel dimension, assuming
107
+    # TensorFlow/channels-last ordering
108
+    inputShape = (width, height, 1)
109
+    chanDim = -1
110
+
111
+    # define the model input
112
+    inputs = Input(shape=inputShape)
113
+    # x = inputs
114
+    # CONV => RELU => BN => POOL
115
+    x = Conv2D(size, kernel_size, strides=2, padding="same")(inputs)
116
+    x = Activation("relu")(x)
117
+    x = BatchNormalization(axis=chanDim)(x)
118
+
119
+    # y = Conv2D(24, (2, 8), strides=2, padding="same")(inputs)
120
+    # y = Activation("relu")(y)
121
+    # y = BatchNormalization(axis=chanDim)(y)
122
+
123
+    # flatten the volume, then FC => RELU => BN => DROPOUT
124
+    x = Flatten()(x)
125
+    x = Dense(output)(x)
126
+    x = Activation("relu")(x)
127
+    x = BatchNormalization(axis=chanDim)(x)
128
+    x = Dropout(0.2)(x)
129
+
130
+    # apply another FC layer, this one to match the number of nodes
131
+    # coming out of the MLP
132
+    x = Dense(output)(x)
133
+    x = Activation("relu")(x)
134
+
135
+    # check to see if the regression node should be added
136
+    if regress:
137
+        x = Dense(1, activation="linear")(x)
138
+
139
+    # construct the CNN
140
+    model = Model(inputs, x)
141
+
142
+    # return the CNN
143
+    return model
144
+
145
+
146
+# create the MLP and CNN models
147
+mlp = create_mlp(train_x_c.shape[1], regress=False)
148
+# cnn_0 = create_cnn(18, 20, 1, kernel_size=(3, 3), size=90, regress=False, output=96)       # 31 97 46
149
+cnn_0 = create_cnn(row, col, 1, kernel_size=(6, col), size=96, regress=False, output=96)         # 29 98 47
150
+# cnn_0 = create_cnn(18, 20, 1, kernel_size=(9, 9), size=90, regress=False, output=96)         # 28 97 53
151
+# cnn_0 = create_cnn(18, 20, 1, kernel_size=(3, 20), size=90, regress=False, output=96)
152
+# cnn_1 = create_cnn(18, 20, 1, kernel_size=(18, 10), size=80, regress=False, output=96)
153
+# cnn_1 = create_cnn(9, 26, 1, kernel_size=(2, 14), size=36, regress=False, output=64)
154
+
155
+# create the input to our final set of layers as the *output* of both
156
+# the MLP and CNN
157
+combinedInput = concatenate([mlp.output, cnn_0.output, ])
158
+
159
+# our final FC layer head will have two dense layers, the final one
160
+# being our regression head
161
+x = Dense(1024, activation="relu", kernel_regularizer=regularizers.l1(0.003))(combinedInput)
162
+x = Dropout(0.2)(x)
163
+x = Dense(1024, activation="relu")(x)
164
+x = Dense(1024, activation="relu")(x)
165
+# 在建设一层
166
+x = Dense(3, activation="softmax")(x)
167
+
168
+# our final model will accept categorical/numerical data on the MLP
169
+# input and images on the CNN input, outputting a single value (the
170
+# predicted price of the house)
171
+model = Model(inputs=[mlp.input, cnn_0.input, ], outputs=x)
172
+
173
+
174
+print("Starting training ")
175
+# h = model.fit(train_x, train_y, batch_size=4096*2, epochs=500, shuffle=True)
176
+
177
+# compile the model using mean absolute percentage error as our loss,
178
+# implying that we seek to minimize the absolute percentage difference
179
+# between our price *predictions* and the *actual prices*
180
+opt = Adam(lr=1e-3, decay=1e-3 / 200)
181
+model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=['accuracy'])
182
+
183
+# train the model
184
+print("[INFO] training model...")
185
+model.fit(
186
+    [train_x_c, train_x_a, ], train_y,
187
+    # validation_data=([testAttrX, testImagesX], testY),
188
+    # epochs=int(3*train_x_a.shape[0]/1300),
189
+    epochs=epochs,
190
+    batch_size=2048, shuffle=True,
191
+    callbacks=[early_stopping]
192
+)
193
+
194
+model.save(model_path)
195
+
196
+test_x_a = test_x[:,:row*col]
197
+test_x_a = test_x_a.reshape(test_x.shape[0], row, col, 1)
198
+# test_x_b = test_x[:, 9*26:9*26+9*26]
199
+# test_x_b = test_x_b.reshape(test_x.shape[0], 9, 26, 1)
200
+test_x_c = test_x[:,row*col:]
201
+
202
+# make predictions on the testing data
203
+print("[INFO] predicting house prices...")
204
+score  = model.evaluate([test_x_c, test_x_a,], test_y)
205
+
206
+print(score)
207
+print('Test score:', score[0])
208
+print('Test accuracy:', score[1])

+ 196 - 0
mix/mix_train_500.py

@@ -0,0 +1,196 @@
1
+import keras
2
+# -*- encoding:utf-8 -*-
3
+import numpy as np
4
+from keras.models import Sequential
5
+# 优化方法选用Adam(其实可选项有很多,如SGD)
6
+from keras.optimizers import Adam
7
+import random
8
+from keras.models import load_model
9
+from imblearn.over_sampling import RandomOverSampler
10
+from keras.utils import np_utils
11
+# 用于模型初始化,Conv2D模型初始化、Activation激活函数,MaxPooling2D是池化层
12
+# Flatten作用是将多位输入进行一维化
13
+# Dense是全连接层
14
+from keras.layers import Conv2D, Activation, MaxPool2D, Flatten, Dense,Dropout,Input,MaxPooling2D,BatchNormalization,concatenate
15
+from keras import regularizers
16
+from keras.models import Model
17
+from keras.callbacks import EarlyStopping
18
+
19
+early_stopping = EarlyStopping(monitor='accuracy', patience=5, verbose=2)
20
+
21
+epochs= 42
22
+size = 420000 #共68W
23
+file_path = 'D:\\data\\quantization\\stock501_28d_train2.log'
24
+model_path = '501_28d_mix_5D_ma5_s_seq.h5'
25
+file_path1='D:\\data\\quantization\\stock501_28d_test.log'
26
+row = 28
27
+col = 19
28
+'''
29
+0    dmi            28*20      38,98,51/5     下跌预判非常准                                    
30
+1    macd           28*19      41,98,53/8       
31
+       
32
+'''
33
+
34
+def read_data(path, path1=file_path1):
35
+    lines = []
36
+    with open(path) as f:
37
+        for x in range(size): #680000
38
+            line = eval(f.readline().strip())
39
+            lines.append(line)
40
+
41
+    with open(path1) as f:
42
+        for x in range(32000): #6w
43
+            line = eval(f.readline().strip())
44
+            lines.append(line)
45
+
46
+    random.shuffle(lines)
47
+    print('读取数据完毕')
48
+
49
+    d=int(0.85*len(lines))
50
+    length = len(lines[0])
51
+
52
+    train_x=[s[:length - 2] for s in lines[0:d]]
53
+    train_y=[s[-1] for s in lines[0:d]]
54
+    test_x=[s[:length - 2] for s in lines[d:]]
55
+    test_y=[s[-1] for s in lines[d:]]
56
+
57
+    print('转换数据完毕')
58
+
59
+    ros = RandomOverSampler(random_state=0)
60
+    X_resampled, y_resampled = ros.fit_sample(np.array(train_x, dtype=np.float32), np.array(train_y, dtype=np.float32))
61
+
62
+    print('数据重采样完毕')
63
+
64
+    return X_resampled,y_resampled,np.array(test_x, dtype=np.float32),np.array(test_y, dtype=np.float32)
65
+
66
+
67
+train_x,train_y,test_x,test_y=read_data(file_path)
68
+
69
+train_x_a = train_x[:,:row*col]
70
+train_x_a = train_x_a.reshape(train_x.shape[0], row, col, 1)
71
+# train_x_b = train_x[:, 9*26:18*26]
72
+# train_x_b = train_x_b.reshape(train_x.shape[0], 9, 26, 1)
73
+train_x_c = train_x[:,row*col:]
74
+
75
+
76
+def create_mlp(dim, regress=False):
77
+    # define our MLP network
78
+    model = Sequential()
79
+    model.add(Dense(256, input_dim=dim, activation="relu"))
80
+    model.add(Dropout(0.2))
81
+    model.add(Dense(256, activation="relu"))
82
+    model.add(Dense(256, activation="relu"))
83
+    model.add(Dense(128, activation="relu"))
84
+
85
+    # check to see if the regression node should be added
86
+    if regress:
87
+        model.add(Dense(1, activation="linear"))
88
+
89
+    # return our model
90
+    return model
91
+
92
+
93
+def create_cnn(width, height, depth, size=48, kernel_size=(5, 6), regress=False, output=24):
94
+    # initialize the input shape and channel dimension, assuming
95
+    # TensorFlow/channels-last ordering
96
+    inputShape = (width, height, 1)
97
+    chanDim = -1
98
+
99
+    # define the model input
100
+    inputs = Input(shape=inputShape)
101
+    # x = inputs
102
+    # CONV => RELU => BN => POOL
103
+    x = Conv2D(size, kernel_size, strides=2, padding="same")(inputs)
104
+    x = Activation("relu")(x)
105
+    x = BatchNormalization(axis=chanDim)(x)
106
+
107
+    # y = Conv2D(24, (2, 8), strides=2, padding="same")(inputs)
108
+    # y = Activation("relu")(y)
109
+    # y = BatchNormalization(axis=chanDim)(y)
110
+
111
+    # flatten the volume, then FC => RELU => BN => DROPOUT
112
+    x = Flatten()(x)
113
+    x = Dense(output)(x)
114
+    x = Activation("relu")(x)
115
+    x = BatchNormalization(axis=chanDim)(x)
116
+    x = Dropout(0.2)(x)
117
+
118
+    # apply another FC layer, this one to match the number of nodes
119
+    # coming out of the MLP
120
+    x = Dense(output)(x)
121
+    x = Activation("relu")(x)
122
+
123
+    # check to see if the regression node should be added
124
+    if regress:
125
+        x = Dense(1, activation="linear")(x)
126
+
127
+    # construct the CNN
128
+    model = Model(inputs, x)
129
+
130
+    # return the CNN
131
+    return model
132
+
133
+
134
+# create the MLP and CNN models
135
+mlp = create_mlp(train_x_c.shape[1], regress=False)
136
+# cnn_0 = create_cnn(18, 20, 1, kernel_size=(3, 3), size=90, regress=False, output=96)       # 31 97 46
137
+cnn_0 = create_cnn(row, col, 1, kernel_size=(6, col), size=96, regress=False, output=96)         # 29 98 47
138
+# cnn_0 = create_cnn(18, 20, 1, kernel_size=(9, 9), size=90, regress=False, output=96)         # 28 97 53
139
+# cnn_0 = create_cnn(18, 20, 1, kernel_size=(3, 20), size=90, regress=False, output=96)
140
+# cnn_1 = create_cnn(18, 20, 1, kernel_size=(18, 10), size=80, regress=False, output=96)
141
+# cnn_1 = create_cnn(9, 26, 1, kernel_size=(2, 14), size=36, regress=False, output=64)
142
+
143
+# create the input to our final set of layers as the *output* of both
144
+# the MLP and CNN
145
+combinedInput = concatenate([mlp.output, cnn_0.output, ])
146
+
147
+# our final FC layer head will have two dense layers, the final one
148
+# being our regression head
149
+x = Dense(1024, activation="relu", kernel_regularizer=regularizers.l1(0.003))(combinedInput)
150
+x = Dropout(0.2)(x)
151
+x = Dense(1024, activation="relu")(x)
152
+x = Dense(1024, activation="relu")(x)
153
+# 在建设一层
154
+x = Dense(4, activation="softmax")(x)
155
+
156
+# our final model will accept categorical/numerical data on the MLP
157
+# input and images on the CNN input, outputting a single value (the
158
+# predicted price of the house)
159
+model = Model(inputs=[mlp.input, cnn_0.input, ], outputs=x)
160
+
161
+
162
+print("Starting training ")
163
+# h = model.fit(train_x, train_y, batch_size=4096*2, epochs=500, shuffle=True)
164
+
165
+# compile the model using mean absolute percentage error as our loss,
166
+# implying that we seek to minimize the absolute percentage difference
167
+# between our price *predictions* and the *actual prices*
168
+opt = Adam(lr=1e-3, decay=1e-3 / 200)
169
+model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=['accuracy'])
170
+
171
+# train the model
172
+print("[INFO] training model...")
173
+model.fit(
174
+    [train_x_c, train_x_a, ], train_y,
175
+    # validation_data=([testAttrX, testImagesX], testY),
176
+    # epochs=int(3*train_x_a.shape[0]/1300),
177
+    epochs=epochs,
178
+    batch_size=2048, shuffle=True,
179
+    callbacks=[early_stopping]
180
+)
181
+
182
+model.save(model_path)
183
+
184
+test_x_a = test_x[:,:row*col]
185
+test_x_a = test_x_a.reshape(test_x.shape[0], row, col, 1)
186
+# test_x_b = test_x[:, 9*26:9*26+9*26]
187
+# test_x_b = test_x_b.reshape(test_x.shape[0], 9, 26, 1)
188
+test_x_c = test_x[:,row*col:]
189
+
190
+# make predictions on the testing data
191
+print("[INFO] predicting house prices...")
192
+score  = model.evaluate([test_x_c, test_x_a,], test_y)
193
+
194
+print(score)
195
+print('Test score:', score[0])
196
+print('Test accuracy:', score[1])