The type for numbers with a fractional part: prices, ratios and indicator values. A float can be na.
A float is a 64-bit floating-point number. Price series (open, high, low, close), volume and almost every ta.* result are floats. A literal with a decimal point or an exponent, such as 1.5, .25 or 2e3, is a float.
Arithmetic on floats gives a float, and dividing two ints also gives a float, so 7 / 2 is 3.5. Any calculation that touches na returns na. Use nz() to replace na with 0 (or another default) and na() to test for it.
Remarks
- float(x) casts an int to a float and keeps na as na. Declare a float variable that starts empty with float x = na.
- Comparing floats with == is fragile because of rounding. Compare with a tolerance, or use
math.round()first, when you need a match. - On Sidechair: na is held internally as NaN. float(x) returns na for na and the numeric value otherwise.
Example
//@version=6
indicator("Percent change")
pct = (close / close[1] - 1) * 100
plot(pct, "Percent", pct >= 0 ? color.green : color.red, style = plot.style_columns)
plot(int(pct), "Whole part", color.orange)See also
Runs on Sidechair