Lesson 2: Settings people can change
Give your script settings, step by step. Numbers with limits, a switch, a colour, the price it reads, a list of choices, and groups.
Lesson 2 of 18. A setting lets you, or anyone who uses the script, change it from the settings panel without opening the code. In the language a setting is an input. In this lesson you add settings to a small channel indicator one step at a time.
A number with limits#
indicator "Channel"
edition = 1
display = price
input length: int = 20, name = "Length", min = 2, max = 500
input width: float = 1.5, name = "Width", min = 0.1, step = 0.1
let middle = tech.sma(close, length)
let band = width * tech.atr(length)
plot(middle + band, name = "Upper", color = color.blue)
plot(middle - band, name = "Lower", color = color.blue)
plot(middle, name = "Middle", color = color.gray)input length: int = 20declares a setting calledlength.intis a whole number;floatis a number with decimals.name = "Length"is the label in the settings panel.minandmaxare the smallest and largest value the panel accepts.stepis how much the arrows in the field add or take away.- Inside the script
lengthandwidthare plain numbers. When you change a setting, the script runs again from the first bar with the new value.
tech.atr(length) is the average true range, the usual size of a bar. The channel is the average plus and minus width times that size.
A switch, a colour and the price it reads#
indicator "Channel"
edition = 1
display = price
input src: source = close, name = "Source"
input length: int = 20, name = "Length", min = 2, max = 500
input width: float = 1.5, name = "Width", min = 0.1, step = 0.1
input show_middle: bool = true, name = "Show the middle line"
input band_color: color = color.blue, name = "Band colour"
let middle = tech.sma(src, length)
let band = width * tech.atr(length)
plot(middle + band, name = "Upper", color = band_color)
plot(middle - band, name = "Lower", color = band_color)
plot(if show_middle then middle else na, name = "Middle", color = color.gray)- A
sourceinput lets the member choose which price the average reads:close,open,high,low,hl2(the middle of the bar) and the others listed in Inputs. - A
boolinput is a switch,trueorfalse.if show_middle then middle else nadraws the middle line only while the switch is on:nameans "no value", and a plot draws nothing where its value isna. - A
colorinput opens a colour picker.
A list of choices#
indicator "Channel"
edition = 1
display = price
input kind: text = "SMA", name = "Average", options = ["SMA", "EMA"]
input length: int = 20, name = "Length", min = 2, max = 500
input width: float = 1.5, name = "Width", min = 0.1, step = 0.1
let sma = tech.sma(close, length)
let ema = tech.ema(close, length)
let middle = if kind == "SMA" then sma else ema
let band = width * tech.atr(length)
plot(middle + band, name = "Upper", color = color.blue)
plot(middle - band, name = "Lower", color = color.blue)
plot(middle, name = "Middle", color = color.gray)options = [...] turns a setting into a dropdown. The default must be one of the choices. Both averages are worked out on every bar and the dropdown only chooses which one is drawn: a function such as tech.ema keeps its own memory from bar to bar, so it is written on a line of its own, not inside the if.
Groups and tooltips#
indicator "Channel"
edition = 1
display = price
group "Average"
/// How many bars the average and the band look at
input length: int = 20, name = "Length", min = 2, max = 500
input src: source = close, name = "Source"
group "Band"
input width: float = 1.5, name = "Width", min = 0.1, step = 0.1
input band_color: color = color.blue, name = "Colour"
let middle = tech.sma(src, length)
let band = width * tech.atr(length)
plot(middle + band, name = "Upper", color = band_color)
plot(middle - band, name = "Lower", color = band_color)
plot(middle, name = "Middle", color = color.gray)group "Average" puts the inputs indented under it together under that heading. A line that starts with /// just above an input is its tooltip, the short help shown beside the field.
Try changing#
- In the first program, change
max = 500tomax = 50, save, and open the settings: the field stops at 50. - In "A switch, a colour and the price it reads", give
show_middlethe defaultfalse: the middle line stays hidden until the switch is turned on. - In "A list of choices", add a third choice: write
options = ["SMA", "EMA", "WMA"], addlet wma = tech.wma(close, length)and choose withif kind == "SMA" then sma else if kind == "EMA" then ema else wma. - In "Groups and tooltips", move
input srcundergroup "Band"and look at the settings panel again. - Add the tooltip line
/// The colour of both band linesjust aboveinput band_color.
Checkpoint#
- You can declare
int,float,bool,color,sourceandtextinputs. - You can limit a number with
min,maxandstep, and offer choices withoptions. - You can show a plot only while a switch is on, with
if ... then ... else na. - You can arrange the settings with
groupand give an input a tooltip with///.
See also: Inputs, Your first script.