Coming soon.
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 Wizards​
​Our Pine Faq & Code​
Updated Daily
//@version=4study(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)
//@version=4study(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)
//@version=4study("Auto Fib Retracement", overlay=true)​// pivots thresholdthreshold_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.noneif extendLeft and extendRightextending := extend.bothif extendLeft and not extendRightextending := extend.leftif not extendLeft and extendRightextending := 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 = navar int iLast = 0var int iPrev = 0var float pLast = 0var isHighLast = false // otherwise the last pivot is a low pivot​pivots(src, length, isHigh) =>l2 = length * 2c = nz(src[length])ok = truefor i = 0 to l2if isHigh and src[i] > cok := falseif not isHigh and src[i] < cok := falseif 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) / pricepivotFound(dev, isHigh, index, price) =>if isHighLast == isHigh and not na(lineLast)// same directionif isHighLast ? price > pLast : price < pLastline.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 significantid = 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 != lineLastline.delete(lineLast)lineLast := idisHighLast := isHighiPrev := iLastiLast := iHpLast := pHelseif not na(iL)dev = calc_dev(pLast, pL)[id, isHigh] = pivotFound(dev, false, iL, pL)if not na(id)if id != lineLastline.delete(lineLast)lineLast := idisHighLast := isHighiPrev := iLastiLast := iLpLast := 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 > endPricediff = (iHL ? -1 : 1) * abs(startPrice - endPrice)if sl1l1 = startPrice + diff * vall1_draw_line(l1, #808080)_draw_label(l1, _label_txt(vall1, l1), #808080)if sl2l2 = startPrice + diff * vall2_draw_line(l2, #a61c00)_draw_label(l2, _label_txt(vall2, l2), #a61c00)if sl3l3 = startPrice + diff * vall3_draw_line(l3, #95cc28)_draw_label(l3, _label_txt(vall3, l3), #95cc28)if sl4l4 = startPrice + diff * vall4_draw_line(l4, #28cc28)_draw_label(l4, _label_txt(vall4, l4), #28cc28)if sl5l5 = startPrice + diff * vall5_draw_line(l5, #28cc95)_draw_label(l5, _label_txt(vall5, l5), #28cc95)if sl6l6 = startPrice + diff * vall6_draw_line(l6, #2895cc)_draw_label(l6, _label_txt(vall6, l6), #2895cc)if sl7l7 = startPrice + diff * vall7_draw_line(l7, #808080)_draw_label(l7, _label_txt(vall7, l7), #808080)if sl8l8 = startPrice + diff * vall8_draw_line(l8, #82CA89)_draw_label(l8, _label_txt(vall8, l8), #82CA89)​if sl9l9 = startPrice + diff * vall9_draw_line(l9, #F32C42)_draw_label(l9, _label_txt(vall9, l9), #F32C42)if sl10l10 = startPrice + diff * vall10_draw_line(l10, #2796ED)_draw_label(l10, _label_txt(vall10, l10), #2796ED)if sl11l11 = startPrice + diff * vall11_draw_line(l11, #a61c00)_draw_label(l11, _label_txt(vall11, l11), #a61c00)if sl12l12 = startPrice + diff * vall12_draw_line(l12, #9B03AE)_draw_label(l12, _label_txt(vall12, l12), #9B03AE)if sl13l13 = startPrice + diff * vall13_draw_line(l13, #E80065)_draw_label(l13, _label_txt(vall13, l13), #E80065)​if sl14l14 = startPrice + diff * vall14_draw_line(l14, #a61c00)_draw_label(l14, _label_txt(vall14, l14), #a61c00)if sl15l15 = startPrice + diff * vall15_draw_line(l15, #95cc28)_draw_label(l15, _label_txt(vall15, l15), #95cc28)if sl16l16 = startPrice + diff * vall16_draw_line(l16, #28cc95)_draw_label(l16, _label_txt(vall16, l16), #28cc95)​p1 = reverse ? line.get_y1(lineLast) : pLastp2 = reverse ? pLast : line.get_y1(lineLast)_draw_retracement(p1, p2)
//@version=4study("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]elseisFound = truefor i = 0 to length - 1if isHigh and src[i] > pisFound := falseif not isHigh and src[i] < pisFound := falsefor i = length + 1 to 2 * lengthif isHigh and src[i] >= pisFound := falseif not isHigh and src[i] <= pisFound := falseif 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 = navar int iLast = 0var float pLast = 0var bool isHighLast = true // otherwise the last pivot is a low pivotvar int linesCount = 0​pivotFound(dev, isHigh, index, price) =>if isHighLast == isHigh and not na(lineLast)// same directionif isHighLast ? price > pLast : price < pLastif linesCount <= 1line.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 significantif abs(dev) >= dev_thresholdid = 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 == iLdev1 = calc_dev(pLast, pH)[id2, isHigh2, isNew2] = pivotFound(dev1, true, iH, pH)if isNew2linesCount := linesCount + 1if not na(id2)lineLast := id2isHighLast := isHigh2iLast := iHpLast := pHdev2 = calc_dev(pLast, pL)[id1, isHigh1, isNew1] = pivotFound(dev2, false, iL, pL)if isNew1linesCount := linesCount + 1if not na(id1)lineLast := id1isHighLast := isHigh1iLast := iLpLast := pLelseif not na(iH)dev1 = calc_dev(pLast, pH)[id, isHigh, isNew] = pivotFound(dev1, true, iH, pH)if isNewlinesCount := linesCount + 1if not na(id)lineLast := idisHighLast := isHighiLast := iHpLast := pHelseif not na(iL)dev2 = calc_dev(pLast, pL)[id, isHigh, isNew] = pivotFound(dev2, false, iL, pL)if isNewlinesCount := linesCount + 1if not na(id)lineLast := idisHighLast := isHighiLast := iLpLast := pL​
//@version=4study("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)]elsesumX = 0.0sumY = 0.0sumXSqr = 0.0sumXY = 0.0for i = 0 to len - 1val = src[i]per = i + 1.0sumX := sumX + persumY := sumY + valsumXSqr := sumXSqr + per * persumXY := sumXY + val * perslope = (len * sumXY - sumX * sumY) / (len * sumXSqr - sumX * sumX)average = sumY / lenintercept = average - slope * sumX / len + slope[slope, average, intercept]​[s, a, i] = calcSlope(src, len)​startPrice = i + s * (len - 1)endPrice = ivar 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)elseline.set_xy1(baseLine, bar_index - len + 1, startPrice)line.set_xy2(baseLine, bar_index, endPrice)na​calcDev(src, len, slope, average, intercept) =>upDev = 0.0dnDev = 0.0stdDevAcc = 0.0dsxx = 0.0dsyy = 0.0dsxy = 0.0periods = len - 1​daY = intercept + (slope * periods) / 2val = interceptfor i = 0 to periodsprice = high[i] - valif (price > upDev)upDev := price​price := val - low[i]if (price > dnDev)dnDev := price​price := src[i]dxt = price - averagedyt = val - daYprice := price - valstdDevAcc := stdDevAcc + price * pricedsxx := dsxx + dxt * dxtdsyy := dsyy + dyt * dytdsxy := dsxy + dxt * dytval := val + slopestdDev = 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)elseline.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)elseline.set_xy1(lower, bar_index - len + 1, lowerStartPrice)line.set_xy2(lower, bar_index, lowerEndPrice)na​// Pearson's Rvar label r = natransparent = 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)​