How do I create & train a neural net to predict a stock closing price at T+5min using OHLC and other data?



I'm trying to create a neural network and train it to test predictability of short term stock price movements.

I've collated a 1-min open, high, low, closing and volume dataset for a particular stock. The idea is to train a network to crunch the data for times T - 1, T - 2, T - 3, T - 4, T - 5 to predict T + 5 closing price. Ideally, the network takes 7 input vectors

{{open}, {high}, {low}, {close}, {volume}, {dayofweek}, {minutes_since_open}}

over the past 5 minutes, i.e., 7 inputs x 5 time-steps, to produce a single output: {close} at T + 5.

I'm still a bit rough, dusting off my Mathematica skills, as the last time I used it was with V7, before all the new features :)

Would really love some help here...

Here's a link to the data set (csv) :)


Posted 2019-07-07T21:59:39.363

Reputation: 363

Have you looked into the Predict function to get started? – kickert – 2019-07-08T01:53:26.703

My understanding is that Predict is more of a statistical function not a NN per se.... am I correct? – CuriousDudeFromEgypt – 2019-07-08T10:16:46.570

@CuriousDudeFromEgypt, Predict is considered supervised machine learning. You put labeled data in and it builds a model you can use for future data. I recommend you start there before you move into the NetTrain family of functions. – kickert – 2019-07-08T11:33:14.200

@CuriousDudeFromEgypt Predict[data, Method -> "NeuralNetwork"] does use NeuralNetwork. – Xminer – 2019-07-08T11:40:25.857

I retracted the flag because I found the person in the wolfram community dealing with the topic. – Xminer – 2019-07-08T11:48:47.040

@Xminer will give this a spin... (a) do you know what type of neural net does Predict use? (b) I managed to create & train a sample subset of the data using a GatedRecurrentLayer[10] + LinearLayer (borrowing from another post), what’s a better network chain? (c) is there any documentation/pointer that explains how many inputs does each layer type have, how many outputs? I’m still a bit confused... Thanks :) – CuriousDudeFromEgypt – 2019-07-08T20:35:34.517

(a) after run Predict,you can see NN with List @@ % // "Model" /. # & // "Network" /. # & (b) optimal structure depends on your objective function,the problem you're working on. For What (really important) are you trying to predict the short-term movement? (c) How to visualize Network Structure,you mean? Play with NetGraph Examples

– Xminer – 2019-07-09T07:33:02.453

@Xminer ... so, I couldn't get Predict to work with my input vectors, not sure what I'm doing wrong (error msg below):
Predict::mlincfttp: Incompatible variable type (Numerical) and variable value ({30.7399}).
An example of the input vectors below:
{{{31.07, 30.95, 30.99, 30.946, 30.915}, {31.12, 31.05, 31., 30.97, 30.97}, {30.9, 30.905, 30.88, 30.895, 30.89}, {30.96, 30.99, 30.94, 30.9154, 30.895}, {1216676, 600033, 462164, 357901, 248249}, {1, 2, 3, 4, 5}, {5, 5, 5, 5, 5}} -> {30.7399},{..etc..}
– CuriousDudeFromEgypt – 2019-07-16T17:53:28.437

@CuriousDudeFromEgypt Why close-open is always negative in your data? – Xminer – 2019-08-01T09:11:16.020



This is for example

data = Import@
  "C:\\Users\\myAccount\\Downloads\\fb_1min_1q2019_ohlcvmd (2).csv";
trainset = 
 Table[data[[i]] -> data[[i, 4]], {i, 2, 
   Length@data - .3 Length@data}];
predictor=Predict[trainset, Method -> "NeuralNetwork"]
List @@ predictor // "Model" /. # & // "Network" /. # &

Mathematica graphics

Next,For Answer.

First Setting Up the Network. I used eLU as activation layer.


Mathematica graphics

Train it.

trainedNet = 
  NetTrain[trainet, <|"Input" -> Keys@trainset, 
    "Output" -> Evaluate@({#} & /@ Values@trainset)|>, 
   LossFunction -> MeanAbsoluteLossLayer[]];

Make ValidationSet And Check the error.

validationset = 
  Table[data[[i]] -> data[[i, 4]], {i, 
    IntegerPart[Length@data - .3 Length@data + 1], Length@data}];
errors = (trainedNet[Keys@#1] - Values@#1) & /@ validationset;

Mathematica graphics

When Predict receives a matrix as an input, it is internally vectorized, so it seems that there is no problem in this way. enter image description here


Posted 2019-07-07T21:59:39.363

Reputation: 1 119

I spent some time trying to get my head around the example above...some stuff I don't understand... could you break down List @@ % // "Model" /. # & // "Network" /. # & to me please? Also the training set is a {7} -> {1} set....what I'm looking for is a {5x7}->{1} set to use for training, is there a way to use Predict with that? – CuriousDudeFromEgypt – 2019-07-25T17:26:15.107

updated my code. check the structure of List@@predictor,then you'll notice something like <|"Model"-> ....<|"Network"->...|>|> I used 7-dimensional input in the answer because it took time, or as an example it was cumbersome. Try replacing the input part with Flatten@data [[i-4;;i]]-> data[[i,4]] and "Input"->7 with "Input"->35 – Xminer – 2019-07-25T17:35:27.363 the idea is to flatten the 5x7 input matrix into 35? Also, the 5 = T-1, T-2... T-5 vectors , each vector is o,h,l,c,vol,minuteindex,dayoftheweek. The o,h,l,c numbers are similar but vol is different, min runs from 1 to 390 and day runs from 1 to 5... wouldn’t flattening make a mess of all of that? Also does normalization also make its own mess? Is it better to normalize each vector individually before feeding the network? – CuriousDudeFromEgypt – 2019-07-25T20:01:04.150

please check my update. – Xminer – 2019-07-25T22:03:55.510