Welcome back!
The Bars Since function is quite self explanatory but is a fundamental feature of MetaStock Professional; 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.
MetaStock Syntax: BarsSince(Expression)
Expression _ The technical condition that you are going back through the MetaStock Data to search for. When found, MetaStock will count the number of bars since it last occurred.
Here’s an example of how you could use this formula.
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))
Here’s how you would use the formula in a more useful application of this example:
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.
If you are new to MetaStock formula then this might seem complex or confusing but once you ‘get it’ you’ll understand why it’s used by so many trading professionals.
For more MetaStock lessons watch this vid: