gpt4 book ai didi

python - 如何将 MATLAB 文件转换为 PYTHON?

转载 作者:太空宇宙 更新时间:2023-11-03 19:33:07 26 4
gpt4 key购买 nike

在某人的博客上,我发现了一些我想用python修改的代码。
但我不知道 Matlab 语法或代码或有 MATLAB 来测试......谁能帮忙用python重写一下,少评论。

我感兴趣的雅虎符号是 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/

最佳答案

这是一个 MATLAB 到 Python 的“编译器”:http://ompc.juricap.com/

甚至还有一个在线版本,您可以将代码剪切并粘贴到:http://ompclib.appspot.com/m2py

它转换了您的代码而没有崩溃,但我可以发现转换中的一些问题。然而,这可能是一个很好的起点,您可以手动清理。

关于python - 如何将 MATLAB 文件转换为 PYTHON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4811136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com