Is it possible to run this loop?

1 view (last 30 days)
Laurentiu Galan
Laurentiu Galan on 16 Jan 2012
Hi guys,
I am trying to run a nested loop that will pull data from Yahoo Finance and will export some information into a column.
Here is the code I have so far:
%Open File
fid=fopen('C:\Users\Laurentiu Galan\Desktop\pca1.csv');
C = textscan(fid, '%s %s %s %*s %*s %*s %*s', 'delimiter', ',', ...
'HeaderLines', '1');
fclose(fid);
%Strcat Identifier
tickername=C{1};
year=C{2};
month=C{3};
%Get Size of Loop for filepath
D=size(tickername);
numval=D(1,1);
%Calculate Loop to download tickers
for i=1:numval
url_string = strcat('http://ichart.finance.yahoo.com/table.csv?s=', tickername{i}, '&d=1', '&e=', month{i}, '&f=', year{i}, '&g=m&a=1', '&b=', month{i}, '&c=', year{i}, '&ignore=.csv')
end
%Create Loop for url_string and buffers by opening connection and
%retrieving data into a buffer
%for j=1:numval
buffer = java.io.BufferedReader(...
java.io.InputStreamReader(...
openStream(...
java.net.URL({j}))));
% Begin with 2nd line (exclude header)
ptr = 1;
while 1
% Read line
buff_line = char(readLine(buffer));
% Break if this is the end
if length(buff_line)<3, break; end
% Find comma delimiter locations
commas = find(buff_line== ',');
% Extract high, low, open, close, etc. from string
adj_close = str2num( buff_line(commas(6)+1:end) )
ptr = ptr + 1;
end
%end
How Do I finish this code to simply give me the 'adj_close' for each corresponding ticker in the file? Please Note, I don't want multiple dates, just the price data for the first of the month for each tickername, month and year.
Please note, there is a header every time I read the lines, so the header must be excluded somehow.
I really appreciate any help with this. I've fairly new at Matlab and would appreciate any advice or additional guidance...

Answers (0)

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!