lilianweng / stock-rnn

Predict stock market prices using RNN model with multilayer LSTM cells + optional multi-stock embeddings.
https://lilianweng.github.io/lil-log
1.72k stars 656 forks source link

The prediction accuracy is not good enough to tell the trend happens in the real market #28

Open Zhang-Jack opened 5 years ago

Zhang-Jack commented 5 years ago

Hi, Thank you for your sharing, it is very good tutorial for us to learn how to predict stock price with LSTM method. I tested the SP500 data with lstm = 128 and epoch =500, but the result is not so good. As the log shows below, the days that the network predict correctly is always up and down around the base value 103, and the profit is also going up and down around 0. So I wonder how to optimize this network to make it more accuracy and profitable? Could you please give me some hint or tutorial? Thank you very much! log_20181218.txt

(base) D:\Codes\lstm_stock_prediction\stock-rnn>python main.py --stock_symbol=SP500 --train --input_size=1 --lstm_size=128 --max_epoch=500 Start training for stocks: ['SP500'] Step:1 [Epoch:0] [Learning rate: 0.001000] train_loss:0.010059 test_loss:0.005225 profit = -0.025619479475635365, correct days = 103 Step:101 [Epoch:0] [Learning rate: 0.001000] train_loss:0.001643 test_loss:0.001153 profit = -0.025619479475635365, correct days = 103 Step:201 [Epoch:0] [Learning rate: 0.001000] train_loss:0.000441 test_loss:0.000495 profit = -0.025619479475635365, correct days = 103 Step:301 [Epoch:1] [Learning rate: 0.001000] train_loss:0.000119 test_loss:0.000147 profit = 0.03595531261737828, correct days = 109 Step:401 [Epoch:1] [Learning rate: 0.001000] train_loss:0.000701 test_loss:0.000571 profit = -0.025619479475635365, correct days = 103 Step:501 [Epoch:1] [Learning rate: 0.001000] train_loss:0.000854 test_loss:0.000868 profit = -0.025619479475635365, correct days = 103 Step:601 [Epoch:2] [Learning rate: 0.001000] train_loss:0.000214 test_loss:0.000535 profit = -0.0031643654923697584, correct days = 96 Step:701 [Epoch:2] [Learning rate: 0.001000] train_loss:0.000310 test_loss:0.000181 profit = -0.07838784660630804, correct days = 99 Step:801 [Epoch:3] [Learning rate: 0.001000] train_loss:0.000716 test_loss:0.000284 profit = 0.013234707421988623, correct days = 98 Step:901 [Epoch:3] [Learning rate: 0.001000] train_loss:0.000177 test_loss:0.000135 profit = -0.03711239240076847, correct days = 105 Step:1001 [Epoch:3] [Learning rate: 0.001000] train_loss:0.000171 test_loss:0.000161 profit = -0.15784789364253704, correct days = 94 Step:1101 [Epoch:4] [Learning rate: 0.001000] train_loss:0.000148 test_loss:0.000140 profit = -0.09045673542451504, correct days = 103 Step:1201 [Epoch:4] [Learning rate: 0.001000] train_loss:0.000112 test_loss:0.000126 profit = 0.011780450020617894, correct days = 96 Step:1301 [Epoch:5] [Learning rate: 0.000990] train_loss:0.000123 test_loss:0.000091 profit = 0.2644821257042925, correct days = 108 Step:1401 [Epoch:5] [Learning rate: 0.000990] train_loss:0.000087 test_loss:0.000110 profit = 0.03148794396024146, correct days = 109 Step:1501 [Epoch:5] [Learning rate: 0.000990] train_loss:0.000095 test_loss:0.000151 profit = -0.018273540195676286, correct days = 91 Step:1601 [Epoch:6] [Learning rate: 0.000980] train_loss:0.000209 test_loss:0.000134 profit = -0.10868626366788636, correct days = 95 Step:1701 [Epoch:6] [Learning rate: 0.000980] train_loss:0.000080 test_loss:0.000109 profit = 0.020400411456001355, correct days = 99 Step:1801 [Epoch:6] [Learning rate: 0.000980] train_loss:0.000095 test_loss:0.000112 profit = -0.014259134507715987, correct days = 102 Step:1901 [Epoch:7] [Learning rate: 0.000970] train_loss:0.000123 test_loss:0.000107 profit = -0.00934783026174768, correct days = 104 Step:2001 [Epoch:7] [Learning rate: 0.000970] train_loss:0.000070 test_loss:0.000113 profit = 0.1037896495909153, correct days = 102 Step:2101 [Epoch:8] [Learning rate: 0.000961] train_loss:0.000118 test_loss:0.000111 profit = -0.08947914245565769, correct days = 97 Step:2201 [Epoch:8] [Learning rate: 0.000961] train_loss:0.000136 test_loss:0.000178 profit = -0.02450670956447809, correct days = 102 Step:2301 [Epoch:8] [Learning rate: 0.000961] train_loss:0.000100 test_loss:0.000102 profit = -0.21570318289249812, correct days = 98 Step:2401 [Epoch:9] [Learning rate: 0.000951] train_loss:0.000088 test_loss:0.000083 profit = 0.09128718752173082, correct days = 107 Step:2501 [Epoch:9] [Learning rate: 0.000951] train_loss:0.000074 test_loss:0.000093 profit = -0.08180089271755475, correct days = 88 Step:2601 [Epoch:10] [Learning rate: 0.000941] train_loss:0.000094 test_loss:0.000108 profit = 0.011144804352940407, correct days = 100 Step:2701 [Epoch:10] [Learning rate: 0.000941] train_loss:0.000228 test_loss:0.000209 profit = 0.021127617819259314, correct days = 98 Step:2801 [Epoch:10] [Learning rate: 0.000941] train_loss:0.000115 test_loss:0.000087 profit = -0.032739640272618664, correct days = 96 Step:2901 [Epoch:11] [Learning rate: 0.000932] train_loss:0.000293 test_loss:0.000187 profit = 0.007579035723463634, correct days = 98 Step:3001 [Epoch:11] [Learning rate: 0.000932] train_loss:0.000024 test_loss:0.000093 profit = -0.0618005583512683, correct days = 100 Step:3101 [Epoch:12] [Learning rate: 0.000923] train_loss:0.000091 test_loss:0.000100 profit = -0.05968355101690015, correct days = 102 Step:3201 [Epoch:12] [Learning rate: 0.000923] train_loss:0.000051 test_loss:0.000099 profit = -0.09661448827067709, correct days = 104 Step:3301 [Epoch:12] [Learning rate: 0.000923] train_loss:0.000093 test_loss:0.000084 profit = 0.0021639726211727384, correct days = 94 Step:3401 [Epoch:13] [Learning rate: 0.000914] train_loss:0.000090 test_loss:0.000090 profit = 0.06872824583359372, correct days = 101 Step:3501 [Epoch:13] [Learning rate: 0.000914] train_loss:0.000041 test_loss:0.000098 profit = 0.023954294962177713, correct days = 103 Step:3601 [Epoch:13] [Learning rate: 0.000914] train_loss:0.000076 test_loss:0.000096 profit = -0.04558137123357253, correct days = 97 Step:3701 [Epoch:14] [Learning rate: 0.000904] train_loss:0.000054 test_loss:0.000085 profit = 0.15187505724517514, correct days = 108 Step:3801 [Epoch:14] [Learning rate: 0.000904] train_loss:0.000096 test_loss:0.000129 profit = 0.07888562137218325, correct days = 101 Step:3901 [Epoch:15] [Learning rate: 0.000895] train_loss:0.000121 test_loss:0.000094 profit = 0.0340183348742743, correct days = 96 Step:4001 [Epoch:15] [Learning rate: 0.000895] train_loss:0.000532 test_loss:0.000122 profit = 0.031501323257416725, correct days = 101 Step:4101 [Epoch:15] [Learning rate: 0.000895] train_loss:0.000069 test_loss:0.000086 profit = -0.21811953823266494, correct days = 90 Step:4201 [Epoch:16] [Learning rate: 0.000886] train_loss:0.000150 test_loss:0.000087 profit = 0.19773472237464762, correct days = 111 Step:4301 [Epoch:16] [Learning rate: 0.000886] train_loss:0.000075 test_loss:0.000092 profit = -0.045499799223870485, correct days = 100 Step:4401 [Epoch:17] [Learning rate: 0.000878] train_loss:0.000101 test_loss:0.000096 profit = 0.0660783915371651, correct days = 91 Step:4501 [Epoch:17] [Learning rate: 0.000878] train_loss:0.000078 test_loss:0.000110 profit = -0.028332832025963373, correct days = 98 Step:4601 [Epoch:17] [Learning rate: 0.000878] train_loss:0.000160 test_loss:0.000092 profit = -0.031151056620531414, correct days = 98 Step:4701 [Epoch:18] [Learning rate: 0.000869] train_loss:0.000140 test_loss:0.000083 profit = 0.17060743776145848, correct days = 113 Step:4801 [Epoch:18] [Learning rate: 0.000869] train_loss:0.000104 test_loss:0.000081 profit = 0.02184867743697627, correct days = 99 Step:4901 [Epoch:18] [Learning rate: 0.000869] train_loss:0.000080 test_loss:0.000093 profit = -0.02471952081072526, correct days = 95 Step:5001 [Epoch:19] [Learning rate: 0.000860] train_loss:0.000087 test_loss:0.000096 profit = 0.2184404621144641, correct days = 107 Step:5101 [Epoch:19] [Learning rate: 0.000860] train_loss:0.000172 test_loss:0.000083 profit = -0.011192151034966291, correct days = 98 Step:5201 [Epoch:20] [Learning rate: 0.000851] train_loss:0.000226 test_loss:0.000089 profit = 0.14785778266773175, correct days = 107 Step:5301 [Epoch:20] [Learning rate: 0.000851] train_loss:0.000060 test_loss:0.000084 profit = 0.1429639411365925, correct days = 103 Step:5401 [Epoch:20] [Learning rate: 0.000851] train_loss:0.000066 test_loss:0.000092 profit = 0.08617641740816884, correct days = 103 Step:5501 [Epoch:21] [Learning rate: 0.000843] train_loss:0.000162 test_loss:0.000096 profit = 0.15679533090121722, correct days = 103 Step:5601 [Epoch:21] [Learning rate: 0.000843] train_loss:0.000127 test_loss:0.000091 profit = -0.06225312490171231, correct days = 93 Step:5701 [Epoch:22] [Learning rate: 0.000835] train_loss:0.000086 test_loss:0.000094 profit = 0.006754305152290363, correct days = 103 Step:5801 [Epoch:22] [Learning rate: 0.000835] train_loss:0.000057 test_loss:0.000080 profit = 0.07339482376505135, correct days = 98 Step:5901 [Epoch:22] [Learning rate: 0.000835] train_loss:0.000087 test_loss:0.000091 profit = -0.0453350830909246, correct days = 98 Step:6001 [Epoch:23] [Learning rate: 0.000826] train_loss:0.000055 test_loss:0.000094 profit = -0.06847208303629626, correct days = 105 Step:6101 [Epoch:23] [Learning rate: 0.000826] train_loss:0.000188 test_loss:0.000078 profit = 0.11029498773202973, correct days = 108 Step:6201 [Epoch:24] [Learning rate: 0.000818] train_loss:0.000147 test_loss:0.000083 profit = -0.028336466555966555, correct days = 91 Step:6301 [Epoch:24] [Learning rate: 0.000818] train_loss:0.000090 test_loss:0.000084 profit = -0.1484526580651776, correct days = 86 Step:6401 [Epoch:24] [Learning rate: 0.000818] train_loss:0.000113 test_loss:0.000086 profit = 0.012674613668498425, correct days = 99 Step:6501 [Epoch:25] [Learning rate: 0.000810] train_loss:0.000153 test_loss:0.000088 profit = 0.04810375951538559, correct days = 109 Step:6601 [Epoch:25] [Learning rate: 0.000810] train_loss:0.000066 test_loss:0.000083 profit = -0.1404029246915609, correct days = 93 Step:6701 [Epoch:25] [Learning rate: 0.000810] train_loss:0.000041 test_loss:0.000088 profit = -0.05891110625639906, correct days = 91 Step:6801 [Epoch:26] [Learning rate: 0.000802] train_loss:0.000057 test_loss:0.000088 profit = 0.1322223423826442, correct days = 104 Step:6901 [Epoch:26] [Learning rate: 0.000802] train_loss:0.000063 test_loss:0.000085 profit = -0.06354392570483236, correct days = 88 Step:7001 [Epoch:27] [Learning rate: 0.000794] train_loss:0.000021 test_loss:0.000084 profit = -0.05990871436288636, correct days = 92 Step:7101 [Epoch:27] [Learning rate: 0.000794] train_loss:0.000073 test_loss:0.000083 profit = -0.06882257311584372, correct days = 95 Step:7201 [Epoch:27] [Learning rate: 0.000794] train_loss:0.000085 test_loss:0.000084 profit = 0.009471227578168873, correct days = 101 Step:7301 [Epoch:28] [Learning rate: 0.000786] train_loss:0.000332 test_loss:0.000088 profit = 0.013246611564542321, correct days = 98 Step:7401 [Epoch:28] [Learning rate: 0.000786] train_loss:0.000026 test_loss:0.000080 profit = -0.004331457631420066, correct days = 90 Step:7501 [Epoch:29] [Learning rate: 0.000778] train_loss:0.000102 test_loss:0.000082 profit = 0.08324904433963864, correct days = 105 Step:7601 [Epoch:29] [Learning rate: 0.000778] train_loss:0.000149 test_loss:0.000089 profit = -0.06631912575570487, correct days = 96 Step:7701 [Epoch:29] [Learning rate: 0.000778] train_loss:0.000032 test_loss:0.000078 profit = 0.11948236955133507, correct days = 108 Step:7801 [Epoch:30] [Learning rate: 0.000770] train_loss:0.000074 test_loss:0.000078 profit = 0.139726090498706, correct days = 106 Step:7901 [Epoch:30] [Learning rate: 0.000770] train_loss:0.000034 test_loss:0.000076 profit = -0.016972300396177586, correct days = 99 Step:8001 [Epoch:31] [Learning rate: 0.000762] train_loss:0.001605 test_loss:0.000131 profit = -0.09545949421338473, correct days = 90 Step:8101 [Epoch:31] [Learning rate: 0.000762] train_loss:0.000035 test_loss:0.000081 profit = 0.08556577392037978, correct days = 103 Step:8201 [Epoch:31] [Learning rate: 0.000762] train_loss:0.000091 test_loss:0.000091 profit = -0.19325060724099796, correct days = 94 Step:8301 [Epoch:32] [Learning rate: 0.000755] train_loss:0.000044 test_loss:0.000087 profit = -0.10109258556147382, correct days = 100 Step:8401 [Epoch:32] [Learning rate: 0.000755] train_loss:0.000031 test_loss:0.000079 profit = -0.11426217750847367, correct days = 103 Step:8501 [Epoch:32] [Learning rate: 0.000755] train_loss:0.000041 test_loss:0.000083 profit = 0.15642649504928452, correct days = 109 Step:8601 [Epoch:33] [Learning rate: 0.000747] train_loss:0.000079 test_loss:0.000079 profit = -0.13959935335863138, correct days = 97 Step:8701 [Epoch:33] [Learning rate: 0.000747] train_loss:0.000135 test_loss:0.000082 profit = -0.13748259876316216, correct days = 95 Step:8801 [Epoch:34] [Learning rate: 0.000740] train_loss:0.000061 test_loss:0.000081 profit = -0.1009435581331456, correct days = 100 Step:8901 [Epoch:34] [Learning rate: 0.000740] train_loss:0.000121 test_loss:0.000080 profit = -0.15250229851266173, correct days = 100 Step:9001 [Epoch:34] [Learning rate: 0.000740] train_loss:0.000032 test_loss:0.000084 profit = -0.0038252172792784256, correct days = 106 Step:9101 [Epoch:35] [Learning rate: 0.000732] train_loss:0.000088 test_loss:0.000082 profit = -0.026277332670350595, correct days = 94 Step:9201 [Epoch:35] [Learning rate: 0.000732] train_loss:0.000059 test_loss:0.000085 profit = -0.23764513492667605, correct days = 91 Step:9301 [Epoch:36] [Learning rate: 0.000725] train_loss:0.000049 test_loss:0.000085 profit = -0.13331856363652206, correct days = 101 Step:9401 [Epoch:36] [Learning rate: 0.000725] train_loss:0.000121 test_loss:0.000084 profit = -0.08096660712024006, correct days = 104 Step:9501 [Epoch:36] [Learning rate: 0.000725] train_loss:0.000050 test_loss:0.000079 profit = -0.035681628188596215, correct days = 97 Step:9601 [Epoch:37] [Learning rate: 0.000718] train_loss:0.000047 test_loss:0.000074 profit = 0.25255923647534206, correct days = 111 Step:9701 [Epoch:37] [Learning rate: 0.000718] train_loss:0.000052 test_loss:0.000082 profit = 0.1405303968308479, correct days = 105 Step:9801 [Epoch:37] [Learning rate: 0.000718] train_loss:0.000047 test_loss:0.000076 profit = -0.10761144730282124, correct days = 105 Step:9901 [Epoch:38] [Learning rate: 0.000711] train_loss:0.000035 test_loss:0.000078 profit = -0.23780492199793624, correct days = 93 Step:10001 [Epoch:38] [Learning rate: 0.000711] train_loss:0.000218 test_loss:0.000087 profit = 0.0986011845157021, correct days = 100 Step:10101 [Epoch:39] [Learning rate: 0.000703] train_loss:0.000072 test_loss:0.000075 profit = 0.3832697111375293, correct days = 113 Step:10201 [Epoch:39] [Learning rate: 0.000703] train_loss:0.000168 test_loss:0.000080 profit = 0.02633109726977001, correct days = 103 Step:10301 [Epoch:39] [Learning rate: 0.000703] train_loss:0.000037 test_loss:0.000082 profit = -0.07761773262888172, correct days = 102 Step:10401 [Epoch:40] [Learning rate: 0.000696] train_loss:0.000065 test_loss:0.000075 profit = 0.06679532257823129, correct days = 105 Step:10501 [Epoch:40] [Learning rate: 0.000696] train_loss:0.000049 test_loss:0.000082 profit = 0.017065803047925487, correct days = 110 Step:10601 [Epoch:41] [Learning rate: 0.000689] train_loss:0.000040 test_loss:0.000074 profit = 0.004761161756203225, correct days = 97 Step:10701 [Epoch:41] [Learning rate: 0.000689] train_loss:0.000043 test_loss:0.000078 profit = 0.14491704099142855, correct days = 100 Step:10801 [Epoch:41] [Learning rate: 0.000689] train_loss:0.000886 test_loss:0.000095 profit = -0.02807158063995374, correct days = 96 Step:10901 [Epoch:42] [Learning rate: 0.000683] train_loss:0.000043 test_loss:0.000078 profit = 0.2315954200925946, correct days = 111 Step:11001 [Epoch:42] [Learning rate: 0.000683] train_loss:0.000119 test_loss:0.000083 profit = 0.03790362437540595, correct days = 97 Step:11101 [Epoch:43] [Learning rate: 0.000676] train_loss:0.000064 test_loss:0.000089 profit = -0.005365731314907918, correct days = 103 Step:11201 [Epoch:43] [Learning rate: 0.000676] train_loss:0.000112 test_loss:0.000088 profit = 0.1674707399976857, correct days = 112 Step:11301 [Epoch:43] [Learning rate: 0.000676] train_loss:0.000103 test_loss:0.000081 profit = -0.11721664955763622, correct days = 93 Step:11401 [Epoch:44] [Learning rate: 0.000669] train_loss:0.000014 test_loss:0.000097 profit = 0.057232079693976146, correct days = 107 Step:11501 [Epoch:44] [Learning rate: 0.000669] train_loss:0.000081 test_loss:0.000079 profit = -0.052250048124326764, correct days = 93 Step:11601 [Epoch:44] [Learning rate: 0.000669] train_loss:0.000064 test_loss:0.000077 profit = 0.1571850086461729, correct days = 110 Step:11701 [Epoch:45] [Learning rate: 0.000662] train_loss:0.000121 test_loss:0.000078 profit = -0.18832862493339786, correct days = 92 Step:11801 [Epoch:45] [Learning rate: 0.000662] train_loss:0.000043 test_loss:0.000076 profit = 0.042838998582060195, correct days = 102 Step:11901 [Epoch:46] [Learning rate: 0.000656] train_loss:0.000250 test_loss:0.000078 profit = 0.043138550283145505, correct days = 101 Step:12001 [Epoch:46] [Learning rate: 0.000656] train_loss:0.000115 test_loss:0.000083 profit = -0.09371476046460014, correct days = 98 Step:12101 [Epoch:46] [Learning rate: 0.000656] train_loss:0.000033 test_loss:0.000080 profit = -0.05255161267961761, correct days = 94 Step:12201 [Epoch:47] [Learning rate: 0.000649] train_loss:0.000227 test_loss:0.000078 profit = -0.1225627615973589, correct days = 96 Step:12301 [Epoch:47] [Learning rate: 0.000649] train_loss:0.000050 test_loss:0.000080 profit = 0.15474970939366506, correct days = 117 Step:12401 [Epoch:48] [Learning rate: 0.000643] train_loss:0.000034 test_loss:0.000078 profit = 0.24680065663228767, correct days = 109 Step:12501 [Epoch:48] [Learning rate: 0.000643] train_loss:0.000111 test_loss:0.000080 profit = 0.09462146415524841, correct days = 105 Step:12601 [Epoch:48] [Learning rate: 0.000643] train_loss:0.000213 test_loss:0.000080 profit = 0.018383620867890582, correct days = 94 Step:12701 [Epoch:49] [Learning rate: 0.000636] train_loss:0.000107 test_loss:0.000079 profit = 0.06755930317461545, correct days = 102 Step:12801 [Epoch:49] [Learning rate: 0.000636] train_loss:0.000192 test_loss:0.000083 profit = -0.10714939290982162, correct days = 98 Step:12901 [Epoch:50] [Learning rate: 0.000630] train_loss:0.000079 test_loss:0.000081 profit = 0.1120229320455085, correct days = 104 Step:13001 [Epoch:50] [Learning rate: 0.000630] train_loss:0.000037 test_loss:0.000077 profit = 0.22241459898910543, correct days = 106 Step:13101 [Epoch:50] [Learning rate: 0.000630] train_loss:0.000149 test_loss:0.000078 profit = -0.03342298517580489, correct days = 102 ... Step:127901 [Epoch:495] [Learning rate: 0.000007] train_loss:0.000223 test_loss:0.000072 profit = -0.006937739971851875, correct days = 99 Step:128001 [Epoch:496] [Learning rate: 0.000007] train_loss:0.000021 test_loss:0.000072 profit = 0.07255974747543392, correct days = 105 Step:128101 [Epoch:496] [Learning rate: 0.000007] train_loss:0.000048 test_loss:0.000072 profit = -0.03654345662007563, correct days = 100 Step:128201 [Epoch:496] [Learning rate: 0.000007] train_loss:0.000022 test_loss:0.000072 profit = 0.0370857372949388, correct days = 106 Step:128301 [Epoch:497] [Learning rate: 0.000007] train_loss:0.000044 test_loss:0.000071 profit = 0.08336513334204343, correct days = 105 Step:128401 [Epoch:497] [Learning rate: 0.000007] train_loss:0.000052 test_loss:0.000072 profit = -0.001579383542026247, correct days = 101 Step:128501 [Epoch:498] [Learning rate: 0.000007] train_loss:0.000073 test_loss:0.000071 profit = 0.010168036018454285, correct days = 101 Step:128601 [Epoch:498] [Learning rate: 0.000007] train_loss:0.000269 test_loss:0.000072 profit = -0.032829104619034655, correct days = 103 Step:128701 [Epoch:498] [Learning rate: 0.000007] train_loss:0.000168 test_loss:0.000072 profit = 0.00882121564952476, correct days = 104 Step:128801 [Epoch:499] [Learning rate: 0.000007] train_loss:0.000034 test_loss:0.000071 profit = -0.00046510322141923854, correct days = 99 Step:128901 [Epoch:499] [Learning rate: 0.000007] train_loss:0.000060 test_loss:0.000071 profit = 0.03810759671788244, correct days = 98

dmallon1 commented 5 years ago

How did you get profit to show up in the output? Did you add that yourself?

Zhang-Jack commented 5 years ago

How did you get profit to show up in the output? Did you add that yourself? Hi dmalion, Yes, I added the codes to statics the profit days as below, you can check:

@@ -254,12 +256,24 @@ class LstmRNN(object): preds = _flatten(preds)[-200:] days = range(len(truths))[-200:]