Functions

Typed Inputs

Expose script settings with input(), covering number, select, color, slider, boolean, source, and more.

input() turns a value into a setting users can change without editing code. Each input you declare shows up in the script's settings panel, and the value you get back drives your logic. This is how you ship a configurable indicator: an adjustable period, a color picker, a source selector, an on/off toggle.

//@version=2
define(title="Configurable SMA", position="onchart", axis=true)

timeseries trade = ohlcv(symbol=currentSymbol, exchange=currentExchange)

// Surfaces as a "Length" number field in the settings panel.
var length = input(name="Length", type="number", defaultValue=20, label="Length", constraints={min: 2, max: 200, step: 1})

plotLine(value=sma(source=trade.close, period=length), colors=["#2563eb"], width=2, label=["SMA"], desc=["SMA over the user-set length"])

Every input takes a name, a type, a defaultValue, and usually a label (the text shown in the panel). Numeric and slider types accept constraints={min, max, step}. A group string buckets related inputs under a heading in the panel.

Input types

typeSurfaces asNotes
numberNumber fieldAccepts constraints={min, max, step}.
intNumber fieldInteger only; non-integer default is rejected.
floatNumber fieldDecimal value.
booleanToggle / checkboxDefault must be true or false.
stringText fieldFree-form text.
selectDropdownRequires options=[...]; default must be one of them.
multiSelectMulti-pick dropdownChoose several of the provided options.
sourceSource dropdownDefault is a source name like "ohlcv"; feed it to source(...).
colorColor pickerDefault is a color (hex or constant).
color[]Color listA list of colors.
sliderSliderNumeric; pair with constraints={min, max, step}.
timeframeTimeframe pickere.g. "1h", "1D".
sessionSession fieldA trading-session string.
symbolSymbol pickerA symbol string.
textMulti-line textLonger free-form text.

Examples by type

Number with min/max/step. An adjustable period clamped to a sensible range:

var length = input(name="Length", type="number", defaultValue=14, label="Length", constraints={min: 2, max: 100, step: 1})

Select with options. A dropdown that switches behavior:

var maType = input(name="MA Type", type="select", defaultValue="ema", label="MA Type", options=["sma", "ema"])
var ma = maType == "ema" ? ema(source=trade.close, period=length) : sma(source=trade.close, period=length)

Color picker. Let the user recolor the plot:

var lineColor = input(name="Line Color", type="color", defaultValue="#2563eb", label="Line Color")
plotLine(value=ma, colorIndex=lineColor, width=2, label=["MA"], desc=["MA in the chosen color"])

Slider. A bounded value with a draggable handle (here, line width):

var width = input(name="Width", type="slider", defaultValue=2, label="Line Width", constraints={min: 1, max: 5, step: 1})

Boolean. A simple on/off toggle:

var showFast = input(name="Show Fast", type="boolean", defaultValue=true, label="Show Fast Line")

Source. Pick which data feeds the script, then pass the name to source(...):

var src = input(name="Source", type="source", defaultValue="ohlcv", label="Source")
timeseries selected = source(type=src, symbol=currentSymbol, exchange=currentExchange)

Organizing the settings panel

The group argument places inputs under named headings, so a busy indicator stays readable. Inputs sharing a group string are shown together. The example below uses Periods, Bands, Display, and Source.

Every input type in one script

scripts/probes/inputs/all_inputs.ks
//@version=2
define(title="Verified Inputs", position="offchart", axis=true)

var numberLength = input(name="Number Length", type="number", defaultValue=12, label="Number Length", constraints={min: 2, max: 50, step: 1}, group="Periods")
var intLength = input(name="Int Length", type="int", defaultValue=14, label="Int Length", constraints={min: 2, max: 50, step: 1}, group="Periods")
var floatMultiplier = input(name="Float Multiplier", type="float", defaultValue=1.5, label="Float Multiplier", constraints={min: 0.5, max: 5, step: 0.5}, group="Bands")
var showFast = input(name="Show Fast", type="boolean", defaultValue=true, label="Show Fast", group="Display")
var labelText = input(name="Label Text", type="string", defaultValue="Close", label="Label Text", group="Display")
var averageChoice = input(name="Average", type="select", defaultValue="ema", label="Average", options=["sma", "ema"], group="Periods")
var lineColor = input(name="Line Color", type="color", defaultValue="#2563eb", label="Line Color", group="Display")
var sliderWidth = input(name="Slider Width", type="slider", defaultValue=2, label="Slider Width", constraints={min: 1, max: 5, step: 1}, group="Display")
var sourceName = input(name="Source Type", type="source", defaultValue="ohlcv", label="Source Type", group="Source")

timeseries selected = source(type=sourceName, symbol=currentSymbol, exchange=currentExchange)
var baseAverage = averageChoice == "ema" ? ema(source=selected.close, period=intLength) : sma(source=selected.close, period=numberLength)
var adjusted = baseAverage + (showFast ? floatMultiplier : 0) + (labelText == "Close" ? 0 : 1)

plotLine(value=adjusted, colorIndex=lineColor, width=sliderWidth, label=["Input result"], desc=["input driven moving average"])

Defaults are type-checked

The defaultValue must match the declared type, and the engine checks it at compile time. This catches typos before the script runs:

MistakeError
String default for a booleandefaultValue for type 'boolean' must be true or false
String default for a numberdefaultValue for type 'number' must be a number
Decimal default for an intdefaultValue for type 'int' must be an integer
String default for a floatdefaultValue for type 'float' must be a number
Number default for a stringdefaultValue for type 'string' must be a string
select default not in optionsdefaultValue '...' for type 'select' must be one of the provided options
Number default for a colordefaultValue for type 'color' must be a color
String default for a sliderdefaultValue for type 'slider' must be a number
Number default for a sourcedefaultValue for type 'source' must be a string

If you see one of these, line up the defaultValue with the type (and for select, make sure the default is one of the listed options).