yufeng 4 years ago
parent
commit
08b772217f
4 changed files with 224 additions and 15 deletions
  1. 1 1
      mix/mix_predict_180.py
  2. 19 4
      mix/mix_predict_everyday.py
  3. 10 10
      mix/mix_train_180.py
  4. 194 0
      mix/mix_train_200.py

+ 1 - 1
mix/mix_predict_180.py

@@ -92,7 +92,7 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
92 92
 
93 93
 if __name__ == '__main__':
94 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\\stock186A_18d_train1.log', model_path='186A_18d_mix_6D_ma5_s_seq.h5')
95
+    predict(file_path='D:\\data\\quantization\\stock186E_18d_train1.log', model_path='186E_18d_mix_6D_ma5_s_seq.h5')
96 96
     # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
97 97
     # multi_predict(model='15_18d')
98 98
     # predict_today(20200229, model='11_18d')

+ 19 - 4
mix/mix_predict_everyday.py

@@ -18,10 +18,25 @@ holder_stock_list = [
18 18
                         # 基础建设
19 19
                         '300041.SZ', '603568.SH', '000967.SZ', '603018.SH',
20 20
                         # B
21
-                        '002555.SZ', '002174.SZ'
22
-
23
-
24
-    ]
21
+                        '002555.SZ', '002174.SZ',
22
+                        # ROE
23
+    '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',
24
+    '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',
25
+    '603949.SH', '002970.SZ', '300809.SZ', '002968.SZ', '300559.SZ', '002512.SZ', '300783.SZ', '300003.SZ', '603489.SH', '300564.SZ', '600802.SH', '002600.SZ',
26
+    '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',
27
+    '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',
28
+    '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',
29
+    '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',
30
+    '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',
31
+    '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',
32
+    '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',
33
+    '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',
34
+    '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',
35
+    '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',
36
+    '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',
37
+
38
+
39
+]
25 40
 
26 41
 
27 42
 def read_data(path):

+ 10 - 10
mix/mix_train_180.py

@@ -18,11 +18,11 @@ from keras.callbacks import EarlyStopping
18 18
 
19 19
 early_stopping = EarlyStopping(monitor='accuracy', patience=5, verbose=2)
20 20
 
21
-epochs= 138
22
-size = 400000 #18W 60W
23
-file_path = 'D:\\data\\quantization\\stock186B_18d_train2.log'
24
-model_path = '186B_18d_mix_6D_ma5_s_seq.h5'
25
-file_path1='D:\\data\\quantization\\stock186B_18d_test.log'
21
+epochs= 68
22
+size = 380000 #18W 60W
23
+file_path = 'D:\\data\\quantization\\stock186E_18d_train2.log'
24
+model_path = '186E_18d_mix_6D_ma5_s_seq.h5'
25
+file_path1='D:\\data\\quantization\\stock186E_18d_test.log'
26 26
 '''
27 27
 1 MACD                      
28 28
 4 MACD不在cnn范围内         50,97,43
@@ -34,10 +34,10 @@ file_path1='D:\\data\\quantization\\stock186B_18d_test.log'
34 34
 '''
35 35
 '''
36 36
 a 加上大盘涨停和个股统计 涨停/跌停/创业板涨停跌停   39,98,39
37
-b 加上大盘涨停和个股统计 t1,t2,t3....    
38
-c MACD + ROC
39
-d DMI + ROC
40
-e RSI + ROC           
37
+b 加上大盘涨停和个股统计 t1,t2,t3....               35,98,41
38
+c MACD + ROC                                        50,98,43
39
+d DMI + ROC                                         48,98,43
40
+e RSI + ROC                                         44,97,43
41 41
 '''
42 42
 
43 43
 def read_data(path, path1=file_path1):
@@ -48,7 +48,7 @@ def read_data(path, path1=file_path1):
48 48
             lines.append(line)
49 49
 
50 50
     with open(path1) as f:
51
-        for x in range(50000):
51
+        for x in range(20000):
52 52
             line = eval(f.readline().strip())
53 53
             lines.append(line)
54 54
 

+ 194 - 0
mix/mix_train_200.py

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