%% Bloomberg Data License Workflow example

%   Copyright 1999-2014 The MathWorks, Inc.

% Make a connection to the Data License host
b = bdl('dl338','Lb=cYaZ','dlsftp.bloomberg.com',30206,'pDyJaV')

% b = 
% 
%   bdl with properties:
% 
%          Login: 'dl338'
%       Hostname: 'dlsftp.bloomberg.com'
%           Port: 30206
%     AuthOption: 'password'
%        KeyFile: ''
%     Connection: [1x1 com.bloomberg.datalic.api.ExtendedFTPConnection]

% Submit the request file that was created using the Bloomberg Data License Request Builder
b.Connection.put('getdatarequest.req')

% Get the directory listing to check for the existence of the output file.
% Note that the output file name is defined in the Request Builder UI.
s = dir(b)

% s = 
% 
%     'd--x--x--x    2 root     root         4096 Sep  5 11:25 bin'
%     'dr--r--r--    2 root     root         4096 Sep  5 11:25 etc'
%     'drwxrwxrwx    2 op       general     61440 Sep 24 02:12 notices'
%     ...
%     '-rw-rw-rw-    2 op       general      1194 Sep 24 10:14 getdataoutput.out'
%     '-rw-rw-rw-    1 op       general      1194 Sep 24 10:07 getdataoutput.out.20140924'
%     '-rw-r--r--    1 op       general       478 Sep 24 10:13 getdatarequest.req.copied'
%     ...
   
% Get the output file and save it to the current directory.  The first input
% argument is the name of the generated output file on the Data License host.
% The second input argument defines the name of the locally saved file.
b.Connection.get('getdataoutput.out','getdataoutput.out')

% For decrypted files, use the Data License decryption routine available as
% a download from Bloomberg
!des -D -u -k "pDyJaV" getdataoutput.out  getdataoutput.dat

% Use the example function, bdlloader, to parse the output file into MATLAB
% structure
d = bdlloader('getdataoutput.dat')

% d = 
% 
%          Header: [1x1 struct]
%      Identifier: {4x1 cell}
%           Rcode: {4x1 cell}
%         Nfields: {4x1 cell}
%         PX_OPEN: {4x1 cell}
%         PX_LAST: {4x1 cell}
%         PX_HIGH: {4x1 cell}
%          PX_LOW: {4x1 cell}
%     PX_CLOSE_DT: {4x1 cell}

d.Header

% ans = 
% 
%            RUNDATE: '20140924'
%        PROGRAMFLAG: 'oneshot'
%           FIRMNAME: 'dl338'
%           FILETYPE: 'pc'
%      REPLYFILENAME: 'getdataoutput.out'
%     PRICING_SOURCE: 'BVAL'
%      CLOSINGVALUES: 'yes'
%              SECID: 'TICKER'
%          YELLOWKEY: 'Equity'
%        PROGRAMNAME: 'getdata'
%        TIMESTARTED: 'Wed Sep 24 10:19:59 EDT 2014'
%       TIMEFINISHED: 'Wed Sep 24 10:20:17 EDT 2014'

d.Identifier

% ans = 
% 
%     'IBM US Equity'
%     'GOOG US Equity'
%     'AAPL US Equity'
%     'ELY US Equity'

d.PX_LAST

% ans = 
% 
%     '190.520000'
%     '582.040000'
%     '101.900000'
%     '7.310000'
