Every Algo must, at some point, launch a child order to be useful. Each child order can be another Algo or a regular exchange order. When the child order is updated or filled, those changes are also sent to the parent.
Since launching a child order is one of the most latency-sensitive things an Algo can do, CQG Custom Algo SDK enables you to create a child order template. An order template prepares an order based on a known OrderDefinition and gets it ready to send on a background thread. When the Algo determines that it is time to send the order, it only needs to provide the price and quantity to complete and send the order. If an Algo expects to send multiple child orders, it can reserve more than one. Preparing a child template is typically done in do_before_launch callback.
virtual ChildTemplate& IBeforeLaunchAlgoServices::make_child_template( api::orderman::OrderDefinition const &child_def, size_t reserve = 1);
IAlgoServices provides two overloads for launching child orders. By default, launching a child order automatically subscribes the Algo to its state changes.
Launch a child order from a template if you have one prepared. This is the fastest option.
OrderKey IAlgoServices::launch_child(ChildTemplate&, Price, Quantity, bool subscribe = true);
Launch a child order by constructing an OrderDefinition as needed.
OrderKey launch_child(OrderDefinition const &, Price, Quantity, bool subscribe = true);
When a child is launched, its OrderKey
is returned to the caller. This ID can be used to update, cancel, or detach the child order.
To update an existing child order, call update_child and pass an Update. The Update type lets you change the price, size or the entire OrderDefinition. You only need to specify the fields that you intend to update, so only set the price() field if that is what you are changing.
void IAlgoServices::update_child(OrderKey child, api::orderman::Update const &);
To cancel an existing child order, call cancel_child
. When the order is canceled you will receive an update though the on_child_change
callback.
void IAlgoServices::cancel_child(OrderKey child);
The lifetime of a child order is tied to its parent. If a parent is canceled or completed, the associated child orders are automatically stopped as well. If the Algo needs to launch an order that will continue after the Algo ends, use detach_child
to sever the connection. This order hierarchy can be viewed in Algo API test client Order Book.
void IAlgoServices::detach_child(OrderKey child);
To detach all children at once use detach_all_children
.
void IAlgoServices::detach_all_children();
When a child order is updated, the parent receives the on_child_change
callback with an updated ChildState object.
void on_child_change(OrderKey, ChildState const &) override; struct ChildState { Price price; Quantity quantity; Quantity total_fills; Price avg_price; Quantity last_fills; Price last_price; ChildStatus status; Quantity fills_at_attach_time; };