Chapter 3. Defining Inputs

In this chapter we will discuss how to make your study more flexible. Most of the built-in studies are adjustable in terms of timeframe, price data, or mode of calculation to employ. It is only a matter of seconds to switch from a larger timeframe to a shorter one using input parameters which will appear in your Edit Studies and Strategies dialog window if you properly use reserved word input in the beginning of the script. Let’s see how it works.

In chapter 2, the following script was discussed:

def val = close/open;
def data1 = Exp(val);
def data2 = Power(Double.E, val);

Now that you know that the variables data1 and data2 will yield identical values, let’s exclude the latter from the script:

def val = close/open;
def data1 = Exp(val);

Here is the question: what if you don’t need the Close to Open ratio anymore but need the inverse one? What if you don’t need this one either and want the High to Low ratio instead?

Here is the best way to do it:

input val1 = close;
input val2 = open;
def data = Exp (val1/val2);

Once added, this script will provide the same results as the above mentioned would. But this one is much better: whenever you need to change the ratio, you could go to the Edit Studies and Strategies dialog window and build this ratio directly in it. In order to do that, specify the fundamentals for val1 and val2 and click Apply; the script will be recalculated.

You are not restricted to only using the price data for inputs: thinkScript® provides you with numerous types of inputs, automatically deciding which type it would be based on the default value specified by you (this is what you type after the equals sign in the input declaration row).

Integer inputs will allow you to enter another integer with which the calculation will be performed.

input length = 10;
plot SMA = Average(close, length);

This will calculate a 10 period SMA of close price (if you are not sure what it is, next chapter will explain everything) with an option to change its length via the corresponding input. And, of course, we could make it even more flexible:

input length = 10;
input price = close;
plot SMA = Average(price, length);

Now both price and length can be adjusted.

Floating numbers can also be used for inputs, however, be careful as there are types of data for which only integers can be used, e.g., number of bars. In order to tell thinkScript® that you need a floating number, use a period in the default value:

input percentShift = 10.0;
plot UpperBand = close * (1 + percentShift / 100);
plot LowerBand = close * (1 - percentShift / 100);

This script will draw an envelope based on the Close price. In this envelope, bands will be shifted up and down from the close price by 10 percent, although you are free to specify any percentage you need: 25.5, 47.8, 99.9, etc.

Using a string input can be especially useful when you need a quick change of symbol for which some value is calculated:

input symbol = "SPX";
plot Comparison = close(symbol);

This plot draws the Close price of the specified symbol which is set to “SPX” by default. Note that string inputs are defined using values in quotes.

Symbols are not the only application of string inputs as this type of input can be used for any function that accepts string values as input parameters.

Boolean inputs can quickly help you select a certain calculation mode out of two defined in the script: to define a Boolean input, use yes or no as the default value:

input useHighLow = yes;
plot HighPrice = if useHighLow then Highest(high, 10) else Highest(close, 10);
plot LowPrice = if useHighLow then Lowest(low, 10) else Lowest(close, 10);

This script will plot a channel based on the highest High and lowest Low price on the period of 10 bars if the input is set to yes. If this input is set to no, channel we be plotted based on the highest and lowest Close price. Note that we use an explicit condition in this script: it starts with word if and tests if we chose yes or no as the input value. Using conditional expressions will be discussed in chapter 6.

If you need more than two options to select from or if you prefer more meaningful naming for them, the enum type should be used. This will require just a little bit more scripting, but it’s not too difficult. First of all, list all the options for the input in braces and specify the default one:

input fastLength = 14;

input slowLength = 28;
input diffType = {default points, percent};

After that, you will need to specify what should happen if a certain option is selected. This is done by using commands switch and case:

input fastLength = 14;

input slowLength = 28;
input diffType = {default points, percent};

plot VolumeOsc;
switch (diffType) {
case points:
VolumeOsc = Average(volume, fastLength) - Average(volume, slowLength);
case percent:
VolumeOsc = (Average(volume, fastLength) - Average(volume, slowLength)) / Average(volume, slowLength);
}

This example script plots the volume oscillator, a difference between two simple moving averages (SMAs) of volume with different lengths. Here, command switch defines whether the oscillator should be plotted as the difference itself (case points) or as its percent ratio to the slower moving average (case percent). Speaking of the averages, if you wonder what these words and abbreviations stand for, move on to next chapter where we are going to discuss several ways of averaging data in thinkScript®.