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

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

Advance/Decline Line

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

Auto-Fibonacci Retracement

//@version=4
study("Auto Fib Retracement", overlay=true)
// pivots threshold
threshold_multiplier = input(title="Deviation", type=input.float, defval=3, minval=0)
dev_threshold = atr(10) / close * 100 * threshold_multiplier
depth = input(title="Depth", type=input.integer, defval=10, minval=1)
var extendLeft = input(false, "Extend Lines Left")
var extendRight = input(true, "Extend Lines Right")
var extending = extend.none
if extendLeft and extendRight
extending := extend.both
if extendLeft and not extendRight
extending := extend.left
if not extendLeft and extendRight
extending := extend.right
reverse = input(false, "Reverse")
prices = input(true, "Prices")
levels = input(true, "Levels")
levelsFormat = input("Values", "Levels Format", options = ["Values", "Percent"])
labelsPosition = input("Left", "Labels Position", options = ["Left", "Right"])
var line lineLast = na
var int iLast = 0
var int iPrev = 0
var float pLast = 0
var isHighLast = false // otherwise the last pivot is a low pivot
pivots(src, length, isHigh) =>
l2 = length * 2
c = nz(src[length])
ok = true
for i = 0 to l2
if isHigh and src[i] > c
ok := false
if not isHigh and src[i] < c
ok := false
if ok
[bar_index[length], c]
else
[int(na), float(na)]
[iH, pH] = pivots(high, depth / 2, true)
[iL, pL] = pivots(low, depth / 2, false)
calc_dev(base_price, price) =>
100 * (price - base_price) / price
pivotFound(dev, isHigh, index, price) =>
if isHighLast == isHigh and not na(lineLast)
// same direction
if isHighLast ? price > pLast : price < pLast
line.set_xy2(lineLast, index, price)
[lineLast, isHighLast]
else
[line(na), bool(na)]
else // reverse the direction (or create the very first line)
if abs(dev) > dev_threshold
// price move is significant
id = line.new(iLast, pLast, index, price, color=color.gray, width=1, style=line.style_dashed)
[id, isHigh]
else
[line(na), bool(na)]
if not na(iH)
dev = calc_dev(pLast, pH)
[id, isHigh] = pivotFound(dev, true, iH, pH)
if not na(id)
if id != lineLast
line.delete(lineLast)
lineLast := id
isHighLast := isHigh
iPrev := iLast
iLast := iH
pLast := pH
else
if not na(iL)
dev = calc_dev(pLast, pL)
[id, isHigh] = pivotFound(dev, false, iL, pL)
if not na(id)
if id != lineLast
line.delete(lineLast)
lineLast := id
isHighLast := isHigh
iPrev := iLast
iLast := iL
pLast := pL
_draw_line(price, col) =>
var id = line.new(iLast, price, bar_index, price, color=col, width=1, extend=extending)
if not na(lineLast)
line.set_xy1(id, line.get_x1(lineLast), price)
line.set_xy2(id, line.get_x2(lineLast), price)
_draw_label(price, txt, txtColor) =>
x = labelsPosition == "Left" ? line.get_x1(lineLast) : line.get_x2(lineLast)
var id = label.new(x=x, y=price, text=txt, textcolor=txtColor, style=label.style_none)
label.set_xy(id, x, price)
label.set_text(id, txt)
label.set_textcolor(id, txtColor)
_wrap(txt) =>
"(" + tostring(txt, "#.##") + ")"
_label_txt(level, price) =>
l = levelsFormat == "Values" ? tostring(level) : tostring(level * 100) + "%"
(levels ? l : "") + (prices ? _wrap(price) : "")
sl1 = input(true, "0")
vall1 = 0.000
sl2 = input(true, "0.236")
vall2 = 0.236
sl3 = input(true, "0.382")
vall3 = 0.382
sl4 = input(true, "0.5")
vall4 = 0.5
sl5 = input(true, "0.618")
vall5 = 0.618
sl6 = input(true, "0.786")
vall6 = 0.786
sl7 = input(true, "1")
vall7 = 1
sl8 = input(true, "1.272")
vall8 = 1.272
sl9 = input(true, "1.414")
vall9 = 1.414
sl10 = input(true, "1.618")
vall10 = 1.618
sl11 = input(false, "2.618")
vall11 = 2.618
sl12 = input(false, "3.618")
vall12 = 3.618
sl13 = input(false, "4.236")
vall13 = 4.236
sl14 = input(false, "-0.236")
vall14 = -0.236
sl15 = input(false, "-0.382")
vall15 = -0.382
sl16 = input(false, "-0.618")
vall16 = -0.618
_draw_retracement(startPrice, endPrice) =>
iHL = startPrice > endPrice
diff = (iHL ? -1 : 1) * abs(startPrice - endPrice)
if sl1
l1 = startPrice + diff * vall1
_draw_line(l1, #808080)
_draw_label(l1, _label_txt(vall1, l1), #808080)
if sl2
l2 = startPrice + diff * vall2
_draw_line(l2, #a61c00)
_draw_label(l2, _label_txt(vall2, l2), #a61c00)
if sl3
l3 = startPrice + diff * vall3
_draw_line(l3, #95cc28)
_draw_label(l3, _label_txt(vall3, l3), #95cc28)
if sl4
l4 = startPrice + diff * vall4
_draw_line(l4, #28cc28)
_draw_label(l4, _label_txt(vall4, l4), #28cc28)
if sl5
l5 = startPrice + diff * vall5
_draw_line(l5, #28cc95)
_draw_label(l5, _label_txt(vall5, l5), #28cc95)
if sl6
l6 = startPrice + diff * vall6
_draw_line(l6, #2895cc)
_draw_label(l6, _label_txt(vall6, l6), #2895cc)
if sl7
l7 = startPrice + diff * vall7
_draw_line(l7, #808080)
_draw_label(l7, _label_txt(vall7, l7), #808080)
if sl8
l8 = startPrice + diff * vall8
_draw_line(l8, #82CA89)
_draw_label(l8, _label_txt(vall8, l8), #82CA89)
if sl9
l9 = startPrice + diff * vall9
_draw_line(l9, #F32C42)
_draw_label(l9, _label_txt(vall9, l9), #F32C42)
if sl10
l10 = startPrice + diff * vall10
_draw_line(l10, #2796ED)
_draw_label(l10, _label_txt(vall10, l10), #2796ED)
if sl11
l11 = startPrice + diff * vall11
_draw_line(l11, #a61c00)
_draw_label(l11, _label_txt(vall11, l11), #a61c00)
if sl12
l12 = startPrice + diff * vall12
_draw_line(l12, #9B03AE)
_draw_label(l12, _label_txt(vall12, l12), #9B03AE)
if sl13
l13 = startPrice + diff * vall13
_draw_line(l13, #E80065)
_draw_label(l13, _label_txt(vall13, l13), #E80065)
if sl14
l14 = startPrice + diff * vall14
_draw_line(l14, #a61c00)
_draw_label(l14, _label_txt(vall14, l14), #a61c00)
if sl15
l15 = startPrice + diff * vall15
_draw_line(l15, #95cc28)
_draw_label(l15, _label_txt(vall15, l15), #95cc28)
if sl16
l16 = startPrice + diff * vall16
_draw_line(l16, #28cc95)
_draw_label(l16, _label_txt(vall16, l16), #28cc95)
p1 = reverse ? line.get_y1(lineLast) : pLast
p2 = reverse ? pLast : line.get_y1(lineLast)
_draw_retracement(p1, p2)

Zig-Zag

//@version=4
study("Zig Zag", overlay=true)
dev_threshold = input(title="Deviation (%)", type=input.float, defval=5, minval=1, maxval=100)
depth = input(title="Depth", type=input.integer, defval=10, minval=1)
pivots(src, length, isHigh) =>
p = nz(src[length])
if length == 0
[bar_index, p]
else
isFound = true
for i = 0 to length - 1
if isHigh and src[i] > p
isFound := false
if not isHigh and src[i] < p
isFound := false
for i = length + 1 to 2 * length
if isHigh and src[i] >= p
isFound := false
if not isHigh and src[i] <= p
isFound := false
if isFound and length * 2 <= bar_index
[bar_index[length], p]
else
[int(na), float(na)]
[iH, pH] = pivots(high, floor(depth / 2), true)
[iL, pL] = pivots(low, floor(depth / 2), false)
calc_dev(base_price, price) =>
100 * (price - base_price) / base_price
var line lineLast = na
var int iLast = 0
var float pLast = 0
var bool isHighLast = true // otherwise the last pivot is a low pivot
var int linesCount = 0
pivotFound(dev, isHigh, index, price) =>
if isHighLast == isHigh and not na(lineLast)
// same direction
if isHighLast ? price > pLast : price < pLast
if linesCount <= 1
line.set_xy1(lineLast, index, price)
line.set_xy2(lineLast, index, price)
[lineLast, isHighLast, false]
else
[line(na), bool(na), false]
else // reverse the direction (or create the very first line)
if na(lineLast)
id = line.new(index, price, index, price, color=color.red, width=2)
[id, isHigh, true]
else
// price move is significant
if abs(dev) >= dev_threshold
id = line.new(iLast, pLast, index, price, color=color.red, width=2)
[id, isHigh, true]
else
[line(na), bool(na), false]
if not na(iH) and not na(iL) and iH == iL
dev1 = calc_dev(pLast, pH)
[id2, isHigh2, isNew2] = pivotFound(dev1, true, iH, pH)
if isNew2
linesCount := linesCount + 1
if not na(id2)
lineLast := id2
isHighLast := isHigh2
iLast := iH
pLast := pH
dev2 = calc_dev(pLast, pL)
[id1, isHigh1, isNew1] = pivotFound(dev2, false, iL, pL)
if isNew1
linesCount := linesCount + 1
if not na(id1)
lineLast := id1
isHighLast := isHigh1
iLast := iL
pLast := pL
else
if not na(iH)
dev1 = calc_dev(pLast, pH)
[id, isHigh, isNew] = pivotFound(dev1, true, iH, pH)
if isNew
linesCount := linesCount + 1
if not na(id)
lineLast := id
isHighLast := isHigh
iLast := iH
pLast := pH
else
if not na(iL)
dev2 = calc_dev(pLast, pL)
[id, isHigh, isNew] = pivotFound(dev2, false, iL, pL)
if isNew
linesCount := linesCount + 1
if not na(id)
lineLast := id
isHighLast := isHigh
iLast := iL
pLast := pL

Linear Regression

//@version=4
study("Linear Regression", shorttitle="LinReg", overlay=true)
upperMult = input(title="Upper Deviation", defval=2)
lowerMult = input(title="Lower Deviation", defval=-2)
useUpperDev = input(title="Use Upper Deviation", defval=true)
useLowerDev = input(title="Use Lower Deviation", defval=true)
showPearson = input(title="Show Pearson's R", defval=true)
extendLines = input(title="Extend Lines", defval=false)
len = input(title="Count", defval=100)
src = input(title="Source", defval=close)
extend = extendLines ? extend.right : extend.none
calcSlope(src, len) =>
max_bars_back(src, 300)
if not barstate.islast or len <= 1
[float(na), float(na), float(na)]
else
sumX = 0.0
sumY = 0.0
sumXSqr = 0.0
sumXY = 0.0
for i = 0 to len - 1
val = src[i]
per = i + 1.0
sumX := sumX + per
sumY := sumY + val
sumXSqr := sumXSqr + per * per
sumXY := sumXY + val * per
slope = (len * sumXY - sumX * sumY) / (len * sumXSqr - sumX * sumX)
average = sumY / len
intercept = average - slope * sumX / len + slope
[slope, average, intercept]
[s, a, i] = calcSlope(src, len)
startPrice = i + s * (len - 1)
endPrice = i
var line baseLine = na
if na(baseLine) and not na(startPrice)
baseLine := line.new(bar_index - len + 1, startPrice, bar_index, endPrice, width=1, extend=extend, color=color.red)
else
line.set_xy1(baseLine, bar_index - len + 1, startPrice)
line.set_xy2(baseLine, bar_index, endPrice)
na
calcDev(src, len, slope, average, intercept) =>
upDev = 0.0
dnDev = 0.0
stdDevAcc = 0.0
dsxx = 0.0
dsyy = 0.0
dsxy = 0.0
periods = len - 1
daY = intercept + (slope * periods) / 2
val = intercept
for i = 0 to periods
price = high[i] - val
if (price > upDev)
upDev := price
price := val - low[i]
if (price > dnDev)
dnDev := price
price := src[i]
dxt = price - average
dyt = val - daY
price := price - val
stdDevAcc := stdDevAcc + price * price
dsxx := dsxx + dxt * dxt
dsyy := dsyy + dyt * dyt
dsxy := dsxy + dxt * dyt
val := val + slope
stdDev = sqrt(stdDevAcc / (periods == 0 ? 1 : periods))
pearsonR = dsxx == 0 or dsyy == 0 ? 0 : dsxy / sqrt(dsxx * dsyy)
[stdDev, pearsonR, upDev, dnDev]
[stdDev, pearsonR, upDev, dnDev] = calcDev(src, len, s, a, i)
upperStartPrice = startPrice + (useUpperDev ? upperMult * stdDev : upDev)
upperEndPrice = endPrice + (useUpperDev ? upperMult * stdDev : upDev)
var line upper = na
lowerStartPrice = startPrice + (useLowerDev ? lowerMult * stdDev : -dnDev)
lowerEndPrice = endPrice + (useLowerDev ? lowerMult * stdDev : -dnDev)
var line lower = na
if na(upper) and not na(upperStartPrice)
upper := line.new(bar_index - len + 1, upperStartPrice, bar_index, upperEndPrice, width=1, extend=extend, color=#0000ff)
else
line.set_xy1(upper, bar_index - len + 1, upperStartPrice)
line.set_xy2(upper, bar_index, upperEndPrice)
na
if na(lower) and not na(lowerStartPrice)
lower := line.new(bar_index - len + 1, lowerStartPrice, bar_index, lowerEndPrice, width=1, extend=extend, color=#0000ff)
else
line.set_xy1(lower, bar_index - len + 1, lowerStartPrice)
line.set_xy2(lower, bar_index, lowerEndPrice)
na
// Pearson's R
var label r = na
transparent = color.new(color.white, 100)
label.delete(r[1])
if showPearson and not na(pearsonR)
r := label.new(bar_index - len + 1, lowerStartPrice, tostring(pearsonR, "#.################"), color=transparent, textcolor=#0000ff, size=size.normal, style=label.style_labelup)