Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

___________________________________________________

www.meta-formula.com Trading Excellence Pty Ltd 2002


USING VARIABLES WITHIN METASTOCK

A variable is a letter or a word that is assigned to an expression or single value. This letter or
word can then be used instead oI the original expression or single value. Basically, it enables
us to select one word and use it to replace whole pieces oI coding. We like to think oI it as a
way oI writing shorthand`: rather than repeating expressions over and over we can assign a
variable and then reIerence that variable. This simpliIies Iormulas, making them easier to read
and maintain.

For MetaStock to understand our shorthand` we must Iirst deIine what the variable will be
replacing. The basic syntax Ior this is:

variable name} : expression or single value};

The :` separates the variable name Irom the expression.
The semi-colon denotes the end oI a variable statement.

Let`s look at a basic variable statement:
x:10;
C ~ Mov(C, x, E) AND
Mov(C, x, E) ~ Mov(C, 20, E)

Now beIore you are overwhelmed by this Iormula, remember we haven`t introduced the
moving average Iunction. All you have to understand is that x` is assigned to the number
10`, shown by x:10;`. ThereIore whenever the letter x` is written this would be the same
as writing 10`.

In our example, the beneIit oI this type oI variable is that we can use it to easily change the
time periods within our Iormula. Rather than having to edit each use oI the time periods
individually, we can just alter the variable. For example, iI we wanted to alter the time periods
in the above example, Irom 10 to 5, we could simply change the variable statement to x:5;`.
Now whenever x` is written, the new assigned variable would be the number 5`. With more
complex Iormulas the beneIit oI using variables cannot be overstated.

Evidently, more than one variable can be used in a Iormula. In Iact up to 20 variables can be
used in a Iormula, as long as they`re assigned beIore you use them. In the example on the next
page we`ve used three separate variables. The code itselI looks Ior securities that exhibit the
Iollowing attributes: the close to be greater than the short-term moving average, the short-
term moving average to be greater than the medium-term moving average, and the medium-
term moving average to be greater than the long term moving average.

x:5;
y:10;
z:20;
C ~ Mov(C, x, E) AND
Mov(C, x, E) ~ Mov(C, y, E) AND
Mov(C, y, E) ~ Mov(C, z, E)

When looking at the code, imagine that typing the variable would be the same as typing the
single value the variable is assigned to.


___________________________________________________
www.meta-formula.com Trading Excellence Pty Ltd 2002
While assigning variables to a single value can make the maintenance oI a complex Iormula
easier, perhaps the most eIIective use oI variables is in assigning them to an expression. This
handy tool enables complex Iormulas to be shortened, making them easier to read and easier
to modiIy.

Using our previous example oI multiple variables, let`s rewrite this, assigning whole
expressions to variables.

x: Mov(C, 5, E);
y: Mov(C, 10 , E);
z: Mov(C, 20, E);
C ~ x AND
x ~ y AND
y ~ z

See how each expression is now assigned to a variable? This means that whenever the
variable`s corresponding letter is typed, the expression will appear. For example, iI x` was
typed, this would be the same as typing Mov(C,5,E)`.

Although this next example is outside the scope oI this chapter, let`s look at a complex
Iormula and how it can be simpliIied by variables. We show you this Iormula not to impress
you, but to impress upon you the useIulness oI variables.

The use oI variables can transIorm the Iormula 'CMO Volatility, Irom this:

((Stdev(CMO(C,5),5)*CMO(C,5))( Stdev(CMO(C,8),8)*CMO(C,8))
(Stdev(CMO(C,13),13)*CMO(C,13)))/( Stdev(CMO(C,5),5) Stdev(CMO(C,8),8)
Stdev(CMO(C,13),13))

Into this:

S1: Stdev(CMO(C,5),5);
S2: Stdev(CMO(C,8),8);
S3: Stdev(CMO(C,13),13);
((S1*CMO(C,5))(S2*CMO(C,8))(S3*CMO(C,13)))/(S1S2S3)

Not only is this code shorter, but it`s easier to read and easier to modiIy. This concludes the
introduction to variables, and although they can be quite overwhelming at Iirst, remember
they simply assign a letter or a word to an expression or single value. This letter or word can
then be used instead oI the original expression or single value.

When naming variables, MetaStock outlines that there are a Iew rules to Iollow:
Variable names cannot contain commas, parenthesis, spaces, underscores etc.
Variable names cannot duplicate names already used by Iunctions (e.g., mov, rsi, iI,
etc.).
Variables cannot be assigned a name that matches the parameters reserved Ior use in
Iormulas (e.g., open, high, low, close, simple etc. Moreover this includes their
respective shortcuts).
Variable names must contain at least one alpha letter (e.g., T1234 ).
Variable names are not case sensitive (e.g., 'PERIODS is the same as 'periods).

***************************************************************************
Taken Irom the MetaStock Programming Home Study Guide
Written by Stuart McPhee & David Jenyns

___________________________________________________
www.meta-formula.com Trading Excellence Pty Ltd 2002
Unless You Own The MetaStock
Programming Study Guide, You're Not
Using MetaStock To Its Full Potential

Visit www.meta-formula.com
& click on 'MetaStock Secrets' to find out more

You might also like