WhooL !

Fantastic World 数量投资 程序交易 以及 市场动态

WhooL  ! header image 2

How to Import Stock Statistics into Matlab(luminouslogic)

七月 23rd, 2009 by Crews

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

看看Whool!更多的文章,把 Whool!加入到你的订阅吧!

或者...

  • Is the oil to be the keydriver for NYSE in 2009? (0)
    Althought the price of oil is very important to analysis the world's economic,it is not the keydriver for the NYSE in  following 2009. 1.supply and demand.Some people think the price of oil goes up du...
  • 道指狂泻1000点,CITI按错一个键?? (0)
    下午看股票那个激动啊,恨不得把自己卖了进去买。大盘狂泻600点,一片哀鸿遍野。 大家的解释是希腊问题,不过再后来,一个新的解释出来了。 CITI的交易员在交易时卖股票组合的时候把million按成了billion。于是大盘狂跌。不过消息没有得到证实,按照CNBC的说法这中间涉及的公司至少有有P&G,ACN,3M,iShares Russell 1000 Value Index 按照c...
  • binomial MATLAB CODE (0)
    上周的一个作业,很简单的考虑,没有div 只有r sigma t so K M function value=Amercall(r,sigma,S0,T,K,M) % compute constants dt=T/M;  Q=(exp(-r*dt)); Beta=(Q+exp((r+sigma^2)*dt))/2; u=Beta+sqrt(Beta^2-1);&#...
  • 用4句话概括四大名著 (0)
    没有工作了,世界一下闲下来了 珍惜生命,远离博客! 提问西游:(请填空)红楼:(请填空)水浒:(请填空)三国:(请填空) 回答1、西游:(一个人类和三个动物的故事)红楼:(一个男人和一群女人的故事)水浒:(一群男人和三个女人的故事)三国:(三个男人和战争的故事) 2、西游:(疯狂的猴子)红楼:(疯狂的石头)水浒:(杀杀人,喝喝酒)三国:(大哥,我们动手吧) 3、西游:(西域历险记)红楼:(乱伦...

Tags:   No Comments

Leave a Comment

0 responses so far ↓

There are no comments yet...Kick things off by filling out the form below.