Nov
22

Formula 201 – #7 Advanced Indicator Access

We will talk about the more advanced features in the formula language that deals with multi-plot indicators.

Accessing Individual Plots of an Indicator

To access individual plot of an indicator, we attach the suffix .plotN or its short form .pN to the indicator name. The value N starts from 1 and is referring to the first plot of the indicator. If there are 2 plots in the indicator, then N can be either 1 or 2.

For example, if you are trying to get the upper channel of the bollinger band, then the formula should look like this,

plot1 := bbands3.plot3 (data1, 100, 1)

In the formula above, we are getting the value from the third plot of the indicator bbands3. Since the third plot is the upper channel, it is exactly the value we want.

When you use an indicator within your formula, you can link to a specific plot of another indicator, just like linking to a specific field of a data series. For example, to obtain the highest high value of bbands3.plot3 over the past 10 period, you can write the formula like this,

plot1 := hhv (bbands3.plot3 (data1, 100, 1), 10)

MakeIndicator Function

When we access an individual plot within a multi-plot indicator, if it is the only thing we need from that indicator, it works fine. What if we are going to reuse the same indicator many times within our formula, including access to different plots of the same indicator?

Repeating the same formula expression again and again within our formula indicator not only make the code less readable, it is also a source of error if typo slipped into one of the many copies of the same code.

The makeindicator function is designed to take care of this issue. The general form of the makeindicator function is as follows,

MakeIndicator (InstanceName, Indicator, Link1, Link2, ..., Param1, Param2, Param3, ...)

In short, the MakeIndicator function creates an indicator instance of the Indicator named by the parameter InstanceName. The rest of the parameters are the same ones as calling the Indicator directly.

For example, we can create an indicator instance called mybbands of the Bollinger Bands 3 Lines indicator as follows,

makeindicator (mybbands, bbands3, data1, 100, 1)

Once you have created the instance, you will be able to use it just like a data series or a series variable.

plot1 := mybbands

Or, you can access a specific plot in the indicator similar to calling the indicator function.

plot1 := mybbands.plot2

You can also access the previous values in the series.

plot1 := mybbands.plot3 (1)

You can even use the indicator instance as a link series in another indicator.

plot1 := hhv (mybbands.plot3, 10)

Finally, you can of course use it in another makeindicator call.

makeindicator (myhhv, hhv, mybbands.plot3, 10)

Creating an Indicator Called Linear Regression Oscillator

I am going to show you an interesting indicator. It is called Linear Regression Oscillator.

The idea is to show the relative position of the current price with respect to its linear regression value and its distance from the regression value in terms of standard deviation. It sounds very complicated but the indicator is very easy to implement since we are now equipped with the MakeIndicator function.

Here is the Indicator Specification.

formula201 part7 indicator spec2

And there are 2 plots in the indicator, so we need to set the default colors for the individual plots.

formula201 part7 indicator spec

Now, the script.

formula201 part7 scriot

Notice how we use all the values returned by the Linear Regression Channel 3 Lines indicator (linregchannel3).

Here is how the indicator looks like on a daily emini S&P chart.

formula201 part7 chart

Summary

When we need to access the same indicator multiple times in various parts of our formula indicator, it is preferred to use makeindicator function to create a single instance of the indicator. That can help us design the indicator with more clarity and easier to maintain in the long run.

Complete Indicator

Linear Regression Oscillator.

Exercise

1. Create a special bollinger band indicator that adjust the band over a moving window of extremes.
(Hint: use highest high value and lowest low value indicators)

Answers For Previous Exercise

Here is the indicator.

current_day_high := if (date <> date (1), high, maxList (high, current_day_high (1)));
current_day_low := if (date <> date (1), low, minList (low, current_day_low (1)));

$prev_day_high := if (date <> date (1), current_day_high (1), $prev_day_high);
$prev_day_low := if (date <> date (1), current_day_low (1), $prev_day_low);
$prev_day_close := if (date <> date (1), close (1), $prev_day_close);

plot1 := high;
success1 := h >= $prev_day_high and $prev_day_high >= l;

plot2 := low;
success2 := h >= $prev_day_low and $prev_day_low >= l;

plot3 := close;
success3 := h >= $prev_day_close and $prev_day_close >= l;

Discuss this article.

Leave a Comment

Blog Developed
By ContentRobot