Condition Expressions & Terms
Condition Terms define a conditional expression that is either true or false (called the state of the condition). For example, in the following, the ‘Risk On’ term is a constant condition term, while ‘Lower Long Reversal’ is a variable condition expression, whose sub-terms are joined with logical operators:
‘Risk On’ = a condition that is false
‘Lower Long Reversal’ = a condition that is true if the evaluated security’s lowest low price, up to today, has previously been in or below the long channel’s Lower Turn-Band (outer), AND the ‘Short Channel Rising’ term is true
Like with constant value terms, constant condition terms can be varied between the true and false states using a Batch Test to automatically run a couple of back-tests with term in each state. They can also be set through System Parameters, where they can be used as switches to turn parts of your trading system on or off, without recompiling your Trading Spec, and they can be referenced by List Selectors.
Condition Constructs
These options help you define condition expressions by performing various true/false tests. These are:
- Named condition term is: returns the true or false value represented by a Named Term
- Security’s value, condition or characteristic is: returns the result of testing a single security value against some other value or ranking, or testing an attribute (e.g. currency, exchange, sector, etc.), trading position, trade, trading signal, channel data, note value or position in a list of securities. Please see Security Expressions & Terms to understand how the security is specified, and Security Values, Characteristics & Attributes for more details on the security values, attributes, etc. that can be tested.
- Value expression is: tests the result of an arbitrary, multi-term expression (which may include security values and characteristics) against the result of another such expression
- Value has just crossed another value: tests if the value of one expression has crossed, or crossed above/below, the value of another expression
- Value has reached a top or bottom: tests if a value series (e.g. a price-series) has reached a top or bottom in the recent past. This is determined by calculating a best-fit trend line for the series over a period, which ends some days in the past, then extrapolating it forward so it can be compared with the series value today. If the series is no longer continuing in the direction of the previous trend then the difference between today’s value and the extrapolated value will be much larger than if it had continued the trend.
This difference is measured in terms of the standard deviation of the series relative to the trend, so it’s implicitly adjusted for natural variability. Comparing this difference to a threshold determines if a top or bottom (i.e. a change in direction of the value series) occurred at the previous end of the trend. The diagram below makes this clearer:

The indicator also requires a minimum standard deviation to be specified, expressed as a percentage of the latest value in the value-series. The purpose of this is to counteract situations where the trend’s values are trending in a nearly straight line (i.e. they are all on, or nearly on, the trend-line). Such values have a near zero standard deviation vs the trend-line, which makes the detector unduly sensitive, as even the smallest deviation from the trend would then trigger the detector. If this is not what you require, then you can counteract this situation by setting the minimum standard deviation to be a percentage of the final value. The detector will then not trigger until the value has moved at least this percentage from the trend-line.
- Value for some period is: tests if the value of an expression has some absolute value, or is growing by some amount, for a specified number of days in a specified period
- Condition has transitioned: tests if a condition expression transitioned from true → false, or false → true, or in either direction, either today or yesterday
- Historical condition was: tests if a condition expression was true or false at some time in the past
- Condition from a choice of conditions is: returns the state of a condition expression from a choice of two or more condition expressions. The particular expression’s value is selected based on a set of separate selection conditions
- Security is in/not in a list: tests if a security is present in a security list or not
- Some securities in a list meet a condition: tests if the value or characteristic of each security in a Security List meets some criteria. You may test if all securities, any securities, at least some number of securities, or no more than some number meet the criteria
- A strategy’s trading list was selected today: tests if a List Selector for a Trading Strategy was triggered today
- It is a single-share test: tests if the Trading Spec is being tested in a single-security back-test. This may be useful for switching aspects of your Spec to suit this type of test
- Period has started/ended: tests if a calendar or financial period has started or ended today, or today is a number of days after/before the start/end of a period, or today is within a range of days after/before the start/end of a period
- The date is: tests if today is, or is before / after, or is the first / last trading day on or before / after, a dividend or other specific date
- The date is on or around: tests if today is a specific number of days before / after, or is in a range of days before / after, a dividend or other specific date
Condition Expression Operators
Condition expressions are formed by combining sub-terms created with the constructs above, with logical operators AND, OR and OR (but not both). Expressions are completed by selecting end condition. These operators work the way they sound i.e.:
- A AND B is true when both A and B are true
- A OR B is true if either or both A or B is true
- A OR (but not both) B is true if either but not both A or B is true (i.e. it’s got to be one or the other but not both)
Operator Precedence
Like value expressions, condition expressions may contain sub-expressions – please see Expressions And Sub-Expressions for further information). Also like value expressions, RuleTrader does not require you to remember the precedence of conditional operators. Instead it simply evaluates the expression from left to right. So when evaluating the expression:
‘Some Condition 1’ = a condition that is true if the ‘A’ term is true, OR the ‘B’ term is true, AND the ‘C’ term is true, OR the ‘D’ term is true
…it first evaluates A OR B, which is true if either A or B is true. If that result is true AND C is true, then the next result is true, otherwise it is false. If that result is true OR D is true then the result of the whole expression is true. But if both are false, then the expression is false.
The trick is to take it from left to right, a step at a time, and work out each intermediate result before considering the next operator. You can also make things much easier for yourself and control the order in which sub-expressions are evaluated, by breaking things down using sub-terms. For example, say you wanted to determine C OR D, before determining the rest of the expression, then you could break it down like this:
‘Some Condition 2’ = a condition that is true if the ‘First’ term is true, AND the ‘Second’ term is true; where:
- First’ = a condition that is true if the ‘A’ term is true, OR the ‘B’ term is true
- ‘Second’ = a condition that is true if the ‘C’ term is true, OR the ‘D’ term is true
With only two terms in each expression, the meaning becomes much clearer. However, note that ‘Some Condition 1’ will not produce the same result as ‘Some Condition 2’ because you have changed the order of evaluation: ‘Some Condition 2’ now evaluates ‘A OR B’, and ‘C OR D’ separately before ANDing the two results, whereas ‘Some Condition 1’ evaluated A OR B, then (A OR B’s result) AND C, then ((A OR B) AND C’s result) OR D, which can have a quite different outcome.
If all you wanted to do was to simplify ‘Some Condition 1’, so there were two terms per expression, without changing the result then you need to preserve the evaluation order, with something like:
‘Some Condition 1’ = a condition that is true if the ‘First Three’ term is true, OR the ‘D’ term is true; where:
- ‘First Three’ = a condition that is true if the ‘First Two’ term is true, AND the ‘C’ term is true
- ‘First Two’ = a condition that is true if the ‘A’ term is true, OR the ‘B’ term is true
This now produces exactly the same result, as it preserves the evaluation order.