MetaStock Programming Language – Rules In Naming Variables

Posted in Blog

Learn the rules of naming variables.

This is a continuation of our series on the basics of MetaStock Programming Language. In this post we are going to talk about tips in naming variables as a Metastock Professional.

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

•    Variable names cannot contain commas, parenthesis, spaces, underscores etc.

•    Variable names cannot duplicate names already used by functions (e.g., mov, rsi, if, etc.).

•    Variables cannot be assigned a name that matches the parameters reserved for use in formulas (e.g., open, high, low, close, simple etc. Moreover this includes their respective shortcuts).

•    Variable names must contain at least one alpha letter (e.g., T1234 ).

•    Variable names are not case sensitive (e.g., “PERIODS” is the same as “periods”).

The If() Function

The if() function employs one of the most common idioms of modern programming languages, the “if… then… else…” construct. This is a type of conditional statement that says if a predefined expression occurs, perform ‘x’, but if it does not occur, then perform ‘y’.

The correct syntax for an if() function is: If({expression},{then data},{else data})

•    The “expression” is the required condition.

•    The “then data” is the action or value if the expression is true.

•    The “else data” is the action or value if the expression is false. Let’s look at an example called ‘positive and negative volume’:

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

This formula plots “positive volume” if the selected security’s close is greater than its open. Otherwise, “negative volume” is plotted. Graphically, this is shown in Figure 2.7. The formula itself doesn’t have much practical use. However, it shows the if() function in action.

The if() function is more practically used in the “On Balance Volume” indicator (discussed in Chapter 3). The formula for this indicator is a bit advanced, however, it provides a useful example of the if() function.

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

Later, we will learn these types of formulas and their intricacies.

Figure 2.7 – Positive & Negative Volume

Language

The Input Function

The Input function is used in conjunction with custom indicators to prompt the user for input. Most probably you’ve already encountered the input function and didn’t even notice it. I say this since a good number of MetaStock’s pre-defined indicators use the input function. For example, do you remember when applying the moving average indicator MetaStock prompts you to “enter time periods”, “method”, “price fields”, etc? These are all input functions.

When coding an input function into a custom indicator the basic syntax is:

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

•     The prompt text is the text displayed to the user next to the input box. It’s used to describe what input is required i.e. moving average period.

•    The minimum value is the smallest value that can be entered.

•    The maximum value is the largest value that can be entered.

•    The default value is the value that will appear initially within the input box.

When using the ‘Input’ function, we define the minimum and maximum values to be entered. If a value is set outside this range, an error message is displayed. The error message will ask that the value must be greater than or less than what was entered.

Guide

Let’s look at an example of a Metastock market data. The following custom indicator asks for the time periods that 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, the following input dialog appears prompting you to “Enter the number of OBV smoothing periods”.

Now you know these Metastock tips on naming variables. In our next post, we will be discussing input on “On Balance Volume” indicator. Do you want to learn how to use MetaStock formula like an expert? 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 *