access fundamental data from matlab with the Trading Toolbox

1 次查看(过去 30 天)
I wanted to know if it was possible to access fundamental data Interactive Brokers from matlabTrading Toolbox - MATLAB ? (https://interactivebrokers.github.io/tws-api/fundamentals.html)

回答(1 个)

Annie Leonhart
Annie Leonhart 2020-1-4
编辑:Annie Leonhart 2020-1-4
Yes, it's possible... but, data is returned in XML... it'll take a lot of work to cleanup the XML file... a LOT. The code below will create a struct with the XML data. Good luck cleaning that up.
%% Connect to IBTWS or GATEWAY
ib = ibtws('',4001,0);
%% Create Contract
contract = ib.Handle.createContract;
contract.symbol = 'AAPL';
contract.secType = 'STK';
contract.exchange = 'SMART';
contract.primaryExchange = 'SMART';
contract.currency = 'USD';
%% register event
ib.Handle.registerevent({'fundamentalData',@(varargin)fundHandler(varargin{:},ib)});
%% Request Data
tickerid = randperm(10000,1);
ib.Handle.reqFundamentalData(tickerid,contract,'ReportsFinSummary'); pause(0.2);
ib.Handle.cancelFundamentalData(tickerid);
%% Unregister the event(s)
listeners = ib.Handle.eventlisteners;
i = strcmp(listeners(:,1),'fundamentalData');
ib.Handle.unregisterevent([{listeners{i,1}}' {listeners{i,2}}']);
% Event handler
function fundHandler(varargin)
switch varargin{end-1}
case 'fundamentalData'
fundamentaldata = varargin{5}.data
% Store the XML data in a temp *.xml file
filename = ['fundamentaldata.xml'];
fid = fopen(filename,'Wt');
fwrite(fid,fundamentaldata);
fclose(fid);
% Read the file into an XML model object
data = xml2struct(filename);
% Assign the data to a variable
assignin('base','fundamentaldata',data)
end
end
Output:

类别

Help CenterFile Exchange 中查找有关 Instrument Control Toolbox Supported Hardware 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by