Pinescript Source Library
Tradingview's native programming language is Pinescript. It's latest version, Pinescript v4 enables traders to build their own strategy or indicator.

Full Pinescript Guide

Coming soon.

Pinescript Resources

Pinescript Is The Native Programming Language Of Tradingview.Com. Users Are Able To Build Their Own Indicators And Strategies Based On Technical Calculations And Plot Them All In Pine. We Will Continuously Add To The Scripts Listed Here So We Encourage You To Check Back From Time To Time. These Resources Are Also Available To Those Interested In Learning.

Pine Templates

Updated Daily

Source Code Library

Accumulation/Distribution

1
//@version=4
2
study(title="Accumulation/Distribution", shorttitle="Accum/Dist", format=format.volume, overlay=false, resolution="")
3
ad = cum(close==high and close==low or high==low ? 0 : ((2*close-low-high)/(high-low))*volume)
4
plot(ad, title = "Accumulation/Distribution", color=#999915)
Copied!

Advance/Decline Line

1
//@version=4
2
study(title = "Advance Decline Line", shorttitle="ADL", format=format.price, precision=2)
3
sym(s) => security(s, timeframe.period, close)
4
difference = (sym("USI:ADVN.NY") - sym("USI:DECL.NY"))/(sym("USI:UNCH.NY") + 1)
5
adline = cum(difference > 0 ? sqrt(difference) : -sqrt(-difference))
6
plot(adline)
Copied!

Auto-Fibonacci Retracement

1
//@version=4
2
study("Auto Fib Retracement", overlay=true)
3
4
// pivots threshold
5
threshold_multiplier = input(title="Deviation", type=input.float, defval=3, minval=0)
6
dev_threshold = atr(10) / close * 100 * threshold_multiplier
7
8
depth = input(title="Depth", type=input.integer, defval=10, minval=1)
9
var extendLeft = input(false, "Extend Lines Left")
10
var extendRight = input(true, "Extend Lines Right")
11
12
var extending = extend.none
13
if extendLeft and extendRight
14
extending := extend.both
15
if extendLeft and not extendRight
16
extending := extend.left
17
if not extendLeft and extendRight
18
extending := extend.right
19
20
reverse = input(false, "Reverse")
21
prices = input(true, "Prices")
22
levels = input(true, "Levels")
23
levelsFormat = input("Values", "Levels Format", options = ["Values", "Percent"])
24
labelsPosition = input("Left", "Labels Position", options = ["Left", "Right"])
25
26
var line lineLast = na
27
var int iLast = 0
28
var int iPrev = 0
29
var float pLast = 0
30
var isHighLast = false // otherwise the last pivot is a low pivot
31
32
pivots(src, length, isHigh) =>
33
l2 = length * 2
34
c = nz(src[length])
35
ok = true
36
for i = 0 to l2
37
if isHigh and src[i] > c
38
ok := false
39
40
if not isHigh and src[i] < c
41
ok := false
42
if ok
43
[bar_index[length], c]
44
else
45
[int(na), float(na)]
46
[iH, pH] = pivots(high, depth / 2, true)
47
[iL, pL] = pivots(low, depth / 2, false)
48
49
calc_dev(base_price, price) =>
50
100 * (price - base_price) / price
51
52
pivotFound(dev, isHigh, index, price) =>
53
if isHighLast == isHigh and not na(lineLast)
54
// same direction
55
if isHighLast ? price > pLast : price < pLast
56
line.set_xy2(lineLast, index, price)
57
[lineLast, isHighLast]
58
else
59
[line(na), bool(na)]
60
else // reverse the direction (or create the very first line)
61
if abs(dev) > dev_threshold
62
// price move is significant
63
id = line.new(iLast, pLast, index, price, color=color.gray, width=1, style=line.style_dashed)
64
[id, isHigh]
65
else
66
[line(na), bool(na)]
67
68
if not na(iH)
69
dev = calc_dev(pLast, pH)
70
[id, isHigh] = pivotFound(dev, true, iH, pH)
71
if not na(id)
72
if id != lineLast
73
line.delete(lineLast)
74
lineLast := id
75
isHighLast := isHigh
76
iPrev := iLast
77
iLast := iH
78
pLast := pH
79
else
80
if not na(iL)
81
dev = calc_dev(pLast, pL)
82
[id, isHigh] = pivotFound(dev, false, iL, pL)
83
if not na(id)
84
if id != lineLast
85
line.delete(lineLast)
86
lineLast := id
87
isHighLast := isHigh
88
iPrev := iLast
89
iLast := iL
90
pLast := pL
91
92
_draw_line(price, col) =>
93
var id = line.new(iLast, price, bar_index, price, color=col, width=1, extend=extending)
94
if not na(lineLast)
95
line.set_xy1(id, line.get_x1(lineLast), price)
96
line.set_xy2(id, line.get_x2(lineLast), price)
97
98
99
_draw_label(price, txt, txtColor) =>
100
x = labelsPosition == "Left" ? line.get_x1(lineLast) : line.get_x2(lineLast)
101
var id = label.new(x=x, y=price, text=txt, textcolor=txtColor, style=label.style_none)
102
label.set_xy(id, x, price)
103
label.set_text(id, txt)
104
label.set_textcolor(id, txtColor)
105
106
_wrap(txt) =>
107
"(" + tostring(txt, "#.##") + ")"
108
109
_label_txt(level, price) =>
110
l = levelsFormat == "Values" ? tostring(level) : tostring(level * 100) + "%"
111
(levels ? l : "") + (prices ? _wrap(price) : "")
112
113
sl1 = input(true, "0")
114
vall1 = 0.000
115
116
sl2 = input(true, "0.236")
117
vall2 = 0.236
118
119
sl3 = input(true, "0.382")
120
vall3 = 0.382
121
122
sl4 = input(true, "0.5")
123
vall4 = 0.5
124
125
sl5 = input(true, "0.618")
126
vall5 = 0.618
127
128
sl6 = input(true, "0.786")
129
vall6 = 0.786
130
131
sl7 = input(true, "1")
132
vall7 = 1
133
134
sl8 = input(true, "1.272")
135
vall8 = 1.272
136
137
sl9 = input(true, "1.414")
138
vall9 = 1.414
139
140
sl10 = input(true, "1.618")
141
vall10 = 1.618
142
143
sl11 = input(false, "2.618")
144
vall11 = 2.618
145
146
sl12 = input(false, "3.618")
147
vall12 = 3.618
148
149
sl13 = input(false, "4.236")
150
vall13 = 4.236
151
152
sl14 = input(false, "-0.236")
153
vall14 = -0.236
154
155
sl15 = input(false, "-0.382")
156
vall15 = -0.382
157
158
sl16 = input(false, "-0.618")
159
vall16 = -0.618
160
161
_draw_retracement(startPrice, endPrice) =>
162
iHL = startPrice > endPrice
163
diff = (iHL ? -1 : 1) * abs(startPrice - endPrice)
164
if sl1
165
l1 = startPrice + diff * vall1
166
_draw_line(l1, #808080)
167
_draw_label(l1, _label_txt(vall1, l1), #808080)
168
169
if sl2
170
l2 = startPrice + diff * vall2
171
_draw_line(l2, #a61c00)
172
_draw_label(l2, _label_txt(vall2, l2), #a61c00)
173
174
if sl3
175
l3 = startPrice + diff * vall3
176
_draw_line(l3, #95cc28)
177
_draw_label(l3, _label_txt(vall3, l3), #95cc28)
178
179
if sl4
180
l4 = startPrice + diff * vall4
181
_draw_line(l4, #28cc28)
182
_draw_label(l4, _label_txt(vall4, l4), #28cc28)
183
184
if sl5
185
l5 = startPrice + diff * vall5
186
_draw_line(l5, #28cc95)
187
_draw_label(l5, _label_txt(vall5, l5), #28cc95)
188
189
if sl6
190
l6 = startPrice + diff * vall6
191
_draw_line(l6, #2895cc)
192
_draw_label(l6, _label_txt(vall6, l6), #2895cc)
193
194
if sl7
195
l7 = startPrice + diff * vall7
196
_draw_line(l7, #808080)
197
_draw_label(l7, _label_txt(vall7, l7), #808080)
198
199
if sl8
200
l8 = startPrice + diff * vall8
201
_draw_line(l8, #82CA89)
202
_draw_label(l8, _label_txt(vall8, l8), #82CA89)
203
204
if sl9
205
l9 = startPrice + diff * vall9
206
_draw_line(l9, #F32C42)
207
_draw_label(l9, _label_txt(vall9, l9), #F32C42)
208
209
if sl10
210
l10 = startPrice + diff * vall10
211
_draw_line(l10, #2796ED)
212
_draw_label(l10, _label_txt(vall10, l10), #2796ED)
213
214
if sl11
215
l11 = startPrice + diff * vall11
216
_draw_line(l11, #a61c00)
217
_draw_label(l11, _label_txt(vall11, l11), #a61c00)
218
219
if sl12
220
l12 = startPrice + diff * vall12
221
_draw_line(l12, #9B03AE)
222
_draw_label(l12, _label_txt(vall12, l12), #9B03AE)
223
224
if sl13
225
l13 = startPrice + diff * vall13
226
_draw_line(l13, #E80065)
227
_draw_label(l13, _label_txt(vall13, l13), #E80065)
228
229
if sl14
230
l14 = startPrice + diff * vall14
231
_draw_line(l14, #a61c00)
232
_draw_label(l14, _label_txt(vall14, l14), #a61c00)
233
234
if sl15
235
l15 = startPrice + diff * vall15
236
_draw_line(l15, #95cc28)
237
_draw_label(l15, _label_txt(vall15, l15), #95cc28)
238
239
if sl16
240
l16 = startPrice + diff * vall16
241
_draw_line(l16, #28cc95)
242
_draw_label(l16, _label_txt(vall16, l16), #28cc95)
243
244
p1 = reverse ? line.get_y1(lineLast) : pLast
245
p2 = reverse ? pLast : line.get_y1(lineLast)
246
_draw_retracement(p1, p2)
Copied!

Zig-Zag

1
//@version=4
2
study("Zig Zag", overlay=true)
3
4
dev_threshold = input(title="Deviation (%)", type=input.float, defval=5, minval=1, maxval=100)
5
depth = input(title="Depth", type=input.integer, defval=10, minval=1)
6
7
pivots(src, length, isHigh) =>
8
p = nz(src[length])
9
10
if length == 0
11
[bar_index, p]
12
else
13
isFound = true
14
for i = 0 to length - 1
15
if isHigh and src[i] > p
16
isFound := false
17
if not isHigh and src[i] < p
18
isFound := false
19
20
for i = length + 1 to 2 * length
21
if isHigh and src[i] >= p
22
isFound := false
23
if not isHigh and src[i] <= p
24
isFound := false
25
26
if isFound and length * 2 <= bar_index
27
[bar_index[length], p]
28
else
29
[int(na), float(na)]
30
31
[iH, pH] = pivots(high, floor(depth / 2), true)
32
[iL, pL] = pivots(low, floor(depth / 2), false)
33
34
calc_dev(base_price, price) =>
35
100 * (price - base_price) / base_price
36
37
var line lineLast = na
38
var int iLast = 0
39
var float pLast = 0
40
var bool isHighLast = true // otherwise the last pivot is a low pivot
41
var int linesCount = 0
42
43
pivotFound(dev, isHigh, index, price) =>
44
if isHighLast == isHigh and not na(lineLast)
45
// same direction
46
if isHighLast ? price > pLast : price < pLast
47
if linesCount <= 1
48
line.set_xy1(lineLast, index, price)
49
line.set_xy2(lineLast, index, price)
50
[lineLast, isHighLast, false]
51
else
52
[line(na), bool(na), false]
53
else // reverse the direction (or create the very first line)
54
if na(lineLast)
55
id = line.new(index, price, index, price, color=color.red, width=2)
56
[id, isHigh, true]
57
else
58
// price move is significant
59
if abs(dev) >= dev_threshold
60
id = line.new(iLast, pLast, index, price, color=color.red, width=2)
61
[id, isHigh, true]
62
else
63
[line(na), bool(na), false]
64
65
if not na(iH) and not na(iL) and iH == iL
66
dev1 = calc_dev(pLast, pH)
67
[id2, isHigh2, isNew2] = pivotFound(dev1, true, iH, pH)
68
if isNew2
69
linesCount := linesCount + 1
70
if not na(id2)
71
lineLast := id2
72
isHighLast := isHigh2
73
iLast := iH
74
pLast := pH
75
76
dev2 = calc_dev(pLast, pL)
77
[id1, isHigh1, isNew1] = pivotFound(dev2, false, iL, pL)
78
if isNew1
79
linesCount := linesCount + 1
80
if not na(id1)
81
lineLast := id1
82
isHighLast := isHigh1
83
iLast := iL
84
pLast := pL
85
86
else
87
if not na(iH)
88
dev1 = calc_dev(pLast, pH)
89
[id, isHigh, isNew] = pivotFound(dev1, true, iH, pH)
90
if isNew
91
linesCount := linesCount + 1
92
if not na(id)
93
lineLast := id
94
isHighLast := isHigh
95
iLast := iH
96
pLast := pH
97
else
98
if not na(iL)
99
dev2 = calc_dev(pLast, pL)
100
[id, isHigh, isNew] = pivotFound(dev2, false, iL, pL)
101
if isNew
102
linesCount := linesCount + 1
103
if not na(id)
104
lineLast := id
105
isHighLast := isHigh
106
iLast := iL
107
pLast := pL
108
Copied!

Linear Regression

1
//@version=4
2
study("Linear Regression", shorttitle="LinReg", overlay=true)
3
4
upperMult = input(title="Upper Deviation", defval=2)
5
lowerMult = input(title="Lower Deviation", defval=-2)
6
7
useUpperDev = input(title="Use Upper Deviation", defval=true)
8
useLowerDev = input(title="Use Lower Deviation", defval=true)
9
showPearson = input(title="Show Pearson's R", defval=true)
10
extendLines = input(title="Extend Lines", defval=false)
11
12
len = input(title="Count", defval=100)
13
src = input(title="Source", defval=close)
14
15
extend = extendLines ? extend.right : extend.none
16
17
calcSlope(src, len) =>
18
max_bars_back(src, 300)
19
if not barstate.islast or len <= 1
20
[float(na), float(na), float(na)]
21
else
22
sumX = 0.0
23
sumY = 0.0
24
sumXSqr = 0.0
25
sumXY = 0.0
26
for i = 0 to len - 1
27
val = src[i]
28
per = i + 1.0
29
sumX := sumX + per
30
sumY := sumY + val
31
sumXSqr := sumXSqr + per * per
32
sumXY := sumXY + val * per
33
slope = (len * sumXY - sumX * sumY) / (len * sumXSqr - sumX * sumX)
34
average = sumY / len
35
intercept = average - slope * sumX / len + slope
36
[slope, average, intercept]
37
38
[s, a, i] = calcSlope(src, len)
39
40
startPrice = i + s * (len - 1)
41
endPrice = i
42
var line baseLine = na
43
44
if na(baseLine) and not na(startPrice)
45
baseLine := line.new(bar_index - len + 1, startPrice, bar_index, endPrice, width=1, extend=extend, color=color.red)
46
else
47
line.set_xy1(baseLine, bar_index - len + 1, startPrice)
48
line.set_xy2(baseLine, bar_index, endPrice)
49
na
50
51
calcDev(src, len, slope, average, intercept) =>
52
upDev = 0.0
53
dnDev = 0.0
54
stdDevAcc = 0.0
55
dsxx = 0.0
56
dsyy = 0.0
57
dsxy = 0.0
58
59
periods = len - 1
60
61
daY = intercept + (slope * periods) / 2
62
val = intercept
63
64
for i = 0 to periods
65
price = high[i] - val
66
if (price > upDev)
67
upDev := price
68
69
price := val - low[i]
70
if (price > dnDev)
71
dnDev := price
72
73
price := src[i]
74
dxt = price - average
75
dyt = val - daY
76
77
price := price - val
78
stdDevAcc := stdDevAcc + price * price
79
dsxx := dsxx + dxt * dxt
80
dsyy := dsyy + dyt * dyt
81
dsxy := dsxy + dxt * dyt
82
val := val + slope
83
84
stdDev = sqrt(stdDevAcc / (periods == 0 ? 1 : periods))
85
pearsonR = dsxx == 0 or dsyy == 0 ? 0 : dsxy / sqrt(dsxx * dsyy)
86
[stdDev, pearsonR, upDev, dnDev]
87
88
[stdDev, pearsonR, upDev, dnDev] = calcDev(src, len, s, a, i)
89
90
upperStartPrice = startPrice + (useUpperDev ? upperMult * stdDev : upDev)
91
upperEndPrice = endPrice + (useUpperDev ? upperMult * stdDev : upDev)
92
var line upper = na
93
94
lowerStartPrice = startPrice + (useLowerDev ? lowerMult * stdDev : -dnDev)
95
lowerEndPrice = endPrice + (useLowerDev ? lowerMult * stdDev : -dnDev)
96
var line lower = na
97
98
if na(upper) and not na(upperStartPrice)
99
upper := line.new(bar_index - len + 1, upperStartPrice, bar_index, upperEndPrice, width=1, extend=extend, color=#0000ff)
100
else
101
line.set_xy1(upper, bar_index - len + 1, upperStartPrice)
102
line.set_xy2(upper, bar_index, upperEndPrice)
103
na
104
105
if na(lower) and not na(lowerStartPrice)
106
lower := line.new(bar_index - len + 1, lowerStartPrice, bar_index, lowerEndPrice, width=1, extend=extend, color=#0000ff)
107
else
108
line.set_xy1(lower, bar_index - len + 1, lowerStartPrice)
109
line.set_xy2(lower, bar_index, lowerEndPrice)
110
na
111
112
// Pearson's R
113
var label r = na
114
transparent = color.new(color.white, 100)
115
label.delete(r[1])
116
if showPearson and not na(pearsonR)
117
r := label.new(bar_index - len + 1, lowerStartPrice, tostring(pearsonR, "#.################"), color=transparent, textcolor=#0000ff, size=size.normal, style=label.style_labelup)
118
Copied!
Last modified 1yr ago