The following section describes how query blocks are constructed.
Query blocks form the skeleton of the MIM query language and contain two functional sections, the event or scenario, and the action. The event is the combination of one or more conditions which triggers the associated action. The actions fall into three general categories:
Show a report of attribute values
Simulate trade placement and position management
Process variable assignments
In all cases, the event conditions are evaluated in a “fall through” manner. If a statement is found to be true, evaluation proceeds to the next statement. If a statement is false, evaluation of the event stops, its associated action is not triggered, and execution proceeds to the next query block, or next data point. In order for the action to be triggered, all statements must evaluate as true.
The following sections detail each query block type by outlining the processing sequence and the nature of the associated action. Examples of each type are provided.
| FORM | ORDER of EXECUTION | ACTION |
|---|---|---|
DO <conditions> SHOW <attributes> WHEN <conditions> | 1: WHEN 2: DO 3: SHOW is | SHOW – reports attribute values and optionally calculates summary statistics about the columns of data reported. |
This models the event in the WHEN block, then performs calculations and flag settings in the DO, and reports attribute values with the SHOW.
Example 1:
DO
myCounter = myCounter + 1
SHOW
1: myCounter
2: percent_move from today to 2 days later of Close of DJIA
WHEN
Close of DJIA crosses above 52 week average of Close of DJIA 1 day
agoEnglish: This query answers the question, ‘How many times has the DJIA crossed above its 52 week average, and after each crossing, what was the percent change over the next 2 days’.
In all 3 sections, zero or more statements could have been used. When a section has zero statements, the related keyword can be left off as in example 2. |
In the second show attribute, we have legally substituted the percent sign for the word percent. |
Example 2:
SHOW
1: move from today to 2 days later of Close of DJIA
WHEN
Date is after 1998
AND
Date is WednesdayEnglish: This query answers the question, ‘How much has the DJIA moved in two days since its closing when the date is a Wednesday after the year 1998’.
The DO section is not being used and the keyword was omitted. |
The following options affect the DO-SHOW-WHEN block: See the XMIM User Guide for details on these options.
Execution units
Attribute Units
Execute Options
| FORM | ORDER of EXECUTION | ACTION |
|---|---|---|
<label>: ORDER <label>: <order placement rules> WHEN <conditions> EXIT <conditions> | 1: WHEN 2: <order placement rules> 3: EXIT | ORDER – order placed when condition is true. EXIT – position is exited when condition is true. |
The ORDER block can best be understood by looking at how a trade is made in the real world.
First an event is observed. This is represented by the WHEN condition. Next an order is placed (not to be confused with executing a trade). Generally the order contains rules of execution, either implied, such as wait until the market opens tomorrow, or specified such as only if we trade down to a specific price over the next 3 days. This is handled in the <order placement rules>. If the <order placement rules> are satisfied, the trade is filled, if they are not the order is canceled. Finally, the EXIT block is evaluated each day there is a position on, and when true, the position is closed.
1: ORDER
1.1: Buy 1 contract of JY
WHEN
Close of JY crosses above 30 day average of JY
EXIT
Close of JY crosses below 10 day average of JYEnglish: This ORDER block will get long 1 contract of JY when JY closes above its 30 day average, and exit when JY closes below its 10 day average.
In the DO-SHOW-WHEN block, some parts can be omitted, namely the WHEN, and EXIT sections. When these sections have no conditions, the keywords can be omitted. |
The text ‘1:’ and ‘1.1’ are labels and can be replaced with text strings as long as they do not contain white spaces or special characters. |
The following options affect the ORDER block:
Execution Units
Attribute Units
Execute Options
Profit-Loss Options
The following shows the order placement rules:
<trade><time offset><size> <instrument> <entry rules> <exit rules>
where:
<trade> - This is the type trade to make, either Buy or Sell
<size> - This is the quantity to trade, it can be an <attribute> shares; percent; dollars
<instrument> - This is the symbol to trade, it can also be an <attribute>
<entry rules>–
[Enter [on the (open | close | market)]
[ with [entry_stop at <attribute>]
[limit at <attribute>] ] ]<exit rules> –
[Exit [on the (Open | Close| Market)]
[ with
[stop_loss at <attribute>]
[trail stop_loss at <attribute>]
[take profit at <attribute>]
[trail profit at <attribute]
]
]Example:
1: ORDER
1.1: Buy 1 contract of US
Enter on the market
Exit with stop_loss at Low of US 1 day ago
trail stop_loss at Low of US 1 day ago
take profit at Close of US 1 day ago + 10 day average of range of US
trail profit at Low of US 1 day agoThis block will be executed once per day, and on days that are
Monday, and the DJIA closes higher than it did on Friday, the
variables sumMoves and numMoves will be
updated to reflect the new data. This query answers the question ‘what
is the average Monday up move in the DJIA’. (Answer is 0.81% vs. 0.95%
for down moves.) Clearly, this set of conditions could have been in
the WHEN condition in a DO-SHOW-WHEN block, or an ORDER block.
The following options affect the DO-EVERY block.
Execution Units
Attribute Units
Execute Options
The DO ON ENTRY block will be executed each time a trade entry occurs.
DO ON ENTRY someVariable = 1
The DO ON EXIT block will be executed each time a trade exit occurs.
DO ON EXIT someVariable = 0
Options which effect the DO ON ENTRY / EXIT block:
Execution Units
Attribute Units
Execute Options
A single query can have multiple query blocks. This feature can be used to debug a query by showing the values and variables. It can also be used to clarify meaning by separating variable initialization from general manipulation. Profit and loss statistics can be displayed in this manner as well. Generally the blocks are executed in the order they appear in the query. Any query block “knows” what is going on in another with the exception of some special cases related to ORDER blocks. This allows the passing of variables from one query block to another.
Lexical scoping is a measure pertaining to ORDER blocks.
An example of this is the system variable entry_price. This variable can be referred to safely within the ORDER block to which it refers. Once execution moves beyond that particular ORDER block, the value becomes nonsensical in that it is unknown what ORDER block is referred to.
Initialization statements are used to initialize (zero), variables used in a query.
INITIALIZE numberTrades = 0 AND endingEquity = 0
The INITIALIZE block will be executed once per query execution, depending on where the block is located. When this block is placed above the LET, the variables initialized will survive all LET iterations.
The FINALIZE block will be executed once per query execution, depending on where the block is located.
FINALIZE myCommission = numberTrades * 25