Formula Language Course · Lesson 5 of 9
Functions can live inside other functions. Once you can nest, you can build a moving average of a moving average, and hold a signal open while you wait for confirmation.
With so many functions available in MetaStock, it is important to know that functions can be nested within a function. That is, a nested function can act as another function's data array parameter. Assume you wanted to code the moving average of another moving average. It looks like this:
Mov( Mov( C, 5, W), 20, W) We will see nesting applied throughout the rest of the course. For now, let's look at the first major function category, analysis tools. Most functions in this category return specific values from a data array: anything from the number of bars since a condition occurred, to the highest value in a data array.
The first function we will study is Alert. It is used in conjunction with other functions to extend a signal for a specified number of periods. In other words, it holds an expression as true for as long as we wish.
Think about Alert with gates and triggers in mind. Using this function we can hold a gate open for a specific period while waiting for a confirming trigger, a vital ingredient in a fully automated system. First, the syntax:
Alert(Expression, Periods) The formula below identifies stocks whose volume has been 50% greater than the 21-period moving average of volume, a sign that interest in the stock has increased. By using Alert we extend the life of that condition from one period to five: the volume spike on the initial period provides the same signal for the following five periods.
Alert(V > Mov(V,21,S)*1.5,5) Broken down:
V > Mov(V,21,S)*1.55We can build on this example to show a more useful application:
Mov(C,5,S) > Mov(C,22,S) AND
Alert(V > Mov(V,21,S)*1.5,5) This formula identifies securities in an uptrend (denoted by the 5-period simple moving average being greater than the 22-period simple moving average) that have also shown a 50% increase in average volume at least once within the previous five periods. Remove the Alert function and both conditions would have to be true simultaneously, which is a much stricter and less realistic requirement. Combined with other functions (such as Cross, later in the course) you will realise its power.
Throughout the function lessons there are exercises to practise what we've learnt. They are easily skipped, but they build your MetaSpeak vocabulary. Construct formulas for the following:
Cross(RSI(14),30) true for 13 periods.In Lesson 6 we cover the rules for naming variables, plus two workhorses of custom indicators: the If() function and the Input() function.
Everything in these lessons works in the free 30-day MetaStock trial: full software, live data, your own watchlist.
Start Your Free 30-Day Trial →30-DAY TRIAL · FULL VERSION · CANCEL ANYTIME · AFFILIATE LINK, SAME PRICE FOR YOU
The Formula Language Course: Course home · 1. First formulas · 2. Operators · 3. Precedence & periodicity · 4. Built-in functions · 5. Nesting · 6. Variables · 7. Comments · 8. Pasting functions · 9. Exercises