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
网页上down数据的脚本。其实改改就能download其他的了。
% Example of How to Import Stock Statistics from Web into Matlab
% — This example retrieves trailing P/E ratio from Yahoo! Finance
% LuminousLogic.com
% Initialize Workspace
function trailing_pe = get_trailing_pe(stock_symbol)
stock_symbol = upper(stock_symbol);
% Open connection to Yahoo! Finance statistics page
fprintf(1,’Retrieving trailing P/E ratio 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 trailing P/E…
while 1
line_buff = char(readLine(br));
ptr = strfind(line_buff, ‘Trailing P/E’);
% …And break when we find it
if length(ptr) > 0,break; end
% Handle the case where either data not available or no trailing PE
no_data = strfind(line_buff, ‘There is no’);
if length(no_data) > 0,
fprintf(1,’Yahoo! Finance does not currently have trailing P/E for %s.\n’,stock_symbol);
trailing_pe = 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 trailing P/E
ptr_gt = strfind(line_buff,’>’);
ptr_lt = strfind(line_buff,’<’);
trailing_pe = str2num(line_buff(ptr_gt(2)+1:ptr_lt(3)-1));
if length(trailing_pe) == 0
fprintf(1,’N/A\n’);
else
fprintf(1,’ = %3.2f\n’, trailing_pe);
end








0 responses so far ↓
There are no comments yet...Kick things off by filling out the form below.