Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
回复
 
主题工具 显示模式
旧 2019-12-14, 08:13 PM   #1
高级会员
 
注册日期: 2019-11-21
帖子: 2,976
声望力: 64
poster 正向着好的方向发展
帖子 如何将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},'
poster 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码
Trackbacks are 禁用
Pingbacks are 禁用
Refbacks are 禁用



所有时间均为北京时间。现在的时间是 02:06 PM


Powered by vBulletin
版权所有 ©2000 - 2024,Jelsoft Enterprises Ltd.

SEO by vBSEO ©2009, Crawlability, Inc.