## Популярные книги

# Chapter 2 Being Logical

Logic means many things to different people. To avoid confusion, this document will define

logic in MetaStock's formulas language as a reasoned decision between two options base on a

mathematical comparison. A reasoned decision means that MetaStock is forced to make a

choice. The choice is always limited to two options and the decision is based upon comparing

something to something else using math.

This means that a logical decision in MetaStock can always be broken down to three parts. The

first part is the mathematical comparison. This is followed by the option to use if the comparison

is true. The last item is the option to use if the comparison is false. To write this in pseudologic:

IF( Condition, True result, False result )

Does all this sound artificial to you? How are you supposed to relate to this? Here’s a secret:

You do this exact same thing every day of your life. Consider the following examples:

You are in your car:

If the gas tank is near empty then buy gas, else wait till later

You are shopping for some item:

If Brand A is cheaper then buy Brand A, else buy Brand B

You are driving and approach an intersection:

If the light is red then stop, else keep going

Logic can be easy if you reduce everything down to a condition and a choice of two results.

Logic in Formulas

In the MetaStock formula language, logic is applied with an If() function. This consists of the

same three parts discussed earlier. First define a mathematical condition. Then list the action to

take if the condition is true and lastly, the action for a false condition.

SYNTAX if( DATA ARRAY > >= < <= <> =

DATA ARRAY, THEN DATA ARRAY,

ELSE DATA ARRAY )

FUNCTION A conditional function that returns the

second parameter (THEN) if the

conditional expression defined by the first

parameter is true; otherwise, the third

parameter is returned (ELSE).

EXAMPLE The formula "if(1<2,3,4)" will always

return the value three.

How do you compare two values? The function description shows a confusing jumble of arrows

and equal signs. If this doesn’t make sense, don’t worry about it. The various meanings are

listed below.

Symbols Meaning Example

> greater than 2 > 1

>= greater than or equal to ( 2 + 2 ) >= 4

< less than 3 < 4

<= less than or equal to ( 3 - 1 ) <= 2

<> not equal to ( 3 - 1 ) <> 3

= equal to ( 2 + 2 ) = 4

Exercise 8:

Suppose you wanted to know when a security was above its 40-period simple moving average.

MetaStock can plot a 1 when this is true and a 0 when it’s false. What formula would you write?

Look at the If() function and the different logic comparisons. Write a formula using the If()

function and name it Above 40-period average. Look at Formula 13 for the answer.

Formula 13:

Formula 13 in English

If the closing price is greater than a 40 period simple

moving average of the closing price, plot a 1, otherwise

plot a 0.

Above 40-period Average has the three parts required for an If() function. First it compares the

close to the moving average. Then it lists the value to be plotted if the comparison is true. The

last number is the value to be plotted if the comparison is false.

Surfing the Binary Wave

Plotting Above 40-period Average displays what some have called a binary wave. The term

binary applies to a counting system that only uses 0's and 1's. A binary wave, therefore, is a line

that moves back and forth between 0 and 1. As you get more proficient you may wish to

elaborate on the basic binary wave and it may no longer stop at 1 or 0 but it will still have its

roots in the If() function.

This binary wave is very important for other reasons. It is how system tests, explorations, and

expert advisors are created. In these other formula-based tools, you give a condition under

which an action is performed.

For example, the condition of the close being greater than a moving average could be used in a

system test as the signal to enter a long position. It could also be used as the filter of an

exploration to tell you all the securities you have that are currently in this condition. You could

also use it in an expert advisor as the condition for displaying the bullish trend.

In all these cases, the same condition is used. The only difference is that in these other tools, the

If() function is automatically assumed so you can abbreviate the formula to:

Formula 14:

C > mov ( C, 40, S )

This same abbreviated formula can be written and plotted with the indicator builder. Whenever a

condition is written as the formula and no other information follows, MetaStock assumes the

formula to be:

IF ( condition, 1, 0 )

This is because MetaStock, like most computer languages, equates 0 with a false condition.

Logically, anything that is not false must be true. For simplicity sake, a 1 is used to plot true

conditions. This creates a binary wave similar to the one from the Above 40-period Average

formula.

Remember, when you are creating system tests, experts and explorations, the formulas are

looking for true or false conditions. MetaStock interprets 0 as false and ANY non-zero number

will be read as true.

AND Now for Something Completely Different

Now suppose you had several conditions that you needed to examine. Real life, after all, can get

a whole lot more complicated that two conditions. Consider these situations:

You are at a theater's snack bar:

If you have money AND you are hungry then buy a snack, else leave

You want to turn right at an intersection:

If the light is red AND somebody is coming then don't go, else turn right.

The word AND is a keyword in MetaStock. It means both conditions on either side of it must be

true or the entire statement is false. This diagrammed in table below

Condition 1 Condition 2 Result

True AND True = True

True AND False = False

False AND True = False

False AND False = False

Whenever you are using the AND function, it helps to put parentheses around the two conditions

to make sure MetaStock understands what you want compared. These may not be required, but

they are a good safety precaution.

Exercise 9:

For your first formula combining the If() and the AND functions, say you wanted to know when

the close was above both its 40-period moving average and its 120-period moving average. Once

again, have MetaStock plot a 1 for this condition being true and a 0 for false conditions. You can

use the Above 40-period Average as a starting point. Write a formula to do this and name it 2

Averages. Remember to separate the two conditions with parentheses. Look at Formula 15 for

the answer.

Formula 15:

Formula 15 in English

If the closing price is greater than a 40 period simple

moving average of the close and the closing price is greater

than a 120 period simple moving average of the close, plot

a 1, otherwise plot a 0.

Only if the close is above both moving averages will the above formula plot a 1. At all other

times, it will plot a 0. You should notice that the binary wave of Above 40-period Average has

wider plateaus than 2 Averages. See the figure below.

23 30

July

7 14 21 28 4

August

11 18 25 2 8

September

15 22 29 6

October

13 20

13

14

15

16

17

18

19

20

21

ADV MICRO DEV

0.0

0.5

2 averages 1.0

0.0

0.5

above 40-period average 1.0

This is because the more specific you make your conditions, the less often they occur. This may

seem obvious but sometimes formulas have such detailed conditions that they never get a true

result; what is being sought is theoretically possible, but practically never seen.

When making complex If() functions, try plotting each condition as a separate formula and then

see where all the separate parts return true results. This way, you can make sure each part is

accurate and make changes as needed.

OR You Can Have It All

Some times your decisions are based on one of several conditions. If any of the conditions are

true, you will take a specific action. Consider the following real world examples.

You look at the weather outside:

If it is raining OR it is snowing then take an umbrella, else leave the umbrella.

You are at home:

If it is time for bed OR you are tired then go to bed, else stay up.

Where the AND function limited the condition, OR returns a true result if either of the conditions

is true. The following table diagrams this for you

Condition 1 Condition 2 Result

True OR True = True

True OR False = True

False OR True = True

False OR False = False

Just like the AND function, I strongly recommend using parentheses to group the conditions of

OR functions. They may not be needed, but they help you to be sure you are asking what you

think you are asking.

Exercise 10:

To see the OR function in action, consider the following situation. The Relative Strength Index

(RSI) can be used in several ways, including displaying overbought and oversold conditions.

An RSI value of over 70 is considered overbought and a sell signal. If you were a really cautious

investor, you might want to know when either a 14-period RSI rises above 70 or the stock loses

five percent, or more, of its value in three periods.

Below is the RSI() function. You can use either the Ref() or the RoC() functions for the second

condition. Look these functions over and then write a formula that will help such cautious

investing and name it Cautious Sell. Have the formula plot a 1 when either condition occurs and

a 0 other wise. Look at formula 16 for the answer.

Relative Strength Index ( RSI )

SYNTAX rsi( PERIODS )

FUNCTION Calculates the predefined RSI indicator.

EXAMPLE rsi( 14 )

Formula 16:

Formula 16 in English

If the 14-period Relative Strength Index is above 70 or the

close has dropped by 5 percent or more over the last 3

periods, plot a 1, otherwise plot a 0.

This formula uses the RoC() function. It is the easier method. However, if you wanted to use

Ref() instead, look at the next formula.

Formula 17:

Formula 17 in English

If the 14 period Relative Strength Index is above 70 or; the

result of the close minus the close 3 periods ago divided by

the close 3 periods ago, all multiplied by 100 is less than -

5; then plot a 1, else plot a 0

Both these plot exactly the same line.

Formula 16 is a lot easier to read and write. This is why it helps to be familiar with the different

functions. You can reproduce any built in function with a custom formula but why not use your

time and effort for other tasks. After all, wouldn’t you rather write

ROC( C, 3, % )

instead of

((C-Ref(C,-3))/Ref(C,-3))*100

These two formulas also prove that there is usually more than one way to write any formula.

Some may be more elegant but if it does what you want, it’s not wrong!

Back to the OR

While AND requires both conditions to be true and narrows the number of occurrences, OR

requires only one true condition. This will, therefore, increase the frequency of true results. The

following chart demonstrates this.

When you use OR, there is always the danger of a final result so general that it is always true.

Again, try plotting all conditions separately before combining them. This will help you to

visually see what the approximate results will be before the final formula is made.

This AND / OR That

The AND and OR functions can be combined in a single If() function. The resulting condition

part of the IF may look something like this

((condition 1) OR (condition 2)) AND ((condition 3) OR

(condition 4))

You could replace any of the ANDs and ORs with the other function. You could also make the

formula more complex by replacing any of the conditions with another set of conditions

combined by an AND or an OR.

When doing something like this, testing each part separately can save you tremendous amounts

of time correcting problems later. For example, if you made a formula similar to the fourcondition

beast shown above, you should first create the four formulas below:

Condition 1

Condition 2

Condition 3

Condition 4

After confirming that each of these formulas plot what you want them to, they can be combined

to form the next layer of commands:

( Condition 1 ) OR ( Condition 2 )

( Condition 3 ) OR ( Condition 4 )

These two formulas will show the combined values of the paired conditions. After making sure

they are the results you want the final version of the formula should be written.

((condition 1) OR (condition 2)) AND ((condition 3) OR

(condition 4 ))

Obviously, you should only do this when first creating the formula. Once the formula has been

created and you know it is performing as expected, just plot the completed formula. As you gain

proficiency in writing formulas, you will be able to skip writing the separate parts of the If()

function and just write the final version. However, should you ever need to see exactly what is

being done at some point in a formula, just break it down to its components and see how it looks.

An alternative to breaking the formula down into its separate parts, is using comments to mark

parts of the formula to be ignored. For example:

{ ((condition 1) OR ( } condition 2 { )) AND ((condition 3)

OR (condition 4 )) }

is the same as:

condition 2

By putting the { } brackets around a section of text, you are telling MetaStock to ignore that part

of the formula.

Try writing some If() functions with and without the AND and OR functions. The review

questions will also help you gauge your understanding of this chapter. Please be sure you have

a grasp on these concepts as they are used far more than you might initially think. When you

feel comfortable with these commands, go on to the next chapter.

Review Questions

1. Today, stock XYZ has a closing price of 30, a high of 35, a low of 25, and an opening

price of 28. Which of the following conditions will MetaStock evaluate to be true when

used in an If() function and plotted on this security?

A

Close > Open

B

Close < 35

C

Open + Close

2. Using the same data from question one, is the following condition true or false?

( Close < 35 ) AND ( Open > 28 )

3. Are the following statements true or false:

1) ( true OR false ) AND true

2) ( false AND true ) OR false

3) ( false AND false) OR ( true OR false )

4) ( true OR ( false AND false )) AND ( true OR true )

4. What are the three components of an If() function?