Output prediction Problem for ANN

2 views (last 30 days)
Mario Viola
Mario Viola on 29 Jan 2021
Answered: Rishik Ramena on 5 Feb 2021
I'm trying to implement an ANN (shallow, one hidden layer, not of the recurrent/convolutional type), in which based on closing prices of stock data and some other inputs (so far, just moving averages, as i'm tryinfìg to develop it for the first time), it should predict the direction of next day return, =1 if it is positive, =0 otherwise. Of course i'm feeding the network with those target values, in which i calculated the returns and labeled them as 0/1 accordingly. Even tho in both of the layers i am using the logsig transformation function, the outputs are always around 0.5, while i need them expressed as a 0 or 1. I was thinking about rounding them by myself (i.e. if output >0.5 --> output =1, some for the negative case), but i suppose the network should be able to do it by itself, fixing the errors that i probably (almost for sure) made.
I will add the code i wrote so far, i really hope you can help me some way. Any type of suggestion would be really appreciated as well.
StockData = readtable('ENI.MI.csv');
Open =cellfun(@str2double,StockData.Open);
High = cellfun(@str2double,StockData.High);
Low = cellfun(@str2double,StockData.Low);
Close = cellfun(@str2double,StockData.Close);
AdjustedClose = cellfun(@str2double,StockData.AdjClose);
Volume = cellfun(@str2double,StockData.Volume);
Date = StockData.Date;
StockData_TimeTable = timetable(Date,Open,High,Low,Close);
if any(any(ismissing(Close)))== 1
Close = fillmissing(Close,'linear');
%Prices are from the furthest to the nearest, flip to calculate direction
%of return (1 if >0, 0 otherwise);
Close = flip(Close);
Signal = zeros(length(Close),1);
for i=1:length(Close)-1;
if Close(i) > Close(i+1);
Signal(i) = 1;
Signal(i) =0;
%The last value of Signal is 0 as it has not been calculated, so i deleted it. As for the first (actual day) value of Close, i decided
%to feed the network with last day closing prices (and predictors as well) and actual day target output (direction of the market).
Signal = Signal(1:end-1);
Close = Close(2:end);
Close = flip(Close);
Signal = flip(Signal);
%Close = Data(:,4);
Ma9 = movavg(Close,'simple',9);
Ma18 = movavg(Close,'simple',18);
EMa9 = movavg(Close,'exponential',9);
EMa18 = movavg(Close,'exponential',18);
X = [Close Ma9 Ma18 EMa9 EMa18];
hiddenLayerSize = 30;
net = fitnet(hiddenLayerSize);
net.divideFcn = 'divideblock';
net.layers{1}.transferFcn = 'logsig';
net.layers{2}.transferFcn = 'logsig';
net.trainParam.max_fail =100;
net.performFcn = 'crossentropy';
xt = X';
yt = Signal';
[net tr] = train(net, xt, yt);

Answers (1)

Rishik Ramena
Rishik Ramena on 5 Feb 2021
Your code looks just fine. Do you intend to compare the closing prices of one day with the closing prices of the next day or with the opening prices of the next day?
As you are using the logsig transfer function, you are supposed to get values between 0 and 1. You might want to use the classify function at the end of your training to classify using the 0-1 labels.

Community Treasure Hunt

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

Start Hunting!