Products
Fetching all products for a given market
To fetch all products for a given market, you:
Instantiate the ProductCatalog class.
Choose a fetch type:
Synchronous
- Call the Get() method.
Asynchronous
- Create an event handler method that will be called when notifications regarding this Product Product are available.
- Register this event handler with the ProductCatalog instance.
- Call the GetAsync() method of the ProductCatalog instance.
Instantiating the ProductCatalog class
The ProductCatalog class provides the following constructors that let you identify a market using specific criteria.
public ProductCatalog(Market market, Dispatcher dispatcher);
public ProductCatalog(MarketId marketId, Dispatcher dispatcher);
The following code snippet illustrates how to instantiate this class using a MarketId.
ProductCatalog prod_cat = new ProductCatalog(MarketId.CME, tt_net_sdk.Dispatcher.Current);
Synchronous fetch
Once an ProductCatalog object has been created, you can simply call the Get() method to perform a synchronous lookup of the products as shown below. Note that although this is normally fairly quick, your execution thread will be blocked until this action is complete.
ProductDataEvent e = prod_cat.Get();
if (e == ProductDataEvent.Found)
{
// Products were found
foreach (KeyValuePair<ProductKey, Product> kvp in prod_cat.Products)
{
Console.WriteLine("Key = {0} : Value = {1}", kvp.Key, kvp.Value);
}
}
else
{
// Products were not found
Console.WriteLine("Cannot find product: {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.
prod_cat.OnData += new EventHandler<ProductCatalogEventArgs>(Prod_cat_OnData);
prod_cat.GetAsync();
After the GetAsync() method of the ProductCatalog instance is called, TT .NET SDK performs a lookup for the products based on the market that it is given. Whether the products were found or not, the specified event handler method is called. The following code snippet illustrates the structure of this event handler.
private void prod_cat_OnData(object sender, ProductCatalogEventArgs e)
{
if (e.Event == ProductDataEvent.Found)
{
// Products were found
foreach (KeyValuePair<ProductKey, Product> kvp in e.ProductCatalog.Products)
{
Console.WriteLine("Key = {0} : Value = {1}", kvp.Key, kvp.Value);
}
}
else
{
// Products were not found
Console.WriteLine("Cannot find product: {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 ProductCatalog class as follows:
prod_cat.OnData -= prod_cat_OnData;
prod_cat.Dispose();
About the Product class
The Product class provides the data as it relates to a given product. Its members include:
// product name
public string Name;
// product description
public string Description;
// product’s currency
public Currency Currency;
// ProductKey struct
public ProductKey Key;
// product’s market
public Market Market;
// product’s product type
public ProductType Type;
The ProductKey struct is a simple collection of the product’s descriptive information. Its constructors and members include:
// Constructor
public ProductKey(MarketId marketId, tt_net_sdk.ProductType type, string name);
// TT’s identifier for this product
public UInt64 ProductId;
// MarketKey struct
public MarketKey MarketKey;
// product’s name
public string Name;
// product’s product type
public tt_net_sdk.ProductType Type;
Note that the ProductId
is only populated if the instance is constructed as a result of a product fetch via the ProductCatalog class.