To create a basic TT Core SDK application that downloads the Order Book on start up, you need to:
- Create a class that is derived from ttsdk::IEventHandler and defines your SDK event handler. You must provide implementations for:
- virtual void OnStatus(const Status status) = 0;
- Create a class that is derived from ttsdk::IOrderBookEventHandler and defines your Order Book event handler.
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. - Initialize the TT Core SDK passing your TTSDKOptions instance variable, your SDK event handler instance variable, and your Order Book event handler instance variable.
- Once an application initializes the TT Core SDK shared library, all other applications will use the same instance, so any subsequent calls will be a no-op.
- Note that both the SDK and Order Book event handlers must have a lifespan that is valid for the life of the SDK (until shutdown is completed).
After the initialization is started, TT Core SDK downloads:
- the accounts belonging to the user
- the orders associated with each account
- the positions associated with each account
TT Core SDK synchronizes the Order Book and positions by account. As such, any given account can be used to trade without needing to wait for all accounts to be ready. An OnAccountDownloadEnd event is fired when an account is ready to be used for trading. An OnOrderBookDownloadEnd event is fired when all accounts are ready.
The following code snippet demonstrates an example of this process.
#include <iostream>
#include <iomanip>
#include <atomic>
#include <sstream>
#include <string.h>
#include <tt_cplus_sdk.h>
#include <condition_variable>
std::mutex mutex;
std::condition_variable sdkReadyCondition;
//
// SDK event handler class
//
class SDKEventHandler : public ttsdk::IEventHandler
{
public:
SDKEventHandler() : IEventHandler() {}
virtual ~SDKEventHandler() {}
// received on an SDK managed thread that can be used for processing.
// beware that time consuming tasks can delay the delivery of another status event.
virtual void OnStatus(const ttsdk::IEventHandler::Status status) override
{
std::cout lock(mutex);
sdkReadyCondition.notify_one();
}
// the SDK synchronizes the order book and positions by account. as such,
// any given account can be able to trade without needing to wait
// for all accounts to be ready
virtual void OnAccountDownloadEnd(const uint64_t accountId) override
{
std::cout GetAlias() lock(mutex);
if (sdkReadyCondition.wait_for(lock, std::chrono::seconds(300)) == std::cv_status::timeout)
{
std::cout >>>>" > command)
{
if (command == "q")
{
std::cout