MetaStock Programming Basics
Article
by www.meta-formula.com
Two MetaStock Basics, Order
of Precedence And Periodicity
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 while creating our
own MetaStock
formulas, since whenever MetaStock
Professional calculates a piece
of coding it refers to the order of
operator precedence.
Now
that you're getting comfortable with
the operator precedence, we can introduce
parentheses. By "parentheses"
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:
H-(H-L)*2
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
Pro calculates this, it would
initially multiply the low by two
`L*2', since multiplication has higher
precedence than subtraction. Then,
the high would be subtracted 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.
Periodicity
Earlier 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 particluar period, we must identify
which opening price we are referring
to.
To do this 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.
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 and MetaStock
data. 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.
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. As such, you need to
monitor this closely when using indicators
such as the average
true range when setting your stop
loss or when backtesting systems
with MetaStock
and TradeSim.
Learn
more about MetaStock visit www.meta-formula.com
or download
MetaStock today.
|