MATLAB code to predict stock price

6 views (last 30 days)
Bhaswar Sarkar
Bhaswar Sarkar on 31 Jul 2017
Commented: Abolfazl Nejatian on 13 Dec 2020
I am trying to build a neural network to predict stock market data. However, since I am new to MATLAB i just following the GUI way to build the model. My code generated by MATALAB is as below. I have a doubt. Is the value ys calculated in StepAhead section holding the future values ahead in time. Can someone explain this to me ? Is there training material on this ?
% IP_data - input time series. % OP_data - feedback time series.
X = tonndata(IP_data,false,false); T = tonndata(OP_data,false,false);
trainFcn = 'trainlm'; % Levenberg-Marquardt backpropagation.
% Create a Nonlinear Autoregressive Network with External Input inputDelays = 1:5; feedbackDelays = 1:5; hiddenLayerSize = 20; net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize,'open',trainFcn);
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'}; net.inputs{2}.processFcns = {'removeconstantrows','mapminmax'};
[x,xi,ai,t] = preparets(net,X,{},T);
net.divideFcn = 'dividerand'; % Divide data randomly net.divideMode = 'time'; % Divide up every sample net.divideParam.trainRatio = 70/100; net.divideParam.valRatio = 15/100; net.divideParam.testRatio = 15/100;
net.performFcn = 'mse'; % Mean Squared Error
net.plotFcns = {'plotperform','plottrainstate', 'ploterrhist', ... 'plotregression', 'plotresponse', 'ploterrcorr', 'plotinerrcorr'};
[net,tr] = train(net,x,t,xi,ai);
% Test the Network y = net(x,xi,ai); e = gsubtract(t,y); performance = perform(net,t,y)
% Recalculate Training, Validation and Test Performance trainTargets = gmultiply(t,tr.trainMask); valTargets = gmultiply(t,tr.valMask); testTargets = gmultiply(t,tr.testMask); trainPerformance = perform(net,trainTargets,y) valPerformance = perform(net,valTargets,y) testPerformance = perform(net,testTargets,y)
% View the Network view(net)
netc = closeloop(net); netc.name = [net.name ' - Closed Loop']; view(netc) [xc,xic,aic,tc] = preparets(netc,X,{},T); yc = netc(xc,xic,aic); closedLoopPerformance = perform(net,tc,yc)
% Multi-step Prediction numTimesteps = size(x,2); knownOutputTimesteps = 1:(numTimesteps-5); predictOutputTimesteps = (numTimesteps-4):numTimesteps; X1 = X(:,knownOutputTimesteps); T1 = T(:,knownOutputTimesteps); [x1,xio,aio] = preparets(net,X1,{},T1); [y1,xfo,afo] = net(x1,xio,aio); % Next the the network and its final states will be converted to % closed-loop form to make five predictions with only the five inputs % provided. x2 = X(1,predictOutputTimesteps); [netc,xic,aic] = closeloop(net,xfo,afo); [y2,xfc,afc] = netc(x2,xic,aic); multiStepPerformance = perform(net,T(1,predictOutputTimesteps),y2) % Step-Ahead Prediction Network % For some applications it helps to get the prediction a timestep early. % The original network returns predicted y(t+1) at the same time it is % given y(t+1). For some applications such as decision making, it would % help to have predicted y(t+1) once y(t) is available, but before the % actual y(t+1) occurs. The network can be made to return its output a % timestep early by removing one delay so that its minimal tap delay is now % 0 instead of 1. The new network returns the same outputs as the original % network, but outputs are shifted left one timestep. nets = removedelay(net); nets.name = [net.name ' - Predict One Step Ahead']; view(nets) [xs,xis,ais,ts] = preparets(nets,X,{},T); ys = nets(xs,xis,ais); stepAheadPerformance = perform(nets,ts,ys)
  1 Comment
Abolfazl Nejatian
Abolfazl Nejatian on 13 Dec 2020
This is my forecasting code which allows you to predict your time series data with LSTM, CNN, and MLP Networks.
please visit my Mathworks account and download it from the link below:

Sign in to comment.

Answers (1)

Greg Heath
Greg Heath on 1 Aug 2017
In addition to the documentation
help narxnet
doc narxnet
There are hundreds of NEWSGROUP and ANSWERS posts. For example, search
greg narxnet
and
greg narxnet tutorial
Hope this helps.
Greg
  1 Comment
Greg Heath
Greg Heath on 1 Aug 2017
I disagree with MATLAB w.r.t. the best way to learn NNs.
I prefer to start with the help and doc command line examples that use default parameter settings. Then the NEWBIE is not bombarded with information that is peripheral to learning basics.
Greg

Sign in to comment.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!