MetaStock Programming Language – Tips On Inserting Comments

Posted in Blog

Insert comments when needed.

This is a continuation of our series on the basics of MetaStock Programming Language. In this post we are going to talk about inserting comments within a formula.

Inserting Comments

MetaStock allows you to make comments anywhere within a formula. Comments are denoted by enclosing any text in braces i.e. ‘{’ and ‘}’. These make it possible to annotate your code without wrecking the syntax. Ultimately, as a Metastock expert this makes code easier to read by yourself and others. Moreover, comments are a great tool when used in the design phase of a formula. For example, assume we wanted to test a specific section of a formula. With the use of braces we can “comment out” other specific sections of code, leaving only the code in question. Later we can simply remove the braces to restore the other code.  Here are some examples:


Mov(C,5,S) > Mov(C,14,S) {AND Rsi(c,14) > 60} – this is an example of commenting out coding

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} – this is an example of making comments

Advanced Features

Next, we’ll look at a few of the advanced features within MetaStock guide after inserting comments within a formula. If you’re not familiar with any type of programming, most of these concepts will be new to you. To help you overcome the learning curve, there’ll be plenty of examples and questions to tackle. Additionally, in later chapters we’ll refer to all of these features and how to apply them practically.


We’ll begin with variables: a variable is a letter or a word that is assigned to an expression or single value. This letter or word can then be used instead of the original expression or single value. Basically, it enables us to select one word and use it to replace whole pieces of coding. We like to think of it as a way of writing ‘shorthand’: rather than repeating expressions over and over we can assign a variable and then reference that variable. This simplifies formulas, making them easier to read and maintain.

For MetaStock to understand our ‘shorthand’ we must first define what the variable will be replacing. The basic syntax for this is:

{variable name} := {expression or single value};

•    The ‘:=’ separates the variable name from the expression.

•    The semi-colon denotes the end of a variable statement.

Let’s look at a basic variable statement:


C > Mov(C, x, E) AND Mov(C, x, E) > Mov(C, 20, E)

Now before you are overwhelmed by this formula, remember we haven’t introduced the moving average func- tion. All you have to understand is that ‘x’ is assigned to the number ‘10’, shown by ‘x:=10;’. Therefore when- ever the letter ‘x’ is written this would be the same as writing ‘10’.

In our example, the benefit of this type of variable is that we can use it to easily change the time periods within our formula. Rather than having to edit each use of the time periods individually, we can just alter the variable. For example, if we wanted to alter the time periods in the above example, from 10 to 5, we could simply change the variable statement to ‘x:=5;’. Now whenever ‘x’ is written, the new assigned variable would be the number ‘5’. With more complex formulas the benefit of using variables cannot be overstated.

Evidently, more than one variable can be used in a formula. In fact up to 20 variables can be used in a formula, as long as they’re assigned before you use them. In the example on the next page we’ve used three separate variables. The code itself looks for securities that exhibit the following attributes: the close to be greater than the short-term moving average, the short-term moving average to be greater than the medium-term moving average, and the medium-term moving average to be greater than the long term moving average.


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)

When looking at the code, imagine that typing the variable would be the same as typing the single value the variable is assigned to. While assigning variables to a single value can make the maintenance of a complex formula easier, perhaps the  2 most effective use of variables is in assigning them to an expression. This handy tool enables complex formulas to be shortened, making them easier to read and easier to modify.

Using our previous example of multiple variables, let’s rewrite this, assigning whole expressions 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? This means that whenever the variable’s corresponding letter is typed, the expression will appear.

For example, if ‘x’ was typed, this would be the same as typing


Although this next example is outside the scope of this chapter, let’s look at a complex formula and how it can be simplified by variables. We show you this formula not to impress you, but to impress upon you the usefulness of variables.

The use of variables can transform the formula “CMO Volatility”, 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);



Not only is this code shorter, but it’s easier to read and easier to modify as a MetaStock Professional. This concludes the introduction to variables, and although they can be quite overwhelming at first, remember they simply assign a letter or a word to an expression or single value. This letter or word can then be used instead of the original expression or single value.

Now you know tips on inserting comments anywhere within a formula. In our next post, we will be discussing few rules to follow when naming variables in MetaStock Programming Language. Do you want to learn how to use MetaStock formula like the pros? Just click here to learn more.

Watch this video for a quick introduction to various aspects of the Metastock data platform, QuoteCenter.

Leave a Reply

Your email address will not be published. Required fields are marked *