Working with time and sales subscriptions
Subscribing for time and sales data
Time and Sales subscriptions follow the same pattern as other subscriptions within TT .NET SDK. You create the TimeAndSalesSubscription, attach the event handler, and then start the subscription, as shown in the following example.
void m_instrLookupRequest_OnData(object sender, InstrumentLookupEventArgs e)
{
if (e.Event == ProductDataEvent.Found)
{
// Instrument was found
Instrument instrument = e.InstrumentLookup.Instrument;
Console.WriteLine("Found: {0}", instrument);
// Subscribe for Time & Sales Data
m_tsSubscription = new TimeAndSalesSubscription(instrument, tt_net_sdk.Dispatcher.Current);
m_tsSubscription.Update += m_tsSubscription_Update;
m_tsSubscription.Start();
}
else if (e.Event == ProductDataEvent.NotAllowed)
{
Console.WriteLine("Not Allowed : Please check your Token access");
}
else
{
// Instrument was not found and TT API has given up looking for it
Console.WriteLine("Cannot find instrument: {0}", e.Message);
}
}
Creating the subscription event handler
Because many trades can occur within a short time span, when TT .NET SDK fires the TimeAndSalesSubscription.Update event, it passes a collection of TimeAndSalesData objects in the TimeAndSalesEventArgs event handler parameter, where each object represents a single trade that occurred for this contract. The following code snippet shows the basic structure of a sample callback for the TimeAndSalesSubscription.Update event.
void m_tsSubscription_Update(object sender, TimeAndSalesEventArgs e)
{
if (e.Error == null)
{
// More than one LTP/LTQ may be received in a single event
foreach (TimeAndSalesData tsData in e.Data)
{
Price ltp = tsData.TradePrice;
Quantity ltq = tsData.TradeQuantity;
Console.WriteLine("LTP = {0} : LTQ = {1}", ltp.ToString(), ltq.RawValue);
}
}