This is a continuation of our series on the basics of MetaStock Programming Language. In this Metastock guide post we are going to talk about the mathematical and logical operators.
Mathematical Operators
‘Mathematical Operators’ is just a fancy name for basic mathematical terms used within coding. These can be broken into three categories: arithmetic operators, comparison operators and logical operators.
Do you remember basic arithmetic from school? Table 2.2 lists the arithmetic operators and their corresponding coding shortcuts.
-
Arithmetic Operators Shortcut Division /
Multiplication *
Addition +
Subtraction –
Table 2.2 – Arithmetic Operators Language
Comparison operators, as their name implies, allow you to compare two values. Table 2.3 lists the comparison operators and their corresponding coding shortcuts.
-
Comparison Operators Shortcut L ess than <
L ess than or equal to <=
G reater than >
G reater than or equal to >=
E qual to =
N ot equal to <>
Table 2.3 – Comparison Operators 2
Note that within the MetaStock Programming Language only mathematical operator shortcuts can be used when referencing arithmetic and comparison operators. For example, if we wished to use multiplication we would simply type ‘*’.
Logical Operators – ‘And’ and ‘Or’
Logical Operators are a little harder to understand than the other two mathematical operators. Nevertheless, they offer increased coding flexibility. The two logical operators are ‘and’ and ‘or’. They allow a formula to involve multiple conditions.
Their use is best illustrated by way of example. Let’s assume that we wanted a security to exhibit three attributes;: the close to be equal to the high, the open to be equal to the low and the close to be greater than the open (i.e. a bullish period, as shown in Figure 2.2).
Figure 2.2 – Bullish OHLC Bar
Table 2.4 displays the required conditions and their corresponding code, written in the Metastock Professional formula language.
-
Condition Code The close to be equal to the high C=H The open to be equal to the low O=L The close to be greater than the open C>O
Table 2.4 – Conditions Guide
Now before a bullish period can exist, we need all three statements (shown in Table 2.4) to exist. Using the ‘and’ operator we can combine these three conditions. This would effectively link the three statements, requiring that each condition exists. Written in ‘MetaSpeak’ this would look like:
C=H AND O=L AND C>O
To reinforce your understanding, look at the code, and Figure 2.1, and ask yourself ‘can I visually identify what the code is saying?’ If you can not, break down the coding into its separate components and visualise each component separately.
Let’s look at another example. Assume that we wanted a security to exhibit three different attributes: the close to be equal to the high, the open to be greater than the low and the close to be greater than the open (shown in Figure 2.3).
Figure 2.3 – Bullish Hammer OHLC Bar
In the space below, write the formula for the above conditions:
Again, visually identify how the coding reflects the price bar (shown in Figure 2.3). See how the ‘and’ operator links each component?
Now presume that we wanted to identify one of these price bar patterns or the other. This is shown in Figure 2.4
Figure 2.4 – Bullish Hammer and Bullish OHLC Bar
Notice that we want one ‘or’ the other. This is where we would use the ‘or’ operator to link the coding together. In this way we would isolate securities displaying either the first set of conditions or the second set.
The code would simply look like this: (C=H AND O=L AND C>O)
OR
(C=H AND LO)
Can you now see the difference between the two logical operators? The ‘and’ operator requires that all the linked conditions be correct whereas, the ‘or’ operator only requires one or the other of the linked statements to be correct.
In our next post, we will be discussing 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 trading software? Watch this video: