Values and types
Every value carries two labels. Its data type says what it holds. Its form type says how often it is allowed to change. This page covers the first. The second has a page of its own, const, simple and series.
The five data types
The grammar names five data types directly, and there are no others.
| Type | Written as |
|---|---|
color | #188A6B |
int | 14 |
float | 1.5 |
bool | true, false |
string | "length" |
true and false are the two boolean constants.
Colours are written directly
A colour is a value like any other. You write one as a hex literal after a hash. Six digits give the colour. Eight digits give the colour and its alpha.
color line = #188A6B
color shade = #188A6B40The color library holds the functions that build and inspect colours, and those are documented with chart output.
na, the value that is not there
na is what a name reads when there is no data behind it. Market data reads na on a bar with nothing to report. Instrument details read na where they are unknown, rather than reading zero or an empty string. Plotting an na leaves a gap on that bar instead of drawing a zero, and the colour functions hand back na when you give them one.
So a script that reads a value it did not set itself has to allow for na.
na is also a function. It takes a value of any type and returns a bool.
na(series <any_type> value) → series boolConverting a value to another type
There are five conversion functions, one per data type. Each carries one overload per form type, and the form you pass in is the form you get back: pass a series and the result is a series, pass a const and the result is a const. That is why each conversion appears four times below rather than once.
What each will accept differs:
| Function | Accepts |
|---|---|
color | a colour |
int | an int |
float | an int or a float |
bool | an int, a float or a bool |
string | a string |
color
color(const color value) → const colorcolor(input color value) → input colorcolor(series color value) → series colorcolor(simple color value) → simple colorint
int(const int value) → const intint(input int value) → input intint(series int value) → series intint(simple int value) → simple intfloat
float(const float value) → const floatfloat(input float value) → input floatfloat(series float value) → series floatfloat(simple float value) → simple floatbool
bool(const bool value) → const boolbool(input bool value) → input boolbool(series bool value) → series boolbool(simple bool value) → simple boolstring
string(const string value) → const stringstring(input string value) → input stringstring(series string value) → series stringstring(simple string value) → simple stringnz and fixnan
Two further functions take a value and hand back one of the same type.
nz takes a value and an optional alternative of the same type. It carries one overload per data type.
nz(series bool value, series bool alt) → series boolnz(series color value, series color alt) → series colornz(series float value, series float alt) → series floatnz(series int value, series int alt) → series intnz(series string value, series string alt) → series stringfixnan takes a series and hands back a series of the same type.
fixnan(series bool value) → series boolfixnan(series color value) → series colorfixnan(series float value) → series floatfixnan(series int value) → series intOnly the contract of each is documented here - what it accepts and what it returns - and not their behaviour beyond that.
A complete script
indicator("Typed values", "types", true)
color positive = #188A6B
color negative = #A6323C
bool rising = close > close[1]
plot(close, color = rising ? positive : negative)