Nov
5

Formula 201- #5 Multiple Statements and Series Variables

So far we have worked on indicators with a single statement only. Although that can create many useful indicators, it may not be enough if we need to create indicators with multiple plots, or results that depends on multiple intermediate values.

Concept of Multiple Statements

The assignment statement we have used since the first tutorial is a statement. Each formula indicator can host an unlimited number of statements. To write more then one statement in your formula indicator, you need to follow the proper syntax to identify the end of each statement.

In general, you can write multiple statements within a formula using the semicolon as the statement separator,

statement1;statement2;statement3 ...

Each statement within the formula is executed sequentially.

Basic Use of Multiple Statements

Basic usage of multiple statements is to define the values for each of the plot series within an indicator. For example, if you defined your indicator to have 3 plot series. Then you need to provide the values for each of the 3 plot series within your formula.

plot1 := average (data1, 5);
plot2 := average (data1, 10);
plot3 := average (data1, 20)

Remember that plot1, plot2, plot3 are called predefined series variables. Assigning values to them has the special meaning of showing these values in the plots of the indicator.

Series Variables

When you assign values to variable names not predefined by NeoTicker, you are creating a series variable. This series variable behaves exactly like the data series functions. For example, if you want to access the previous value of a series variable called myaverage, then the proper syntax is myaverage (1).

Here is a formula example utilizing a single series variable.

myaverage := average (data1, 10);
plot1 := myaverage;

Here is a formula example utilizing multiple series variables.

myaverage := average (data1, 10);
plot1 := myaverage;
mydoublesmoothaverage := average (myaverage, 10);
plot2 := mydoublesmoothaverage;

Notice that we use the myaverage series variable as a Link Specification when we define the series variable mydoublesmoothaverage.

The above formula can be written without using the extra series variables at all,

plot1 := average (data1, 10);
plot2 := average (plot1, 10)

Since plot1 is a series variable, it can be used the same way as myaverage.

When our indicator gets more complex with more statements, writing indicators directly using the plot series will make the indicator less readable and harder to understand. It is good programming practice to utilize the series variables to carry out intermediate steps in calculations.

Creating Our Special MACD

MACD standards for Moving Average Convergence Divergence. It is a very common technical indicator.

Standard MACD uses the closing price as the underlying data series. It is calculated by taking the difference between 2 moving averages of the underlying data. Confirmation of turning signal is based on the crossover of the difference series and its own smoothed series.

In our case, we want our customized MACD to calculate based on MySpecialAverage2 indicator we have created in previous tutorial.

Here is the Indicator Specification.

formula201 part5 indicator spec

Different from previous tutorials, as we have multiple plots in this indicator, we will need to specify the visual setting of each plot,

formula201 part5 indicator spec2

And the script.

formula201 part5 script

When you apply the indicator onto a chart, you can see the effects of the visual setting.

formula201 part5 chart

Summary

When we can write an indicator with multiple statements, the flexibility in our indicator designs can get as complex as we want.

From this tutorial onward, most of the concepts that we are going to cover will require the use of multiple statements.

Complete Indicator

My Special MACD.

Exercise

1. Create an indicator that shows 5 plots – the momentum, the smoothed momentum, and a long term standard deviation band on the momentum.

Answers For Previous Exercise

1. 2 possible solution depending on the flexibility of the indicator.

If you allow only one parameter for specifying the period, then

plot1 := average (average (average (data1, param1), param1), param1)

If you allow 3 different periods to be specified in the parameters,

plot1 := average (average (average (data1, param1), param2), param3)

2. The built-in indicator mov can take a string parameter as the type. So to define our MySpecialAverage3 indicator,

plot1 := mov (MySpecialPrice (data1), param1, param2)

where param1 is a string parameter and param2 is an integer parameter.

One issue is that you do not know how to popup the selection list of available moving average types yet. For now, you need to type in the string yourself (like Simple or Exponential) when you apply the indicator onto a chart.

For quote window usage, the quote formula will look like this,

MySpecialAverage3 (M5, "Exponential", 10)

Discuss this article.

Leave a Comment

Blog Developed
By ContentRobot