+++ title = "Child Orders" weight = 30 +++ Every Algo must, at some point, launch a child order to be useful. Each child order can be another Algo or regular exchange order. When the child order is updated or filled, those changes are also sent to the parent. # Templates Since launching a child order is one of the most latency-sensitive things an Algo can do, the Server SDK enables you to create a child 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 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](http://127.0.0.1:1111/server/ref/initialization/#before-launch) callback. ```cpp virtual ChildTemplate& IBeforeLaunchAlgoServices::make_child_template( api::orderman::OrderDefinition const &child_def, size_t reserve = 1); ``` # Launch 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. ```cpp OrderKey IAlgoServices::launch_child(ChildTemplate&, Price, Quantity, bool subscribe = true); ``` Launch a child order by constructing an OrderDefinition as needed. ```cpp 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. ## Update 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. ```cpp void IAlgoServices::update_child(OrderKey child, api::orderman::Update const &); ``` ## Cancel 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. ```cpp void IAlgoServices::cancel_child(OrderKey child); ``` ## Detach The lifetime of a child order is tied to its parent. If a parent is canceled or completed 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 the Spark [Order Book](https://www.bluetradingsystems.com/docs/bts_spark/Trading/#order-book). ```cpp void IAlgoServices::detach_child(OrderKey child); ``` To detach all children at once use `detach_all_children`. ```cpp void IAlgoServices::detach_all_children(); ``` # Child Updated When a child order is updated the parent receives the `on_child_change` callback with an updated ChildState object. ```cpp 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; }; ```