Note: Please read the disclaimer. The author is not providing professional investing advice or recommendations.
There is a matlab script that goes with this article: get_trailing_pe.m
Other posts have dealt with how to download historical stock prices into Matlab and how to adjust for splits and dividends so that you can try your luck optimizing neural networks and other homebrewed trading algorithms that are going to bring dumptrucks, DUMPTRUCKS! full of money. ‘Mon back as Cramer would say…
But perhaps you felt the article about Matlab data mining from Yahoo! Finance was just a special case because they provide special CSV files just for this application. You want more flexibility – you’d like to be able to tell Matlab to go and get any stock statistic you’re after from a web page, not a special file provided by the host.
Well the good news is it can be done (fairly) easily in Matlab. You just have to write a script to parse the source code of the web page.
If you’re like me, you learn best from example rather than theory. So here’s a link to my script that retrieves the trailing P/E for the stock of your choice from Yahoo! Finance. Invoke this function in the following way from the command line in Matlab using the stock’s ticker symbol, for example, for Google:
>>get_trailing_pe(‘goog’)
This is the same information you’d get if you entered a stock ticker on the Yahoo! Finance website – then clicked on Key Statistics and read it visually out of the table.
Feel free to modify this scripts to get any other statistic you’re after.
There is a matlab script that goes with this article: get_trailing_pe.m
{ 5 comments… read them below or add one }
Hey man, I have been reading all the posts in your blog.please help me, i have few queries.
This matlab thing is interesting to me since, i am an engineer myself.
So can you tell me how to make use of these codes with matlab?
should i enter the same code in matlab?
where and how should i use this code?
your response will be greatly valuable to me, and enhance my learning.
Thanking you in advance……………
Hi, I am trying to use the code to extract “forward pe’ data. But the thing is, on the source code the forward pe value is located on the next line. please help
To get Forward P/E I had to make 3 changes to the file (if you’re interested search the code for comments “Change #1″, “Change #2″, and “Change #3″).
Here’s a link to it.
get_fwd_pe.m
@lumilog.
Thanks a ton for your reply.
@lumilog
Hey thanks for helping out earlier. I need some help again.
I have modified your code to get various statistical values from yahoo finance. But, the problem is when I try to write that value in excel(for further equity report update).
for eg: to get avg_vol
function avg_vol = get_avg_vol(stock_symbol)
stock_symbol = upper(stock_symbol);
% Open connection to Yahoo! Finance statistics page
fprintf(1,’Retrieving Average Volume(3 month) for %s…’, stock_symbol);
url_name = strcat(‘http://finance.yahoo.com/q/ks?s=', stock_symbol);
url = java.net.URL(url_name); % Construct a URL object
is = openStream(url); % Open a connection to the URL
isr = java.io.InputStreamReader(is);
br = java.io.BufferedReader(isr);
% Cycle through the source code until we find Average Volume…
while 1
line_buff = char(readLine(br));
ptr = strfind(line_buff, ‘Average Volume’);
% …And break when we find it
if length(ptr) > 0,break; end
% Handle the case where either data not available or no Average Volume
no_data = strfind(line_buff, ‘There is no’);
if length(no_data) > 0,
fprintf(1,’Yahoo! Finance does not currently have Average Volume for %s.\n’,stock_symbol);
avg_vol = NaN;
return;
end
end
% Just to make it easier, strip off all the preceeding stuff we don’t want
line_buff = line_buff(ptr:end);
% Extract the Average Volume(3 month)
ptr_gt = strfind(line_buff,’>’);
ptr_lt = strfind(line_buff,’<’);
avg_vol = (line_buff(ptr_gt(6)+1:ptr_lt(7)-1));
if length(avg_vol) == 0
fprintf(1,’N/A\n’);
else
fprintf(1,’ = %s\n’, avg_vol);
end
——————————–
by doing this, avg_vol is stored as a character with commas. How can I get the vol amount directly so that I can write it directly to excel in matlab.
This holds true even for market cap which is represented in ‘M’
thanks for helping out earlier.
{ 2 trackbacks }