Fetching a single instrument
Steps for fetching a single instrument
To fetch a single Instrument, you:
Instantiate the InstrumentLookup class.
Choose a fetch type:
Synchronous
- Call the Get() method.
Asynchronous
- Create an event handler method that will be called when notifications regarding this instrument are available.
- Register this event handler with the InstrumentLookup instance.
- Call the GetAsync() method of the InstrumentLookup instance.
Instantiating the InstrumentLookup class
The InstrumentLookup class provides several overloaded constructors that let you identify instruments using different sets of criteria. These are listed below along with code snippets showing their use.
Fetch by instrument key
Creates an instrument subscription given an InstrumentKey instance.
InstrumentKey ik = new InstrumentKey( MarketId.CME, ProductType.Future, "CL", “CL Dec19”); InstrumentLookup m_instrLookupRequest = new InstrumentLookup( tt_net_sdk.Dispatcher.Current, ik);
Fetch by Market ID, product key, product name and instrument alias
Creates an instrument subscription given a MarketId, ProductType, product name and instrument alias (ex. "CL Dec20").
InstrumentLookup m_instrLookupRequest = new InstrumentLookup( tt_net_sdk.Dispatcher.Current, MarketId.CME, ProductType.Future, "CL", "CL Dec19");
Note
- The instrument alias parameter must match the string displayed in TT and TT Desktop, where it is called the "contract alias".
- The instrument alias parameter for Autospreader and Aggregator instruments must match the name with which it was created.
- The product type for Autospreader and Aggregator instruments is “Synthetic”.
Synchronous fetch
Once an InstrumentLookup object has been created, you can simply call the Get() method to perform a synchronous lookup of the instrument as shown below. Note that although this is normally fairly quick, your execution thread will be blocked until this action is complete.
ProductDataEvent e = m_instrLookupRequest.Get();
if (e == ProductDataEvent.Found)
{
// Instrument was found
Instrument instrument = m_instrLookupRequest.Instrument;
Console.WriteLine("Found: {0}", instrument);
}
else
{
// Instrument was 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.
m_instrLookupRequest.OnData +=
new EventHandler<InstrumentLookupEventArgs>(m_instrLookupRequest_OnData);
m_instrLookupRequest.GetAsync();
After the GetAsync() method of the InstrumentLookup instance is called, TT .NET SDK performs a lookup for the instrument based on the inputs that it are given. 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 m_instrLookupRequest_OnData(object sender, InstrumentLookupEventArgs e)
{
if (e.Event == ProductDataEvent.Found)
{
// Instrument was found
Instrument instrument = e.Instrument;
Console.WriteLine("Found: {0}", instrument);
}
else
{
// Instrument was 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 InstrumentLookup class as follows:
m_instrLookupRequest.OnData -= m_instrLookupRequest_OnData;
m_instrLookupRequest.Dispose();