TT CORE SDK

Working with Time and Sales Subscriptions

Working with Time and Sales Subscriptions

To create a Time and Sales subscription and start receiving updates, you:

  1. Create a class that is derived from ttsdk::ITimeSalesEventHandler and defines your event handler. You must provide implementations for:
    • virtual void OnTimeSalesUpdate(const uint64_t subId, const InstrumentPtr& instrument, const TimeSales& ts) = 0;
    • Callback fired when trades occur in the market
    • virtual void OnError(const uint64_t subId, const InstrumentPtr& instrument, const SubscriptionError code, const char* error) = 0;
    • Callback fired when an error occurs fulfilling a price subscription request
    • virtual void OnDisconnect(const uint64_t subId, const InstrumentPtr& instrument, const PriceEventType type) = 0;
    • Callback fired when an disconnection event happens
    • virtual void OnUnsubscribed(const uint64_t subId) = 0;
    • Callback fired when the unsubscribe request is complete and it is safe to destroy the handler object
  2. Call the SubscribeTimeAndSales() function passing your event handler instance variable.
    • The SubscribeTimeAndSales() function allows you to specify whether to wish to optionally receive TT’s simulation price stream.

The following code snippet demonstrates an example of this process.


class TSObserver : public ttsdk::ITimeSalesEventHandler
  {
    public:
      TSObserver() {}

      virtual void OnTimeSalesUpdate(const uint64_t subId, 
                                     const ttsdk::InstrumentPtr& instrument, 
                                     const ttsdk::TimeSales& ts)
      {
        std::cout << “Time and Sales event received.” 
                  << std::endl;
      }

      virtual void OnError(const uint64_t subId, 
                           const ttsdk::InstrumentPtr& instrument, 
                           const ttsdk::SubscriptionError code, 
                           const char* error)
      {
        std::cout << "Error [" << error  << "] occurred for instrument:" 
                  << instrument->GetAlias() 
                  << " subId=" << subId 
                  << " code=" << (int)code 
                  << std::endl;
      }

      virtual void OnDisconnect(const uint64_t subId, 
                                const ttsdk::InstrumentPtr& instrument, 
                                const ttsdk::PriceEventType type)
      {
        std::cout << "TimeAndSales disconnected for subscription for instrument:" 
                  << instrument->GetAlias() 
                  << " subid=" << subId 
                  << std::endl;
      }

      virtual void OnUnsubscribed(const uint64_t subId) 
      {
      std::cout << "TimeAndSales observer for subId=" 
                << subId 
                << " has been successfully unsubscribed." 
                << std::endl;
      }  
  };


void foo()
{
  // …

  TSObserver tsObserver;

  ttsdk::Instrument::ResponseCode respCode;
  ttsdk::MarketId market = ttsdk::MarketId::CME;
  std::string product = “GE”;
  ttsdk::ProductType productType = ttsdk::ProductType::Future;
  std::string alias = "GE Sep28";
  ttsdk::InstrumentPtr instrument = ttsdk::GetInstrument(market, product.c_str(),
  productType, alias.c_str(), respCode);
  if (!instrument)
  {
    std::cout << "Unable to find instrument with alias = “ 
              << alias 
              << std::endl;
  }
  else
  {
    uint64_t subId = 0;
    subId = ttsdk::SubscribeTimeAndSales(instrument, false, &tsObserver);
    std::cout << "Subscription id=" << subId 
              << std::endl; 
  }

  // …
}