Value Expressions & Terms
Value Terms and expressions define a number or percentage, either based on an expression or as a value. For example, in the following, ‘Liquidity Cover’ is defined as a constant term (constant because it is defined with a fixed number) and ‘Required Number Of Positions’ and ‘Estimated Trade Value In Pence’ are defined as variable term expressions.
‘Liquidity Cover’ = 10
‘Required Number Of Positions’ = the rounded value of (the cash available (£), DIVIDED by the nominal trade-size)
‘Estimated Trade Value In Pence’ = the cash available (£), PLUS the total value of all open positions, MULTIPLIED by 100, DIVIDED by 25
The advantage of using constant terms in an expression (rather than just replacing them with their number) is that constant terms can be automatically varied over a range, in a series of batched back-tests, to determine a good value for them, or to test if your trading system is unduly sensitive to the value chosen. They can also be set in Live session System Parameters, which means the Live Trading Spec can be modified without needing to recompile it and do a soft-reset of the Live system. Finally, they can also be referenced by List Selector definitions.
Value Constructs
RuleTrader offers a number of constructs to help you define value expressions. These are:
- Named value term: returns the value represented by a Named Term
- Security’s value: returns a value associated with a security e.g. price, volume, company result, note column value, position value, trade value, etc. Please see Security Expressions & Terms to understand how the security is specified, and Security Values, Characteristics & Attributes for more details on the security values, etc. that are available to you.
- Account(s) value: returns a value associated with a trading account, or summed across all accounts e.g. cash / opening / minimum balance, or the minimum trade value (see Trading Accounts)
- Open positions value: returns the total, average minimum or maximum of a value for ALL currently open positions e.g. quantity, cost, value, dividends, charges, gain/loss, return, etc.
- Trade value (any share): returns a value associated with a previous trade (e.g. oldest open buy, most recent open buy, most recent buy / sell) for any share e.g. number of days since the trade, quantity, charges, price, cost, risk-level, etc.
- Nominal trade-size: returns the nominal trade size defined in the Trade Sizing Dashboard tab
- Number: lets you define a specific, decimal number
- Random number: returns a random number over some range
- Undefined number: returns an undefined value. This is useful if you want to set a Persistent Term, for example, to undefined so you can test it to determine if it has ever been set (i.e. it’s no longer undefined)
- Percentage: lets you define a specific, percentage number
- Number as a percentage of another number: returns the ratio of two numbers, expressed as a percentage
- Random percentage: returns a random percentage over some range
- Undefined percentage: returns an undefined value (see Undefined number, above)
- Mathematical function of a value: provides a large range of arithmetic, log and trigonometric functions of a value e.g. negation, absolute value, raising to a power, exponentiation, logs, rounding, trigonometry, etc.
- Value from a choice of values: returns the value of an expression from a choice of two or more expressions. The particular expression’s value is selected based on a set of selection conditions
- Historical value expression: returns the value of an expression, as it was at some point in the past
- Change in a value over a period: returns the numeric or percentage change in the value of an expression over time
- Value statistic over a period: calculates a statistic (e.g. total, average, mean, highest, lowest, trend growth, variance, standard deviation, correlation, covariance, volatility, etc.) for a series of expression values over a period
- Number of times a condition is: counts the number of times a condition is true or false over a period
- Share-list statistics: calculates the highest, lowest, average, median or total of some expression’s value for all securities in a Security List
- Number of securities in a list: returns the number of securities in a Security List
Value Expression Operators
Composite value expressions are created by joining multiple instances of the constructs above, with the operators: PLUS, MINUS, DIVIDED by, and MULTIPLIED by. In the selection list that provides these operators, you’ll also find the option end expression, to end the addition of any more terms to the expression. Expressions may also contain sub-expressions – please see Expressions And Sub-Expressions for a discussion of some of the issues that can arise from this).
To keep things simple (and to save you having to remember the rules) RuleTrader does not enforce operator precedence; instead it just evaluates expressions from left to right. So the expression:
‘Some Sum’ = ‘A’, PLUS ‘B’, MULTIPLIED by ‘C’, PLUS ‘D’
…is evaluated as A + B, first, which is then multiplied by C, the result of which is then added to D. If you wanted to change the order of evaluation e.g. to enforce normal arithmetic precedence, where multiply and divide take precedence over plus and minus (so B * C would be calculated first), then use Terms to define the sub-terms separately e.g.
‘Some Sum’ = ‘A’, PLUS ‘Middle Term’, PLUS ‘D’, where:
- ‘Middle Term’ = ‘B’, MULTIPLIED by ‘C’
Combining Numbers And Percentages
RuleTrader also supports vernacular maths, so an expression of form: A = B + 20%, where B is a number, is acceptable and is evaluated as A = B * 1.2. Similarly A = B – 20% is equivalent to A = B * 0.8, while A = B * 20% is equivalent to A = B * 0.2. However, A = B / 20% makes little sense, so RuleTrader will not allow it.
The DEfT Wizard will always guide you through what you can do, with context sensitive instructions at every step. It will never allow you to define an expression that makes no sense or cannot be computed.
Currency Handling
The DEfT Wizard will ask you what currency to use when it encounters the first term (if any) in an expression that is associated with a currency. Thereafter, each subsequent term in that expression, that is also associated with a currency, will automatically be converted to the first term’s currency. This is one of many examples of how RuleTrader works in the background to make things as easy for you as possible. The currencies that RuleTrader supports are: GBP (£), EUR (€), USD ($), CNY (¥).