Appearance
Inputs
How do you let a user set your script's lengths, sources and colours without editing its text? Read each setting through an input.* call. The call returns its default until the user changes the value in the study's Settings, and then the script runs again with the new one.
pine
//@version=6
indicator("Moving average", overlay = true)
int len = input.int(20, "Length", minval = 1, group = "Average")
float src = input.source(close, "Source", group = "Average")
string kind = input.string("SMA", "Kind", options = ["SMA", "EMA"],
group = "Average")
color col = input.color(color.orange, "Line", group = "Style")
float sma = ta.sma(src, len)
float ema = ta.ema(src, len)
// a saved setting finds its input by title: keep titles unique
plot(kind == "SMA" ? sma : ema, "MA", col)An input's value is fixed for the whole run. It can't change from bar to bar, so you can use it where Pine wants a value known before the first bar, such as a length.
The kinds
Each kind gives the Settings panel its own control.
| Call | Gives | Settings shows |
|---|---|---|
input.int(14, "Length") | int | A number field. With options, a list |
input.float(2.0, "Mult") | float | A number field. With options, a list |
input.bool(true, "Show") | bool | A check box |
input.string("Fast", "Mode", options = [...]) | string | A text field. With options, a list |
input.text_area("...", "Notes") | string | A box of several lines |
input.color(color.red, "Colour") | color | A colour picker |
input.source(close, "Source") | series float | A list of the bar's prices, and other studies' plots |
input.symbol("AAPL", "Symbol") | string | A symbol search |
input.timeframe("1D", "Timeframe") | string | A timeframe list |
input.session("0930-1600", "Session") | string | Session hours |
input.time(timestamp("2024-01-01"), "Start") | int, in UNIX ms | A date and time, and a handle you drag on the chart |
input.price(100.0, "Level") | float | A price, and a handle you drag on the chart |
input.enum(Side.long, "Side") | The enum | A list of the enum's titles |
input(20, "Length") | The default's type | What that type gets |
The source list holds open, high, low, close, hl2, hlc3, hlcc4, ohlc4 and volume, in that order. It also holds the plots of the other studies on the chart, so one study can read another's output bar by bar. A plain input() takes its kind from its default, and a default of a price such as close makes it a source input.
Laying out the Settings panel
The arguments after the title shape the panel:
| Argument | Does |
|---|---|
group | Puts the input under a heading of that name. Inputs with no group sit under Inputs |
inline | Puts the inputs of one group that share this name on one row |
tooltip | Shows this text beside the input |
options | Turns the field into a list of these values |
minval, maxval, step | Bound and step a number field. A typed value out of range is refused, with a note saying the range |
display | Whether the value shows after the study's name on the chart. By default every kind shows but bool, color and input.time. display.none hides it |
confirm | Asks for the value when the study is added (below) |
TradingView's active argument, which greys an input out, is accepted and does nothing here.
How a saved setting finds its input
A value set from outside the script is an override. The Settings panel writes one, and so do saved presets and each try of an Optimizer sweep. Each override names its input by a key, and the engine tries three keys in turn:
- The input's
id, which only AmenoScript has (input.int(20, "Length", id = "len")). See AmenoScript inputs. - Its title. A title belongs to the first input that uses it, so a second input with the same title is never reached by it.
in_N, its place among the script's input calls, counting from 0. An input with no title shows in the panel asInput in_N, with N its number, as inInput in_2.
So give every input its own title. A title is a key that survives moving the input up or down the script, where in_N changes whenever you add or remove an input above it.
When you edit a script that's already on a chart, the study keeps each saved value it can still match to an input. It matches by id, then by title. An input with no title, or whose title an input above it took first, is matched by its position, and only while no input above it was added or removed. A value it can't match is dropped, and the study's legend shows how many were dropped until you next change a value.
Asking for a point when the study is added
confirm = true asks the user for the value as the study goes on the chart. A time or a price input with it asks for a click on the chart, and a toast at the chart's foot names what it wants. A time input and a price input on the same inline row make one point, set by one click.
pine
//@version=6
indicator("Level from a click", overlay = true)
// a time and a price on one inline row: one click sets both
int anchor = input.time(timestamp("2024-01-01"), "Anchor",
confirm = true, inline = "point")
float level = input.price(0.0, "Price", confirm = true, inline = "point")
plot(time >= anchor ? level : na, "Level", color.orange,
style = plot.style_linebr)After the clicks, the Confirm Inputs panel lists every input marked confirm, of any kind. Cancel or the panel's close button takes the study off the chart, and so does Esc while the chart is still asking for clicks. While it waits, the study has already run on its defaults, so its plots are hidden until you finish. A study that's selected shows a handle for each time and price input, which you can drag, and its menu's Reset points asks for every point again.
The chart asks only when a study is added fresh, from the picker, from the editor, or pasted as a bare title. A saved layout, a pasted line with values and a copy made on the chart don't ask.
Traps
- A default must be a constant. Our engine runs
input.int(bar_index > 5 ? 10 : 20)with aqualifier:warning, but TradingView refuses to compile it. - An enum input's saved value is the option's title, such as
"Long only", not the field's name. minvalandmaxvalbound what the Settings field takes. A value from a preset or a run reaches the script as it is, so test it yourself if a bad one would break the script.- Renaming an input's title loses the values saved under the old one, unless the input has an AmenoScript
id. - Two inputs with the same title share one key, and only the first is reached by it.
See also
- The input reference, every argument of every kind
- AmenoScript inputs:
id,sweepandinput.check - Enums, for
input.enum - Time and sessions, for what
input.timeandinput.sessionhold