matlab文件怎么转python,如何将MATLAB文件转换为PYTHON?

icon1.gif 如何将MATLAB文件转换为PYTHON?

在某人的博客上,我找到了一些要使用python修改的代码。

但是我不知道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 = '.*?'; if(isempty(newlines)) NextExpiry = {}; return end % Get the matches of particular expression [tok mat] = regexp(newlines{1}, expr, 'tokens', 'match'); id1= regexp(mat{1},'','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},'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值