Description
The following comparison operators are available in thinkScript®:
Operator | Description |
---|---|
==
|
is equal to |
equals
|
is equal to |
is equal to
|
is equal to |
!=
|
is not equal to |
<>
|
is not equal to |
is not equal to
|
is not equal to |
<
|
is less than |
is less than
|
is less than |
>
|
is greater than |
is greater than
|
is greater than |
<=
|
is less than or equal to |
is less than or equal to
|
is less than or equal to |
>=
|
is greater than or equal to |
is greater than or equal to
|
is greater than or equal to |
between
|
between |
crosses
|
crosses |
crosses above
|
crosses above |
crosses below
|
crosses below |
All of these operators except for between
are binary. These operators return yes
or no
depending whether the corresponding condition is satisfied or not. Note that in thinkScript, equality and inequality operators can be applied to string data, see Example 4 for details.
The x between y and z
statement is equal to the y <= x and x <= z
statement.
For information on using crosses
, crosses above
, and crosses below
operators, refer to the crosses
reserved word article.
For information on using the human-readable operators containing word 'is', refer to the is
reserved word article.
Example 1
plot uptick = close > close[1];
plot downtick = close < close[1];
plot neutral = close equals close[1];
uptick.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
downtick.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
neutral.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Marks bars with different signs depending whether the Close value was less, equal or greater than the previous one.
Example 2
input price = close;
input length = 12;
input StudyType = {default SMA, EMA};
plot Avg;
if (StudyType == StudyType.SMA) {
Avg = Average(price, length);
} else {
Avg = ExpAverage(price, length);
}
AddLabel(StudyType != StudyType.SMA, "Exponential moving average is displayed.");
This example uses a condition operator to choose an averaging function and to set the hiding setting of a label.
Example 3
input percent = 5;
plot Doji = AbsValue(open - close) <= (high - low) * percent / 100;
Doji.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
This example tests if the difference between the Open and Close prices does not exceed a specific percentage of the price spread.
Example 4 (String Comparison)
def a = getSymbolPart(1) == "SPX";
def b = getSymbolPart(1) != "GOOG";
First condition tests if the first part of a composite symbol is "SPX" and the second checks if it is not "GOOG".