Thomson Financial DataStream data access (Matlab)

From LiteratePrograms

Jump to: navigation, search

Contents

Thomson Financial DataStream data access

This data provider provides a simple access to historical financial data.

You need the MATLAB datafetch toolbox and an access to the Thomson Datafeeds service.

To use it, you simply have to

  1. create a connection
  2. fetch the needed data
>> my_ds_connection = datastream( my_login , my_password, 'Datastream');
>> my_dataset       = fetch(my_ds_connection, {'US2825061045', 'CA0192261091'}, {'P', 'VO'}, today-150, today)
my_dataset = 
2x1 struct array with fields:
   Source
   Instrument
   StatusType
   StatusCode
   StatusMessage
   CCY
   DATE
   DISPNAME
   P
   SYMBOL
   VO

As you can see, the instruments can be named after their ISIN codes, and you can access in only one function several variables.

The variables you get back are in char arrays:

>> my_dataset(1).DATE(1)
ans = 
   '2005-12-30T00:00:00'
>> my_dataset(1).P(1)
ans = 
   '0.0006'

So you will need to convert them yourself. A good idea is to store them into a structured dataset.

datastream2struct.m

Enlarge
Result of the use of those functions
This function convert a Datastream structure to a properly structured dataset, that can be used with functions from my Swiss army knife MATLAB programs for quantitative finance (especially the plotstruct function).

An example of its use:

ds_connection = datastream('login', 'password','Datastream');
vals = fetch(ds_connection, 'F:RENU', {'P', 'PO', 'PH', 'PL'})
data = datastream2struct(vals)
h    = plotstruct(data, 'dates', true)
<<datastream2struct.m>>=
function data = datastream2struct( vals)
% DATASTREAM2STRUCT - convert
% example:
%   ds_connection = datastream('login', 'password','Datastream');
%   vals          = fetch(ds_connection, 'F:RENU', {'P', 'PO', 'PH', 'PL', 'VO'})
%   data          = datastream2struct(vals)
DataStream to structured data_2

DataStream to structured data

Convert datastream data to a properly structured dataset.

<<DataStream to structured data_2>>=
Find column names_3
Get cols_4
Get date_5
data = struct('title', vals.DISPNAME, 'value', nval, 'date', dts, 'names', {names}, ...
              'symbol', struct('type', 'datastream', 'value', vals.SYMBOL), 'currency', vals.CCY);

Find column names

I read the field named |Instrument|.

<<Find column names_3>>=
instr = vals.Instrument;
d     = findstr(instr, '~=');
sname = instr(d+2:end);
d     = [0, findstr(sname, ','), length(sname)+1];
names = {};
for i=1:length(d)-1
    names{end+1} = sname(d(i)+1:d(i+1)-1);
end

Get cols

I convert each data field.

<<Get cols_4>>=
nval  = repmat(nan,length(vals.DATE), length(names));
for n=1:length(names)
   nval(:,n) = cellfun(@(x)(str2num(x)), vals.(names{n}));
end

Get date

I convert dates.

<<Get date_5>>=
dts = cellfun(@(x)(datenum(x, 'yyyy-mm-ddTHH:MM:SS')), vals.DATE);

External links

  • MATLAB functions:
    • datastream connection function,
    • datastream fetch function
  • Thomson financial
    • Thomson datastream extranet (to get a list of available instruments and variables and their codes)
Download code
Views