-1

我需要查找 7500 多家公司的股票代码。我希望 MATLAB 为我做这件事。

我将如何编写一个脚本来查找给定搜索字符串的股票代码?例如,自动查询这样的网站:http: //www.nasdaq.com/quotes/symbol-lookup.aspx

进行模糊搜索或通配符的能力是一个加号。

任何见解表示赞赏!

4

3 回答 3

1

这是一种类似于我以前使用过的快速而肮脏的方式。

雅虎!有一个 API:http ://d.yimg.com/autoc.finance.yahoo.com/autoc?query=facebook&callback=YAHOO.Finance.SymbolSuggest.ssCallback 。(尝试链接)您只需用每个公司替换查询参数 - 一个简单的 for 循环就可以了。

在 Matlab 中,调用它

result = urlread(url);

如果您查看 API,它看起来会返回"YAHOO.Finance.SymbolSuggest.ssCallback("JSON")"包装 JSON。由于 urlread() 只返回一个字符串,因此请替换这些字符以获取有效的 JSON。

最后使用像这样的 JSON 库并读取结果:

stock = parse_json(result.ResultSet.Result(1).symbol); 

对于“模糊”查找,您可能只想检查第一个结果。

于 2013-07-16T21:04:47.183 回答
1

感谢 bbill 让我走上正轨。我决定回答我自己的问题,因为它代表了一个完整的答案,并且希望对这个问题的其他人更有帮助。此外,此实现不需要 JSON 包。这个功能比较完善,使用了bbill推荐的Yahoo Finance API。我还提供了第二个使用 MarketWatch.com 但不完整的答案(见下文)。

% Get a stock ticker symbol from Yahoo Finance.
%
% get_stocksymbol('searchstring','default1') takes two arguments:
%   'searchstring' is the company name to be looked up.
%   'default1' is y/n -- set to 'y' to default to the first result if
%       the search return multiple results.
%
% stocksymbol = get_stocksymbol(searchstring,default1) returns a string
% with the result of the stock symbol lookup.
%
% [stocksymbol,choices] = get_stocksymbol(searchstring,default1) returns
% the optional cell array of company name and symbol choices.
%
% The function looks up the search string using the Yahoo Finance API (but
% without requiring the use of a JSON library) and returns all possible
% stock symbols provided by Yahoo. If more than one symbol is returned,
% the user is given the option to select the most appropriate.
%
% Trevor Zink 2013

function [stocksymbol,choices] = get_stocksymbol(searchstring,default1)

fsearchstring=strrep(searchstring,' ','%20');
url = strcat('http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=',fsearchstring,'&callback=YAHOO.Finance.SymbolSuggest.ssCallback');

try
jsonread = urlread(url);
catch errmsg
    stocksymbol = 'NA';
    fprintf('No company named %s exists in the database \n',searchstring)
    return
end
% search the JSON output for the desired term (in this case, ticker symbol)

% handle the case where either data not available or no symbol
no_data = strfind(jsonread, '"Result":[]}})');
if ~isempty(no_data),
    fprintf(1,'No symbol for %s.\n',searchstring);
    stocksymbol = 'NA';
    return;
end

% extract the symbol(s)
% find a term in the JSON output that preceeds the stock symbol(s) (in this
% case, "symbol"):
occ = strfind(jsonread, '"symbol"');
clear choices
for i=1:length(occ)
% the next four lines find characters at the beginning and the end of
% the actual stock symbol itself
sym_lt = strfind(jsonread(occ(i):end),':"');
sym_rt = strfind(jsonread(occ(i):end),'",');
nam_lt = strfind(jsonread(occ(i):end),'"nam');
nam_rt = strfind(jsonread(occ(i):end),'","ex');
jsonshort = jsonread(occ(i):end);
% then just grab the section of the JSON output inbetween the delimiters we
% established above
choices(i,1)= {i};
choices(i,2) = cellstr(jsonshort(sym_lt+2:sym_rt-1));
choices(i,3) = cellstr(jsonshort(nam_lt+9:nam_rt-1));
end

% choose from multiple options, if available
clear choice
if or(size(choices,1)==1,default1=='y')
    stocksymbol=choices{1,2};
else
    for i=1:size(choices,1)
        % this part is a bit over the top, but formats the choices in a
        % pretty table for the user to choose from
        if size(choices{i,2},1)>6
        fprintf('%i)\t\t%s\t\t%s \n',i,choices{i,2},choices{i,3})
        else
            if size(choices{i,2},1)>3 && length(choices{i,2})<=6
        fprintf('%i)\t\t%s\t\t\t%s \n',i,choices{i,2},choices{i,3})
            else
        fprintf('%i)\t\t%s\t\t\t\t%s \n',i,choices{i,2},choices{i,3})
            end
        end
    end
    fprintf('Company: %s\n',searchstring)
    choice = input('Choose an option number (default 1; choose 0 for ''NA''):');
    if isempty(choice)
        choice=1;
    end
    % account for incorrect entries
    if choice > length(choices)
        valid_response = 0;
        while ~valid_response
            choice = input('Invalid selection. Choose an option number: ');
            if choice <= length(choices)
                valid_response = 1;
            end
        end
    end
        if choice==0
            stocksymbol='NA';
        else
            stocksymbol=choices{choice,2};
        end
end
于 2013-07-17T00:35:23.727 回答
0

这是一个稍微不完整的版本(无法从竞争选项中进行选择),但它使用了 MarketWatch.com 的输出。这主要基于Luminous Logic 博客中的一个示例。

% Retrieve stock ticker symbols from MarketWatch.com
% Trevor Zink 2013
% Code based largely on example from
% http://www.luminouslogic.com/matlab_stock_scripts/get_trailing_pe.m


% Initialize Workspace
% function trailing_pe = get_symbol(searchstring)

fprintf(1,'Retrieving symbol for %s... \n', searchstring);
url_name = strcat('http://www.marketwatch.com/tools/quotes/lookup.asp?siteID=mktw&Lookup=',searchstring,'&Country=all&Type=All');
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 searchstring...
while 1
    line_buff = char(readLine(br));
    ptr       = strfind(line_buff, strcat('title="',searchstring));

    % ...And break when we find it
    if ~isempty(ptr),break; end

    % Handle the case where either data not available or no symbol
    no_data   = strfind(line_buff, 'There were no matches found');
    if ~isempty(no_data), 
        fprintf(1,'MarketWatch.com does not have a symbol for %s.\n',searchstring);
        symbol = 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 symbol
ptr_gt      = strfind(line_buff,'>');
ptr_lt      = strfind(line_buff,'<');
symbol = line_buff(ptr_gt(1)+1:ptr_lt(1)-1);
if printsymbol=='y'
    if isempty(symbol)
        fprintf(1,'N/A\n');
    else
        fprintf(1,'%s\n',symbol);
    end
end
于 2013-07-17T00:38:45.627 回答