Formula Language Course · Lesson 7 of 9
Two habits separate readable formulas from spaghetti: comments that explain your thinking, and variables that turn repeated expressions into shorthand.
MetaStock lets you make comments anywhere within a formula. Comments are denoted by
enclosing any text in braces, { and }. They
make it possible to annotate your code without wrecking the syntax, so the code is
easier to read, for you and for others.
Comments are also a great tool in the design phase of a formula. Assume you wanted to test a specific section of a formula: with braces you can "comment out" the other sections, leaving only the code in question, then remove the braces later to restore it. Some examples:
Mov(C,5,S) > Mov(C,14,S) {AND Rsi(c,14) > 60} ...an example of commenting out code, and:
Mov(C,15,S) > Mov(C,30,S) AND C>0.5 {I might have to adjust this from 50 cents to something higher if too many stocks appear in the explorer - will see what happens} ...an example of making working notes.
Now for the advanced features. If you are not familiar with any type of programming, most of these concepts will be new, so there are plenty of examples to work through. We begin with variables: a variable is a letter or a word that is assigned to an expression or single value, which can then be used instead of the original. Think of it as writing shorthand: rather than repeating expressions over and over, we assign a variable and reference that. It simplifies formulas, making them easier to read and maintain.
For MetaStock to understand our shorthand, we must first define what the variable replaces. The basic syntax:
{variable name} := {expression or single value}; := separates the variable name from the expression.A basic variable statement:
x:=10;
C > Mov(C, x, E) AND Mov(C, x, E) > Mov(C, 20, E)
Don't be overwhelmed by the moving average function here. All you need to see is
that x is assigned the number 10, shown by
x:=10;. Wherever the letter x is written, it is the same as
writing 10. The benefit: to change the time periods from 10 to 5 you edit one
variable statement, x:=5;, instead of every use individually.
With complex formulas the benefit cannot be overstated.
More than one variable can be used in a formula, up to 20 in fact, as long as each is assigned before you use it. The following code looks for securities where the close is greater than the short-term moving average, the short-term average is greater than the medium-term, and the medium-term is greater than the long-term:
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) Reading the code, imagine typing the variable is the same as typing the single value it is assigned to.
Assigning variables to single values makes maintenance easier, but perhaps the most effective use of variables is assigning them to whole expressions. Complex formulas shorten, becoming easier to read and modify. The previous example, rewritten with expressions assigned 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? Whenever the variable's
letter is typed, the expression stands in its place: typing x
is the same as typing Mov(C,5,E).
Although this next example is beyond the scope of this lesson, look at how a genuinely complex formula is tamed by variables. We show it not to impress you, but to impress upon you their usefulness. Variables transform the "CMO Volatility" formula from 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)))/(S1+S2+S3) Not only is the code shorter, it is easier to read and easier to modify. That concludes the introduction to variables. They can feel overwhelming at first, but remember: a variable simply assigns a letter or word to an expression or single value, and that letter or word is used instead of the original.
In Lesson 8 we look at the Paste Functions dialog: the shortcut that saves you memorising the syntax of 200-plus functions.
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