Formula Language Course · Lesson 6 of 9

Naming variables, If() and Input()

Variables get a full workout in Lesson 7. First, the naming rules MetaStock enforces, and two functions that make custom indicators genuinely interactive.

BY DAVID JENYNS REFRESHED AUGUST 2026

When naming variables, MetaStock outlines a few rules to follow:

The If() function

The if() function employs one of the most common idioms of modern programming languages, the "if... then... else..." construct. It is a conditional statement: if a predefined expression occurs, perform x; if it does not, perform y. The correct syntax is:

If({expression},{then data},{else data})

Let's look at an example called "positive and negative volume":

if( C > O, +V, -V )

This formula plots positive volume if the security's close is greater than its open; otherwise negative volume is plotted. The formula itself doesn't have much practical use, but it shows if() in action. A more practical use appears in the On Balance Volume indicator, covered in our OBV tutorial. Its formula is a bit advanced, but it is a useful example of if() at work:

(If(C > Ref(C,-1),1,-1) * VOLUME) + PREV

We will unpack formulas like this, and their intricacies, later in the course.

The Input function

The Input function is used with custom indicators to prompt the user for input. You have probably met it without noticing: a good number of MetaStock's predefined indicators use it. When applying the moving average indicator, MetaStock prompts you to enter time periods, method, price fields and so on. Those are all input functions. The basic syntax:

input("{prompt text}",{minimum value},{maximum value},{default value});

Because we define the minimum and maximum, a value outside that range triggers an error message asking for a value greater than or less than what was entered.

Here is an example. The following custom indicator asks for the time periods you wish to use for a moving average of the On Balance Volume indicator, i.e. the number of smoothing periods:

SmoothingPeriods:=Input("Enter the number of OBV smoothing periods",1,21,7);
Mov(OBV(21),SmoothingPeriods,S);

When this custom indicator is plotted, an input dialog appears prompting you to "Enter the number of OBV smoothing periods".

VARIABLES: NAME ONCE, USE ANYWHERE 1. NAME IT ONCE SmoothingPeriods:=Input("Enter the number of OBV smoothing periods",1,21,7); Mov(OBV(21),SmoothingPeriods,S); 2. USE IT ANYWHERE
Assign the name once with := and a semicolon, then every later mention of SmoothingPeriods stands in for the value the user typed

Next lesson

In Lesson 7 we cover inserting comments into your formulas, then give variables the full treatment: assigning values and whole expressions to shorthand names.

MetaStock

Formulas need software to run in

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