def <variable_name>=<expression>;


def <variable_name>;



Defines a variable you would like to work with.

Example 1. Non-recursive usage

def base = Average(close, 12);

plot UpperBand = base * 1.1;
plot LowerBand = base * 0.9;

This example shows a simplified SMAEnvelope study, where the def reserved word is used to define the base. The rationale of defining this variable is to avoid double calculations (increase performance) in UpperBand and LowerBand plots, because def variable values are cached (by means of increasing memory usage).

You can separate the variable definition from its value assignment. Consider the following example:

declare lower;

input index = {default SPX, "Nasdaq Composite", NDX};

def data;
switch (index) {
case SPX:
data = close("SPX");
case "Nasdaq Composite":
data = close("COMP");
case NDX:
data = close("NDX");

plot IndexCorrelation = Correlation(close, data, 10);

This example script plots the correlation between the current instrument's data and the data of a specified index. Values of the data variable are controlled by the selected index input parameter via the switch command.

Example 2. Recursive usage

def C = C[1] + volume;
plot CumulativeVolume = C;

This example script illustrates how def variable "C" references its own historical values, i.e., "C" designates a recursive variable. Here, the plot will represent cumulative volume starting from the beginning of time period.

Example 3. Non-recursive and recursive enumerations

You can define a variable as having a limited number of possible values (in which case this variable is called enumeration):

def trend = {default bullish, bearish};
if close > Average(close, 10) {
trend = trend.bullish;
} else {
trend = trend.bearish;

AssignBackgroundColor(if trend == trend.bullish then Color.UPTICK else if trend == trend.bearish then Color.DOWNTICK else Color.CURRENT);

This example script defines variable trend as having two possible values: bullish or bearish. The first value is assigned to this variable when the Close price is greater than its 10 period simple moving average and the second value otherwise. This variable controls the color of the background: it is filled with Uptick color when the value is "bullish" and Downtick otherwise. Note that enumerations can only use string values.

The example above shows non-recursive usage of enumerations, however, these can be used recursively:

def a = {default neutral, up, down};	### line 1: declaration of a def enumeration
a = if (close == close[1]) then a.neutral ### line 2: assignment of the values
else if (close > close[1]) then a.up
else if (close < close[1]) then a.down
else a[1];
plot q; ### line 6: plot declaration
switch (a) { ### line 7: switch statement
case up:
q = low;
case down:
q = high;
case neutral:
q = close;

The first line of the script is a declaration of a def enumeration a having values neutral, up, and down. The default keyword indicates the default value of the enumeration. If a value is not assigned to the declared variable, it will always be equal to its default neutral value. The value is assigned to the variable in the second line, followed by plot declaration. Enumerations can only have one assignment statement. Command switch defines what should be plotted based on the value of variable a.

Note that enumerations are not interchangeable. This means that you cannot assign same values to two different enumerations. Note also that both sides of statements in the enumeration assignment should be of the same type:

def a = {q, w, e, default r, t, y};
def b = {q, w, e, default r, t, y};
a = a.q;
b = b.q;
plot z = a[1] == a.q;
plot x = a == z; ### error in this line
plot y = a != if (1==2) then a else a.q;
plot w = if (1==2) then a else a.q != if (1==2) then b else b.q; ### error in this line