2011-01-26 5 views
1

누군가의 블로그에서 파이썬으로 수정할 코드를 발견했습니다.
하지만 Matlab 구문이나 코드를 알지 못했거나 테스트 할 MATLAB이 있습니다 ... 아무도 파이썬으로 적은 글을 다시 쓸 수 있습니까?MATLAB 파일을 PYTHON으로 변환하는 방법은 무엇입니까?

내가 좋아할만한 Yahoo 기호는 MSFT, IBM 및 SPY입니다. CSV로 출력은 잘 작동합니다.

function DataOut = Get_Yahoo_Options_Data(symbolid) 
%Get_Yahoo_Options_Data get Option Chain Data from Yahoo 
% Get Options Chain Data from Yahoo 
% DataOut = Get_Yahoo_Options_Data(symbol) 
% Inputs: Symbol name as a character String 
% Output: A structure with the following fields 
%  data : A 1xN cell where N is the number of Expiries available 
%  ExpDates : A 1xN cell array of Expiry Dates 
%  Calls : A 1xN cell array of Call Option data for each expiry 
%  Puts : A 1xN cell array of Put Option data 
%  CPHeaders : Headers for the calls and puts option data 
%  Headers: Headers for the data 
%  FullOptionData : A combined cell array of DataOut.data 
%  Last : Last Price 
% Example: 
%   DataOut = Get_Yahoo_Options_Data('LVS'); 
% (c)tradingwithmatlab.blogspot.com 
DataOut = struct; 
% Construct and read the URL from Yahoo Finance Website 
urlText = urlread(['http://finance.yahoo.com/q/os?s=' symbolid]); 
% Try getting the Table Data from URL Text 
TableData = getTableData(); 
% If Empty return 
if(isempty(TableData)) 
    return 
else 
    DataOut.data{1} = TableData; 
end 
% Get the Expiry Date for later use 
DataOut.ExpDates{1} = Get_Exp_Dates(); 
% Get Expiry Dates that are listed in the website to construct separate 
% URLS for each month 
NextExpiryURL = Get_Next_Expiry_URL(); 
if(isempty(NextExpiryURL)) 
    return 
end 

% Now read Option Tables of each Expiry month 
for ik = 1:length(NextExpiryURL) 
    urlText = urlread(NextExpiryURL{ik}); 
    DataOut.ExpDates{ik+1} = Get_Exp_Dates(); 
    DataOut.data{ik+1} = getTableData(); 
end 
% Clean Up 
% Convert the strings into numbers 
f = @(x)[x(:,1) num2cell(str2double(x(:,[2:8]))) x(:,9) num2cell(str2double(x(:,10:end)))]; 
DataOut.data = cellfun(f,DataOut.data,'uni',false); 
% Separate the data into Calls, Puts, Headers 
DataOut.Calls = cellfun(@(x) x(:,[1 8 2:7]),DataOut.data,'uni',false); 
DataOut.Puts = cellfun(@(x) x(:,[9 8 10:end]),DataOut.data,'uni',false); 
DataOut.CPHeaders = {'Symbol','Strike','Last','Change','Bid','Ask','Volume','Open Int'}; 
DataOut.Headers = {'Symbol','Last','Change','Bid','Ask','Volume','Open Int','Strike',... 
    'Symbol','Last','Change','Bid','Ask','Volume','Open Int'}; 
DataOut.FullOptionData = [DataOut.Headers ; cat(1,DataOut.data{:})]; 
% Get the Last Price 
DataOut.Last = str2num(urlread(['http://download.finance.yahoo.com/d/quotes.csv?s=' symbolid '&f=l1&e=.csv'])); 

%% Get_Next_Expiry_URL 
    function NextExpiry = Get_Next_Expiry_URL() 
     % Get the start and end indices and look for a particular text 
     Start = regexp(urlText,'View By Expiration:','end'); 
     end1 = regexp(urlText,'Return to Stacked View...','start'); 

     Data = urlText(Start:end1); 
     Data=Data(2:end); 
     % Trim the data 
     Data=strtrim(Data); 
     % Split the data into new lines 
     newlines = regexp(Data, '[^\n]*', 'match'); 
     expr = '<(\w+).*?>.*?</\1>'; 
     if(isempty(newlines)) 
      NextExpiry = {}; 
      return 
     end 
     % Get the matches of particular expression 
     [tok mat] = regexp(newlines{1}, expr, 'tokens', 'match'); 
     id1= regexp(mat{1},'</b>','start')-1; 
     month{1} = mat{1}(4:id1); 
     %Month and Next Expiries 
     for j = 2:length(mat)-1 
      id2 = regexp(mat{j},'">','end'); 
      id3 = regexp(mat{j},'</a','start'); 
      if(isempty(id3)) 
       return 
      end 
      month{j} = mat{j}(id2+1:id3-1); 
      id4 = regexp(mat{j},'"','start'); 
      NextExpiry{j-1} = ['http://finance.yahoo.com' mat{j}(id4(1)+1:id4(2)-1)]; %#ok<*AGROW> 
      NextExpiry{j-1} = regexprep(NextExpiry{j-1},'amp;',''); 
     end 

    end 
%% Get_Exp_Dates 

    function ExpDates = Get_Exp_Dates() 

     id1 = regexp(urlText,'Options Expiring','end'); 
     id2 = regexp((urlText(id1+1:id1+51)),'</b>','start'); 
     ExpDates = strtrim(urlText(id1+1:id1+1+id2-2)); 
     ExpDates=datestr(datenum(ExpDates,'dddd, mmmm dd,yyyy')); 
    end 

%% getTableData 
    function out = getTableData() 
     Main_Pattern = '.*?</table><table[^>]*>(.*?)</table'; 
     Tables = regexp(urlText, Main_Pattern, 'tokens'); 
     out = {}; 
     if(isempty(Tables)) 
      return 
     end 
     try 
     for TableIdx = 1 : length(Tables) 

      %Establish a row index 
      rowind = 0; 


      % Build cell aray of table data 

       rows = regexpi(Tables{TableIdx}{:}, '<tr.*?>(.*?)</tr>', 'tokens'); 
       for rowsIdx = 1:numel(rows) 
        colind = 0; 
        if (isempty(regexprep(rows{rowsIdx}{1}, '<.*?>', ''))) 
         continue 
        else 
         rowind = rowind + 1; 
        end 

        headers = regexpi(rows{rowsIdx}{1}, '<th.*?>(.*?)</th>', 'tokens'); 
        if ~isempty(headers) 
         for headersIdx = 1:numel(headers) 
          colind = colind + 1; 
          data = regexprep(headers{headersIdx}{1}, '<.*?>', ''); 
          if (~strcmpi(data,'&nbsp;')) 
           out{rowind,colind} = strtrim(data); 
          end 
         end 
         continue 
        end 
        cols = regexpi(rows{rowsIdx}{1}, '<td.*?>(.*?)</td>', 'tokens'); 
        for colsIdx = 1:numel(cols) 
         if(rowind==1) 
          if(isempty(cols{colsIdx}{1})) 
           continue 
          else 
           colind = colind + 1; 
          end 
         else 
          colind = colsIdx; 
         end 
         % The following code is required to get the sign 
         % of the change in Bid ask prices 
         data = regexprep(cols{colsIdx}{1}, '&nbsp;', ' '); 
         down=false; 
         % If Down is found then it is negative 
         if(~isempty(regexp(data,'"Down"', 'once'))) 
          down=true; 
         end 
         data = regexprep(data, '<.*?>', ''); 
         if(down) 
          data = ['-' strtrim(data)]; 
         end 
         if (~isempty(data)) 
          out{rowind,colind} = strtrim(data) ; 
         end 
        end % colsIdx 
       end 


     end 
     out = out(3:end,:); 
     catch %M %#ok<CTCH> This depends on which version of matlab you are using 
       %M.stack 
     end 
    end 
end 

Get_Yahoo_Options_Data.m http://tradingwithmatlab.blogspot.com/

+7

StackOverflow 정말 "내 codez 작성해주십시오"포럼 아닙니다. Matlab (또는 Matlab과 부분적으로 호환되는 오픈 소스 프로젝트 인 Octave)을 구해서 직접 시도해야합니다. 구체적인 질문이있는 경우 여기에서 질문 할 수 있습니다. –

+0

야후 금융을 근근이 살아가는 데 관심이 있다면 (이 모든 것 같습니다) ystockquote http://www.goldb.org/ystockquote.html –

+0

Matlab에 대한 질문은 Mathworks를 참조하십시오. 함수 또는 구문 http://www.mathworks.com/help/techdoc/ – gary

답변

1

여기에 MATLAB 파이썬 "컴파일러는"입니다 : http://ompc.juricap.com/ 당신이 잘라으로 코드를 붙여 넣을 수 있습니다 심지어 온라인 버전있다

: http://ompclib.appspot.com/m2py

그것은 충돌하지 않고 코드를 변환했지만 변환에 몇 가지 문제가있을 수 있습니다. 그러나 손으로 정리할 수있는 좋은 출발점이 될 수 있습니다.

+0

무슨 prob를 보았 니? 적어도, 나는 일할 것이 있습니다 .... 파이썬 수준을 넘어서있는 것 중 일부는 알아낼 수 있습니다. – Merlin

+0

언뜻보기에 (체계적으로 통과하지 못했지만) 일부 try/catch 섹션은 제대로 보이지 않았습니다. – payne

+0

덕분에, 아직도 나를 도울 수 있습니까? 그들은 게시물을 닫았습니다 – Merlin

관련 문제