Fetching all instruments for a given product
To fetch all instruments for a single product, you:
Instantiate the InstrumentCatalog class.
Choose a fetch type:
Synchronous
- Call the Get() method.
Asynchronous
- Create an event handler method that will be called when notifications regarding these instruments are available.
- Register this event handler with the InstrumentCatalog instance.
- Call the GetAsync() method of the InstrumentCatalog instance.
Instantiating the InstrumentCatalog class
The InstrumentCatalog class provides several constructors that let you identify a product using specific criteria.
public InstrumentCatalog(Product product, Dispatcher dispatcher);
public InstrumentCatalog(MarketId marketId, ProductType productType, string productName, Dispatcher dispatcher);
The following code snippet shows how to instantiate an InstrumentCatalog object for the CME ES Future product.
InstrumentCatalog instr_cat = new InstrumentCatalog(MarketId.CME, ProductType.Future, "ES",
tt_net_sdk.Dispatcher.Current);
Note
- The product name for Autospreader instruments is “ASE” and the product name for Aggregator instruments is “AGGREGATOR”.
- The product type for Autospreader and Aggregator instruments is “Synthetic”.
Synchronous fetch
Once an InstrumentCatalog object has been created, you can simply call the Get() method to perform a synchronous lookup of the product's instruments as shown below. Note that although this is normally fairly quick, your execution thread will be blocked until this action is complete.
ProductDataEvent e = instr_cat.Get();
if (e == ProductDataEvent.Found)
{
// Instruments were found
foreach (KeyValuePair<InstrumentKey, Instrument> kvp in instr_cat.Instruments)
{
Console.WriteLine("Key = {0} : Value = {1}", kvp.Key, kvp.Value);
}
}
else
{
// Instruments were not found
Console.WriteLine("Cannot find instrument: {0}", e.ToString());
}
Asynchronous fetch
If you prefer not to be blocked, you can register a callback function to be called once the fetching has been completed as shown below.
instr_cat.OnData += new EventHandler<InstrumentCatalogEventArgs>(instr_cat_OnData);
instr_cat.GetAsync();
After the GetAsync() method of the InstrumentCatalog instance is called, TT .NET SDK performs a lookup for the instruments based on the inputs that it is given. Whether the instruments were found or not, the specified event handler method is called. The following code snippet illustrates the structure of this event handler.
private void instr_cat_OnData(object sender, InstrumentCatalogEventArgs e)
{
if (e.Event == ProductDataEvent.Found)
{
// Instruments were found
foreach (KeyValuePair<InstrumentKey, Instrument> kvp in e.InstrumentCatalog.Instruments)
{
Console.WriteLine("Key = {0} : Value = {1}", kvp.Key, kvp.Value);
}
}
else
{
// Instruments were not found
Console.WriteLine("Cannot find instrument: {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 InstrumentCatalog class as follows:
instr_cat.OnData -= instr_req_OnData;
instr_cat.Dispose();