Pine Script v6 basics: a syntax primer with examples that run
Pine Script v6 basics for SuperCharting: series, history, inputs, var, if and switch, functions, loops and higher timeframes, with four examples that compile.
Pine Script v6 is a small language for chart calculations: your script runs once on every bar, from the oldest to the newest, and each line works on that bar's values. SuperCharting runs Pine-compatible scripts in this version 6 syntax, so the basics below apply directly. Every example on this page compiles in the SuperCharting Script Editor and runs on a chart.
Pine Script is TradingView's language. SuperCharting supports the same syntax but is a separate product.
Every script starts by naming its version and saying what kind of script it is:
//@version=6on the first code line.indicator("Name", overlay=true)for a study that draws on the chart, orstrategy("Name", ...)for a script that places simulated trades for the Strategy Tester.
overlay=true draws on the price pane. overlay=false (the default) gives the script its own pane under the chart, which suits oscillators such as RSI.
Series and history: how a script sees bars
Built-in values such as open, high, low, close, volume and time are series: they have one value per bar. The square-bracket operator looks back in history: close[1] is the previous bar's close and close[10] is ten bars ago.
//@version=6
indicator("Basics: series and history", overlay=false)
change = close - close[1]
pct = 100 * change / close[1]
plot(pct, "Change %", color=pct >= 0 ? color.green : color.red, style=plot.style_columns)
hline(0, "Zero")
On a daily NSE:RELIANCE chart this draws each day's percentage change as columns, green when the stock closed higher and red when it closed lower. The ? : operator is a short if-else: condition ? value_if_true : value_if_false.
On the first bar there is no previous bar, so close[1] is na (no value) and nothing is drawn there. Pine handles na quietly in most maths, which is why the first bars of a 20-bar average are empty.
Inputs, functions and tuples
input.* calls become settings the user can change in the indicator's Settings dialog without touching the code: input.int, input.float, input.bool, input.string, input.source, input.time and others. A function is written with =>, and it can return several values at once as a tuple in square brackets.
//@version=6
indicator("Basics: inputs and functions", overlay=true)
length = input.int(20, "Length", minval=2)
mult = input.float(2.0, "Width", step=0.1)
band(src, len, k) =>
mid = ta.sma(src, len)
dev = k * ta.stdev(src, len)
[mid, mid + dev, mid - dev]
[mid, upper, lower] = band(close, length, mult)
plot(mid, "Middle")
plot(upper, "Upper", color=color.gray)
plot(lower, "Lower", color=color.gray)
This is a hand-made Bollinger-style band. Indentation matters: the function body is indented by four spaces (or one tab). The ta. namespace holds the usual technical functions: ta.sma, ta.ema, ta.rsi, ta.atr, ta.stdev, ta.crossover, ta.highest and many more.
var, if, switch and reassignment
A normal variable is calculated again on every bar. A var variable is set once and then keeps its value from bar to bar until you change it with :=. That is how you carry a running value, such as the high of the day so far.
//@version=6
indicator("Basics: var and conditions", overlay=true)
var float dayHigh = na
if ta.change(time("D")) != 0
dayHigh := high
else
dayHigh := math.max(dayHigh, high)
trend = switch
close > ta.ema(close, 50) => 1
close < ta.ema(close, 50) => -1
=> 0
plot(dayHigh, "Day high so far", color=color.purple, style=plot.style_stepline)
bgcolor(trend == 1 ? color.new(color.green, 92) : trend == -1 ? color.new(color.red, 92) : na)
Put it on a 5-minute XAUUSD chart: the purple step line resets at each new day and climbs with every new high, and the background is tinted by where price sits against the 50-bar EMA. Three things to remember:
=declares a variable;:=changes one that already exists.ifandelseblocks are indented, with no braces.switchwith no value tests each condition in order and takes the first that is true; the bare=>line is the default.
Loops and higher timeframes
for i = 0 to 9 repeats a block with i from 0 to 9. request.security reads a value from another timeframe of the chart's symbol, for example the daily close on an intraday chart.
//@version=6
indicator("Basics: higher timeframe and loops", overlay=true)
dailyClose = request.security(syminfo.tickerid, "D", close[1], lookahead=barmerge.lookahead_on)
plot(dailyClose, "Previous daily close", color=color.blue, style=plot.style_linebr)
upCount = 0
for i = 0 to 9
if close[i] > open[i]
upCount += 1
plotchar(upCount >= 7, "Strong run", "▲", location.belowbar, color.green)
On a 1-hour EURUSD chart the blue line is yesterday's daily close, and a green ▲ marks bars where at least 7 of the last 10 bars closed higher than they opened. Using close[1] with lookahead_on takes the completed previous day, so the line does not change once the day is over.
How to try these examples in SuperCharting
- Open a chart, for example NSE:NIFTY on 15 minutes, and press Ctrl + E to open the Script Editor.
- Select all the template text and paste one example.
- Wait for the console to show the ✓ line with the script's title, then click Add to chart.
- Open the indicator's settings from its name in the chart legend to change the inputs.
- Click Save to keep the script in your account.
If the console shows ✗ with a line number, check the indentation and brackets on that line first: they cause most errors.
What works and what to check
The engine covers the core language: series and history, var and varip, user functions, if, switch, for and while, tuples, user-defined types and enums, arrays and maps, and the ta., math., str., color., input., array., map., label., line., box. and table. namespaces, plus the plot functions and alertcondition. Two honest notes:
request.securityis tested here for higher timeframes of the chart's own symbol, as in the example above. If you request other symbols or unusual expressions, compare the result with values you know.- Scripts run on SuperCharting's servers, so very heavy scripts over many thousands of bars take longer to refresh.
Next, turn these pieces into a complete script in write your first indicator, then try a first strategy. Writing and running scripts is part of every account, and SuperCharting is currently free.
Frequently asked questions
Do I need to write //@version=6?
Yes, put it at the top. The editor's status bar shows the version it found, for example "Script v6".
What is the difference between = and :=?
= creates a variable on that line. := gives a new value to a variable that already exists, which you need for var variables and inside if blocks.
Why are the first bars of my plot empty?
Functions such as ta.sma(close, 20) need 20 bars before they have a value, and close[1] has no value on the very first bar. Those bars hold na and are not drawn.
Can I use another indicator's output in my script?
Inside one script, yes: calculate both in the same code. Across scripts, use an input.source and pick the other indicator's plot in the settings, as explained in indicators on indicators.