ScriptingWriting LipiValues and types

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.

TypeWritten as
color#188A6B
int14
float1.5
booltrue, 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 = #188A6B40

The 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 bool

Converting 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:

FunctionAccepts
colora colour
intan int
floatan int or a float
boolan int, a float or a bool
stringa string

color

color(const color value) → const color
color(input color value) → input color
color(series color value) → series color
color(simple color value) → simple color

int

int(const int value) → const int
int(input int value) → input int
int(series int value) → series int
int(simple int value) → simple int

float

float(const float value) → const float
float(input float value) → input float
float(series float value) → series float
float(simple float value) → simple float

bool

bool(const bool value) → const bool
bool(input bool value) → input bool
bool(series bool value) → series bool
bool(simple bool value) → simple bool

string

string(const string value) → const string
string(input string value) → input string
string(series string value) → series string
string(simple string value) → simple string

nz 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 bool
nz(series color value, series color alt) → series color
nz(series float value, series float alt) → series float
nz(series int value, series int alt) → series int
nz(series string value, series string alt) → series string

fixnan takes a series and hands back a series of the same type.

fixnan(series bool value) → series bool
fixnan(series color value) → series color
fixnan(series float value) → series float
fixnan(series int value) → series int

Only 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)
Ready to put this into practice?
Try GoCharting Premium
Unlock advanced orderflow, market profile, options desk, and real-time data — everything you just read about, live in your charts.
Upgrade