Refinitiv Data Library for .Net

Streaming Cache
Tutorial Source Code

.NET/C#

Examples referenced

  • 2.2.03-Pricing-StreamingCache
Last Update July 2021
.NET/C# Requirements See Getting Started with .Net.
Pre-requisites

Enable the session environment as outlined within the Getting Started guide.

To build and run the above example:

  1. Right-click the specific project and choose "Set As Startup Project"
  2. From the Visual Studio menu, choose "Build - Build Solution"
  3. Once built, run the example by hitting (Ctrl+F5).


Overview

Whether accessing from the desktop, directly to the cloud, or through your deployed managed streaming services, the Refinitiv Data Libraries provide ease-of-use interfaces to retrieve real-time streaming content from the platform. By utilizing streaming interfaces, developers can easily define properties to request, receive, and process market data in real-time.

The following tutorial requests for a small watchlist of instruments, retrieving level 1 (MarketPrice) streaming content that will automatically update an internally-manage cache in real-time.  The Pricing interface includes a number of powerful features, one of which is the ability to cache real-time events in the background for the purpose of fast and easy retrieval.  The clear advantage is the convenience and simplicity when accessing live prices but also provides fast access by avoiding the need to traverse the network whenever there is a choice to retrieve up-to-date prices. 

Executing the Application

In our example, we're requesting to stream 3 instruments.  The goal of the example is to show the initial values coming in and how they change as market conditions change.  The example does not capture and display the real-time changes but rather updates an internally managed cache behind the scenes.  The example demonstrates the ability to retrieve values from the Pricing cache and shows values changing every few seconds.  For example, the following output highlights the initial values for one of the instruments and what happens a few seconds later when we display the cache.  We will see the new values based on the changes in the market for that specific instrument.

Within the source code package, select the source code example within the Delivery section.  To execute the example, refer to the pre-requisites section at the top of this tutorial.

The expected output should look similar to this:

How to request and extract live values from the cache

The following code segment requests for a small watchlist of items from the streaming services defined within the platform:

    	
            

// Define the streaming details....

var definition = Pricing.Definition("EUR=", "CAD=", "GBP=").Fields("DSPLY_NAME", "BID", "ASK");

 

// Retrieve a stream

var stream = definition.GetStream();

 

// Open the stream to retrieve and stream real-time updates

stream.Open();

The above code segment was intentionally broken out into 3 distinct steps for the purpose of explanation, but also provides flexibility when using the interfaces.  In general, the above code performs the following:

  • Define a Pricing request
    The Pricing interface provides the ability to specify a list of instruments to stream.  In addition, includes a number of other options, including the ability to specify a list of fields.  It's worth noting that if no fields are specified in the definition, all available fields will be returned.  While not specifying the fields is a convenient way to look at all available fields, in practice, it's best to limit the fieldset based on requirements to optimize performance.

  • Retrieve a stream interface
    Using the pricing definition, retrieve a streaming interface providing the ability to request and manage real-time streaming data.

  • Open the stream
    Using the stream interface, open the stream.  Opening a stream performs 2 distinct actions.  First, the request will instruct the streaming services to deliver a complete image, or refresh, of the latest values for the specified fields.  Second, the streaming services will begin to deliver real-time updates based on changes in the live market.  The stream interface will automatically react to these real-time events in order to properly maintain a live pricing cache.

Once in place, we can begin to perform different actions on our streams to extract live data.  For example, the following code segment will extract a snapshot of the live cache and iterate through the cache to display values.

    	
            

// Retrieve a snapshot of the whole cache.

var snapshot = stream.GetCacheSnapshot();

 

// Print out the contents of the snapshot

foreach (var entry in snapshot)

    DisplayPriceData(entry.Value);

The individual images available within the cache define simple access methods to retrieve properties for each.  For example:

    	
            

private static void DisplayPriceData(IPriceData data)

{

    if (data != null)

    {

        // Retrieve the name of item associated with the data image

        Console.WriteLine($"\nPrices for item: {data.ItemName}");

 

        // Print 1 field

        Console.WriteLine($"\n{data.ItemName}[DSPLY_NAME]: {data["DSPLY_NAME"]}");

 

        // Print the contents of one item

        Console.WriteLine($"{data.ItemName} contents: {data.Fields()}");

    }

    else

        Console.WriteLine("\n**********Error displaying price data**********");

}

While the above will allow the ability to extract a snapshot of the cache, the interface also supports simple cache access methods to pull out a specific instrument without the need to extract a copy of the cache.  For example:

    	
            

// Print out values directly within the live cache

Console.WriteLine($"\nDirect cache access => cache[CAD=][ASK] = {stream["CAD="]["ASK"]}");

Closing the stream

When you wish to stop the flow of live updates, you will need to issue a close on the stream.  The following code segment will close the stream:

    	
            

// Close the stream

stream.Close();

 

// Alternatively, you can utilize the 'using' feature.  A stream implements the IDisposable interface.

using (var stream = Pricing.Definition("EUR=", "CAD=", "GBP=").Fields("DSPLY_NAME", "BID", "ASK")

                                                              .GetStream())

{

    if (stream.Open() == Stream.State.Opened)

    {

        // Process data

    }

}

// The stream is now closed.

In our example code, we included a simple way to extract a snapshot of the entire cache.  However, the interface also supports the ability to snapshot a single or subset of items from the cache as opposed to copying the whole collection.  In addition, the interface also allows the specification of individual fields to be extracted when performing a cache snapshot.  In the last example code snippet, we showed a convenient way to access elements directly from the stream as opposed to performing a cache snapshot.  The choice between accessing the stream directly or performing an explicit cache snapshot depends on the specific workflow and use case.

It's worth noting that the Pricing interface inherently creates a live cache as part of its design.  While this is automatic and convenient, users may choose to manage their own cache by utilizing the event capabilities of streaming.  Users can optionally utilize the OMM Stream interface defined within the Delivery layer if they choose to have more control over how items are cached within their applications or if they prefer to forego the need for a cache.

The Pricing interface defined within the Refinitiv Data Library for .Net includes other useful features, such as the ability to retrieve real-time prices available within the RDP snapshot pricing service.  This capability can be extremely useful for users that may not have access to or require the need to stream prices through real-time streaming services.  Refer to the tutorial package for additional examples.  In addition, we encourage the use of IntelliSense documentation within your Visual Studio editor to discover other useful capabilities.