TT CORE SDK

Working with Price Subscriptions

Working with Price Subscriptions

To create a price subscription and start receiving updates, you:

  1. Create a class that is derived from ttsdk::IPriceEventHandler and defines your event handler. You must provide implementations for:
    • virtual void OnPriceUpdate(const uint64_t subId, const InstrumentPtr& instrument, const PriceSnap& snap) = 0;
      • Callback delivering price updates
    • 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 SubscribePrices() function passing your event handler instance variable.
    • The SubscribePrices() function allows you to specify whether to wish to optionally receive:
      • market depth
      • exchange calculated and disseminated implied prices
      • TT calculated and disseminated implied prices
      • TT’s simulation price stream

The following code snippet demonstrates an example of this process.


    
    class PriceObserver : public ttsdk::IPriceEventHandler
    {
      public:
           PriceObserver() {}
    
          virtual void OnPriceUpdate(const uint64_t subId, 
                  const ttsdk::InstrumentPtr& instrument, const ttsdk::PriceSnap& snap)
          {
                  std::cout << “Price snapshot 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 << "Prices disconnected for subscription for instrument:" 
                          << instrument->GetAlias() << " subid=" << subId << std::endl;
          }
    
          virtual void OnUnsubscribed(const uint64_t subId) 
          {
                  std::cout << "Price observer for subId=" << subId 
                          << " has been successfully unsubscribed. " << std::endl;
          }        
    };
    
    void foo()
    {
            // …
    
            PriceObserver prcObserver;
    
            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::SubscribePrices(instrument, 10, true, true, false, &prcObserver);
              std::cout << "Subscription id=" << subId << std::endl; 
            }
    
              // ...
    }