Formula Language Course · Lesson 3 of 9

Order of precedence and periodicity

Two ideas that quietly control every formula you will ever write: the order MetaStock evaluates your operators in, and the period of the data it evaluates them against.

BY DAVID JENYNS REFRESHED AUGUST 2026

Order of precedence

The order of operator precedence specifies the standard order used when calculating any mathematical expression. When you use the operators from Lesson 2 there is a universally agreed order in which they are evaluated. You may recall this from basic arithmetic classes, but a refresher never hurts.

In simple terms, the precedence of an operator specifies how tightly two expressions are bound together. Take the expression 1 + 5 * 3. Rather than working left to right, we obey the order of precedence: multiplication ranks higher than addition, so we first multiply five by three and then add one, giving 16. Calculated left to right the answer would be 18, which is wrong. Keep this in mind when creating your own formulas, because whenever MetaStock calculates a piece of code it refers to the order of operator precedence.

Without parentheses, the order runs like this, highest first:

Mathematical operatorShortcut
Negative values-
Multiplication*
Division/
Addition+
Subtraction-
Less than<
Greater than>
Less than or equal to<=
Greater than or equal to>=
Equal to=
Not equal to<>
Logical ANDAND
Logical OROR
ORDER OF PRECEDENCE 1 ( ) BRACKETS 2 -(neg) * / + - ARITHMETIC 3 < > <= >= = <> COMPARISON 4 AND LOGICAL 5 OR LOGICAL FIRST LAST
Evaluated top to bottom: brackets first, then arithmetic (negative, multiply, divide, add, subtract), then comparisons, then AND, with OR last

Parentheses

Now that you are comfortable with operator precedence, we can introduce parentheses: brackets that act as a grouping symbol, ( ). They give you further control over the order in which expressions are calculated. Everything inside parentheses is calculated first, and if there are parentheses inside parentheses, MetaStock works from the innermost pair outward. The order of precedence still applies within them; it is simply as though the parenthesis itself holds the highest ranking.

This may appear trivial, but the effect can be dramatic. Suppose you encounter a MetaSpeak expression such as:

H-(H-L)*2

MetaStock evaluates this by calculating the parenthesis first, (H-L). The multiplication comes next, since it has higher precedence than subtraction, giving (H-L)*2. Finally that result is subtracted from the high.

Conversely, imagine someone who did not understand parentheses coding "the same thing" as H-H-L*2. MetaStock would first multiply the low by two (L*2, multiplication outranks subtraction), then subtract the high from the high, then subtract the L*2 result from that. A completely different number. Parentheses dramatically affect the outcome.

Periodicity

In Lesson 1 we defined the price array identifiers: open, high, low, close, volume and open interest. The astute reader may have noticed the definitions made no reference to a particular period. "The price of the first trade for the period" could mean daily, weekly or monthly, so we must tell MetaStock which period we mean.

To do this, pull up the periodicity setting in the X-Axis Properties dialog: select X-Axis from the Format menu, or right-click a chart's X-Axis and choose X-Axis Properties from the shortcut menu. You can also change periodicity directly with the Periodicity button on the chart toolbar.

Either way, you can display the chart in daily, weekly, monthly, quarterly or yearly periods, or choose the "other" option and define your own number of periods to compress the data into.

Keep an eye on periodicity, because it changes what your identifiers return. For example, the ref(c,-1) function (covered later in the course) returns the close from the previous period. Set to weekly, the previous close is from one week ago; set to daily, it is from one day ago. Periodicity also affects volume and open interest: MetaStock calculates them by summing the values over the selected period, so a weekly chart shows the entire week's volume in one bar.

Next lesson

In Lesson 4 we meet the pre-programmed functions: the 200-plus building blocks MetaStock ships with, and the parameters that feed them.

MetaStock

Formulas need software to run in

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