5
$\begingroup$

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

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

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.

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]

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

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

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – CuriousDudeFromEgypt Jul 25 at 17:26
  • $\begingroup$ 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 $\endgroup$ – Xminer Jul 25 at 17:35
  • $\begingroup$ 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? $\endgroup$ – CuriousDudeFromEgypt Jul 25 at 20:01
  • $\begingroup$ please check my update. $\endgroup$ – Xminer Jul 25 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.