Text and numbers
Writing numbers in drawing texts, and reading what happens during the calculation.
f-string
F-strings are written in the simple form, f"RSI {value}": each brace writes the value like str(). Format specs inside the braces, like {x:.2f}, aren’t used: for numbers there is format.
format
The format functions write numbers the way the chart does: decimal point, no thousands separator, and nan as empty text.
| Function | Example |
|---|---|
format.price(x) | With the symbol’s decimals: 21534 → "21534.00" |
format.quantity(x) | Like volumes: 12345 → "12.3K" |
format.percent(x) | 12.5 → "12.50%" |
format.number(x, precision) | format.number(1.23456, 2) → "1.23" |
high = bars.high[i]
draw.text((i, high), text={"value": f"High {format.price(high)}"})Integers and decimals are the same number: str(3.0) gives "3", and very large numbers are written in full. For text meant to be read, use format.
print writes to the Log tab of the console, with the calculation time and the code line; on the chart it does nothing. It helps you understand what the script calculates: you see the lines of the last calculation, up to 1000, and if the calculation stops with an error the lines written up to that point remain, with the error below.
rsi = ta.rsi(bars.close, 14)
print("last RSI", rsi[-1])