Building your first algo
Capturing fills data
In this section, you will create the logic to extract data from your entry order fills and use the data to calculate your running average open price. The average open price will later be used to calculate the exit price of our Scalper algo.
This section covers:
- The difference between continuous and discrete messages.
- Using ADL's Formula Editor.
- Using the math blocks to perform calculations.
Messages in ADL: Continuous Messages vs Discrete Messages
Data passed between blocks in ADL are referred to as messages. The two types of messages in ADL are:
- Continuous Messages: a continuous input of specified data.
- Discrete Messages: an individual message sent at a specific point in time, triggered by an event, storing details about the event in fields.
Thus far, the blocks of the Scapler algo use continuous messages:
- The Instrument block outputs a continuous feed of instrument data.
- The Field block continuously outputs the Best Bid price for the selected instrument.
- The Number block provides a continuous output of 5.
Next, you will use discrete event messages such as those emitted through the Entry Order block's fills output port which outputs an individual discrete message for each fill that occurs using the orders placed by this block.
The fills discrete message contains fields that capture information about the fill, such as the fill price and fill quantity.
Calculating the average open price
The Scalper algo will calculate the average open price of all executed entry orders. This will later be used to calculate the exit price. To calculate the average open price:
- Calculate value at risk which is the cost of your position.
- Total the fill quantities of all entry orders.
- Divide the value at risk by the total fill quantities to determine the average open price.
Calculating the value at risk
As fills are received from your entry order, the total cost of those fills can be calculated by multiplying the price by the quantity of each fill. The ValueAccumulator block lets you perform this calculation for each fill and then accumulates a running total of each result. The block uses the provided formula to calculate a new value to accumulate each time a discrete message enters the block.
To calculate the value at risk from your order fills:
From the Blocks panel, locate the ValueAccumulator block and place it on the canvas to the right of the Entry Order block.
Connect the fills output port of the Entry Order block to the top input port of the ValueAccumulator block.
Notice that the edge displays differently to provide a visual indication that this edge passes discrete messages.
In the Block Properties panel, set the Name to VAR.
Define the formula for the block to use in its calculations by clicking the edit link in the Formula field.
The Formula Editor for the block appears.
In the Formula Builder, you will create a formula to multiply the fill price and the fill quantity to determine the cost of the incoming fill.
To access discrete message fields sent into the block, you type #, followed by the name of the value you want to use. The editor displays the available message fields as you type.
Type # and select fillPrice from the list to add the fillPrice field to the formula.
Type * for multiplication and then select the fillQuantity field to complete the formula.
Notice the Formula field of the Block Properties now shows the block formula.
Tracking the total filled quantities
To determine the average price of all fills received for your order, you need to keep track of quantities of all fills. In a similar manner used to calculate the value at risk, you will use the ValueAccumulator block to keep a running total of the fill quantities.
To track the total filled quantities:
Drag another ValueAccumlator block to the canvas beneath the VAR block and connect it to the Entry Order fills output port.
In the Block Properties panel, set the Name to Fill Qty.
Open the Formula Editor and type # to add the fillQty field to the formula.
Calculate the average open price
Finally, you need to use the values from the VAR and Fill Qty blocks to determine the average open price. You will use the Divide block to perform this calculation.
To determine the average open price:
From the Blocks panel, drag a Divide block to the canvas. Rename it to Average Open Price.
Connect the top numeric output port of the VAR block to the top input port of the Average Open Price block.
Connect the top numeric output port of the Fill Qty block to the bottom input port of the Average Open Price block.
Testing your logic
Before continuing with the algo, it is a good idea to test your fills logic to make sure it is functioning as intended.
To test your logic, using the same setup as before:
In the Values tab, enter a larger order quantity so you can verify each fill updates the blocks values properly.
Play the algo and watch the numeric outputs of the VAR, Fill Qty and Average Open Price blocks to ensure they are updating as expected.
Stop the algo.
You have successfully completed capturing data from your fills and performing calculations with the data. Now you are ready to use these values to determine the price and quantity for your exit order.