Build your first indicator
Create a configurable moving average and understand each part of the script.
This indicator draws a moving average over the chart. It is deliberately small, but it uses the same structure as larger scripts.
script "Simple Moving Average"
data chart = subscribe(data.ohlcv)
input (
length = input.int(20, title: "Length", min: 1, max: 200)
lineColor = input.color(color.blue, title: "Line Color")
)
plot average = plot.line(title: "SMA", color: lineColor, width: 2)
on chart.update {
average.plot(ta.sma(chart.close, length))
}1. Name the script
Every script starts with a name. Use something a person will recognise in their indicator list.
2. Choose the data
subscribe(data.ohlcv) gives the script the chart's candles. The chart name is local to this script; use it to read values such as chart.close, chart.high, and chart.volume.
3. Add settings
Inputs let people change the indicator without editing its source. Here they can select the averaging length and line colour.
4. Declare the output
The plot declaration creates the line and sets its default appearance. Declaring outputs near the top keeps the script's visible result easy to scan.
5. Calculate on each update
The update handler receives each new candle. ta.sma returns null until enough history is available, and the plot simply leaves those early points empty.
Next, learn how data and update events work or browse complete examples.