Indicator · Pine Script v5 · On the chart

Next Pivot Projection [Trendoscope]

by regalouisei· code credited to HeWhoMustNotBeNamed

This indicator projects future pivot levels on the main chart by combining zigzag price swings with a choice of underlying momentum oscillators, such as RSI, CCI, CMO, COG, MFI, or ROC.

View on GitHub

Repaints

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code431
Settings11inputs
LicenceNone statedfile: MPL-2.0
Last update8 months ago25 Feb 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

431 lines of Pine Script v5 from pinescript/editors_picks/next-pivot-projection-trendoscope.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

next-pivot-projection-trendoscope.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HeWhoMustNotBeNamed
//                                       ░▒             
//                                  ▒▒▒   ▒▒      
//                              ▒▒▒▒▒     ▒▒      
//                      ▒▒▒▒▒▒▒░     ▒     ▒▒          
//                  ▒▒▒▒▒▒           ▒     ▒▒          
//             ▓▒▒▒       ▒        ▒▒▒▒▒▒▒▒▒▒▒  
//   ▒▒▒▒▒▒▒▒▒▒▒ ▒        ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒         
//   ▒  ▒       ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░        
//   ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒▒▒▒▒▒▒▒         
//   ▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ▒▒                       
//    ▒▒▒▒▒         ▒▒▒▒▒▒▒                            
//                 ▒▒▒▒▒▒▒▒▒                           
//                ▒▒▒▒▒ ▒▒▒▒▒                          
//               ░▒▒▒▒   ▒▒▒▒▓      ████████╗██████╗ ███████╗███╗   ██╗██████╗  ██████╗ ███████╗ ██████╗ ██████╗ ██████╗ ███████╗
//              ▓▒▒▒▒     ▒▒▒▒      ╚══██╔══╝██╔══██╗██╔════╝████╗  ██║██╔══██╗██╔═══██╗██╔════╝██╔════╝██╔═══██╗██╔══██╗██╔════╝
//              ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒        ██║   ██████╔╝█████╗  ██╔██╗ ██║██║  ██║██║   ██║███████╗██║     ██║   ██║██████╔╝█████╗ 
//             ▒▒▒▒▒       ▒▒▒▒▒       ██║   ██╔══██╗██╔══╝  ██║╚██╗██║██║  ██║██║   ██║╚════██║██║     ██║   ██║██╔═══╝ ██╔══╝  
//            ▒▒▒▒▒         ▒▒▒▒▒      ██║   ██║  ██║███████╗██║ ╚████║██████╔╝╚██████╔╝███████║╚██████╗╚██████╔╝██║     ███████╗
//             ▒▒             ▒                        
//@version=5
indicator("Next Pivot Projection [Trendoscope]", "NPP[Trendoscope]", overlay=true, max_lines_count = 500, max_labels_count = 500)
import HeWhoMustNotBeNamed/mZigzag/12 as zg
import HeWhoMustNotBeNamed/enhanced_ta/14 as eta
import HeWhoMustNotBeNamed/arrays/1 as pa
length = input.int(8, 'Length', group='Zigzag', display = display.none)
oscillatorType = input.string("rsi", title="Oscillator", inline="osc", options=["cci", "cmo", "cog", "mfi", "roc", "rsi"],
                     group='Oscillator', display = display.none)
oscLength = input.int(14, title="", inline="osc", group='Oscillator', display = display.none)
supertrendLength = input.int(5, 'History', inline='st', group='Supertrend', display = display.none)
drawSupertrend = input.bool(false, "Draw Zigzag Supertrend", inline='st2', group='Supertrend', display = display.none)

showTable = input.bool(false, 'Detailed Stats', inline='txt', group = 'Stats and Display', display = display.none)
txtSize = input.string(size.tiny, '', [size.tiny, size.small, size.normal, size.large, size.huge],
                         inline='txt', group = 'Stats and Display', display = display.none)
txtColor = input.color(color.white, '', inline='txt', group = 'Stats and Display', display = display.none)

showPivotLines = input.bool(true, 'Pivot Lines', inline='pli', group = 'Stats and Display', display = display.none)
showPivotLabel = input.bool(false, 'Pivot Label', inline='pla', group = 'Stats and Display', display = display.none)
fillAreas = input.bool(true, 'Fill Bullish/Bearish Sentiments', inline='fil', group = 'Stats and Display', display = display.none)
increment(mtx, row, col, val=1)=>matrix.set(mtx, row, col, matrix.get(mtx, row, col)+val)
gettrendindex(int price, int osc, int trend)=>
    trendFactor = trend > 0 ? 0 : 1
    priceFactor = math.abs(price) > 1? 1 : 0
    oscFactor = math.abs(osc) > 1? 1 : 0
    trendFactor*4 + priceFactor*2 + oscFactor


getSentimentDetails(pDir, oDir, sDir) =>
    sentiment = pDir == oDir ? sDir == pDir or sDir * 2 == -pDir ? -sDir : sDir * 4 : sDir == pDir or sDir == -oDir ? 0 : (math.abs(oDir) > math.abs(pDir) ? sDir : -sDir) * (sDir == oDir ? 2 : 3)
    sentimentSymbol = sentiment == 4 ? '⬆' : sentiment == -4 ? '⬇' : sentiment == 3 ? '↗' : sentiment == -3 ? '↘' : sentiment == 2 ? '⤴' : sentiment == -2 ? '⤵' : sentiment == 1 ? '⤒' : sentiment == -1 ? '⤓' : '▣'
    sentimentColor = sentiment == 4 ? color.green : sentiment == -4 ? color.red : sentiment == 3 ? color.lime : sentiment == -3 ? color.orange : sentiment == 2 ? color.rgb(202, 224, 13, 0) : sentiment == -2 ? color.rgb(250, 128, 114, 0) : color.silver
    sentimentLabel = math.abs(sentiment) == 4 ? 'C' : math.abs(sentiment) == 3 ? 'H' : math.abs(sentiment) == 2 ? 'D' : 'I'
    [sentimentSymbol, sentimentLabel, sentimentColor]


getStatus(int trendIndex, int pivotDir)=>
    trendFactor = int(trendIndex/4)
    remainder = trendIndex % 4
    priceFactor = int(remainder/2)+1
    oscFactor = (remainder % 2)+1
    trendChar = (trendFactor == 0)? 'U' : 'D'
    priceChar = pivotDir > 0? (priceFactor == 2? 'HH' : 'LH') : (priceFactor == 2? 'LL' : 'HL')
    oscChar = pivotDir > 0? (oscFactor == 2? 'HH' : 'LH') : (oscFactor == 2? 'LL' : 'HL')
    trendChar + ' - ' + priceChar + '/'+oscChar

draw_zg_line(idx1, idx2, zigzaglines, zigzaglabels, valueMatrix, directionMatrix, ratioMatrix,  divergenceMatrix, doubleDivergenceMatrix,
                                         barArray, trendArray, lineColor, lineWidth, lineStyle) =>
    if matrix.rows(valueMatrix) > 2
        idxLen1 = matrix.rows(valueMatrix)-idx1
        idxLen2 = matrix.rows(valueMatrix)-idx2
        lastValues = matrix.row(valueMatrix, idxLen1)
        llastValues = matrix.row(valueMatrix, idxLen2)
        
        lastDirections = matrix.row(directionMatrix, idxLen1)
        lastRatios = matrix.row(ratioMatrix, idxLen1)
        lastDivergence = matrix.row(divergenceMatrix, idxLen1)
        lastDoubleDivergence = matrix.row(doubleDivergenceMatrix, idxLen1)

        y1 = array.get(lastValues, 0)
        y2 = array.get(llastValues, 0)
        x1 = array.get(barArray, idxLen1)
        x2 = array.get(barArray, idxLen2)
        zline = line.new(x1=x1, y1=y1, x2=x2, y2=y2, color=lineColor, width=lineWidth, style=lineStyle)
        currentDir = y1 > y2? 1 : -1
        
        priceDir = array.get(lastDirections, 0)
        oscDir = array.get(lastDirections, 1)
        trendDir = array.get(trendArray, idxLen1)
        trendIndex = gettrendindex(priceDir, oscDir, trendDir)
        trendLabel = getStatus(trendIndex, currentDir)
        [sentimentSymbol, sentimentLabel, sentimentColor] = getSentimentDetails(priceDir, oscDir, trendDir)

        labelStyle = currentDir > 0? label.style_label_down : label.style_label_up
        zlabel = showPivotLabel ? label.new(x=x1, y=y1, yloc=yloc.price, color=sentimentColor, style=labelStyle, text=sentimentSymbol + ' ' + trendLabel, 
                                     textcolor=color.black, size = size.small, tooltip=sentimentLabel) : na
        if array.size(zigzaglines) > 0
            lastLine = array.get(zigzaglines, array.size(zigzaglines)-1)
            if line.get_x2(lastLine) == x2 and line.get_x1(lastLine) <= x1
                pa.pop(zigzaglines)
                pa.pop(zigzaglabels)
        
        pa.push(zigzaglines, zline, 500)
        pa.push(zigzaglabels, zlabel, 500)

draw(matrix<float> valueMatrix, matrix<int> directionMatrix, matrix<float> ratioMatrix,  matrix<int> divergenceMatrix, matrix<int> doubleDivergenceMatrix, array<int> barArray, array<int> trendArray,
                 bool newZG, bool doubleZG, color lineColor = color.blue, int lineWidth = 1, string lineStyle = line.style_solid)=>
    var zigzaglines = array.new_line(0)
    var zigzaglabels = array.new_label(0)
    if(newZG)
        if doubleZG
            draw_zg_line(2, 3, zigzaglines, zigzaglabels, valueMatrix, directionMatrix, ratioMatrix, divergenceMatrix, doubleDivergenceMatrix, barArray, trendArray,
                             lineColor, lineWidth, lineStyle)

        if matrix.rows(valueMatrix) >= 2
            draw_zg_line(1, 2, zigzaglines, zigzaglabels, valueMatrix, directionMatrix, ratioMatrix,  divergenceMatrix, doubleDivergenceMatrix, barArray, trendArray,
                             lineColor, lineWidth, lineStyle)
    [valueMatrix, directionMatrix, ratioMatrix, divergenceMatrix, doubleDivergenceMatrix, barArray, zigzaglines, zigzaglabels]

    

indicatorHigh = array.new_float()
indicatorLow = array.new_float()
indicatorLabels = array.new_string()

[oscHigh, _, _] = eta.oscillator(oscillatorType, oscLength, oscLength, oscLength, high)
[oscLow, _, _] = eta.oscillator(oscillatorType, oscLength, oscLength, oscLength, low)
array.push(indicatorHigh, math.round(oscHigh,2))
array.push(indicatorLow, math.round(oscLow,2))
array.push(indicatorLabels, oscillatorType+str.tostring(oscLength))


[valueMatrix, directionMatrix, ratioMatrix, divergenceMatrix, doubleDivergenceMatrix, barArray, trendArray, supertrendDir, supertrend, newZG, doubleZG] = 
                                                         zg.calculate(length, array.from(high, low), indicatorHigh, indicatorLow, supertrendLength = supertrendLength)

nextDirection = matrix.rows(directionMatrix) > 0? matrix.row(directionMatrix, matrix.rows(directionMatrix)-1) : array.new_int()
lastDirection = matrix.rows(directionMatrix) > 1? matrix.row(directionMatrix, matrix.rows(directionMatrix)-2) : array.new_int()
llastDirection = matrix.rows(directionMatrix) > 2? matrix.row(directionMatrix, matrix.rows(directionMatrix)-3) : array.new_int()
lllastDirection = matrix.rows(directionMatrix) > 3? matrix.row(directionMatrix, matrix.rows(directionMatrix)-4) : array.new_int()

var pivotHighStats = matrix.new<int>(64, 3, 0)
var pivotLowStats = matrix.new<int>(64, 3, 0)

var pivotHighRatios = matrix.new<float>(64, 3, 0)
var pivotLowRatios = matrix.new<float>(64, 3, 0)

var pivotHighBars = matrix.new<float>(64, 3, 0)
var pivotLowBars = matrix.new<float>(64, 3, 0)

currentTotalTrendIndex = 0
nextTotalTrendIndex = 0
currentDir = matrix.rows(directionMatrix) > 0? matrix.get(directionMatrix, matrix.rows(directionMatrix)-1, 0) : 0

currentPDir = lastDirection.size() > 0 ? lastDirection.first() : na
changePriceDirection = ta.change(math.sign(currentPDir))
if(array.size(lllastDirection) > 0)
    priceDirection = array.get(lastDirection, 0)
    priceRatio = matrix.get(ratioMatrix, matrix.rows(ratioMatrix)-2, 0)

    numberOfBars = array.get(barArray, array.size(barArray)-2) - array.get(barArray, array.size(barArray)-3)
    currentPriceDirection = array.get(lastDirection, 0)
    currentOscDirection = array.get(lastDirection, 1)
    currentTrend = array.get(trendArray, array.size(trendArray)-2)

    nextPriceDirection = array.get(nextDirection, 0)
    nextOscDirection = array.get(nextDirection, 1)
    nextTrend = array.get(trendArray, array.size(trendArray)-1)

    lastPriceDirection = array.get(llastDirection, 0)
    lastOscDirection = array.get(llastDirection, 1)
    lastTrend = array.get(trendArray, array.size(trendArray)-3)

    llastPriceDirection = array.get(lllastDirection, 0)
    llastOscDirection = array.get(lllastDirection, 1)
    llastTrend = array.get(trendArray, array.size(trendArray)-4)

    colLast = math.abs(priceDirection) %2

    nextTrendIndex = gettrendindex(nextPriceDirection, nextOscDirection, nextTrend)
    currentTrendIndex = gettrendindex(currentPriceDirection, currentOscDirection, currentTrend)
    lastTrendIndex = gettrendindex(lastPriceDirection, lastOscDirection, lastTrend)
    llastTrendIndex = gettrendindex(llastPriceDirection, llastOscDirection, llastTrend)

    totalIndex = lastTrendIndex*8 + llastTrendIndex
    currentTotalTrendIndex := currentTrendIndex*8 + lastTrendIndex
    nextTotalTrendIndex := nextTrendIndex*8 + currentTrendIndex

    matrixToSet = math.sign(priceDirection) > 0? pivotHighStats : pivotLowStats
    ratioMatrixToSet = math.sign(priceDirection) > 0? pivotHighRatios : pivotLowRatios
    barMatrixToSet = math.sign(priceDirection) > 0? pivotHighBars : pivotLowBars

    if(not na(currentPDir) and changePriceDirection != 0)
        increment(matrixToSet, totalIndex, colLast)
        increment(ratioMatrixToSet, totalIndex, colLast, priceRatio)
        increment(barMatrixToSet, totalIndex, colLast, numberOfBars)

        increment(matrixToSet, totalIndex, 2)
        increment(ratioMatrixToSet, totalIndex, 2, priceRatio)
        increment(barMatrixToSet, totalIndex, 2, numberOfBars)


var float hhValue = na
var float lhValue = na

var float llValue = na
var float hlValue = na

var float hhProbability = na
var float llProbability = na

var float htRatio = na
var float ltRatio = na
if(array.size(barArray) > 10)
    currentBar = array.get(barArray, array.size(barArray)-1)
    lastBar = array.get(barArray, array.size(barArray)-2)
    currentPrice = matrix.get(valueMatrix, matrix.rows(valueMatrix)-1, 0)
    lastPrice = matrix.get(valueMatrix, matrix.rows(valueMatrix)-2, 0)
    llastPrice = matrix.get(valueMatrix, matrix.rows(valueMatrix)-3, 0)
    startRow = 2
    var statsTable = showTable ? table.new(position=position.top_right, columns=10, rows=64+startRow, border_color = color.black, border_width = 2) : na
    
    phSortIndices = array.sort_indices(matrix.col(pivotHighStats, 2), order.descending)
    
    phColStart = currentDir > 0 ? 0 : 5
    
    if(showTable)
        table.clear(statsTable, 0, 0, 9, 64+startRow-1)
        table.cell(statsTable, phColStart, 0, 'Pivot High Projection', text_color = txtColor, text_size = txtSize, bgcolor = color.maroon)
        table.cell(statsTable, phColStart, 1, 'Last Two Pivots', text_color = txtColor, text_size = txtSize, bgcolor = color.maroon)
        table.cell(statsTable, phColStart+2, 1, 'HH', text_color = txtColor, text_size = txtSize, bgcolor = color.maroon)
        table.cell(statsTable, phColStart+3, 1, 'LH', text_color = txtColor, text_size = txtSize, bgcolor = color.maroon)
        table.cell(statsTable, phColStart+4, 1, 'T', text_color = txtColor, text_size = txtSize, bgcolor = color.maroon)
        table.merge_cells(statsTable, phColStart, 0, phColStart+4, 0)
        table.merge_cells(statsTable, phColStart, 1, phColStart+1, 1)

    hlr = startRow
    for i=0 to 63
        si = array.get(phSortIndices, i)
        lastTrendIndex = int(si/8)
        llastTrendIndex = si%8

        lastPivotStatus = getStatus(lastTrendIndex, -1)
        llastPivotStatus = getStatus(llastTrendIndex, 1)
        hhStats = matrix.get(pivotHighStats, si, 0)
        lhStats = matrix.get(pivotHighStats, si, 1)
        tStats = matrix.get(pivotHighStats, si, 2)

        hhRatio = math.round(matrix.get(pivotHighRatios, si, 0)/hhStats, 3)
        lhRatio = math.round(matrix.get(pivotHighRatios, si, 1)/lhStats, 3)
        tRatio = math.round(matrix.get(pivotHighRatios, si, 2)/tStats, 3)

        hhBars = math.round(matrix.get(pivotHighBars, si, 0)/hhStats)
        lhBars = math.round(matrix.get(pivotHighBars, si, 1)/lhStats)
        tBars = math.round(matrix.get(pivotHighBars, si, 2)/tStats)
        highlight = math.sign(currentDir) < 0 ? nextTotalTrendIndex == si  : currentTotalTrendIndex == si

        hhTooltip = 'Average Ratio - '+str.tostring(hhRatio)+'\n'+'Average Bars - '+str.tostring(hhBars)
        lhTooltip = 'Average Ratio - '+str.tostring(lhRatio)+'\n'+'Average Bars - '+str.tostring(lhBars)
        tTooltip = 'Average Ratio - '+str.tostring(tRatio)+'\n'+'Average Bars - '+str.tostring(tBars)

        if(highlight)
            var line hhLine = na
            var line lhLine = na
            var line tLine = na
            var label hhLabel = na
            var label lhLabel = na
            var label tLabel = na

            line.delete(hhLine)
            line.delete(lhLine)
            line.delete(tLine)
            label.delete(hhLabel)
            label.delete(lhLabel)
            label.delete(tLabel)
            
            x1 = math.sign(currentDir) < 0 ? currentBar : lastBar
            hhX2 = x1 + hhBars
            lhX2 = x1 + lhBars
            tX2 = x1 + tBars

            y1 = math.sign(currentDir) < 0 ? currentPrice : lastPrice
            prev = math.sign(currentDir) < 0 ? lastPrice : llastPrice
            hhY2 = math.round_to_mintick(y1 + math.abs(y1-prev)*hhRatio)
            lhY2 = math.round_to_mintick(y1 + math.abs(y1-prev)*lhRatio)
            tY2 = math.round_to_mintick(y1 + math.abs(y1-prev)*tRatio)
            hhLine := line.new(x1, y1, hhX2, hhY2, xloc=xloc.bar_index, color=color.green, style=line.style_arrow_right)
            lhLine := line.new(x1, y1, lhX2, lhY2, xloc=xloc.bar_index, color=color.lime, style=line.style_arrow_right)
            tLine := line.new(x1, y1, tX2, tY2, xloc=xloc.bar_index, color=color.yellow, style=line.style_arrow_right)

            hhPercent = str.tostring(hhStats*100/tStats, format.percent)
            lhPercent = str.tostring(lhStats*100/tStats, format.percent)
            hhText = 'Number of Historical References :'+str.tostring(hhStats)+'/'+str.tostring(tStats)+
                         '\nProbability of Higher High :'+hhPercent+
                         '\nAverage Higher High Ratio :'+str.tostring(hhRatio) +
                         '\nAverage Higher High Bars :'+str.tostring(hhBars)
            lhText = 'Number of Historical References :'+str.tostring(lhStats)+'/'+str.tostring(tStats)+
                         '\nProbability of Lower High :'+lhPercent+
                         '\nAverage Lower High Ratio :'+str.tostring(lhRatio) +
                         '\nAverage Lower High Bars :'+str.tostring(lhBars)
            tText = 'Number of Historical References :'+str.tostring(tStats)+
                         '\nAverage Fib Ratio :'+str.tostring(tRatio)+
                         '\nAverage Bars :'+str.tostring(tBars)
            hhLabel := label.new(hhX2, hhY2, str.tostring(hhY2)+ ' - ' +hhPercent, style=label.style_label_lower_left, color=color.new(color.green, 70), textcolor = color.white, size=size.small, tooltip=hhText)
            lhLabel := label.new(lhX2, lhY2, str.tostring(lhY2)+ ' - ' +lhPercent, style=label.style_label_upper_left, color=color.new(color.lime, 70), textcolor = color.white, size=size.small, tooltip=lhText)
            tLabel := label.new(tX2, tY2, str.tostring(tY2)+ '@'+str.tostring(tRatio), style=label.style_label_left, color=color.new(color.yellow, 70), textcolor = color.white, size=size.small, tooltip=tText)
            hhValue := hhY2
            lhValue := lhY2
            hhProbability := hhStats*100/tStats
            htRatio := tRatio

        if(hhStats != 0 and lhStats != 0) and showTable
            table.cell(statsTable, phColStart, hlr, llastPivotStatus, text_color = txtColor, text_size = txtSize, bgcolor = color.new(color.lime, 60))
            table.cell(statsTable, phColStart+1, hlr, lastPivotStatus, text_color = txtColor, text_size = txtSize, bgcolor = color.new(color.orange, 60))
            table.cell(statsTable, phColStart+2, hlr, str.tostring(hhStats)+' - '+str.tostring(hhRatio), text_color = txtColor, text_size = txtSize, bgcolor = color.new(color.green, highlight ? 50 : 90), tooltip = hhTooltip)
            table.cell(statsTable, phColStart+3, hlr, str.tostring(lhStats)+' - '+str.tostring(lhRatio), text_color = txtColor, text_size = txtSize, bgcolor = color.new(color.red, highlight? 50 : 90), tooltip = lhTooltip)
            table.cell(statsTable, phColStart+4, hlr, str.tostring(tStats)+' - '+str.tostring(tRatio), text_color = txtColor, text_size = txtSize, bgcolor = color.from_gradient(hhStats/tStats, 0, 1, color.red, color.green), tooltip = tTooltip)
            hlr+=1


    plColStart = currentDir < 0 ? 0 : 5
    if(showTable)
        table.cell(statsTable, plColStart, 0, 'Pivot Low Projection', text_color = txtColor, text_size = txtSize, bgcolor = color.maroon)
        table.cell(statsTable, plColStart, 1, 'Last Two Pivots', text_color = txtColor, text_size = txtSize, bgcolor = color.maroon)
        table.cell(statsTable, plColStart+2, 1, 'LL', text_color = txtColor, text_size = txtSize, bgcolor = color.maroon)
        table.cell(statsTable, plColStart+3, 1, 'HL', text_color = txtColor, text_size = txtSize, bgcolor = color.maroon)
        table.cell(statsTable, plColStart+4, 1, 'T', text_color = txtColor, text_size = txtSize, bgcolor = color.maroon)
        table.merge_cells(statsTable, plColStart, 0, plColStart+4, 0)
        table.merge_cells(statsTable, plColStart, 1, plColStart+1, 1)
    plSortIndices = array.sort_indices(matrix.col(pivotLowStats, 2), order.descending)

    llr = startRow
    for i=0 to 63
        si = array.get(plSortIndices, i)
        lastTrendIndex = int(si/8)
        llastTrendIndex = si%8

        lastPivotStatus = getStatus(lastTrendIndex, 1)
        llastPivotStatus = getStatus(llastTrendIndex, -1)
        llStats = matrix.get(pivotLowStats, si, 0)
        hlStats = matrix.get(pivotLowStats, si, 1)
        tStats = matrix.get(pivotLowStats, si, 2)

        llRatio = math.round(matrix.get(pivotLowRatios, si, 0)/llStats, 3)
        hlRatio = math.round(matrix.get(pivotLowRatios, si, 1)/hlStats, 3)
        tRatio = math.round(matrix.get(pivotLowRatios, si, 2)/tStats, 3)

        llBars = math.round(matrix.get(pivotLowBars, si, 0)/llStats)
        hlBars = math.round(matrix.get(pivotLowBars, si, 1)/hlStats)
        tBars = math.round(matrix.get(pivotLowBars, si, 2)/tStats)

        highlight = math.sign(currentDir) > 0 ? nextTotalTrendIndex== si  : currentTotalTrendIndex  == si

        llTooltip = 'Average Ratio - '+str.tostring(llRatio)+'\n'+'Average Bars - '+str.tostring(llBars)
        hlTooltip = 'Average Ratio - '+str.tostring(hlRatio)+'\n'+'Average Bars - '+str.tostring(hlBars)
        tTooltip = 'Average Ratio - '+str.tostring(tRatio)+'\n'+'Average Bars - '+str.tostring(tBars)

        if(highlight)
            var line llLine = na
            var line hlLine = na
            var line tLine = na

            var label llLabel = na
            var label hlLabel = na
            var label tLabel = na

            line.delete(llLine)
            line.delete(hlLine)
            line.delete(tLine)

            label.delete(llLabel)
            label.delete(hlLabel)
            label.delete(tLabel)

            x1 = math.sign(currentDir) > 0 ? currentBar : lastBar
            llX2 = x1 + llBars
            hlX2 = x1 + hlBars
            tX2 = x1 + tBars

            y1 = math.sign(currentDir) > 0 ? currentPrice : lastPrice
            prev = math.sign(currentDir) > 0 ? lastPrice : llastPrice
            llY2 = math.round_to_mintick(y1 - math.abs(y1-prev)*llRatio)
            hlY2 = math.round_to_mintick(y1 - math.abs(y1-prev)*hlRatio)
            tY2 = math.round_to_mintick(y1 - math.abs(y1-prev)*tRatio)
            llLine := line.new(x1, y1, llX2, llY2, xloc=xloc.bar_index, color=color.red, style=line.style_arrow_right)
            hlLine := line.new(x1, y1, hlX2, hlY2, xloc=xloc.bar_index, color=color.orange, style=line.style_arrow_right)
            tLine := line.new(x1, y1, tX2, tY2, xloc=xloc.bar_index, color=color.yellow, style=line.style_arrow_right)

            llPercent = str.tostring(llStats*100/tStats, format.percent)
            hlPercent = str.tostring(hlStats*100/tStats, format.percent)
            llText = 'Number of Historical References :'+str.tostring(llStats)+'/'+str.tostring(tStats)+
                         '\nProbability of Lower Low :'+llPercent+
                         '\nAverage Lower Low Ratio :'+str.tostring(llRatio) +
                         '\nAverage Lower Low Bars :'+str.tostring(llBars)
            hlText = 'Number of Historical References :'+str.tostring(hlStats)+'/'+str.tostring(tStats)+
                         '\nProbability of Higher Low :'+hlPercent+
                         '\nAverage Higher Low Ratio :'+str.tostring(hlRatio) +
                         '\nAverage Higher Low Bars :'+str.tostring(hlBars)
            tText = 'Number of Historical References :'+str.tostring(tStats)+
                         '\nAverage Fib Ratio :'+str.tostring(tRatio)+
                         '\nAverage Bars :'+str.tostring(tBars)
            llLabel := label.new(llX2, llY2, str.tostring(llY2)+ ' - ' +llPercent, style=label.style_label_upper_left, color=color.new(color.red, 70), textcolor = color.white, size=size.small, tooltip=llText)
            hlLabel := label.new(hlX2, hlY2, str.tostring(hlY2)+ ' - ' +hlPercent, style=label.style_label_lower_left, color=color.new(color.orange, 70), textcolor = color.white, size=size.small, tooltip=hlText)
            tLabel := label.new(tX2, tY2, str.tostring(tY2)+ '@'+str.tostring(tRatio), style=label.style_label_left, color=color.new(color.yellow, 70), textcolor = color.white, size=size.small, tooltip=tText)
            llValue := llY2
            hlValue := hlY2
            llProbability := llStats*100/tStats
            ltRatio := tRatio

        if(llStats != 0 and hlStats != 0) and showTable
            table.cell(statsTable, plColStart, llr, llastPivotStatus, text_color = txtColor, text_size = txtSize, bgcolor = color.new(color.orange, 60))
            table.cell(statsTable, plColStart+1, llr, lastPivotStatus, text_color = txtColor, text_size = txtSize, bgcolor = color.new(color.lime, 60))
            table.cell(statsTable, plColStart+2, llr, str.tostring(llStats)+' - '+str.tostring(llRatio), text_color = txtColor, text_size = txtSize, bgcolor = color.new(color.red, highlight? 50 : 90), tooltip=llTooltip)
            table.cell(statsTable, plColStart+3, llr, str.tostring(hlStats)+' - '+str.tostring(hlRatio), text_color = txtColor, text_size = txtSize, bgcolor = color.new(color.green, highlight? 50 : 90), tooltip = hlTooltip)
            table.cell(statsTable, plColStart+4, llr, str.tostring(tStats)+' - '+str.tostring(tRatio), text_color = txtColor, text_size = txtSize, bgcolor = color.from_gradient(llStats/tStats, 0, 1, color.green, color.red), tooltip = tTooltip)
            llr+=1


if showPivotLines
    draw(valueMatrix, directionMatrix, ratioMatrix,  divergenceMatrix, doubleDivergenceMatrix, barArray, trendArray, newZG, doubleZG)

plot(drawSupertrend? supertrend:na, color=supertrendDir>0? color.green:color.red, style=plot.style_linebr)


hhp = plot(fillAreas?hhValue:na, 'Higher High Range', color.new(color.green, 100), 1)
lhp = plot(fillAreas?lhValue:na, 'Lower High Range', color.new(color.lime, 100), 1)
llp = plot(fillAreas?llValue:na, 'Lower Low Range', color.new(color.red, 100), 1)
hlp = plot(fillAreas?hlValue:na, 'Higher Low Range', color.new(color.orange, 100), 1)

fill(hhp, lhp, color=color.new(hhProbability > 50 ? color.green : hhProbability < 50? color.red : color.silver, 90))
fill(llp, hlp, color=color.new(llProbability > 50 ? color.red : llProbability < 50? color.green : color.silver, 90))

Overview

Next Pivot Projection [Trendoscope] draws projected pivot points, pivot lines, sentiment background fills, and an optional zigzag supertrend structure. Users can customize zigzag lengths, oscillator parameters, and toggle a detailed statistics table. Because pivot confirmation relies on zigzag mechanics, unconfirmed pivot points repaint until sufficient confirmation bars have closed.

Details

Published by

regalouisei on GitHub

Code credit

HeWhoMustNotBeNamed, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Support & resistance, Trend, Oscillators & momentum

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Lengthint8length
OscillatorstringrsioscillatorType
oscLengthint14oscLength
Historyint5supertrendLength
Draw Zigzag SupertrendboolfalsedrawSupertrend
Detailed StatsboolfalseshowTable
txtSizestringsize.tinytxtSize
txtColorcolorcolor.whitetxtColor
Pivot LinesbooltrueshowPivotLines
Pivot LabelboolfalseshowPivotLabel
Fill Bullish/Bearish SentimentsbooltruefillAreas
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

114 stars

This indicator calculates key support and resistance levels by analyzing the first and second momentum derivatives of a Double Exponential Moving Average (DEMA). When the second derivative (momentum of momentum) crosses zero, it identifies local turning points

Indicator v4 Support & resistanceOscillators & momentum
Updated 1 year ago · None stated GitHub repository of Support and Resistance Levels

Moon Dev Lunar Sniper

by moondevonyt
92 stars

Moon Dev Lunar Sniper is a multi-indicator overlay that scores confluence across fast and slow EMAs, RSI, MACD, DMI, and BB width to fire signals only when score meets the minimum threshold. It plots buy and sell signals on the price chart, highlights cosmic b

Indicator v6 TrendOscillators & momentum Repaints
Updated 8 weeks ago · Custom licence GitHub repository of Moon Dev Lunar Sniper
14 stars

Self-Aware Trend System calculates volatility-adaptive ATR trailing bands using a Trend Quality Index engine that evaluates market efficiency, momentum persistence, and structural pivots. It plots adaptive trend bands and character-flip trend signals directly

Indicator v6 TrendVolatility & bands
Updated 2 weeks ago · MIT GitHub repository of Self-Aware Trend System [WillyAlgoTrader]

SPY 0DTE Scalper - 15min

by chris-c-thomas
14 stars

This intraday overlay establishes directional bias and high-conviction swing setups for SPY on the 15-minute timeframe. It computes a weighted confluence score combining an EMA ribbon, a 50 EMA anchor, session VWAP bands, RSI divergence, ADX, TTM Squeeze, cand

Indicator v6 TrendOscillators & momentum
Updated 6 months ago · MIT GitHub repository of SPY 0DTE Scalper - 15min

Market Trend

by azamiftikhar1000
12 stars

Market Trend is an overlay indicator that identifies market direction and key price levels. It calculates a Relative Strength Index with Bollinger Bands to construct a 20-step color gradient reflecting momentum shifts. The script incorporates exponential movin

Indicator v4 TrendOscillators & momentum
Updated 3 years ago · None stated GitHub repository of Market Trend

BTE Acc AO SMIO

by f-cksociety
11 stars

This script provides an indicator-based backtesting engine framework that simulates trade entries, exits, and filtering directly on the price chart. It evaluates selected entry rules and multi-layered filters, including moving average trends, volume increases,

Indicator v5 TrendOscillators & momentum
Updated 4 years ago · MPL-2.0 GitHub repository of BTE Acc AO SMIO

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers