Building your first algo

Building your first algo

Exiting a position

The exit order portion of your algo act only when your entry order receives a fill. It will take the instrument defined by the instrument block, the Best Ask for the instrument and the fill quantity; then use these inputs to enter a sell order. The exit order portion of your algo needs the following blocks:

  • An Order block configured to submit a Sell Limit order.
  • A Field block to set the order price to the Best Ask price of the instrument.
  • A MsgInfoExtractor block to set the order quantity based on the fill quantity of the entry order.

Add-PIC

Adding the Order block for the sell order

To place the Order block for the exit order:

  1. From the Blocks panel, click and drag the Order block for the Sell side order on to the ADL canvas.

    Add pic

  2. In the Block Properties panel, specify the following properties.

    • In the Side field, select Sell.

    • Optionally, specify "Exit Order" for the Name to make it easier to identify the block.

  3. Connect the Instrument block's ZB Dec17 output port to the inst input port of the Exit Order block.

Adding the Field block for the order price

Next, you need to specify the price of the exit order. This algo uses a Field block to extract the Best Ask price of the instrument. During this process, you will also see how the Search feature can simplify the process of locating and pre-populating blocks.

  1. From the Blocks panel, type ask in the search field to filter the available blocks. Then, click and drag the Field (askPrice) block for the Best Ask Price on to the ADL canvas.

    Add pic

  2. In the Block Properties panel, specify the following properties.

    • In the Field Name drop-down, ensure Ask Price is selected.

    • Optionally, specify "Best Ask" for the Name to make it easier to identify the block.

    • In the Lookup Type drop-down, ensure Best is selected (as it is the default value).

  3. Connect the Instrument block's ZB Dec17 output port to the input port of the Best Ask Field block; then connect the Best Ask Field block output port to the price input port of the Exit Order block.

Adding the MsgInfo Extractor block to get the order quantity

Finally, your exit order needs an order quantity. In this example, you want the algo to submit a sell order to offset the quantity of the fill from the entry order. Because the quantity of a fill might be less than the total entry order quantity, a Number block is not a good choice. The MsgInfoExtractor block lets you extract information contained in its incoming discrete event message. Notice that the fills port of the entry order block outputs a discrete event message. This discrete event message contains information about the fill, including the fill price and quantity. For your exit order, you want to get the fill quantity.

To add the MsgInfoExtractor block and configure it to get the fill quantity from the fill and send it to the exit order:

  1. From the Blocks panel, click and drag the MsgInfoExtractor block for the Sell side order on to the ADL canvas.

    Add pic

  2. In the Block Properties panel, specify the following properties:

    • In the Connectors field, check the fillQuantity connector.

    • Optionally, specify "Get Entry Fill Qty" for the Name to make it easier to identify the block.

  3. Press and hold the Shift key to display the suggested connections.

  4. While holding down the Shift key, click the two dashed lines to connect the fills output port to the MsgInfo Extractor block and from the fillQuantity output port to the exit order's price input port.

    Add-PIC

You have now completed the exit order portion of your algo, which places an order at the best ask for the quantity filled once your entry order executes. Your algo is now complete. The next step is to test your algo.