BullScript: Sample Indicator

Sample Indicator

This topic is designed to help you create advanced indicators by examining one of the indicators that comes built into BullScript: the Stochastic Oscillator. First a simple Stochastic formula script is shown, then a more advanced version, as used in BullCharts, is shown, then a discussion of script follows.

Note: You can access the scripts for all of the indicators in BullCharts

A Simple Stochastic Oscillator Script

The following formula could be used for Stochastic Oscillator. BullScript will let you enter this directly as a script and it will work ok.

(sum(C-llv(L,5),3) / sum(hhv(H,5)-llv(L,5),3)) * 100;

However, , by adding some extra attributes and inputs we can make the script much more useful.

The BullCharts Stochastic Oscillator Script

Note: The line numbers here are not part of BullScript, but are added to assist discussion of the script.
Colouring has been added in the same way that the BullScript syntax highligher would colour a script.

01 [description="When a trend is upward, there is a tendency that close price will be very close to that
02 day's high. During a downward trending market there is a tendency that close price is close to low price.
03 The Stochastic Indicator helps to find trend reversal searching for a period when close prices is close to
04 low price in up trending market or close prices is close to high price in down trending market.
05 This formula has two output values: %K - Simple Stochastic Indicator and %D - Smoothed Stochastic
06 Indicator (Moving Average of %K)."] 07 [citation="''Lane's Stochastics'' by George C. Lane, M.D.Stocks & Commodities March 1994 www.traders.com"] 08 [target=Percent; category=Oscillators; alias=stoch] 09 10 [horzlines=80,20] 11 12 kperiods := input("%K Time periods",5,1);
13 kslowing := input("%K Slowing periods",3,1);
14 dperiods := input("%D Time periods",3,1);
15 method := inputma("%D Method",S);
16 expr := expression("Expression");
17 signalperiods := input("Buy/Sell signal periods",200,1);
18 19 [name=%K] 20 kay := (sum(C - llv(L,kperiods),kslowing) / sum(hhv(H,kperiods) - llv(L,kperiods),kslowing)) * 100;
21 kay;
22 23 [name=%D; linestyle=Dash] 24 ma(kay,dperiods,method);
25 26 { Markers } 27 [name=Above 20; linestyle=marker; marker=type1; tooltip="Stochastic rose above 20"] 28 cross(kay,20) and expr > ma(expr,signalperiods,method);
29 30 [name=Below 80; linestyle=marker; marker=type2; tooltip="Stochastic fell below 80"] 31 cross(80,kay)

Description and Citation

A description attribute is a good way to indicate to users of your BullScript how to interpret the script, or what the script is designed to do. This information is shown in BullCharts on the Insert Indicator dialog. Note that the description tag spans multiple lines. BullCharts will wrap the text of any back into a single line.

If you are not the original designer of an indicator, then a citation will both give credit to the original author, as well as direct users to further information on the script in question. Or if you have designed the indicator yourself, it is a good way to link the script back to your own web site or books. The citation will appear in BullCharts below the description.

It is a good practice to enclose description, citation, and any textual attributes in a pair of quotes.

Target Attribute

Line 8 of the script appears as follows:

[target=Percent; category=Oscillators; alias=stoch]

Any number of attributes can be placed on a single line by separating them with a semicolon (;). This can keep the script tidy. Note: If you want to use a semicolon in a description, the text must be enclosed in quote marks.

The target attribute tells BullCharts the type of data that will be generated. This information allows BullCharts to make intelligent choices about scaling, etc. For example, an indicator with [target=price] will  automatically appear on the same scale as the price. See attributes for more details.

Category Attribute

By specifying a category, this indicator can be more easily found in the Insert Indicatordialog by using the drop down category list. It is recommended that you use the existing categories if applicable – but you may also create a new category simply by picking a unique name. For example, if you have designed and published several indicators you may want to use your name as a category – so users of your indicators can group them easily.

An indicator can be listed under multiple categories by separating them with a comma.

Alias Attribute

You may want to refer to this indicator from another indicator. If the indicator name contains spaces (such as “Stochastic Oscillator”) then normally you can’t directly refer to the indicator in code – the formula function would be required instead.

The alias attribute allows you to specify a short name to be used in other scripts, without sacrificing having a descriptive name for your indicator. In this case, alias=stochmeans that these two lines are equivalent in another script:
formula(“Stochastic Oscillator”)
stoch()

Horzlines

The horzlines attribute, on line 10, allows any number of horizontal lines to be added to the indicator. In BullCharts these can be subsequently modified or hidden by using the indicator properties dialog. Here 80% and 20% are set as critical levels.

Inputs

Lines 12-17 allow the indicator to be easily configured by accepting several inputs, as shown:

kperiods := input("%K Time periods",5,1);

kslowing := input("%K Slowing periods",3,1); dperiods :=


input("%D Time periods",3,1); method := inputma("%D Method",S); expr := expression("Expression"); signalperiods := input("Buy/Sell signal periods",200,1);

Three types of input are used here: input, inputma, and expression, which  allow the input of a number, a moving average type, and a data column respectively.

Note that each input is assigned to a variable. It is possible to use an input directly in a formula, but it is recommended that the input is assigned to a variable. This makes the script more readable, and also means that the result of the input can be used in several places if necessary.

Each input includes a short but informative description. This text will be shown on the Insert Indicator dialog. Lines 12-15 and 17 include a default value for the input. That is, the number following the description. It’s a good idea to supply a default value for every possible input. Following the default is a minimum value for that input. By providing this information, BullCharts can ensure that only sensible number are passed into the script.

The %K Line

Lines 20-21 show the actual calculation of the Stochastic Oscillator as we saw in the simple example, except fixed numbers have been replaced with our input variables. The result is assigned to the variable kay. Line 21 simply has the work kay. This has the effect of returning the previous calculation as a plot – but it was first assigned to a variable, the result can be used in later parts of the script. Put differently, any calculation that is not assigned to a variable gets returned as a plot. An attribute on line 20 precedes the result and tells BullCharts the name of that plot. In this case %K.

The %D Line

[name=%D;
linestyle
=Dash]
ma(kay,dperiods,method);

Lines 23-24 describe the signal line, %D. The linestyle attribute is also added to make the signal appear as a dashed line by default. Because the %K result had been assigned to a variable, it can be re-used in the calculation of %D. Line 24 specifies that the result is a moving average over %K, of the length specified by the input, and of the type specified by the inputma.

Markers

Lines 26-31 describe two markers that indicate when significant events occur on the chart. TThe linestyle=market attribute indicates that the next result should be interpreted as a marker. Whenever the following result evaluates to true, a marker will be drawn. In this case markers are drawn when the %K rises above 20 or falls below 80, among other conditions. Note the use of a comment, in green, to visually separate the markers from other parts of the script. Comments are a useful way of making a script more understandable to others.

See Also

Sample Scripts

Was this article helpful?

Related Articles