Pivot-Based Channels & Bands [Misu]
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © Fontiramisu

//@version=5
indicator("Pivot Channel-Bands [Misu]", shorttitle="P Channel-Band [Misu]", overlay=true)

// import Fontiramisu/fontLib/80 as fontilab
import Fontiramisu/fontilab/6 as fontilab

// res = input.timeframe(title="Resolution", defval=timeframe.period)
// resClose = request.security(syminfo.tickerid, res, close, barmerge.gaps_off, barmerge.lookahead_on)
// resHigh = request.security(syminfo.tickerid, res, high, barmerge.gaps_off, barmerge.lookahead_on)
// resLow = request.security(syminfo.tickerid, res, low, barmerge.gaps_off, barmerge.lookahead_on)

showBands       = input.bool(false, title="Pivot Bands", group="show :")
showMidBands    = input.bool(false, title="Pivot Mid Band", group="show :")

showChannels    = input.bool(true, title="Pivot Channels", group="show :")
showMidChannel  = input.bool(true, title="Pivot Mid Channel", group="show :")

// ------ Find dev pivots ------ [
// -- Var user input --
var devTooltip              = "Deviation is a multiplier that affects how much the price should deviate from the previous pivot in order for the bar to become a new pivot."
var depthTooltip            = "The minimum number of bars that will be taken into account when analyzing pivots."
thresholdMultiplier         = input.float(title="Deviation", defval=2.5, minval=0, tooltip=devTooltip)
depth                       = input.int(title="Depth", defval=10, minval=1, tooltip=depthTooltip) 

// Prepare pivot variables
var line lineLast       = na
var int iLast           = 0 // Index last
var int iPrev           = 0 // Index previous
var float pLast         = 0 // Price last
var float pLastHigh     = 0 // Price last
var float pLastLow      = 0 // Price last
var isHighLast          = false // If false then the last pivot was a pivot low
isPivotUpdate           = false
pivotHighHist           = array.new<float>(0,0)
pivotLowHist            = array.new<float>(0,0)


// Get pivot information from dev pivot finding function
[dupLineLast, dupIsHighLast, dupIPrev, dupILast, dupPLast, dupPLastHigh, dupPLastLow] = 
     fontilab.getDeviationPivots(thresholdMultiplier, depth, lineLast, isHighLast, iLast, pLast, true, close, high, low)//resClose, resHigh, resLow)

if not na(dupIsHighLast)
    lineLast := dupLineLast
    isHighLast := dupIsHighLast
    iPrev := dupIPrev
    iLast := dupILast
    pLast := dupPLast
    pLastHigh := dupPLastHigh
    pLastLow := dupPLastLow
    isPivotUpdate := true


// Get last Pivots.
var highP   = 0.0
var lowP    = 0.0
var midP    = 0.0
highP       := isHighLast ? pLast : highP
lowP        := not isHighLast ? pLast : lowP
midP        := (highP + lowP)/2

// Calculate predicated pivots, support and resistances.
pPivot = (highP + lowP + close)/3
supp1 = 2 * pPivot - highP 
resi1 = 2 * pPivot - lowP
supp2 = pPivot - (resi1 - supp1)
resi2 = pPivot + (resi1 - supp1)


// Plot.
 // Channels.
pPivotPlot = plot(pPivot, title='pPivot', linewidth=2, display=showMidChannel ? display.all : display.none, editable=true, color=color.yellow)

supp1Plot  = plot(supp1, title='supp1', linewidth=1, display=showChannels ? display.all : display.none, editable=true, color=color.green)
supp2Plot  = plot(supp2, title='supp2', linewidth=1, display=showChannels ? display.all : display.none, editable=true, color=color.green)

resi1Plot  = plot(resi1, title='resi1', linewidth=1, display=showChannels ? display.all : display.none, color=color.red)
resi2Plot  = plot(resi2, title='resi2', linewidth=1, display=showChannels ? display.all : display.none, color=color.red)

 // Bands.
midPPlot   = plot(midP, title='Mid Pivot', linewidth=2, display=showMidBands ? display.all : display.none, color=color.yellow)
highPlot   = plot(highP, title='High Pivot', linewidth=2, display=showBands ? display.all : display.none, color=color.red)
lowPlot    = plot(lowP, title='Low Pivot', linewidth=2, display=showBands ? display.all : display.none, color=color.green)


fill(supp1Plot, supp2Plot, color.new(color.green, 90), 'Upper Area', display=showChannels ? display.all : display.none)
fill(resi1Plot, resi2Plot, color.new(color.red, 90), 'Lower Area', display=showChannels ? display.all : display.none)


// ] ------ Find dev pivots ------ [
// show_table = input(true,  "Show table with stats", group = "Drawings")

// if show_table
//     var table ATHtable = table.new(position.bottom_right, 10, 10, frame_color = color.gray, bgcolor = color.gray, border_width = 1, frame_width = 1, border_color = color.white)
    
//     table.cell(ATHtable, 1, 0, text="pLastLow", bgcolor=color.green, tooltip="")
//     table.cell(ATHtable, 1, 0, text=str.tostring(pLastLow), bgcolor=color.green, tooltip="")
    
    // table.cell(ATHtable, 0, 1, text="resWeights", bgcolor=color.silver, tooltip="")
    // table.cell(ATHtable, 1, 1, text=textresWeights, bgcolor=color.green, tooltip="")
    
    // table.cell(ATHtable, 0, 2, text="resBarIndexes", bgcolor=color.silver, tooltip="")
    // table.cell(ATHtable, 1, 2, text=textresBarIndexes, bgcolor=color.green, tooltip="")
    
    // table.cell(ATHtable, 0, 3, text="resNbHighs", bgcolor=color.silver, tooltip="")
    // table.cell(ATHtable, 1, 3, text=textresNbHighs, bgcolor=color.green, tooltip="")
    
    // table.cell(ATHtable, 0, 4, text="resNbLows", bgcolor=color.silver, tooltip="")
    // table.cell(ATHtable, 1, 4, text=textresNbLows, bgcolor=color.green, tooltip="")

    // table.cell(ATHtable, 0, 5, text="Prices", bgcolor=color.silver, tooltip="")
    // table.cell(ATHtable, 1, 5, text=str.tostring(textTopResPrices), bgcolor=color.green, tooltip="")
    
    // table.cell(ATHtable, 0, 6, text="Weights", bgcolor=color.silver, tooltip="")
    // table.cell(ATHtable, 1, 6, text=str.tostring(textTopResWeights), bgcolor=color.green, tooltip="")

// ]

    

