Learning The Basics Of MetaStock Programming Language Part 3

Posted in Blog

Follow the order of operator precedence.

This is a continuation of our series on the basics of MetaStock Programming Language. In this post we are going to talk about the order of precedence and periodicity.

Order of Precedence

The order of operator precedence specifies the standard order to be used when calculating any mathematical expression. That is to say, when using any of the mathematical operators we’ve described previously, there’s a universally agreed-upon order in which to evaluate them. You may recall learning this in basic arithmetic classes, however let’s refresh our memories.

In simple terms, the precedence of an operator specifies how “tightly” two expressions are bound together. For example, when calculating the expression 1 + 5 * 3, rather than working out the math from left to right, we obey the order of precedence. This states that the multiplication operator ranks higher than the addition operator. Therefore we first multiply five by three and then add one, this equals 16. If however we calculated the expression from left to right we would get the answer 18, which is obviously incorrect. We need to keep this in mind as Metastock expert while creating our own formulas, since whenever MetaStock calculates a piece of coding it refers to the order of operator precedence.

Table 2.5 shows the order of operator precedence, when parentheses are not used:

Mathematical Operator       Shortcut
Negative values








Less than


Greater than


Less than or equal to


Greater than or equal to


Equal to


Not equal to


 Logical “And”            and
 Logical “Or”


Table 2.5 – Operator Precedence Guide

Now that you’re getting comfortable with the operator precedence in the Metastock guide, we can introduce parentheses. By “paren- theses” we mean brackets that act as a grouping symbol i.e. ‘( )’. These are used to further control the order in which mathematical expressions are calculated. When used, all expressions within the parentheses are calculated first. If there are parentheses inside parentheses, MetaStock works from the innermost parenthesis outward. The order of operator precedence is still used, however, it’s as though the parenthesis has the highest ranking. This may appear trivial, nevertheless, it is important you understand their use and the dramatic effect they can have. Their use is best illustrated with an example. Suppose you were to encounter a ‘MetaSpeak’ expression such as:


MetaStock would evaluate this first by calculating the inner most parenthesis ‘(H-L)’. The multiplication would then be calculated since it has a higher precedence than subtraction ‘(H-L)*2’. Finally, the subtraction from the high would be calculated ‘H-(H-L)*2’.

Conversely, presume we asked someone who didn’t understand the use of parentheses, to code the same thing. They would write the code as ‘H-H-L*2’. Now when MetaStock calculates this, it would initially multiply the low by two ‘L*2’, since multiplication has higher precedence than subtraction. Then, the high would be sub- tracted from the high ‘H-H.’ Finally, the result of ‘L*2’ would then be taken from this.

As you can see parentheses dramatically affects the outcome.


Earlier in Metastock Programming Language we discussed price array identifiers saying that they reflect the open, high, low, close, volume and open interest. The astute reader may have noticed the wording of each definition made no reference to a particular period, i.e. daily, weekly or monthly. For example the definition for the open was as follows: “This is the price of the first trade for the period.” Since there was no reference to a particular period, we must identify which opening price we are referring to.

To do this as a Metastock professional, we need to pull up the periodicity tab which is located in the ‘X-Axis Properties’ dialog. Simply, select X-Axis from the ‘Format’ menu, or right-click on a chart’s X-Axis and choose ‘X-Axis Properties’ from the shortcut menu. The X-Axis Properties are shown in Figure 2.5

Figure 2.5 – X-Axis Properties

You can also change the periodicity directly by using the Periodicity button on the chart toolbar, represented by the ‘D’ in figure 2.6:

Figure 2.6 – Chart Toolbar

Within the ‘X-Axis Properties’ dialog, or using the periodicity button, we can indicate the period we wish the chart to be displayed in (ie, daily, weekly, monthly, quarterly or yearly periods). We can also select the “other” option which allows us to define the number of periods over which to compress the data, thereby selecting whatever period we like.

Now that we know where it’s located, remember to keep an eye on the periodicity, as it can greatly affect our price array identifiers. For example, assume we were using the ref(c,-1) function (we’ll discuss this function later) which returns the close from the previous period. If the periodicity was set to weekly, the previous close would be from 1 week ago. Conversely, if it was set to daily, the previous close would be from one day ago.

In Metastock Programming Language, the effects of periodicity are also seen in two other data array identifiers, the volume and open interest. MetaStock calculates volume and open interest by merely summing the values over the selected period. For example, a chart showing weekly periodicity would sum the entire week’s volume in one period.

In our next post, we will be discussing how to insert comments within a formula, as part of the advanced features of the MetaStock Programming Language. Do you want to discover how to use MetaStock formula like the pros? Just click here to learn more.

Want a quick introduction to various aspects of the Metastock data platform, QuoteCenter? Watch this video:

Comments are closed.