The Bars Since function is quite self explanatory; it allows
you to count back the number of periods since an expression last occurred. When
coded, this function returns the number of periods (be it days, weeks, months
etc. depending on the periodicity set) since the expression last occurred. Note
that the terms `bars' and `periods' are interchangeable.
SYNTAX BarsSince(Expression)
Expression _ The technical condition that you are going back
through the data to search for. When found, MetaStock will count the number of
bars since it last occurred.
EXAMPLE
The following formula specifies the number of bars since the
closing price crossed with a 30 period weighted moving average. Now before you
are overwhelmed by this formula, take note that we haven't yet introduced the
cross function. Therefore, just understand that the Bars Since function counts
the number of periods since this expression last occurred.
BarsSince(Cross(C,Mov(C,30,W)))
In the formula above:
Expression = Cross(C,Mov(C,30,W))
APPLICATION
A more useful application of this example could be:
BarsSince(Cross(C,Mov(C,30,W)))<=5
This formula specifies the number of bars (eg. days) since
the closing price last crossed with a 30 period weighted moving average. We then
instructed MetaStock to check whether this number was equal to or less than 5,
and if so, provide us a positive return.
This formula could produce a positive return even if the
closing price had since crossed back below the moving average. All we specified
was that it had to have crossed the moving average within the last 5 periods. A
useful conditional statement to add to this could check whether or not the
closing price was still presently above the moving average; and if it were, to
then count the number of bars since that last occurred. Using an `IF' statement
(refer to page 26), we could amend the formula to:
If(C>Mov(C,30,W),BarsSince(Cross(C,Mov(C,30,W))),0)
With the new additions, we've specified that if the closing
price is not above the 30 period weighted moving average (denoted by `If(C>Mov(C,30,W)'
), then MetaStock will return a value of zero. Conversely, if it is above the
moving average, MetaStock will count the number of bars since the closing price
last crossed with the moving average and return that value.
Looking at Figure 3.3, we can see this example applied to the
chart as an indicator.
Figure 3.3 _ BarsSince Indicator
EXERCISES
Construct formulas for the following:
1. Return the number of bars since the closing price last
crossed above the 20 period simple moving average
__________________________________________________
2. A conditional statement that specifies that it must
have been at least 5 bars since the volume was greater than a 20 period:
__________________________________________________
This
article is a snippet from the
MetaStock Programming Study Guide...
"Discover
The Simple Secret to Make Metastock Easy & Identify Profitable
Trades"

copyright 2007 www.metaformula.com