Using time series in Matlab with Thomson Reuters Eikon

Disclaimer

This article provides an example of how Thomson Reuters Eikon can be used alongside Matlab to retrieve time series of financial instruments. The code sample requires Matlab R2016b or later, as it allows to have custom functions at the end of the script files, however, it could easily be modified to work with earlier versions.

Please note that this sample does not have a dependency on the Datafeed Toolbox™.

Prerequisites

  • Thomson Reuters Eikon 4.0.27 or later;
  • Matlab R2016b or later;
  • Visual Studio or Nuget (optional);

Installing the Data API

Depending on the bitness of your Matlab, get 32-bit or 64-bit version of the API.

Before we begin we need to download Eikon 4 Desktop Data APIs package from NuGet. Follow the instructions in the paragraph titled Installing Data API in this tutorial. Note that the bitness of the package must match the bitness of your Matlab. I.e. if you use 64-bit Matlab you need to download from NuGet Eikon 4 Desktop Data APIs (x64) package. Then create a folder for Eikon .NET SDK and copy the following libraries from downloaded package into this folder:

  • Common.Logging.dll EikonPipeDll.dll i18nresource.dll
  • Newtonsoft.Json.dll protobuf-net.dll
  • ThomsonReuters.Desktop.SDK.DataAccess.dll
  • ThomsonReuters.Udap.BusTools.dll
  • ThomsonReuters.Udap.Ipc.Managed.Common.dll
  • ThomsonReuters.Udap.ManagedPS.dll

Where multiple versions of the same library exist in the package pick the version targeted for .NET 4.0 framework. This version is located in lib\net40 folder. If you don't already have version 12 Microsoft C runtime libraries in the PATH you may also need to copy msvcp120.dll and msvcr120.dll into the same folder with all the other libraries. In this example the folder used to store the libraries is C:\Temp\EikonNetSDK\

Create a class to load Eikon .NET SDK assemblies

classdef treikonnet < handle
    properties
        Assembly
        Services
    end
end

Inside this class add a method to load Eikon .NET SDK assemblies and create an instance of Eikon DataServices

methods (Access = 'public')
    function t = treikonnet(eikonsdkpath)
        if exist([eikonsdkpath 'ThomsonReuters.Desktop.SDK.DataAccess.dll'],'file') ~= 2
            error(['Thomson Reuters Eikon .NET SDK not found in the specified folder: ' eikonsdkpath]); 
        end
        t.Assembly{1} = NET.addAssembly([eikonsdkpath 'ThomsonReuters.Desktop.SDK.DataAccess.dll']);
        t.Assembly{2} = NET.addAssembly([eikonsdkpath 'ThomsonReuters.Udap.BusTools.dll']);
        t.Assembly{3} = NET.addAssembly([eikonsdkpath 'ThomsonReuters.Udap.Ipc.Managed.Common.dll']);
        t.Assembly{4} = NET.addAssembly([eikonsdkpath 'ThomsonReuters.Udap.ManagedPS.dll']);
        t.Assembly{5} = NET.addAssembly([eikonsdkpath 'protobuf-net.dll']);
        t.Assembly{6} = NET.addAssembly([eikonsdkpath 'Newtonsoft.Json.dll']);
        t.Services = ThomsonReuters.Desktop.SDK.DataAccess.DataServices.Instance;      
    end
end

Create data retrieval script

Declare a global variable and assign to it an instance of treikonnet class defined above. The parameter passed to treikonnet method defines the location of Eikon .NET SDK libraries.

global t
t=treikonnet('C:\Temp\EikonNetSDK\');

Then initialize Eikon .NET SDK data retrieval services. The Initialize method below requires application ID string. The value can be any string. If data retrieval services are already initialized SendTimeSeriesRequest function is called immediately. Otherwise the script listens for ServiceInformationChanged event to be raised before calling SendTimeSeriesRequest function.

t.Services.Initialize('MyMatlabEikonTestApp');
timeSeries = t.Services.TimeSeries;
if strcmp(t.Services.State,'Up')
    SendTimeSeriesRequest(timeSeries);
else
    addlistener(timeSeries,'ServiceInformationChanged',@OnTimeSeriesServiceInformationChanged);
end

    function r = OnTimeSeriesServiceInformationChanged(timeSeries,eventArgs)
        disp('TimeSeries ServiceInformationChanged event called');
        disp(System.String.Concat('Timeseries service state is ',eventArgs.Information.State));
        r = char(eventArgs.Information.State);
        if strcmp(r,'Up')
            SendTimeSeriesRequest(timeSeries);
        end
    end

Finally SendTimeSeriesRequest function below requests 10 most recent data points from default Daily interval for the RIC EUR=, and DataReceivedCallback function processes the data retrieved.

    function SendTimeSeriesRequest(timeSeries)
        disp('Sending timeseries request');
        timeSeriesRequestSetup = timeSeries.SetupDataRequest('EUR=');
        timeSeriesRequestSetup.WithView('BID');
        timeSeriesRequestSetup.WithNumberOfPoints(10);
        timeSeriesRequestSetup.OnDataReceived(@DataReceivedCallback);
        timeSeriesRequestSetup.CreateAndSend();
    end

    function r = DataReceivedCallback(chunk)
        % The data is returned in chunks. IsLast property of the chunk
        % object indicates if data retrieval is complete or if more data is
        % expected to be retrieved.
        disp(System.String.Concat('RIC: ',chunk.Ric));
        disp(System.String.Concat('Is this the last chunk: ',chunk.IsLast));
        records = chunk.Records.GetEnumerator();
        timeSeriesDataOutput = {};
        k=1;
        while records.MoveNext
            bar = records.Current.ToBarRecord;
            ts = char(records.Current.Timestamp.ToString);
            timeSeriesDataOutput(k,1:5) = {ts,bar.Open.Value,...
                bar.High.Value,bar.Low.Value,bar.Close.Value};
            k=k+1;
        end
        disp(timeSeriesDataOutput);
    end