TT CORE SDK

Initializing a Server Application

Initializing a Server Application

To build this type of application, you must:

  • Create a class that is derived from ttsdk::ISDKAlgoManager and define your external algo request event handler. You must provide implementations for::
    • virtual uint64_t GetInstanceId() = 0;
      • Returns the unique id for this instance of an TT Application Server. This value should remain the same through all runs of this instance since it identifies the application that is managing the algos within the system, allowing working algos to be recovered in the case of the algo server application shutting down with working algo running.
    • virtual const char* GetMarkets() = 0;
      • Returns the preferred markets for this instance in a comma separated list of market ids. The return string should not be created on the stack in the method since it needs to exist outside the method call. It should be a data member or static data. A null or empty return value indicates any market can be routed to this instance. A hard coded example for CME would be "7" and a hard coded example for CME and ICE, "7,32". A complete list of market IDs can be found here.
    • virtual void OnRecoverAlgo(SDKAlgoPtr algoOrder, SDKAlgoRequestPtr req) = 0;
      • Callback when a working algo is downloaded on startup and needs to be recovered
    • virtual void OnRecoveryEnd() = 0;
      • Callback when all algos have been downloaded and OnRecoverAlgo called for each of them.
    • virtual void OnRecoveryFailed() = 0;
      • Callback when the previous algos can not be downloaded and recovered.
    • virtual void OnStartRequest(SDKAlgoPtr algoOrder, SDKAlgoRequestPtr req) = 0;
      • Callback to start a new algo instance.
    • virtual void OnStopRequest(SDKAlgoPtr algoOrder, SDKAlgoRequestPtr req) = 0;
      • Callback to stop an existing algo instance.
    • virtual void OnUpdateRequest(SDKAlgoPtr algoOrder, SDKAlgoRequestPtr req) = 0;
      • Callback when an update request is made for an existing algo.
    • virtual void OnPauseRequest(SDKAlgoPtr algoOrder, SDKAlgoRequestPtr req) = 0;
      • Callback when a request to pause an algo is received. When an algo is in paused state, no fills or user response messages may be generated and sent to the users.
    • virtual void OnResumeRequest(SDKAlgoPtr algoOrder, SDKAlgoRequestPtr req) = 0;
      • Callback when a request to restart a paused algo is received.
    • virtual void OnScheduledEvent(SDKAlgoPtr algoOrder, unsigned int eventId, void* eventUserData) = 0;
      • Callback for an event scheduled by the algo. An event is scheduled programmatically through the ScheduleEvent() method.
    • virtual void OnAlgoCompleted(SDKAlgoPtr algoOrder) = 0;
      • Callback when an algo is finished and should no longer be used. This will be called when a stop request is successful, a full fill is sent, the algo fails or the start request fails. The user should properly shutdown the algo and clean up the resources it is using when this event is received.
  • Create an instance of the TTSDKOptions class. Define the TT environment to which your application will connect and specify your application key for this environment.
  • Create a class that is derived from ttsdk::IEventHandler and defines your SDK event handler as detailed in the Initialzing An Application section.
  • Create a class that is derived from ttsdk::IOrderBookEventHandler and defines your Order Book event handler as detailed Receiving Solicited and Unsolicited Order / Fill Events section.
  • Initialize the TT Core SDK passing your TTSDKOptions instance variable, SDK event handler instance variable, Order Book event handler instance variable, and external algo request event handler instance variable to the following overloaded SDK initialization function.
    • bool Initialize(const TTSDKOptions& options, IEventHandlerPtr sdkEventObserver, IOrderBookEventHandlerPtr orderHandler, ISDKAlgoManagerPtr algoManager = nullptr) noexcept;

The following code snippet demonstrates an example of this process.


#pragma once
#include <string.h>
#include <map>
#include <memory>
#include <tt_cplus_sdk.h>
    
//
//  external algo request event handler class
//
    
class SDKAlgoManager : public ttsdk::ISDKAlgoManager
{
    public:
    SDKAlgoManager() : ttsdk::ISDKAlgoManager() {};
    virtual ~SDKAlgoManager() {};
    virtual uint64_t GetInstanceId() override
    {
        return 1;
    }
    virtual const char* GetMarkets() override
    {
        return markets_.c_str();
    }
    
    virtual void OnRecoverAlgo(ttsdk::SDKAlgoPtr algoOrder, 
                                ttsdk::SDKAlgoRequestPtr req)
    {   //  …  }
    virtual void OnRecoveryEnd()
    {   //  …  }
    virtual void OnRecoveryFailed()
    {   //  …  }
    
    virtual void OnStartRequest(ttsdk::SDKAlgoPtr algoOrder, 
                                ttsdk::SDKAlgoRequestPtr req)
    {   //  …  }
    virtual void OnUpdateRequest(ttsdk::SDKAlgoPtr algoOrder, 
                                ttsdk::SDKAlgoRequestPtr req)
    {   //  …  }
    virtual void OnStopRequest(ttsdk::SDKAlgoPtr algoOrder, 
                                ttsdk::SDKAlgoRequestPtr req)
    {   //  …  }
    virtual void OnPauseRequest(ttsdk::SDKAlgoPtr algoOrder, 
                                ttsdk::SDKAlgoRequestPtr req)
    {   //  …  }
    virtual void OnResumeRequest(ttsdk::SDKAlgoPtr algoOrder, 
                                ttsdk::SDKAlgoRequestPtr req)
    {   //  …  }
    
    virtual void OnAlgoCompleted(ttsdk::SDKAlgoPtr algoOrder)
    {   //  …  }
    virtual void OnScheduledEvent(ttsdk::SDKAlgoPtr algoOrder, 
                                    unsigned int eventId, 
                                    void* eventUserData)
    {   //  …  }
    
protected:
    std::string markets_ = "7,32";
};
    
//
//  SDK event handler class
//  See here for further details
//
    
class SDKEventHandler : public ttsdk::IEventHandler
{
    // …
}
    
//
//  Order Book event handler class
//  See here for further details
//
    
class MyOrderBookHandler : public ttsdk::IOrderBookEventHandler
{
    //  ...
}
    
//
//  Main
//
    
int main(int argc, char* argv[])
{
    //Set the environment the app needs to run in here
    ttsdk::Environment env = ttsdk::Environment::ProdLive;
    
    // Add your app secret Key here. It looks like: 00000000-0000-0000-0000-000000000000:00000000-0000-0000-0000-000000000000
    std::string app_key = “Your App Key”;
    
    ttsdk::TTSDKOptions options;
    options.environment = env;
    options.app_key_secret = app_key.c_str();
    
    SDKEventHandler myObserver;
    MyOrderBookHandler myOrderBookObserver;
    SDKAlgoManager algoManager;
    
    if (!ttsdk::Initialize(options, &myObserver, &myOrderBookObserver, &algoManager))
    {
        std::cout << "Unable to initialize SDK!" << std::endl;
        return -1;
    } 
    
    std::unique_lock lock(mutex);
    if (sdkReadyCondition.wait_for(lock, std::chrono::seconds(300)) ==
            std::cv_status::timeout)
    {
        std::cout << "Timeout waiting for SDK to initialize!" << std::endl;
        return -1;
    }
    
    std::cout << std::endl;
    std::cout << "<<<<< TT CORE SDK is initialized. >>>>>" << std::endl;
    
    std::cout << std::endl << "Press q to exit....." << std::endl;
    std::string command;
    while (std::cin >> command)
    {
        if (command == "q")
        {
            std::cout << "Quitting...\n";
            break;
        }
    }
    std::cout << "Exiting..." << std::endl;
    ttsdk::Shutdown();
}