All-in-one CPR indicator 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// ©johntradingwick
// @version=4

//***************GUIDE***********************************
//CPR - Applicable only for daily pivots
//CPR S/R - All 3 S/R levels are disabled by default
//CPR Pivot - Central Pivot is a Red line by default and it's color can be changed from the settings
//CPR TC and BC - Top Range & Bottom Range. Both are Blue by default and can be changed from the settings
//Daily Pivots - Daily S1, S2, S3 and R1, R2, R3 are enabled by default. S4 and R4 can be enabled as per the need.
//Daily Pivots - Resistance(R) lines are Red in colour by default
//Daily Pivots - Support(S) lines are Green in colour by default

//Weekly Pivots - Pivot is Blue in color by default
//Weekly Pivots - 4 levels provided - S1/R1, S2/R2, S3/R3/ S4/R4. Disabled by default. Can be enabled from the settings.
//Weekly Pivots - Resistance(R) lines are Red in color by default
//Weekly Pivots - Support(S) lines are Green in color by default

//Monthly Pivots - Pivot is Blue in color by default
//Monthly Pivots - 3 levels provided - S1/R1, S2/R2, S3/R3. Disabled by default. Can be enabled from the settings.
//Monthly Pivots - Resistance(R) lines are Red in color by default
//Monthly Pivots - Support(S) lines are Green in color by default

//Tomorrow Pivots - Central Pivot is Red in color by default
//Tomorrow TC and BC - Top Range & Bottom Range. Both are Blue by default and can be changed from the settings
//Tomorrow Pivots - Resistance(R) lines are Red in color by default
//Tomorrow Pivots - Support(S) lines are Green in color by default

//Previous Day High (PDH) and Previous Day Low (PDL) is also provided.

//Simple Moving Average - 3 SMAs are provided because in general all the strategies can be built using the 20MA, 50MA and 200MA.

//VWAP - Volume Weighted Average Price is also provided for the convenience of the user. Can be useful in case of Futures.

// Hull Ribbon for trend identification

// Coloured volume candles to determine the strenngth of the trend


study("All-in-one CPR indicator by johntradingwick", overlay = true, shorttitle="All-in-one CPR", precision=1)
//******************CALCULATIONS**************************
//Central Pivot Range
pivot = (high + low + close) /3 //Central Povit
BC = (high + low) / 2 //Below Central povit
TC = (pivot - BC) + pivot //Top Central povot

//3  support levels
S1 = (pivot * 2) - high
S2 = pivot - (high - low)
S3 = low - 2 * (high - pivot)
S4 = low - 3 * (high - pivot)

//3  resistance levels
R1 = (pivot * 2) - low
R2 = pivot + (high - low)
R3 = high + 2 * (pivot-low)
R4 = high + 3 * (pivot-low)

//Checkbox inputs
CPRPlot = input(title = "Show Daily CPR", type=input.bool, defval=true)
DayS1R1 = input(title = "Show Daily S1/R1", type=input.bool, defval=false)
DayS2R2 = input(title = "Show Daily S2/R2", type=input.bool, defval=false)
DayS3R3 = input(title = "Show Daily S3/R3", type=input.bool, defval=false)
DayS4R4 = input(title = "Show Daily S4/R4", type=input.bool, defval=false)

WeeklyPivotInclude = input(title = "Show Weekly Pivot", type=input.bool, defval=false)
WeeklyS1R1 = input(title = "Show Weekly S1/R1", type=input.bool, defval=false)
WeeklyS2R2 = input(title = "Show Weekly S2/R2", type=input.bool, defval=false)
WeeklyS3R3 = input(title = "Show Weekly S3/R3", type=input.bool, defval=false)
WeeklyS4R4 = input(title = "Show Weekly S4/R4", type=input.bool, defval=false)

MonthlyPivotInclude = input(title = "Show Monthly Pivot", type=input.bool, defval=false)
MonthlyS1R1 = input(title = "Show Monthly S1/R1", type=input.bool, defval=false)
MonthlyS2R2 = input(title = "Show Monthly S2/R2", type=input.bool, defval=false)
MonthlyS3R3 = input(title = "Show Monthly S3/R3", type=input.bool, defval=false)

//******************DAYWISE CPR & PIVOTS**************************
// Getting daywise CPR
DayPivot = security(syminfo.tickerid, "D", pivot[1], barmerge.gaps_off, barmerge.lookahead_on)
DayBC = security(syminfo.tickerid, "D", BC[1], barmerge.gaps_off, barmerge.lookahead_on)
DayTC = security(syminfo.tickerid, "D", TC[1], barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks daywise for CPR
CPColour = DayPivot != DayPivot[1] ? color.red : color.red
BCColour = DayBC != DayBC[1] ? color.blue : color.blue 
TCColour = DayTC != DayTC[1] ? color.blue : color.blue 

//Plotting daywise CPR
plot(DayPivot, title = "Daily Pivot" , style = plot.style_circles, color=color.red, linewidth =2)
plot(CPRPlot ? DayBC : na , title = "Daily BC" , style = plot.style_circles, color=color.black, linewidth =2)
plot(CPRPlot ? DayTC : na , title = "Daily TC" , style = plot.style_circles, color=color.black, linewidth =2)

// Getting daywise Support levels
DayS1 = security(syminfo.tickerid, "D", S1[1], barmerge.gaps_off, barmerge.lookahead_on)
DayS2 = security(syminfo.tickerid, "D", S2[1], barmerge.gaps_off, barmerge.lookahead_on)
DayS3 = security(syminfo.tickerid, "D", S3[1], barmerge.gaps_off, barmerge.lookahead_on)
DayS4 = security(syminfo.tickerid, "D", S4[1], barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for daywise Support levels 
DayS1Color =DayS1 != DayS1[1] ? color.green : color.green
DayS2Color =DayS2 != DayS2[1] ? color.green : color.green
DayS3Color =DayS3 != DayS3[1] ? color.green : color.green
DayS4Color =DayS4 != DayS4[1] ? color.green : color.green

//Plotting daywise Support levels
plot(DayS1R1 ? DayS1 : na, title = "Daily S1" , style = plot.style_circles, color=color.green, linewidth =1)
plot(DayS2R2 ? DayS2 : na, title = "Daily S2" , style = plot.style_circles, color=color.green, linewidth =1)
plot(DayS3R3 ? DayS3 : na, title = "Daily S3" , style = plot.style_circles, color=color.green, linewidth =1)
plot(DayS4R4 ? DayS4 : na, title = "Daily S4" , style = plot.style_circles, color=color.green, linewidth =1)

// Getting daywise Resistance levels
DayR1 = security(syminfo.tickerid, "D", R1[1], barmerge.gaps_off, barmerge.lookahead_on)
DayR2 = security(syminfo.tickerid, "D", R2[1], barmerge.gaps_off, barmerge.lookahead_on)
DayR3 = security(syminfo.tickerid, "D", R3[1], barmerge.gaps_off, barmerge.lookahead_on)
DayR4 = security(syminfo.tickerid, "D", R4[1], barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for daywise Support levels
DayR1Color =DayR1 != DayR1[1] ? color.red : color.red
DayR2Color =DayR2 != DayR2[1] ? color.red : color.red
DayR3Color =DayR3 != DayR3[1] ? color.red : color.red
DayR4Color =DayR4 != DayR4[1] ? color.red : color.red

//Plotting daywise Resistance levels
plot(DayS1R1 ? DayR1 : na, title = "Daily R1" , style = plot.style_circles, color=color.red, linewidth =1)
plot(DayS2R2 ? DayR2 : na, title = "Daily R2" , style = plot.style_circles, color=color.red, linewidth =1)
plot(DayS3R3 ? DayR3 : na, title = "Daily R3" , style = plot.style_circles, color=color.red, linewidth =1)
plot(DayS4R4 ? DayR4 : na, title = "Daily R4" , style = plot.style_circles, color=color.red, linewidth =1)

//******************WEEKLY PIVOTS**************************

// Getting Weely Pivot
WPivot = security(syminfo.tickerid, "W", pivot[1], barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for Weely Pivot 
WeeklyPivotColor =WPivot != WPivot[1] ? na : color.blue

//Plotting Weely Pivot
plot(WeeklyPivotInclude ? WPivot:na, title = "Weekly Pivot" , style = plot.style_circles, linewidth =1)


// Getting Weekly Support levels
WS1 = security(syminfo.tickerid, "W", S1[1],barmerge.gaps_off, barmerge.lookahead_on)
WS2 = security(syminfo.tickerid, "W", S2[1],barmerge.gaps_off, barmerge.lookahead_on)
WS3 = security(syminfo.tickerid, "W", S3[1],barmerge.gaps_off, barmerge.lookahead_on)
WS4 = security(syminfo.tickerid, "W", S4[1],barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for weekly Support levels
WS1Color =WS1 != WS1[1] ? color.green : color.green
WS2Color =WS2 != WS2[1] ? color.green : color.green
WS3Color =WS3 != WS3[1] ? color.green : color.green
WS4Color =WS4 != WS4[1] ? color.green : color.green

//Plotting Weekly Support levels
plot(WeeklyS1R1 ? WS1 : na, title = "Weekly S1" , style = plot.style_circles, color=color.green, linewidth =1)
plot(WeeklyS2R2 ? WS2 : na, title = "Weekly S2" , style = plot.style_circles, color=color.green, linewidth =1)
plot(WeeklyS3R3 ? WS3 : na, title = "Weekly S3" , style = plot.style_circles, color=color.green, linewidth =1)
plot(WeeklyS4R4 ? WS4 : na, title = "Weekly S4" , style = plot.style_circles, color=color.green, linewidth =1)

// Getting Weekly Resistance levels
WR1 = security(syminfo.tickerid, "W", R1[1], barmerge.gaps_off, barmerge.lookahead_on)
WR2 = security(syminfo.tickerid, "W", R2[1], barmerge.gaps_off, barmerge.lookahead_on)
WR3 = security(syminfo.tickerid, "W", R3[1], barmerge.gaps_off, barmerge.lookahead_on)
WR4 = security(syminfo.tickerid, "W", R4[1], barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for weekly Resistance levels
WR1Color = WR1 != WR1[1] ? color.red : color.red
WR2Color = WR2 != WR2[1] ? color.red : color.red
WR3Color = WR3 != WR3[1] ? color.red : color.red
WR4Color = WR4 != WR4[1] ? color.red : color.red

//Plotting Weekly Resistance levels
plot(WeeklyS1R1 ? WR1 : na , title = "Weekly R1" , style = plot.style_circles, color=color.red, linewidth =1)
plot(WeeklyS2R2 ? WR2 : na , title = "Weekly R2" , style = plot.style_circles, color=color.red, linewidth =1)
plot(WeeklyS3R3 ? WR3 : na , title = "Weekly R3" , style = plot.style_circles, color=color.red, linewidth =1)
plot(WeeklyS4R4 ? WR4 : na , title = "Weekly R4" , style = plot.style_circles, color=color.red, linewidth =1)

//******************MONTHLY PIVOTS**************************

// Getting Monhly Pivot
MPivot = security(syminfo.tickerid, "M", pivot[1],barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for Monthly Support levels 
MonthlyPivotColor =MPivot != MPivot[1] ? na : color.blue

//Plotting  Monthly Pivot
plot(MonthlyPivotInclude? MPivot:na, title = " Monthly Pivot" , style = plot.style_circles, linewidth =2)

// Getting  Monthly Support levels
MS1 = security(syminfo.tickerid, "M", S1[1],barmerge.gaps_off, barmerge.lookahead_on)
MS2 = security(syminfo.tickerid, "M", S2[1],barmerge.gaps_off, barmerge.lookahead_on)
MS3 = security(syminfo.tickerid, "M", S3[1],barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for Monthly Support levels
MS1Color =MS1 != MS1[1] ? color.green : color.green
MS2Color =MS2 != MS2[1] ? color.green : color.green
MS3Color =MS3 != MS3[1] ? color.green : color.green

//Plotting  Monthly Support levels
plot(MonthlyS1R1 ? MS1 : na, title = "Monthly S1" , style = plot.style_circles, color=color.green, linewidth =1)
plot(MonthlyS2R2 ? MS2 : na, title = "Monthly S2" , style = plot.style_circles, color=color.green, linewidth =1)
plot(MonthlyS3R3 ? MS3 : na, title = "Monthly S3" , style = plot.style_circles, color=color.green, linewidth =1)


// Getting  Monthly Resistance levels
MR1 = security(syminfo.tickerid, "M", R1[1],barmerge.gaps_off, barmerge.lookahead_on)
MR2 = security(syminfo.tickerid, "M", R2[1],barmerge.gaps_off, barmerge.lookahead_on)
MR3 = security(syminfo.tickerid, "M", R3[1],barmerge.gaps_off, barmerge.lookahead_on)

MR1Color =MR1 != MR1[1] ? color.red : color.red
MR2Color =MR2 != MR2[1] ? color.red : color.red
MR3Color =MR3 != MR3[1] ? color.red : color.red


//Plotting  Monthly Resistance levels
plot(MonthlyS1R1 ? MR1 : na , title = "Monthly R1" , style = plot.style_circles, color=color.red, linewidth =1)
plot(MonthlyS2R2 ? MR2 : na , title = "Monthly R2" , style = plot.style_circles, color=color.red, linewidth =1)
plot(MonthlyS3R3 ? MR3 : na , title = "Monthly R3" , style = plot.style_circles, color=color.red, linewidth =1)



// Calculate Tomorrow's CPR
// Get High, Low and Close
th_d = security(syminfo.tickerid, "D", high, barmerge.gaps_off, barmerge.lookahead_on)
tl_d = security(syminfo.tickerid, "D", low, barmerge.gaps_off, barmerge.lookahead_on)
tc_d = security(syminfo.tickerid, "D", close, barmerge.gaps_off, barmerge.lookahead_on)

// Pivot Range
tP = (th_d + tl_d + tc_d) / 3 
tTC = (th_d + tl_d)/2
tBC = (tP - tTC) + tP

// Resistance Levels
tR3 = th_d + 2*(tP - tl_d)
tR2 = tP + (th_d - tl_d)
tR1 = (tP * 2) - tl_d

// Support Levels
tS1 = (tP * 2) - th_d
tS2 = tP - (th_d - tl_d)
tS3 = tl_d - 2*(th_d - tP)

disp_tomorrowcpr = input(title="Show Tomorrow Pivots", type=input.bool, defval=false)

plot (disp_tomorrowcpr ? tP : na, title="Tomorrow Pivot", linewidth=1, color=color.red, style=plot.style_circles)
plot (disp_tomorrowcpr ? tTC : na, title="Tomorrow TC", linewidth=1, color=color.blue, style=plot.style_circles)
plot (disp_tomorrowcpr ? tBC : na, title="Tomorrow BC", linewidth=1, color=color.blue, style=plot.style_circles)
plot (disp_tomorrowcpr ? tS1 : na, title="Tomorrow S1", linewidth=1, color=color.green, style=plot.style_circles)
plot (disp_tomorrowcpr ? tS2 : na, title="Tomorrow S2", linewidth=1, color=color.green, style=plot.style_circles)
plot (disp_tomorrowcpr ? tS3 : na, title="Tomorrow S3", linewidth=1, color=color.green, style=plot.style_circles)
plot (disp_tomorrowcpr ? tR1 : na, title="Tomorrow R1", linewidth=1, color=color.red, style=plot.style_circles)
plot (disp_tomorrowcpr ? tR2 : na, title="Tomorrow R2", linewidth=1, color=color.red, style=plot.style_circles)
plot (disp_tomorrowcpr ? tR3 : na, title="Tomorrow R3", linewidth=1, color=color.red, style=plot.style_circles)

//*****************************INDICATORS**************************

//SMA 1
PlotSMA1 = input(title = "Plot SMA 1", type=input.bool, defval=false)
SMALength1 = input(title="SMA Length", type=input.integer, defval=20)
SMASource1 = input(title="SMA Source", type=input.source, defval=close)
SMAvg1 = sma (SMASource1, SMALength1)
plot(PlotSMA1 ? SMAvg1 : na,  color= color.green, title="SMA 1")

//SMA 2
PlotSMA2 = input(title = "Plot SMA 2", type=input.bool, defval=false)
SMALength2 = input(title="SMA Length", type=input.integer, defval=50)
SMASource2 = input(title="SMA Source", type=input.source, defval=close)
SMAvg2 = sma (SMASource2, SMALength2)
plot(PlotSMA2 ? SMAvg2 : na,  color= color.blue, title="SMA 2")

//SMA 3
PlotSMA3 = input(title = "Plot SMA 3", type=input.bool, defval=false)
SMALength3 = input(title="SMA Length", type=input.integer, defval=200)
SMASource3 = input(title="SMA Source", type=input.source, defval=close)
SMAvg3 = sma (SMASource3, SMALength3)
plot(PlotSMA3 ? SMAvg3 : na,  color= color.blue, title="SMA 3")

//VWAP
PlotVWAP = input(title = "Plot VWAP?", type=input.bool, defval=false)
VWAPSource = input(title="VWAP Source", type=input.source, defval=close)
VWAPrice = vwap (VWAPSource)
plot(PlotVWAP ? VWAPrice : na,  color= color.teal, title="VWAP")

//PDH and PDL
pdh = security(syminfo.tickerid, "D", high[1], barmerge.gaps_off, barmerge.lookahead_on)
pdl = security(syminfo.tickerid, "D", low[1], barmerge.gaps_off, barmerge.lookahead_on)

plot(pdh, "Previous Day High", color.black, linewidth = 1, style = plot.style_circles)
plot(pdl, "Previous Day Low", color.black, linewidth = 1, style = plot.style_circles)

//MARKET STRUCTURE
//Description: Enhanced Zig Zag indicator that plot points on the chart whenever prices reverse by a percentage greater than a pre-chosen variable.



// Hull trend

tf = input("10", title = "Timeframe for HMA", type = input.resolution, options = ["1", "5", "10", "15", "30", "60","120", "240","360","720", "D", "W", "Y"])
src = input(hl2)
length = input(24, title = "Period", type = input.integer)
shift = input(0, title = "Offset", type = input.integer)

showcross = input(true, "Show Buy/Sell signal")

hma(_src, _length)=>
    wma((2 * wma(_src, _length / 2)) - wma(_src, _length), round(sqrt(_length)))
    
hma3(_src, _length)=>
    p = length/2
    wma(wma(close,p/3)*3 - wma(close,p/2) - wma(close,p),p)


a = security(syminfo.tickerid, tf, hma(src, length)[shift])
b =security(syminfo.tickerid, tf, hma3(src, length)[shift])
c = b > a ? color.lime : color.red
p1 = plot(a,color=c,linewidth=1,transp=75)
p2 = plot(b,color=c,linewidth=1,transp=75)
fill(p1,p2,color=c,transp=25)
crossdn = a > b and a[1] < b[1]
crossup = b > a and b[1] < a[1]
plotshape(showcross and crossdn ? a : na, location=location.absolute, style=shape.labeldown, color=color.red, size=size.tiny, text="Sell", textcolor=color.white, transp=0, offset=0)
plotshape(showcross and crossup ? a : na, location=location.absolute, style=shape.labelup, color=color.green, size=size.tiny, text="Buy", textcolor=color.white, transp=0, offset=0)
alertcondition(showcross and crossdn ? a : na, title = 'hma long', message = 'hma: long ')
alertcondition(showcross and crossup ? a : na, title = 'hma short', message = 'hma: short ')
//


len=input(20)
// Draw channel.
channelType = input(defval="Bollinger", title="Channel Type", options=["NONE", "Donchian", "Bollinger", "Envelope"])
channelLen = input(14, minval=1, title="Channel Length")
envPer = input(4, title="Envelope %", type=input.float, minval=0) / 100

getChannels(src, len) =>
    if channelType == "Bollinger"
        ma = ema(src, len)
        dev = stdev(src, len)
        bbUp = ma + 2*dev
        bbDown = ma - 2*dev
        [bbUp, bbDown]
    else
        if channelType == "Donchian"
            donUp = highest(high[1], len)
            donDown = lowest(low[1], len)
            [donUp, donDown]
        else 
            if channelType == "Envelope"
                ma = ema(src, len)
                envUp = ma * (1 + envPer)
                envDown = ma * (1 - envPer)
                [envUp, envDown]
            else 
                [na, na]

[upperchannel, lowerchannel] = getChannels(src, len)
u = plot(upperchannel, linewidth=1, color=color.black, title="Band High")
plot(avg(upperchannel, lowerchannel), linewidth=1, color=color.red, title="Band Middle")
l = plot(lowerchannel, linewidth=1, color=color.black, title="Band Low")
fill(u, l)

// Coloured volume candles

lengthbar=input(13, "length", minval=1)
avrg=sma(volume,lengthbar)

vold1 = volume > avrg*1.5 and close<open
vold2 = volume >= avrg*0.5 and volume<=avrg*1.5 and close<open
vold3 = volume < avrg *0.5 and close<open

volu1 = volume > avrg*1.5 and close>open
volu2 = volume >= avrg*0.5 and volume<=avrg*1.5 and close>open
volu3 = volume< avrg*0.5 and close>open


cold1=color.maroon 
cold2=color.red
cold3=color.gray


colu1=color.green
colu2=color.lime
colu3=color.gray


color = vold1 ? cold1 : vold2 ? cold2 : vold3 ? cold3 : volu1 ? colu1 : volu2 ? colu2 : volu3 ? colu3 : na

barcolor(color)