hello everyone please can someone help me with stock price prediction. That is I wrote this code and from the I vector , I want a code that will remove indexes from the I vector more than 60 seconds. Thanks

LSE_matrix =log(nstock_val); %log of the data
I=1:(size(LSE_matrix,1)-1); % selecting the indices of all prices but the last time when stock was opened
dLSE_col1 = LSE_matrix(I+1,1) - LSE_matrix(I,1);% log difference

5 Comments

Afua - how or where is time used in the above code? I see that you have valus for stock prices (?) but where is a time array?
Thanks for the feedback. This is a section of the code and i wanted to know how i can write a code to remove the time stocks index that gobeyond 60 seconds. That is we remove the indices from the "I" variable seen above. Thanks
But what variable describes the time? How, from the above code, can we determine which stock index goes beyond 60 seconds?
Atttached is of picture the corresponding times. So say we consider 498 and 498. its more than 60 sec. how do i write a code to remove such an index. Thanks for the help.
Dont you think it would be easier if you upload a sample of the data?

Sign in to comment.

 Accepted Answer

You can adapt this to your needs
A = readtable('LSE1.csv')
t = datetime(A{:,1},'inputformat','dd.MM.yyyy HH:mm:ss.SSS')
data = A{:,2:end};
id = [false;diff(t)>seconds(60)];
data(id,:) = [];
t(id) = [];
If the period between t(n) and t(n+1) is longer than 60s, then the data recorded at t(n+1) is deleted.

6 Comments

Hello thanks for the help. I need one last help. How do i get this code to remove from it indices > 60 seconds. For the open, close, high and low simultaneously.
I think that is exactly what it does.
the line
id = [false;diff(t)>seconds(60)];
finds all row indices where the previous row differs by more than 60 seconds, whereas the line
data(id,:) = [];
deletes all data in those rows.
then you are left with a vector of time, t, and a corresponding matrix of data, data.
Thanks ... but can i show you one more thing.
%Close prices in matrix
nstock_val=[Close(1:30928,1) Close1(1:30928,1) Close2(1:30928,1) Close3(1:30928,1) Close4(1:30928,1)]; % reading the data
t=[Gmttime(1:30928,1) Gmttime1(1:30928,1) Gmttime2(1:30928,1) Gmttime3(1:30928,1) Gmttime4(1:30928,1)]; % reading the data
time = datetime(t,'inputformat','dd.MM.yyyy HH:mm:ss.SSS');
LSE_matrix =log(nstock_val); %log of the datanstock_val=[Close(1:30928,1) Close1(1:30928,1) Close2(1:30928,1) Close3(1:30928,1) Close4(1:30928,1)]; % reading the data
I=1:(size(LSE_matrix,1)-1); % selecting the indices of all prices but the last time when stock was opened
data = t(:,1:end);
id = [false;diff(time(:,1))> seconds(60)]
data(id,:) = [];
dLSE_col1 = LSE_matrix(I+1,1) - LSE_matrix(I,1);% log difference
dLSE_col2 = LSE_matrix(I+1,2) - LSE_matrix(I,2);
dLSE_col3 = LSE_matrix(I+1,3) - LSE_matrix(I,3);
dLSE_col4 = LSE_matrix(I+1,4) - LSE_matrix(I,4);
dLSE_col5 = LSE_matrix(I+1,5) - LSE_matrix(I,5);
matrix_logdiff=[dLSE_col1 dLSE_col2 dLSE_col3 dLSE_col4 dLSE_col5];
nsample = floor((2/3)* length(matrix_logdiff)); %two thirds of the log
% difference of the data
matrix_logdiff_nsample = matrix_logdiff(1:nsample,:);
mean_vec = mean(matrix_logdiff_nsample); %finding the mean.
mean_vec = mean_vec';
cov_matrix = cov(matrix_logdiff_nsample); %the covariance
dt=1;
%GBM parameter estimate
capbhat = sqrtm(cov_matrix./dt);
ahat_vec = (mean_vec/dt)+ diag((capbhat).^2)/2;
s0_vector =[nstock_val(nsample,1) nstock_val(nsample,2) nstock_val(nsample,3) nstock_val(nsample,4) nstock_val(nsample,5)];
s0_vector = s0_vector';
where the different close prices are from the different LSEs
I don't understand exactly what the problem is, but I'm not a big fan of how you structure the data. I would put all my .csv files in a separate folder and read them as follows:
addpath('C:\files\') % change this to the correct path
files = dir('C:\files\*.csv'); % again..
for i = 1:numel(files)
opts = detectImportOptions(files(i).name);
opts.VariableTypes{1} = 'datetime';
opts = setvaropts(opts,'GmtTime','inputformat','dd.MM.yyyy HH:mm:ss.SSS');
A{i} = readtimetable(files(i).name,opts);
end
You will end up with 5 tables stored in cell array A. You can then append them to a single table or loop over each tables, no need to work with "Var1, Var2, Var3..."

Sign in to comment.

Categories

Find more on Financial 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!