# 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) :)

• Have you looked into the Predict function to get started? Commented Jul 8, 2019 at 1:53
• My understanding is that Predict is more of a statistical function not a NN per se.... am I correct? Commented Jul 8, 2019 at 10:16
• @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. Commented Jul 8, 2019 at 11:33
• @CuriousDudeFromEgypt Predict[data, Method -> "NeuralNetwork"] does use NeuralNetwork. Commented Jul 8, 2019 at 11:40
• I retracted the flag because I found the person in the wolfram community dealing with the topic. Commented Jul 8, 2019 at 11:48

This is for example

data = Import@
trainset =
Table[data[[i]] -> data[[i, 4]], {i, 2,
Length@data - .3 Length@data}];
predictor=Predict[trainset, Method -> "NeuralNetwork"]
List @@ predictor // "Model" /. # & // "Network" /. # &


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

trainet=NetGraph[
{BatchNormalizationLayer[],
LinearLayer[30],
ElementwiseLayer[LogisticSigmoid[-500#]*(1*Exp[#]-1)+LogisticSigmoid[500#]*#&],LinearLayer[1]},
{NetPort["Input"]->1,
1->2,
2->3,
3->4},"Input"->7,"Output"->1]


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;
ListLinePlot@Flatten@errors


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

• 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? Commented Jul 25, 2019 at 17:26
• 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 Commented Jul 25, 2019 at 17:35
• Hmmm...so 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? Commented Jul 25, 2019 at 20:01
• please check my update. Commented Jul 25, 2019 at 22:03