Авторы: 159 А Б В Г Д Е З И Й К Л М Н О П Р С Т У Ф Х Ц Ч Ш Щ Э Ю Я

Книги:  184 А Б В Г Д Е З И Й К Л М Н О П Р С Т У Ф Х Ц Ч Ш Щ Э Ю Я

Chapter 5 Getting Fancy It’s a Variable Circus

The MetaStock formula language draws many parallels from computer programming languages.

It performs set calculations, can make decisions based on If() conditions, and has internal

functions. It also has variables.

Anyone familiar with computer programming will find these are different from the variables they

are used to. A variable in MetaStock is a name you can assign a static value to. Static means

that once assigned, the value of the variable may not be changed.

If you have never dealt with programming before, you may still be confused about what exactly

a variable is. Think of them this way: a variable is a symbol that stands for something else. For

example, when you see this sign “=”, you automatically think something like “equals” or “is

equal to.” That’s because our culture has assigned that symbol the value of “equals.”

In the same way, MetaStock allows you to create an object to represent something else. For

instance, suppose you wanted to plot the difference between the stochastic oscillator and its

signal line. Up until now, if you wanted to change the time periods for the stochastic, you would

have to find each place it was used and manually change the value. Now, you can assign the

time periods to the variable at the beginning of the formula and then use the variable where the

Stoch() functions ask for the number of periods. Then, changing the time periods is easily

accomplished by changing the value assigned to the variable. The Stoch() function is listed

below for you convenience.

Stochastic Oscillator

SYNTAX Stoch( %K PERIODS, %K SLOWING )

FUNCTION Calculates the predefined Stochastic

Oscillator.

EXAMPLE The formula "Stoch( 5, 3 )" returns the

value of a 5-period %K slowed 3-

periods.

To create a variable, simply type the name of the variable followed by a colon and the equals

sign. Then put whatever the variable is suppose to stand for. This entire line is ended with a

semicolon. Below is an example of how this might look. Here the variable time is assigned the

value of fourteen.

time:=14;

Exercise 21:

The MACD Histogram is the difference between the MACD and its signal line. For your first

formula using variables, create a Stochastic Histogram. This indicator should subtract a 3-period

exponential moving average of the stochastic from the stochastic. The values of %K and %K

slowing should be assigned to variables named Ktime and Kslow, respectively. Set Ktime to

equal 14 and Kslow to 3. Write a formula to do this and name it Stochastic Histogram. Look at

formula 31 for the answer.

Formula 31:

Formula 31 in English

Assign the number 14 to the value Ktime.

Assign the number 3 to the value Kslow.

Subtract a Ktime periods stochastic using Kslow period

slowing from a 3-period exponential moving average of

the same stochastic.

More Input Please

So far, the variable may not seem that useful. However, consider the Input() function listed

below.

Input

SYNTAX input( "PROMPT TEXT", MINIMUM

VALUE, MAXIMUM VALUE, DEFAULT

VALUE)

FUNCTION This function instructs MetaStock to prompt

for input when a custom indicator is plotted.

This function is only supported by the

Custom Indicator Builder.

prompt text. This defines the text displayed

next to the input box. This is used to

describe what should be entered.

minimum value. This argument is the

smallest value that can be entered. If you

attempt to enter a value smaller than this

value, MetaStock displays a message.

maximum value. This argument is the

largest value that can be entered. If you

attempt to enter a value larger than this

value, MetaStock displays a message.

default value. This argument defines the

default value (i.e., the value that will appear

in the box when the dialog is initially

displayed.) . Note that the default value is

used if another formula using the fml()

function calls the custom indicator.

EXAMPLE input("Enter the number of periods",1,50,9)

This rather verbose function allows the creation of custom indicators that request information

from you when they are plotted.

Just like how the RSI indicator of MetaStock prompts for the time periods, you can add the

Input() prompt to the Stochastic Histogram formula and have it do the same.

Exercise 22:

Edit the formula Stochastic Histogram and change the values of Ktime and Kslow to Input()

functions. The prompt text should state that you are asking for the time periods to be used. For

Ktime, set the minimum value to 2 and the maximum to 40. For Kslow, use a minimum of 1 and

a maximum of 5. The default can be set to any value between the minimum and maximum, use

14 and 3 if no other numbers present themselves. Look at Formula 32 for the answer.

Formula 32:

Formula 32 in English

Use the text “Time periods for %K” to prompt for a

number between 2 and 40 with a default value of 14.

Assign this number to the value Ktime.

Use the text “Time periods for %K slowing” to prompt for

a number between 1 and 5 with a default value of 3.

Assign this number to the value Kslow.

Subtract a Ktime periods stochastic using Kslow period

slowing from a 3-period exponential moving average of

the same stochastic.

You are allowed up to six Input() functions per formula. When the indicator is plotted, a

window comes up with each input on a separate line followed by the text you entered for a

prompt.

A word of caution; if you reference a formula containing an Input() in another formula, the

default value will be used. The Input() function’s prompt is only displayed if the indicator

containing the Input() function is plotted.

More on Subs

The variable can also be used as a type of sub-routine. Any expression that is used several times

in the formula may work better if replaced by a variable. This will shorten the formula overall

and make sure the expression is used the same way throughout the formula. A good example of

this can be found by looking at formula 33. This is the formula for the Chande Momentum

Oscillator. Not exactly something you want to type in just for fun.

Formula 33

periods:= Input("periods",1,100,14);

((( Sum(If(C>Ref(C,-1),C-Ref(C,-1),0),periods) )-(

Sum(If(C<Ref(C,-1),Ref(C,-1)-C,0),periods) ))/((

Sum(If(C>Ref(C,-1),C-Ref(C,-1),0),periods) )+(

Sum(If(C<Ref(C,-1),Ref(C,-1)-C,0),periods) )))*100

Now, look at formula 34. This is the exact same formula except two variables have been

defined.

Formula 34

periods:= Input("periods",1,100,14);

up:= Sum( If( C>Ref(C,-1), C-Ref(C,-1),0), periods);

down:= Sum( If( C<Ref(C,-1), Ref(C,-1)-C,0), periods);

((up-down)/(up+down))*100

This should be a lot easier to read. This formula should also be understandable by you. It uses

only commands that have been covered in this text.

Exercise 23:

This exercise is a little different than the previous ones. Look at formula 34 and see what it is

doing. Write the English version of the formula. When finished, it should be similar to the text

below.

Formula 34 in English

Prompt for a number between 1 and 100. Use 14 for the

default and store the value Periods.

Calculate the difference between this period’s close and

last period’s close if this periods close was higher. Total

this for the last Periods amount of time and store the result

in Up.

Calculate the difference between last period’s close and

this period’s close if last period’s close was higher. Total

this for the last Periods amount of time and store the result

in Down.

Divide the value of Up minus Down by the total of Up

plus Down and multiply the result by 100

The variable’s ability to break out a part of the formula and make it into a sub-formula allows

you to both simplify the formula and make it easier to read. This is especially helpful if you

need to examine a formula for errors or wish to modify an existing formula.