Browse Source

mix 相关

yufeng 4 years ago
parent
commit
643d4acd8f
4 changed files with 240 additions and 26 deletions
  1. 9 7
      mix/mix_predict.py
  2. 81 0
      mix/mix_predict_by_day.py
  3. 128 0
      mix/mix_predict_by_stock.py
  4. 22 19
      mix/mix_train.py

+ 9 - 7
mix/mix_predict.py

@@ -19,7 +19,7 @@ def read_data(path):
19 19
 
20 20
 
21 21
 def _score(fact, line):
22
-    with open('dnn_predict_dmi_18d.txt', 'a') as f:
22
+    with open('mix_predict_dmi_18d.txt', 'a') as f:
23 23
         f.write(str([line[-2], line[-1]]) + "\n")
24 24
 
25 25
     up_right = 0
@@ -43,12 +43,13 @@ def _score(fact, line):
43 43
 
44 44
 def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
45 45
     test_x,test_y,lines=read_data(file_path)
46
+    print('Load data success')
46 47
 
47 48
     test_x_a = test_x[:,:18*18]
48 49
     test_x_a = test_x_a.reshape(test_x.shape[0], 18, 18, 1)
49
-    test_x_b = test_x[:, 18*18:18*18+2*18]
50
-    test_x_b = test_x_b.reshape(test_x.shape[0], 18, 2, 1)
51
-    test_x_c = test_x[:,18*18+2*18:]
50
+    test_x_b = test_x[:, 18*18:18*18+8*18]
51
+    test_x_b = test_x_b.reshape(test_x.shape[0], 18, 8, 1)
52
+    test_x_c = test_x[:,18*18+8*18:]
52 53
 
53 54
     model=load_model(model_path)
54 55
     score = model.evaluate([test_x_c, test_x_a, test_x_b], test_y)
@@ -70,12 +71,12 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
70 71
             if r[0] > 0.5 or r[1] > 0.5:
71 72
                 pass
72 73
         else:
73
-            if r[0] > 0.7 or r[1] > 0.7:
74
+            if r[0] > 0.6 or r[1] > 0.6:
74 75
                 tmp_right,tmp_error = _score(fact, lines[i])
75 76
                 up_right = tmp_right + up_right
76 77
                 up_error = tmp_error + up_error
77 78
                 up_num = up_num + 1
78
-            elif r[3] > 0.6 or r[4] > 0.6:
79
+            elif r[3] > 0.7 or r[4] > 0.7:
79 80
                 if fact[0] == 1:
80 81
                     down_error = down_error + 1
81 82
                     down_right = down_right + 1.12
@@ -83,6 +84,7 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
83 84
                     down_error = down_error + 1
84 85
                     down_right = down_right + 1.06
85 86
                 elif fact[2] == 1:
87
+                    down_error = down_error + 0.5
86 88
                     down_right = down_right + 1
87 89
                 elif fact[3] == 1:
88 90
                     down_right = down_right + 0.94
@@ -100,7 +102,7 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
100 102
 
101 103
 
102 104
 if __name__ == '__main__':
103
-    predict(file_path='D:\\data\\quantization\\stock18_18d_test.log', model_path='18_18d_mix_seq.h5')
105
+    predict(file_path='D:\\data\\quantization\\stock19_18d_test.log', model_path='19_18d_mix_seq.h5')
104 106
     # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
105 107
     # multi_predict(model='15_18d')
106 108
     # predict_today(20200229, model='11_18d')

+ 81 - 0
mix/mix_predict_by_day.py

@@ -0,0 +1,81 @@
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'):
22
+    day_lines = read_data(file_path)
23
+    print('数据读取完毕')
24
+
25
+    models = []
26
+    for x in range(0, 12):
27
+        models.append(load_model(model_path + '_' + str(x) + '.h5'))
28
+    estimator = joblib.load('km_dmi_18.pkl')
29
+    print('模型加载完毕')
30
+
31
+    items = sorted(day_lines.keys())
32
+    for key in items:
33
+        # print(day)
34
+        lines = day_lines[key]
35
+
36
+        up_num = 0
37
+        down_num = 0
38
+        x = 24 # 每条数据项数
39
+        k = 18 # 周期
40
+        for line in lines:
41
+            v = line[1:x*k + 1]
42
+            v = np.array(v)
43
+            v = v.reshape(k, x)
44
+            v = v[:,4:8]
45
+            v = v.reshape(1, 4*k)
46
+            # print(v)
47
+            r = estimator.predict(v)
48
+
49
+            train_x = np.array([line[:-1]])
50
+            result = models[r[0]].predict(train_x)
51
+
52
+            if result[0][3] > 0.5 or result[0][4] > 0.5:
53
+                down_num = down_num + 1
54
+            elif result[0][1] > 0.5 or result[0][0] > 0.5:
55
+                up_num = up_num + 0.6  # 乐观调大 悲观调小
56
+
57
+            # if result[0][0] > 0.5 or result[0][1] > 0.5:
58
+            #     if r[0] in [0,2,3,4,5,9,10,11]:
59
+            #         up_num = up_num + 1
60
+            #     elif r[0] in [8]:
61
+            #         up_num = up_num + 0.6
62
+            #     else:
63
+            #         up_num = up_num + 0.4
64
+            # if result[0][3] > 0.5 or result[0][4] > 0.5:
65
+            #     if r[0] in [4,6,]:
66
+            #         down_num = down_num + 1
67
+            #     elif r[0] in [0,1,3,7,8,]:
68
+            #         down_num = down_num + 0.6
69
+            #     else:
70
+            #         down_num = down_num + 0.4
71
+
72
+        print(key, int(up_num), int(down_num), (down_num*1.2 + 2)/(up_num*1.2 + 2))
73
+
74
+
75
+if __name__ == '__main__':
76
+    # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5')
77
+    # predict(file_path='D:\\data\\quantization\\stock9_18_20200220.log', model_path='18d_dnn_seq.h5')
78
+    # predict(file_path='D:\\data\\quantization\\stock9_18_2.log', model_path='18d_dnn_seq.h5')
79
+    predict(file_path='D:\\data\\quantization\\stock11_18d_20200229.log', model_path='11_18d_dnn_seq')
80
+    # predict(file_path='D:\\data\\quantization\\stock11_18d_20190103_20190604.log', model_path='14_18d_dnn_seq')
81
+    # predict(file_path='D:\\data\\quantization\\stock9_18_4.log', model_path='18d_dnn_seq.h5')

+ 128 - 0
mix/mix_predict_by_stock.py

@@ -0,0 +1,128 @@
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
+    stock_lines = {}
9
+    with open(path) as f:
10
+        for line in f.readlines()[:]:
11
+            line = eval(line.strip())
12
+            stock = str(line[-2][0])
13
+
14
+            if stock in stock_lines:
15
+                stock_lines[stock].append(line)
16
+            else:
17
+                stock_lines[stock] = [line]
18
+    # print(len(day_lines['20191230']))
19
+    return stock_lines
20
+
21
+
22
+import pymongo
23
+from util.mongodb import get_mongo_table_instance
24
+code_table = get_mongo_table_instance('tushare_code')
25
+k_table = get_mongo_table_instance('stock_day_k')
26
+
27
+
28
+def predict(file_path='', model_path='15min_dnn_seq'):
29
+    stock_lines = read_data(file_path)
30
+    print('数据读取完毕')
31
+
32
+    models = []
33
+    models.append(load_model(model_path + '.h5'))
34
+    # estimator = joblib.load('km_dmi_18.pkl')
35
+    print('模型加载完毕')
36
+
37
+    total_money = 0
38
+    total_num = 0
39
+    items = sorted(stock_lines.keys())
40
+    for key in items:
41
+        # print(day)
42
+        lines = stock_lines[key]
43
+        init_money = 10000
44
+        last_price = 1
45
+
46
+        if lines[0][-2][0].startswith('6'):
47
+            continue
48
+
49
+        buy = 0 # 0空 1买入 2卖出
50
+        chiyou_0 = 0
51
+        high_price = 0
52
+
53
+        x = 24 # 每条数据项数
54
+        k = 18 # 周期
55
+        for line in lines:
56
+            # v = line[1:x*k + 1]
57
+            # v = np.array(v)
58
+            # v = v.reshape(k, x)
59
+            # v = v[:,6:10]
60
+            # v = v.reshape(1, 4*k)
61
+            # print(v)
62
+            # r = estimator.predict(v)
63
+
64
+            test_x = np.array([line[:-2]])
65
+            test_x_a = test_x[:,:18*24]
66
+            test_x_a = test_x_a.reshape(test_x.shape[0], 18, 24, 1)
67
+            test_x_b = test_x[:, 18*24:18*24+2*18]
68
+            test_x_b = test_x_b.reshape(test_x.shape[0], 18, 2, 1)
69
+            test_x_c = test_x[:,18*24+2*18:]
70
+
71
+            result = models[0].predict([test_x_c, test_x_a, test_x_b])
72
+
73
+            stock_name = line[-2]
74
+            today_price = list(k_table.find({'code':line[-2][0], 'tradeDate':{'$gt':int(line[-2][1])}}).sort('tradeDate',pymongo.ASCENDING).limit(1))
75
+            today_price = today_price[0]
76
+
77
+            if result[0][0] + result[0][1] > 0.7:
78
+                chiyou_0 = 0
79
+                if buy == 0:
80
+                    last_price = today_price['open']
81
+                    high_price = last_price
82
+                    print('首次买入', stock_name, today_price['open'])
83
+                    buy = 1
84
+                else:
85
+                    init_money = init_money * (today_price['close'] - last_price)/last_price + init_money
86
+                    last_price = today_price['close']
87
+                    print('买入+买入', stock_name, today_price['close'])
88
+                    buy = 1
89
+                    if last_price > high_price:
90
+                        high_price = last_price
91
+            elif buy == 1:
92
+                chiyou_0 = chiyou_0 + 1
93
+                if chiyou_0 > 2 and ((high_price - today_price['close'])/high_price*100 > 5):
94
+                    print('卖出', stock_name, today_price['close'])
95
+                    init_money = init_money * (today_price['close'] - last_price)/last_price + init_money
96
+                    buy = 0
97
+                    chiyou_0 = 0
98
+                if init_money < 9000:
99
+                    print('止损卖出', stock_name, today_price['close'])
100
+                    init_money = init_money * (today_price['close'] - last_price)/last_price + init_money
101
+                    buy = 0
102
+                    chiyou_0 = 0
103
+                else:
104
+                    print('继续持有', stock_name, today_price['close'])
105
+                    init_money = init_money * (today_price['close'] - last_price)/last_price + init_money
106
+                    buy = 1
107
+
108
+                last_price = today_price['close']
109
+                if last_price > high_price:
110
+                    high_price = last_price
111
+
112
+        print(key, init_money)
113
+
114
+        with open('D:\\data\\quantization\\stock_18_18d' + '_' +  'profit.log', 'a') as f:
115
+            if init_money > 10000:
116
+                f.write(str(key) + ' ' + str(init_money) + '\n')
117
+            elif init_money < 10000:
118
+                f.write(str(key) + ' ' + str(init_money) + '\n')
119
+
120
+        if init_money != 10000:
121
+            total_money = total_money + init_money
122
+            total_num = total_num + 1
123
+
124
+    print(total_money, total_num, total_money/total_num/10000)
125
+
126
+
127
+if __name__ == '__main__':
128
+    predict(file_path='D:\\data\\quantization\\stock18_18d_test.log', model_path='18_18d_mix_seq')

+ 22 - 19
mix/mix_train.py

@@ -16,10 +16,13 @@ from keras import regularizers
16 16
 from keras.models import Model
17 17
 
18 18
 
19
+epochs= 130
20
+size = 380000
21
+
19 22
 def read_data(path):
20 23
     lines = []
21 24
     with open(path) as f:
22
-        for x in range(300000): #380000
25
+        for x in range(size): #380000
23 26
             lines.append(eval(f.readline().strip()))
24 27
 
25 28
     random.shuffle(lines)
@@ -46,16 +49,16 @@ train_x,train_y,test_x,test_y=read_data("D:\\data\\quantization\\stock18_18d_tra
46 49
 
47 50
 train_x_a = train_x[:,:18*18]
48 51
 train_x_a = train_x_a.reshape(train_x.shape[0], 18, 18, 1)
49
-train_x_b = train_x[:, 18*18:18*18+2*18]
50
-train_x_b = train_x_b.reshape(train_x.shape[0], 18, 2, 1)
51
-train_x_c = train_x[:,18*18+2*18:]
52
+train_x_b = train_x[:, 18*18:18*18+8*18]
53
+train_x_b = train_x_b.reshape(train_x.shape[0], 18, 8, 1)
54
+train_x_c = train_x[:,18*18+8*18:]
52 55
 
53 56
 
54 57
 def create_mlp(dim, regress=False):
55 58
     # define our MLP network
56 59
     model = Sequential()
57
-    model.add(Dense(32, input_dim=dim, activation="relu"))
58
-    model.add(Dense(32, activation="relu"))
60
+    model.add(Dense(16, input_dim=dim, activation="relu"))
61
+    model.add(Dense(16, activation="relu"))
59 62
 
60 63
     # check to see if the regression node should be added
61 64
     if regress:
@@ -65,7 +68,7 @@ def create_mlp(dim, regress=False):
65 68
     return model
66 69
 
67 70
 
68
-def create_cnn(width, height, depth, filters=(4, 6), kernel_size=(5, 6), regress=False, output=24):
71
+def create_cnn(width, height, depth, filters=32, kernel_size=(5, 6), regress=False, output=24):
69 72
     # initialize the input shape and channel dimension, assuming
70 73
     # TensorFlow/channels-last ordering
71 74
     inputShape = (width, height, 1)
@@ -77,7 +80,7 @@ def create_cnn(width, height, depth, filters=(4, 6), kernel_size=(5, 6), regress
77 80
     x = inputs
78 81
 
79 82
     # CONV => RELU => BN => POOL
80
-    x = Conv2D(32, kernel_size, strides=2, padding="same")(x)
83
+    x = Conv2D(filters, kernel_size, strides=2, padding="same")(x)
81 84
     x = Activation("relu")(x)
82 85
     x = BatchNormalization(axis=chanDim)(x)
83 86
     # x = MaxPooling2D(pool_size=(2, 2))(x)
@@ -111,8 +114,8 @@ def create_cnn(width, height, depth, filters=(4, 6), kernel_size=(5, 6), regress
111 114
 
112 115
 # create the MLP and CNN models
113 116
 mlp = create_mlp(train_x_c.shape[1], regress=False)
114
-cnn_0 = create_cnn(18, 18, 1, kernel_size=(10, 6), regress=False, output=256)
115
-cnn_1 = create_cnn(18, 2, 1, kernel_size=(10,2), regress=False, output=36)
117
+cnn_0 = create_cnn(18, 18, 1, kernel_size=(5, 6), filters=48, regress=False, output=256)
118
+cnn_1 = create_cnn(18, 8, 1, kernel_size=(5, 6), filters=32, regress=False, output=128)
116 119
 
117 120
 # create the input to our final set of layers as the *output* of both
118 121
 # the MLP and CNN
@@ -120,10 +123,10 @@ combinedInput = concatenate([mlp.output, cnn_0.output, cnn_1.output])
120 123
 
121 124
 # our final FC layer head will have two dense layers, the final one
122 125
 # being our regression head
123
-x = Dense(512, activation="relu", kernel_regularizer=regularizers.l1(0.003))(combinedInput)
126
+x = Dense(888, activation="relu", kernel_regularizer=regularizers.l1(0.003))(combinedInput)
124 127
 x = Dropout(0.2)(x)
125
-x = Dense(512, activation="relu")(x)
126
-x = Dense(512, activation="relu")(x)
128
+x = Dense(888, activation="relu")(x)
129
+x = Dense(888, activation="relu")(x)
127 130
 # 在建设一层
128 131
 x = Dense(5, activation="softmax")(x)
129 132
 
@@ -148,14 +151,14 @@ model.fit(
148 151
     [train_x_c, train_x_a, train_x_b], train_y,
149 152
     # validation_data=([testAttrX, testImagesX], testY),
150 153
     # epochs=int(3*train_x_a.shape[0]/1300),
151
-    epochs=100,
152
-    batch_size=2048, shuffle=True)
154
+    epochs=epochs,
155
+    batch_size=1024, shuffle=True)
153 156
 
154 157
 test_x_a = test_x[:,:18*18]
155 158
 test_x_a = test_x_a.reshape(test_x.shape[0], 18, 18, 1)
156
-test_x_b = test_x[:, 18*18:18*18+2*18]
157
-test_x_b = test_x_b.reshape(test_x.shape[0], 18, 2, 1)
158
-test_x_c = test_x[:,18*18+2*18:]
159
+test_x_b = test_x[:, 18*18:18*18+8*18]
160
+test_x_b = test_x_b.reshape(test_x.shape[0], 18, 8, 1)
161
+test_x_c = test_x[:,18*18+8*18:]
159 162
 
160 163
 # make predictions on the testing data
161 164
 print("[INFO] predicting house prices...")
@@ -165,6 +168,6 @@ print(score)
165 168
 print('Test score:', score[0])
166 169
 print('Test accuracy:', score[1])
167 170
 
168
-path="18_18d_mix_seq.h5"
171
+path="19_18d_mix_seq.h5"
169 172
 model.save(path)
170 173
 model=None