Your first indicator, in six lines
Write, run and save a script today.
Pine Script is TradingView's own language. It runs on their servers, over the bars of your chart, and it exists to do exactly one thing: turn data into something drawn on a chart. It is small on purpose.
- 1Open the Pine Editor tab at the bottom of the chart.
- 2Delete everything that is there.
- 3Paste the code below.
- 4Click 'Add to chart'. Save first if you want it in 'My scripts'.
//@version=6
indicator("Mi primera media", overlay = true)
int periodo = input.int(200, "Periodo", minval = 1)
float media = ta.sma(close, periodo)
plot(media, "Media", color.new(color.orange, 0), 2)Six lines: version, declaration, an input the user can change, the calculation, and the drawing.
- ▸//@version=6 must be the first line. It picks the language version, and v5 code does not run under v6 rules.
- ▸indicator(...) declares the script. overlay = true draws on the price chart; false opens a separate panel below.
- ▸input.int(...) creates a field in the settings gear. Anything a user might want to change belongs in an input, not hardcoded.
- ▸plot(...) is the output. A script with no plot, table or label produces nothing visible.
Key idea
The mental model that makes everything else click: your code does not run once. It runs once per bar, from the oldest to the newest, and every variable holds the value it had on that bar. When you write close you mean 'the close of the bar being processed right now'.