Compare Switch
The Compare Switch rule performs arithmetic on two numeric values, compares the result against a threshold, and conditionally outputs one of the original input values based on whether the comparison is true or false. This provides a powerful way to route data through your strategy based on calculated conditions.
How It Works
The Compare Switch operates in two stages. In the first stage, the rule takes a Left Operand and a Right Operand and applies an arithmetic operation to produce a calculated result. The supported operations are addition, subtraction, multiplication, division, modulus, and exponentiation. You can optionally take the absolute value of this intermediate result to ignore negative values.
In the second stage, the calculated result is compared against a threshold value using a comparison operator. If the comparison evaluates to true, the rule outputs the Left Operand as its value. If the comparison evaluates to false, the rule outputs the Right Operand instead. This two-step approach lets you build conditional logic that selects between two values based on a dynamic arithmetic condition.
Inputs
| Input | Type | Required | Description |
|---|---|---|---|
| Left Operand | Number | Yes | The first numeric value used in the arithmetic operation. This value is also output when the comparison evaluates to true. |
| Operator | Selection | Yes | The arithmetic operation to perform on the two operands. Options: Add, Subtract, Multiply, Divide, Modulus, Exponentiation. |
| Right Operand | Number | Yes | The second numeric value used in the arithmetic operation. This value is also output when the comparison evaluates to false. |
| Absolute Result | Boolean | No | When enabled, the absolute value of the arithmetic result is used for the comparison. Default: false. |
| Compare Operator | Selection | Yes | The comparison operator applied to the arithmetic result and the Compare Value. Options: Greater Than (GT), Greater Than or Equal (GTE), Less Than (LT), Less Than or Equal (LTE), Equal (EQ), Not Equal (NEQ). |
| Compare Value | Number | Yes | The threshold value that the arithmetic result is compared against. |
Outputs
| Output | Type | Description |
|---|---|---|
| Operand Output | Number | The selected operand value. Outputs the Left Operand when the comparison is true, or the Right Operand when the comparison is false. |
| Result | Boolean | The boolean outcome of the comparison. True if the arithmetic result satisfies the comparison condition, false otherwise. |
Tips
Use Compare Switch to dynamically select between two indicator values or price levels based on a calculated condition. For example, you could subtract a short-term moving average from a long-term moving average, compare the difference to zero, and output the appropriate price target depending on whether the trend is bullish or bearish. Enable Absolute Result when you only care about the magnitude of the difference, not its direction.
Was this helpful? Let us know