Operators
How to add, subtract, multiply, limit and round indicators in a QTSurfer strategy, with the names each operator registers.
The operators build an indicator out of others: add, diff, mul, clamp and round. Each reads indicators by their names, so register those first, and its result is an indicator like any other: a window can watch it and another operator can read it. They have no chart of their own, since they draw whatever they are given: the demo below shows four of them on a pair of averages.
| Operator | What it computes | With no name of its own it registers |
|---|---|---|
add("a", "b") | a + b | addab |
diff("a", "b") | a − b | diffab |
mul("a", "b") | a × b | mulab |
mul("a", 2) | a × 2 | mula |
clamp("a", 0, 100) | a, held between 0 and 100 | clmpa |
round("a", 2) | a, rounded to 2 decimals | rounda |
Every one also takes the name to register as its first argument: diff("gap", "a", "b") registers gap.
Try them
Choose an operator and move the controls. The two grey lines are a fast and a slow EMA, and the line in colour is what the operator registers. The lines under the chart are the strategy that draws it: change a control and they change with it. The default is round on a series priced near 100, where its steps show.
The series are samples, synthetic ones and real crypto ones. The lines are computed in your browser by the engine’s own indicator classes, so they are the lines a strategy reads. On a series priced in the tens of thousands, rounding the gap to a few decimals changes nothing you can see.
What to watch
- Give each one a name. The default name carries the inputs and not the numbers, so
mul("price", 2)andmul("price", 3)both registermulprice, and the second one registered replaces the first. The engine logs a warning when a name starts to read a different indicator, but it does not stop. - A half rounds upwards, also below zero: 2.5 rounds to 3 and −2.5 to −2.
- They are ready when what they read is, so an operator over an average waits for the average.
- A difference is in the units of its inputs. To compare instruments, use the distance, which is a percentage.
In a strategy
setup:
ema(9)
ema(21)
diff("gap", "ema9", "ema21")
round("gapRounded", "gap", 2) That registers the gap between the two averages and the same rounded to two decimals. In Java they are the methods of the same names on the builder, and clamp also takes a condition and the value to put when it holds.
The catalogues have them with their forms: QTScript indicators and Java indicators.
A comparison of an indicator with a number, which gives 1 or 0, is not an operator but a condition: it has its own page.