built in types
The Built-in Types
allow you to use various ranges of data in your script to make it more efficient and precise. Depending on the purpose of the types, they can be grouped in the following categories.
Series
The series provides a range of historical price data, the timeframe of which you indicate in the script parameters. For example:
Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|---|
close | 15.25 | 15.46 | 15.35 | 15.03 | 15.02 | 14.80 | 15.01 | 12.87 | 12.53 | 12.43 |
close[1] | nil | 15.25 | 15.46 | 15.35 | 15.03 | 15.02 | 14.80 | 15.01 | 12.87 | 12.53 |
close[2] | nil | nil | 15.25 | 15.46 | 15.35 | 15.03 | 15.02 | 14.80 | 15.01 | 12.87 |
close[3] | nil | nil | nil | 15.25 | 15.46 | 15.35 | 15.03 | 15.02 | 14.80 | 15.01 |
Meaning that with the close[1] index, the data of the previously closed candle is returned. The close[2] series will return the value of 2 candles back, and so on. This data will help you create scripts, based on the analysis of the historical price values.
More details on the Series are here.
Values
Integer
In case you need to indicate a whole number, you can use the Integer
built-in type, which is a 64-bit signed integer value. Such type is usually used while declaring the period for the indicator, for example:
indicator_period = input(14, "Indicator Period")
You can also use this built-in type in any script settings, requiring whole numbers, such as:
- The rounded results of the mathematical operations (see Series);
- Price levels;
- Loops, etc.
Numeric
In case you need to indicate a decimal, you can use the Numeric
built-in type, which is a 64-bit floating-point value. With this type, you can declare both integer and decimal values.
For example, you need to declare a decimal variable:
value = 3.14
It is also supported in the mathematical operations:
hlc3 = (high + low + close) / 3
String
The String
type supports values in the text format. With this type, you can indicate the titles of the indicators, their components, as well as writing messages and notifications within the scripts. In order to use this type, the text should be enclosed in quotation marks. For example:
instrument { name = "SMA", overlay = true }
Where "SMA"
is a string value, and true
is a boolean value (See Boolean).
Boolean
The Boolean
type contains two opposite values – True and False*. The most basic example of using this type – visual representation of the indicator in the traderoom. For example:
instrument { name = "ALMA", overlay = true }
Where the true
value indicates the position of your indicator. In the example above, it is a line, moving along with the asset chart.
The boolean value should be declared in the text format and without quotation marks.
*Lua peculiarities: by default, the condition is considered to be false when its value is false or nil. In some cases, that are explicitly documented, the 0 value is considered to be false.
Visualization
Color
To add color customization to your script, you can use the Color
type, declaring the colors in the following formats:
"#RRGGBB"
"#RRGGBB"
"#RRGGBBAA"
"rgb(255,255,255)"
"rgb(255,255,255,1.0)"
- HTML color name.
For example:
plot(close, title="Close Price", color=color.blue)
In case you want your indicator to change color depending on the conditions, you can mention it in the script, for example:
input_group {
"Candle colors",
bullish_color = input { default = "#2E77D1", name = "Bullish Candle", type = input.color },
bearish_color = input { default = "#E74C3C", name = "Bearish Candle", type = input.color }
}
plot_candle(ha_open, ha_high, ha_low, ha_close, "Heikin Ashi Candles", ha_open < ha_close and bullish_color or bearish_color)
In case you would like to use customized colors, the fastest way is to use the rgb
and rgba
functions.