History-referencing operator ([])
The history-referencing operator allows you to fetch
previous bar values. The history-referencing is used after
a variable or function call. Inside square brackets, you
have to pass an integer value, which refers to the offset
in the past. For example, if you want to fetch closing price
value two bars in the past, you would use close[2].
The following script calculates a simple moving average
of length three using the history-referencing operator.
//@version=5
indicator("My script")
past_close1 = close[0]
past_close2 = close[1]
past_close3 = close[2]
sma_3 = (past_close1 + past_close2 + past_close3)/3
plot(sma_3)
Switch Statements
color plot_color = switch
high > 176 => color.yellow
close > open => color.green
close < open => color.red
plot(close, color = plot_color)
For Loop
You should use the for loop when you know the number
of iterations in advance for which you want to execute a
piece of code.
The following script shows how to calculate a simple
moving average of length 10 using a for loop.
//@version=5
indicator("My script")
num_past_bars = 10
sum_10 = 0.0
for i = 0 to num_past_bars - 1
sum_10 := sum_10 + close[i]
sma_10 = sum_10/10
plot(sma_10)
Built-in Variables
https://www.tradingview.com/pine-script-docs/en/v5/language/Built-ins.html#built-in-variables
Single-line Functions
//@version=5
indicator("My script")
close_open_ratio(close_val, open_val, vol_val) => (close_val/open_val) * vol_val
ratio = close_open_ratio(close, open, volume)
plot(ratio)
Multi-line Functions
You can define a function in multiple lines, hence the
multi-line function.
//@version=5
indicator("My script")
close_open_ratio(close_val, open_val, vol_val) =>
ratio = (close_val/open_val) * vol_val
sqrt_ratio = math.sqrt(ratio)
random = math.random() * sqrt_ratio
ratio = close_open_ratio(close, open, volume)
plot(ratio)
Functions Returning Multiple Values
You can return multiple values from a Pine Script
function. To do so, pass the values to return inside square
brackets and separate them by commas.
Likewise, to receive multiple values from a function, pass
receiving variables inside square brackets and separate
them by commas.
//@version=5
indicator("My script")
close_open_ratio(close_val, open_val, vol_val) =>
ratio = (close_val/open_val) * vol_val
sqrt_ratio = math.sqrt(ratio)
random = math.random() * sqrt_ratio
[sqrt_ratio, random]
[ratio, random] = close_open_ratio(close, open, volume)
plot(ratio, title = "ratio", color = color.red)
plot(random, title = "random", color = color.green)
Getting User Inputs
Creating Indicators
shorttitle attribute specifies the short title of the plot.
format attribute set format of your plot.
format.volume, you will see volume values in K (for
thousands) and M (for millions).
linewidth - width of the line plot
style – change plot style
show_last attribute displays the last N bars for a plot
offset attribute shifts a plot to the right or left. In the
following script, we offset the green close plot by -10,
moving it 10 bars to the left.
//@version=5
indicator("My script", shorttitle = "SPSC")
plot(close, color = color.yellow, linewidth = 3)
plot(open, color = color.red, style = plot.style_circles, show_last = 20)
plot(close, color = color.green, offset = -10)
request.security()
barmerge.gaps_off means no gaps are displayed
between bars
Creating Strategies