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

# Summing Things UP

Exercise 16:

Suppose testing of a security revealed a good buy condition every time its closing price is above

its 5-period simple moving average for five consecutive periods. How would you mark these

places on a chart with an indicator? Write a formula to do this and name it Five Day Mark. Hint:

using just the functions discussed so far, you will need to nest functions with the Ref() function.

Look at Formula 23 for the answer.

Formula 23:

Formula 23 in English

If the Close is above a 5-period simple moving average of

the close; and the previous close was above the previous 5-

period simple moving average of the close; and the close 2

periods ago was above the 5-perod simple moving average

of the close 2 periods ago; and the close 3 periods ago was

above the 5-period simple moving average of the close 3

periods ago; and the close 4 periods ago was above the 5-

period simple moving average of the close 4 periods ago;

then plot a 1, otherwise plot a 0

As was stated earlier, this is the long way to write this type of formula. The If() is implied in this

formula and each period is examined individually to see if it is above it corresponding moving

average. If you were to require a security to remain above its moving average for a longer

duration, this formula could quickly get out of control. Formula 24 is much shorter.

Formula 24:

Formula 24 in English

The formula “when the close is greater than the 5-period

simple moving average of the close, plot a 1 else plot a 0”;

is calculated for the past five days and the results added

together. If the result equals 5, plot a 1, else plot a 0.

The Sum() function adds values of a data array for a specified number of periods. In this case, it

adds the last five results of "C>Mov(C,5,S).”. This condition returns either a 1 or a 0. So if it is

true for five straight days, the result would be 1+1+1+1+1=5. Again, the condition contains an

implied If() function. In the future, if a formula is simply plotting whether or not an event

occurred, such as this one is, this document will omit the implied If(). Please refer to Chapter 2

if you have any difficulty understanding the If() function or how it is used.

As you can see from the following chart, both of these formulas plot exactly the same way.

If you are curious, here is the full text of the Sum() function.

Summation

SYNTAX sum( DATA ARRAY, PERIODS )

FUNCTION Calculates a cumulative sum of the DATA

ARRAY for the specified number of

lookback PERIODs (including today).

EXAMPLE The formula "sum( CLOSE, 12 )" returns the

sum of the preceding 12 closing prices. A

12-period simple moving average could be

written "sum(C,12) / 12."

Summation is a very handy function when writing MetaStock formulas. Any time you need to

add consecutive values in a data array, see if the Sum() function can help.

Exercise 17:

Suppose you wanted to know the total volume for a week while still looking at a daily chart.

How would you create such an indicator using the Sum() function. To make it easier, on any day

but Friday, only plot a 0. Write a formula to do this and name it Weekly Volume. Hint: you will

need the DayofWeek() function shown below. Look at Formula 25 for the answer.

Day Of Week

SYNTAX dayofweek()

FUNCTION Plots the day of the week. 1=Monday,

2=Tuesday, 3=Wednesday, 4=Thursday,

5=Friday, 6=Saturday, 7=Sunday.

Formula 25:

Formula 25 in English

If the day of the week is Friday, plot the sum of the last five

periods volume, else plot a 0

The Cumulative function is related to Summation but where Sum() allows you to limit the

addition to a specific amount of data, Cum() automatically counts from the first period loaded.

There is no way to make it use less data. Cum() has its uses, but do not confuse it with Sum() or

your formula won’t work.

Staying Alert

The Sum() function works well when you want to count how many times something occurs or

total the recent values of a data array. But what do you do when you want to see if a specific

event has occurred with in a specified time frame? You are not concerned with exactly when it

occurred or even how many times. You just want to know if it occurred. True, you could use

the Sum() function by writing something like this:

sum( event, number of periods) >0

Copyright © 2002 Equis International

There is a better way, however. Take a look at the alert function below.

Alert

SYNTAX alert( EXPRESSION, PERIODS )

FUNCTION Extends a "true" result of EXPRESSION

for the specified number of periods. This

true result is held true over the number of

periods specified even if a "false" result is

generated.

EXAMPLE alert( cross(rsi(14),70),5 )

With this function, you save a little space, plus the formula becomes a little less cryptic to other

people should you share your ideas with them.

Exercise 18:

Suppose you wanted to know when the price of a security dropped 1 or more whole points in one

day. However, you also know that this security is prone to large moves and usually corrects

itself. Therefore, you only want to know of the price drops if the volume traded is larger than

normal. Write a formula to do this and name it Price and Volume. Use a 20-period simple

moving average for the volume and look for trading twice this average within the last three

periods of the price drop. Look at Formula 26 for the answer.

Formula 26:

Formula 26 in English

If the 1-period rate of change is greater than or equal to 1

and within the last 3 periods the volume has been greater

than or equal to twice a 20-period simple moving average

of the volume, plot a 1, else plot a 0.

The CROSSing Guard

A common buy and sell signal is one line crossing another. The close crossing a moving average

or the MACD crossing its signal line can be either a buy or a sell signal depending on which way

it crosses. If the RSI passes 70, you might want to examine that security.

All of these market signals occur the day the lines cross. If you open a chart and see the security

crossed above its moving average last week and is close to passing back below it now, you

missed the buy in point. While that security might soon be a good short sell candidate, the time

is not yet right. Timing is everything, correct?

Exercise 19:

You can use the If() function to see when a line crosses another. It would only require you to

check that one line was below the other yesterday and above it today. How would you do this to

see when a security’s closing price rises above its 40-period exponential moving average. Write

a formula to do this and name it Buy Signal. Look at Formula 27 for the answer.

Formula 27:

Formula 27 in English

If last period’s close was less than last period’s 40-period

exponential moving average of the close and this period’s

close is greater than this period’s 40-period exponential

moving average of the close, plot a 1, else plot a 0.

That seems like a lot of trouble to go through for such a commonly watched event. Enter the

cross function.

Cross

SYNTAX cross( DATA ARRAY 1, DATA ARRAY 2 )

FUNCTION Plots a "+1" on the day that DATA ARRAY

1 crosses above DATA ARRAY 2.

Otherwise, "0" is plotted.

If you want to know when DATA ARRAY 1

crosses below DATA ARRAY 2, use the

formula "cross( DATA ARRAY 2, DATA

ARRAY 1)"

EXAMPLE cross( close, mov(close,9,e) )

Using Cross(), formula 27 can be rewritten as follows:

Formula 28:

Formula 28 in English

If the close crosses above the 40-period exponential

moving average of the close, plot a 1, else plot a 0

This is a much simpler method. Cross() always lets you know when the value listed first

increases past the value listed second. You can use any data arrays you want in the Cross()

function but it only returns a true value when the first rises above the second.

A Quick Comparison

Here’s another quick shortcut for you. Suppose you wanted to make sure that some value was

greater than two other values. An example would be the close being above its 9 period and 40

period moving averages. Normally, your would have to write this as:

( ( C > mov( C, 9, S ) ) AND ( C > mov( C, 40, S ) ) )

There is a way this can be condensed, however. The Max() function compares two values and

returns the highest. The full text of the command follows:

Maximum

SYNTAX max( DATA ARRAY, DATA ARRAY )

FUNCTION Returns the largest of the two parameters.

EXAMPLE The formula "max( CLOSE, 10 )" returns

either the closing price or 10, whichever is

greater. The formula "max(-14, 13)"

always returns 13.

Exercise 20:

Some trading systems require the closing price to be above two different moving averages to

return a valid buy signal.

Assume one such trading system uses a 13-period and a 40-period simple moving average. How

would you use the Max() function here? Write a formula to do this and name it Above Both

Averages. Look at Formula 29 for the answer:

Formula 29:

Formula 29 in English

If the close is greater than the larger value of the 13-period

simple moving average of the close and the 40-period

simple moving average of the close, plot a 1, else plot a 0.

This function may be a bit more cryptic, but it saves time when writing multiple comparisons. It

also has a companion function, Minimum.

Minimum

SYNTAX min( DATA ARRAY, DATA ARRAY )

FUNCTION Returns the smallest of the two parameters.

EXAMPLE The formula "min( CLOSE, 10 )" returns the

closing price or 10, whichever is less. The

formula "min(-14, 13)" always returns -14.

This function works the exact same way as Max(). In order to change the formula Above Both

Averages to be true when the close was less than both moving averages, you only need to change

the function name to Min and change the greater-than sign to a less-than sign. Formula 30 shows

how the new formula would look.

Formula 30:

C < min( mov( C, 13, S ), mov( C, 40, S ) )

Formula 30 in English

If the close is less than the least of the 13-period simple

moving average of the close and the 40-period simple

moving average of the close, plot a 1, else plot a 0.

Just like other functions, these two can be nested. Thus you could compare multiple values

quickly without having to nest a lot of If()s.

The Zig-Zag Ziggurat

The Zigzag indicator shows you the major moves of a plot by filtering out all moves of a certain

percentage or less. This may seem like the holy grail of technical analysis, until you realize it

does this after the fact.

The Zigzag’s plot is a line moving up and down in sync with the values it is plotted on, however,

the last segment of the line is estimated. The direction of the last part of the line is determined

by assuming that the plot will continue in the same direction. The following chart shows a stock

and the Zigzag indicator with a ten percent filter.

This chart is accurate for the data that is shown. Notice the last part of the line is dipping

downward even though the security has not lost ten percent of its value since the last peak. The

Zigzag is anticipating the continued downward movement. Here is the same chart two weeks

later.

Notice the line is still trending downward. The Zigzag is doing its job and has filtered out the

minor drop and rise during the end of the first week of February. You might think by now that

this last line is confirmed. Below is the same security one week later.

During the previous 3-week period, the prices never dropped the required ten percent. As a

result, when the prices started rising again, the line corrected to show it as one continuous

upward progression.

Understanding how and why the Zigzag does this is important because several functions in the

MetaStock formula language are based on this indicator. These additional functions include

Peak(), Trough(), and Divergence(), etc.

These functions monitor the general trends of the data array and report various values based on

its movement. However, because they are based on the Zigzag, the last value is always

uncertain. These functions have many uses in examining historical data, but you should be

careful with any buy and sell decisions based directly on them. Tomorrow, the signal might not

be there.

Review Questions

1. What would be plotted by the formula: Cum(1)

2. Write the If() equivalent of the formula: Min( close, open )

3. True or False, The Cross() function returns a 1 when its data arrays cross each other.

4. True or False, The Alert() function allows you to specify conditions that do not have to

happen simultaneously.

5. True or False, The Zigzag() function is a great tool for identifying trends and patterns.