TT CORE SDK  0.1
TT CORE SDK documentation
Managing Orders and Fills

Order management

The OrderManager class handles the management of orders that are submitted through them. As such, you must create one or more instances of the OrderManager class class in order to submit, change, and cancel orders.

The OrderManager class is defined in the orders.hpp header file. It has a default constructor that can be used to create an instance as follows.

OrderManager m_manager;

Specifying order parameters

Before you submit an order, you must first create an instance of the order_profile struct. You then set the attributes of your order. This struct is defined in the orders.hpp header file. The attributes available are as follows:

order_profile Struct

Public Attributes Description
account Account ID
instrument

Reference to a valid instance of an Instrument class

The Instrument class is defined in the instrument.hpp file.

price Limit price in decimal (Points)
quantity Order quantity
side

Order side

The order_side enum is defined in the enums.hpp file.

tif

Time in force

The time_in_force enum is defined in the enums.hpp file.

trigger_price Stop order trigger price in decimal (Point); only required when order_type = STOP or STOP_LIMIT.
type

Order type

The order_type enum is defined in the enums.hpp file.

The following example demonstrates how to create an instance of the order_profile struct for a limit order.

order_profile profile;
profile.account = 537;
profile.instrument = instr;
profile.type = order_type::LIMIT;
profile.side = order_side::SELL;
profile.tif = time_in_force::GOOD_TILL_CANCEL;
profile.price = 162300.0;
profile.quantity = 5;

After you populate this structure, you must then create a handle to this order as follows:

auto handle = std::make_shared<OrderHandle>(m_manager, profile);

The OrderHandle instance is a pointer to the internal representation of the order data that will be put on the wire when it is submitted. For the sake of efficiency, it is useful to pre-create one or more of these for the orders that you will be submitting.

Submitting a New Order

An order is submitted by calling the PlaceOrder() method of the same instance of the OrderManager class that was used to create the OrderHandle instance.

m_manager.PlaceOrder(handle, request_id);

A request ID that will be returned with the acknowledgements in order to correlate order events with their request is also passed.

Manipulating Orders

The OrderHandle class provides the following methods for manipulating orders. Note: When the order referenced by the OrderHandle reaches a terminal state (i.e. fully filled, deleted, rejected, etc.) the OrderHandle can no longer manipulate the order. In this case, a new OrderHandle should be constructed.

OrderHandle Class

Member Function Description
ChangeOrder()

Change the order.

void tt_core_sdk::OrderHandle::ChangeOrder(change_request & request);

The change_request struct is defined in the orders.hpp file.

ChangePrice()

Change the order price.

void tt_core_sdk::OrderHandle::ChangePrice(double price, uint32_t request_id = 0);

ChangePriceQuantity()

Change the order price and quantity.

void tt_core_sdk::OrderHandle::ChangePriceQuantity(double price, int32_t quantity, uint32_t request_id = 0);

ChangeQuantity()

Change the order quantity.

void tt_core_sdk::OrderHandle::ChangeQuantity(int32_t quantity, uint32_t request_id = 0);

DeleteOrder()

Delete the order from the market.

void tt_core_sdk::OrderHandle::DeleteOrder(uint32_t request_id = 0);

GetOrderId()

TT Order ID.

const std::string& tt_core_sdk::OrderHandle::GetOrderId(void) const;

GetOrderQuantity()

Order quantity (0 if not available).

GetPrice()

Order price (NAN if not available).

GetWorkingQuantity()

Quantity working in the market (0 if not available).

So, for example, the following code snippet demonstrates how to change an order’s quantity to ten.

handle.ChangeQuantity( 10 );

Order Event Callbacks

As discussed, orders are submitted by calling the PlaceOrder() method of an instance of the OrderManager class. The TT Core SDK will fire the the inherited OrderEventListener() function associated with the instance of the OrderManager class for the initial order acknowledgment as well as all subsequent events for this order. To provide a listener to the OrderManager, call the Subscribe() method.

m_manager.Subscribe(std::make_shared<MyOrderListener>());

The pure virtual methods of the OrderEventListener that must be overridden include:

class MyOrderListener : public OrderEventListener
{
public:
// Callback delivering execution report messages
void OnExecutionReport (const execution_report_response&) override
{
...
}
// Callback delivering order cancel reject messages
void OnOrderCancelReject (const order_cancel_reject_response&) override
{
...
}
// Callback fired when a request delivery surpasses the timeout threshold
void OnOrderTimeout (const order_timeout_response&) override
{
...
}
// Callback fired when a request failed to be delivered
void OnRequestFailed (const request_failed_response&) override
{
...
}
};

In the above snippet one of the callbacks will be fired for every request. The parameter in each of the callbacks contains a request_id that represents the id provided when making the request.

Processing OnExecutionReport() order events

When the OnExecutionReport() callback is called, an instance of the execution_report_response struct is passed. This struct contain the following attributes.

Public Attribute Description
er

Response details.

Returns a ExecutionReport object, which is defined in the orders.hpp file.

handle

Order tied to the response.

Returns a OrderHandle object, which is defined in the orders.hpp file.

request_id Id of the request generating the response.

Internally, TT uses the logical flows prescribed by the FIX Protocol (https://www.fixtrading.org/) to convey order state transitions. The er variable is an instance of the ExecutionReport class, which communicates the data that is part of the FIX Protocol Execution Report message. This includes the reason for the Execution Report (“What happened?”) as well as the current state of the order. The ExecutionReport class provides the following methods for extracting this data.

ExecutionReport Class

Member Function Description
GetAccountId()

Account ID.

std::pair<bool, uint64_t> tt_core_sdk::ExecutionReport::GetAccountId(void) const;
GetAccountName()

Account name.

std::pair<bool, std::string> tt_core_sdk::ExecutionReport::GetAccountName(void) const;
GetBrokerId()

Broker ID.

std::pair<bool, uint64_t> tt_core_sdk::ExecutionReport::GetBrokerId(void) const;
GetCumQty()

Total number of contracts that have filled over the life of this order.

std::pair<bool, int32_t> tt_core_sdk::ExecutionReport::GetCumQty(void) const;
GetExecId()

Unique identifier for this execution report.

std::pair<bool, std::string> tt_core_sdk::ExecutionReport::GetExecId(void) const;
GetExecType()

The reason that this execution report is being sent. 

std::pair<bool, exec_type> tt_core_sdk::ExecutionReport::GetExecType(void) const;

The exec_type enum is defined in the enums.hpp file.

IsDelete()

Helper method for determining if the reason for this message is because the order was deleted.

IsFill()

Helper method for determining if the reason for this message is because the order was filled.

IsFullFill()

Helper method for determining if the reason for this message is because the order was fully filled.

IsPending()

helper method for determining if the reason for this message is because the order is in a pending state.

IsReject()

Helper method for determining if the reason for this message is because the order was rejected.

IsTriggerActivated()

Helper method for determining if the reason for this message is because the order was triggered.

GetInstrumentId()

The TT ID for the instrument.

std::pair<bool, uint64_t> tt_core_sdk::ExecutionReport::GetInstrumentId(void) const;
GetLastFillPrice()

The price of the most recent fill.

std::pair<bool, double> tt_core_sdk::ExecutionReport::GetLastFillPrice(void) const;
GetLastFillQty()

The quantity of the most recent fill.

std::pair<bool, int32_t> tt_core_sdk::ExecutionReport::GetLastFillQty(void) const;
GetLeavesQty()

Number of contracts that are still working in the market. 

std::pair<bool, int32_t> tt_core_sdk::ExecutionReport::GetLeavesQty(void) const;
GetMarket()

The instrument’s market.

std::pair<bool, market> tt_core_sdk::ExecutionReport::GetMarket(void) const;

The market enum is defined in the enums.hpp file.

GetOrderId()

The TT order ID.

std::pair<bool, std::string> tt_core_sdk::ExecutionReport::GetOrderId(void) const;
GetOrderQty()

The order's quantity.

std::pair<bool, int32_t> tt_core_sdk::ExecutionReport::GetOrderQty(void) const;
GetOrderRejectReason()

Reject details.

std::pair<bool, ord_rej_reason> tt_core_sdk::ExecutionReport::GetOrderRejectReason(void) const;

The ord_rej_reason enum is defined in the enums.hpp file.

GetOrderStatus()

Current status of the order.

std::pair<bool, order_status> tt_core_sdk::ExecutionReport::GetOrderStatus(void) const;

The order_status enum is defined in the enums.hpp file.

GetOrderType()

Order type.

std::pair<bool, order_type> tt_core_sdk::ExecutionReport::GetOrderType(void) const;

The order_type enum is defined in the enums.hpp file.

GetPrice()

The current price of the order.

std::pair<bool, double> tt_core_sdk::ExecutionReport::GetLastPrice(void) const;
GetRejectCode()

The reason for the rejection.

std::pair<bool, reject_code> tt_core_sdk::ExecutionReport::GetRejectCode(void) const;

The reject_code enum is defined in the enums.hpp file.

GetRejectSource()

The source of the rejection.

std::pair<bool, reject_source> tt_core_sdk::ExecutionReport::GetRejectSource(void) const;

The reject_source enum is defined in the enums.hpp file.

GetSide()

Order side.

std::pair<bool, order_side> tt_core_sdk::ExecutionReport::GetSide(void) const;

The order_side enum is defined in the enums.hpp file.

GetText()

Any text included in the message.

std::pair<bool, std::string> tt_core_sdk::ExecutionReport::GetText(void) const;
GetTimeInForce()

Order’s time if force.

std::pair<bool, time_in_force> tt_core_sdk::ExecutionReport::GetTimeInForce(void) const;

The time_in_force enum is defined in the enums.hpp file.

GetTriggerPrice()

Order's trigger price. Only populated if the order type is STOP or STOP_LIMIT.

std::pair<bool, double> tt_core_sdk::ExecutionReport::GetTriggerPrice(void) const;

With the exception of the Is…() helper methods, all of these methods return a std::pair. The “first” parameter is a bool that indicates whether the data is present in the message. The “second” parameter is the actual data if the “first” parameter returns true.

The following code snippet shows a sample OnExecutionReport() callback implementation.

// Callback delivering order events (solicited or unsolicited) for all orders placed
// through the OrderManager associated with this listener.
void OnExecutionReport (const execution_report_response& resp) override
{
auto et = resp.er.GetExecType().second;
auto order_id = resp.er.GetOrderId().second;
auto price = resp.er.GetPrice().second;
std::cout << "OnExecutionReport:"
<< "\n request_id=" << resp.request_id
<< "\n order_id=" << order_id
<< "\n exec_type=" << tt_core_sdk::to_string(et)
<< "\n price=" << price
<< "\n is_fill=" << std::boolalpha << resp.er.IsFill()
<< "\n is_full_fill=" << std::boolalpha << resp.er.IsFullFill()
<< "\n is_delete=" << std::boolalpha << resp.er.IsDelete()
<< "\n is_reject=" << std::boolalpha << resp.er.IsReject()
<< std::endl;
if (resp.er.IsPending())
{
// Choosing to ignore any pending status responses. The strategy will only
// react to the exchange confirmation acknowledgements.
return;
}
else if (resp.er.IsFullFill() || resp.er.IsDelete() || resp.er.IsReject())
{
std::cout << "Order is not working - terminating strategy\n";
...
}
...
}

Processing OnOrderCancelReject() order events

When the OnOrderCancelReject() callback is called, an instance of the order_cancel_reject_response struct is passed. This struct contain the following attributes.

Public Attribute Description
handle

Order tied to the response.

Returns a OrderHandle object, which is defined in the orders.hpp file.

ocr

Response details.

Returns a OrderCancelReject object, which is defined in the orders.hpp file.

request_id Id of the request generating the response.

As discussed, internally, TT uses the logical flows prescribed by the FIX Protocol (https://www.fixtrading.org/) to convey order state transitions. The ocr variable is an instance of the OrderCancelReject class, which communicates the data that is part of the FIX Protocol Order Cancel Reject message. This includes the reason for the Order Cancel Reject (“What happened?”) as well as the current state of the order. The OrderCancelReject class provides the following methods for extracting this data.

Member Function Description
GetAccountId()

Account ID.

std::pair<bool, uint64_t> tt_core_sdk::OrderCancelReject::GetAccountId(void) const;
GetAccountName()

Account name.

std::pair<bool, std::string> tt_core_sdk::OrderCancelReject::GetAccountName(void) const;
GetBrokerId()

Broker ID.

std::pair<bool, uint64_t> tt_core_sdk::OrderCancelReject::GetBrokerId(void) const;
GetCancelRejectReason()

Reject details.

std::pair<bool, cxl_rej_reason> tt_core_sdk::OrderCancelReject::GetOrderRejectReason(void) const;

The cxl_rej_reason enum is defined in the enums.hpp file.

GetInstrumentId()

TT ID for the instrument.

std::pair<bool, uint64_t> tt_core_sdk::OrderCancelReject::GetInstrumentId(void) const;

The Instrument class is defined in the instrument.hpp file.

GetMarket()

Instrument's market.

std::pair<bool, market> tt_core_sdk::OrderCancelReject::GetMarket(void) const;

The market enum is defined in the enums.hpp file.

GetOrderId()

TT order ID.

std::pair<bool, std::string> tt_core_sdk::OrderCancelReject::GetOrderId(void) const;
GetOrderStatus()

Current status of the order.

std::pair<bool, order_status> tt_core_sdk::OrderCancelReject::GetOrderStatus(void) const;

The order_status enum is defined in the enums.hpp file.

GetOrderType()

Order type.

std::pair<bool, order_type> tt_core_sdk::OrderCancelReject::GetOrderType(void) const;

The order_type enum is defined in the enums.hpp file.

GetRejectCode()

Reason for the rejection.

std::pair<bool, reject_code> tt_core_sdk::OrderCancelReject::GetRejectCode(void) const;

The reject_code enum is defined in the enums.hpp file.

GetRejectSource()

Source of the rejection.

std::pair<bool, reject_source> tt_core_sdk::OrderCancelReject::GetRejectSource(void) const;

The reject_source enum is defined in the enums.hpp file.

GetSide()

Order side.

std::pair<bool, order_side> tt_core_sdk::OrderCancelReject::GetSide(void) const;

The order_side enum is defined in the enums.hpp file.

GetText()

Any text included in the message.

std::pair<bool, std::string> tt_core_sdk::OrderCancelReject::GetText(void) const;
GetTimeInForce()

Order’s time if force.

std::pair<bool, time_in_force> tt_core_sdk::OrderCancelReject::GetTimeInForce(void) const;

The time_in_force enum is defined in the enums.hpp file.

All of these methods return a std::pair. The “first” parameter is a bool that indicates whether the data is present in the message. The “second” parameter is the actual data if the “first” parameter returns true.

The following code snippet shows a sample OnOrderCancelReject() callback implementation.

// Callback fired when a cancel request has been rejected.
void OnOrderCancelReject (const order_cancel_reject_response& resp) override
{
auto order_id = resp.ocr.GetOrderId().second;
auto reject_msg = resp.ocr.GetText().second;
std::cout << "OnOrderCancelReject:"
<< "\n request_id=" << resp.request_id
<< "\n order_id=" << order_id
<< "\n reject_msg=" << reject_msg
<< std::endl;
}

Processing OnOrderTimeout() order events

When the OnOrderTimeout() callback is called, an instance of the order_timeout_response struct is passed. This struct contains the following attributes.

Public Attribute Description
handle

Order tied to the response.

Returns a OrderHandle object, which is defined in the orders.hpp file.

request_id Id of the request generating the response.

The following code snippet shows a sample OnOrderTimeout() callback implementation.

// Callback fired when an order request has not received a response
void OnOrderTimeout (const order_timeout_response& resp) override
{
auto order_id = resp.handle->GetOrderId();
std::cout << "OnOrderTimeout:"
<< "\n request_id=" << resp.request_id
<< "\n order_id=" << order_id
<< std::endl;
}

Processing OnRequestFailed() order events

When the OnRequestFailed() callback is called, an instance of the request_failed_response struct is passed. This struct contains the following attributes.

Public Attribute Description
handle

Order tied to the response.

Returns a OrderHandle object, which is defined in the orders.hpp file.

message Message describing the failure.
request_id Id of the request generating the response.

Request IDs

When an algo submits a new order or changes an existing order, the TT Core SDK stores the request ID provided with the request. The request IDs are then passed to the algo in the OrderEventListener callbacks to allow developers to determine which order request that a given event is in response to. When an algo submits a change request for an order, the TT Core SDK will immediately route the message to the exchange if no other change request is in flight.

If, however, there is another change request in flight, the TT Core SDK will queue the submitted change request as well as any further change requests for this order. Once a response for the in flight change request has been received, the TT Core SDK will conflate all request messages in the queue and route a single change request to the exchange. In other words, an algo may not receive an order event for every request that it submits. The request ID can be used to detect when this happens. For example, assume that the following order change messages were submitted prior to the algo receiving the acknowledgment for the initial order submission.

  • Order 1: BUY 10 CME ZB-Mar18 @ 155'11 (Request ID = 1)
  • Order 1: Change price to 155'10 (Request ID = 2)
  • Order 1: Change price to 155'09 (Request ID = 3)
  • Order 1: Change quantity to 12 (Request ID = 4)
  • Order 1: Change price to 155'08 (Request ID = 5)
  • Order 1: Change price to 155'07 (Request ID = 6)

When the response is received for the initial order, a single message would be routed to the exchange with a quantity of 12 and a price of 155'07. And the algo would receive one order event callback with request ID set to 6.