Formula Language Course · Lesson 2 of 9
Don't be intimidated by the mathematical operators. They are just the maths you learned at school, dressed up in coding shortcuts, and they are how a formula starts making decisions.
'Mathematical operators' is just a fancy name for the basic mathematical terms used within coding. They break into three categories: arithmetic operators, comparison operators and logical operators.
Remember basic arithmetic from school? Same symbols, same meaning:
| Arithmetic operator | Shortcut |
|---|---|
| Division | / |
| Multiplication | * |
| Addition | + |
| Subtraction | - |
Comparison operators, as the name implies, let you compare two values:
| Comparison operator | Shortcut |
|---|---|
| Less than | < |
| Less than or equal to | <= |
| Greater than | > |
| Greater than or equal to | >= |
| Equal to | = |
| Not equal to | <> |
One rule to note: within the MetaStock formula language, only the shortcuts can be
used when writing arithmetic and comparison operators. If you want multiplication,
you type *, nothing else.
Logical operators take a little more thought than the other two categories, but they are where the flexibility lives. There are two of them, AND and OR, and they allow a formula to involve multiple conditions.
Their use is best shown by example. Assume we want a security to exhibit three attributes: the close equal to the high, the open equal to the low, and the close greater than the open. In other words, a strongly bullish period that opened on its low and closed on its high.
| Condition | Code |
|---|---|
| The close equal to the high | C=H |
| The open equal to the low | O=L |
| The close greater than the open | C>O |
For this bullish period to exist, all three statements must be true at once. The AND operator links them together and requires that every linked condition holds. Written in MetaSpeak:
C=H AND O=L AND C>O To reinforce your understanding, look at the code and ask yourself: can I visually identify what it is saying about the price bar? If not, break the code into its separate components and picture each one on its own.
Now assume we want a different pattern: the close equal to the high, the open greater than the low, and the close greater than the open (a bullish hammer-style bar with a tail underneath it). Before reading on, write the formula for those three conditions yourself. Then check how each component reflects the price bar, and how the AND operator links them.
Presume we want to identify securities showing either of these two price bar patterns. That is where the OR operator comes in. Wrap each pattern in parentheses and link the two groups:
(C=H AND O=L AND C>O)
OR
(C=H AND LO) That second group is the answer to the exercise above, so you can mark your own work. Can you see the difference between the two logical operators now? AND requires that all the linked conditions be true; OR only requires one of the linked statements to be true.
In Lesson 3 we cover the order of operator precedence (why MetaStock calculates your formula in a different order than you might expect) and periodicity, which quietly changes what every price array identifier means.
Everything in these lessons works in the free 30-day MetaStock trial: full software, live data, your own watchlist.
Start Your Free 30-Day Trial →30-DAY TRIAL · FULL VERSION · CANCEL ANYTIME · AFFILIATE LINK, SAME PRICE FOR YOU
The Formula Language Course: Course home · 1. First formulas · 2. Operators · 3. Precedence & periodicity · 4. Built-in functions · 5. Nesting · 6. Variables · 7. Comments · 8. Pasting functions · 9. Exercises