Show / Hide Table of Contents

Searching Instruments by Query text

Steps for searching instruments by Query

To search for an Instrument, you must perform the following tasks:

  1. Instantiate the InstrumentSearcher class.

  2. Choose whether to use a synchronous or asynchronous a search type:

    • Synchronous

      1. Call the Get() method.
    • Asynchronous

      1. Create an event handler method that will be called when notifications regarding this search are available.
      2. Register this event handler with the InstrumentSearcher instance.
      3. Call the GetAsync() method of the InstrumentSearcher instance.

Instantiating the InstrumentSearcher class

The InstrumentSearcher class provides several ways via constructors that let you search instruments by text query using different sets of filters. These are listed below along with code snippets showing their use.

  • To use a query to search for instruments from a given list of markets. Create an instrument subscription given an InstrumentSearcher instance as below.
List<MarketId> marketIds = new List<MarketId>();
marketIds.Add(MarketId.CME);
marketIds.Add(MarketId.ICE);

InstrumentSearcher instrumentSearcher = new
    InstrumentSearcher(tt_net_sdk.Dispatcher.Current,
    "CL Dec23", marketIds, ProductType.NotSet, 0);
  • To search for instruments by using the given product type
InstrumentSearcher instrumentSearcher = new
    InstrumentSearcher(tt_net_sdk.Dispatcher.Current,
    "CL Dec23", null, ProductType.Future, 0);
  • To search for instruments from the list of markets and by using the given product type.
List<MarketId> marketIds = new List<MarketId>();
marketIds.Add(MarketId.CME);
marketIds.Add(MarketId.ICE);

InstrumentSearcher instrumentSearcher = new
    InstrumentSearcher(tt_net_sdk.Dispatcher.Current,
    "CL Dec23", marketIds, ProductType.Future, 0);

Synchronous search

Once an InstrumentSearcher object has been created, you can simply call the Get() method to perform a synchronous search of the instruments as shown below.

Note: Although this process normally completes fairly quickly, your execution thread will remain blocked until this action completes.

ProductDataEvent e = instrumentSearcher.Get();
if (e == ProductDataEvent.Found)
{
  // Instrument was found
  LinkedList<InstrumentSearchResult> instruments  =
                                    instrumentSearcher.Results;
  Console.WriteLine("Found: {0} instruments",
                                    instruments.Count);
}
else
{


  // Instrument was not found
  Console.WriteLine("Cannot find instrument: {0}", e.ToString());
}

Asynchronous fetch

You can register a callback function to be called once the searching has been completed as shown below.

Note: This approach allows the execution thread to remain unblocked.

instrumentSearcher.OnData +=
    new
EventHandler<InstrumentSearcherEventArgs>(instrumentSearcher_OnData);
instrumentSearcher.GetAsync();

After the GetAsync() method of the InstrumentSearcher instance is called, TT .NET SDK performs a search for the instrument based on the given inputs. Whether the instrument is found or not, the specified event handler method is called. The following code snippet illustrates the structure of this event handler.

void instrumentSearcher_OnData(object sender, InstrumentSearcherEventArgs e)
{
  if (e.Event == ProductDataEvent.Found)
  {
    // Instrument was found
    InstrumentSearcher instrumentSearcher = e.InstrumentSearcher;
    LinkedList<InstrumentSearchResult> instruments  =
instrumentSearcher.Results;
    Console.WriteLine("Found: {0} instruments",
instruments.Count);

  }
  else
  {
    // Instrument was not found
    Console.WriteLine("No results were found for the query: {0}", e.Message);
  }
}

Cleaning up

When you shut down your application, you should detach all event handlers and call the Dispose() method for each instance of the InstrumentSearcher class as follows:

instrumentSearcher.OnData -= instrumentSearcher_OnData;
instrumentSearcher.Dispose();
In this article
Back to top