RAHUL CPR
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/



study("RAHUL CPR", overlay = true, shorttitle="C",precision=1)
//******************LOGICS**************************
//cenral 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)

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

//Checkbox inputs
CPRPlot = input(title = "Plot Daily CPR?", type=input.bool, defval=true)
CPRPlot1 = input(title = "Plot Weekly CPR?", type=input.bool, defval=true)
CPRPlot2 = input(title = "Plot Monthly CPR?", type=input.bool, defval=true)
DayS1R1 = input(title = "Plot Daiy S1/R1?", type=input.bool, defval=true)
DayS2R2 = input(title = "Plot Daiy S2/R2?", type=input.bool, defval=false)
DayS3R3 = input(title = "Plot Daiy S3/R3?", type=input.bool, defval=false)
//WeeklyPivotInclude = input(title = "Plot Weekly Pivot?", type=input.bool, defval=false)
//WeeklyS1R1 = input(title = "Plot weekly S1/R1?", type=input.bool, defval=false)
//WeeklyS2R2 = input(title = "Plot weekly S2/R2?", type=input.bool, defval=false)
//WeeklyS3R3 = input(title = "Plot weekly S3/R3?", type=input.bool, defval=false)
//MonthlyPivotInclude = input(title = "Plot Montly Pivot?", type=input.bool, defval=false)
//MonthlyS1R1 = input(title = "Plot Monthly S1/R1?", type=input.bool, defval=false)
//MonthlyS2R2 = input(title = "Plot Monthly S2/R2?", type=input.bool, defval=false)
//MonthlyS3R3 = input(title = "Plot Montly S3/R3?", type=input.bool, defval=false)
//DailyHigh = input(title = "Plot Daily High?", type=input.bool, defval=false)
//DailyLow = input(title = "Plot Daily High?", 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 daywse for CPR
CPColour = DayPivot != DayPivot[1] ? na : color.blue
BCColour = DayBC != DayBC[1] ? na : color.blue
TCColour = DayTC != DayTC[1] ? na : color.blue

//Plotting daywise CPR
plot(DayPivot, title = "CP" , color = CPColour, style = plot.style_linebr, linewidth =2)
plot(CPRPlot ? DayBC : na , title = "BC" , color = BCColour, style = plot.style_line, linewidth =1)
plot(CPRPlot ? DayTC : na , title = "TC" , color = TCColour, style = plot.style_line, linewidth =1)

// Getting weekwise CPR
WkPivot = security(syminfo.tickerid, "W", pivot[1], barmerge.gaps_off, barmerge.lookahead_on)
WBC = security(syminfo.tickerid, "W", BC[1], barmerge.gaps_off, barmerge.lookahead_on)
WTC = security(syminfo.tickerid, "W", TC[1], barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks weekwse for CPR
CPColour1 = WkPivot != WkPivot[1] ? na : color.blue
BCColour1 = WBC != WBC[1] ? na : color.blue
TCColour1 = WTC != WTC[1] ? na : color.blue

//Plotting weekwise CPR
plot(WkPivot, title = "CP" , color = CPColour1, style = plot.style_linebr, linewidth =3)
plot(CPRPlot1 ? WBC : na , title = "BC" , color = BCColour1, style = plot.style_line, linewidth =1)
plot(CPRPlot1 ? WTC : na , title = "TC" , color = TCColour1, style = plot.style_line, linewidth =1)

// Getting Mwise CPR
MPivot = security(syminfo.tickerid, "M", pivot[1], barmerge.gaps_off, barmerge.lookahead_on)
MBC = security(syminfo.tickerid, "M", BC[1], barmerge.gaps_off, barmerge.lookahead_on)
MTC = security(syminfo.tickerid, "M", TC[1], barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks Mwse for CPR
CPColour2 = MPivot != MPivot[1] ? na : color.blue
BCColour2 = MBC != MBC[1] ? na : color.blue
TCColour2 = MTC != MTC[1] ? na : color.blue

//Plotting Mwise CPR
plot(MPivot, title = "CP" , color = CPColour2, style = plot.style_linebr, linewidth =4)
plot(CPRPlot2 ? MBC : na , title = "BC" , color = BCColour2, style = plot.style_line, linewidth =1)
plot(CPRPlot2 ? MTC : na , title = "TC" , color = TCColour2, style = plot.style_line, linewidth =1)




// 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)
//Adding linebreaks for daywise Support levels 
DayS1Color =DayS1 != DayS1[1] ? na : color.green
DayS2Color =DayS2 != DayS2[1] ? na : color.green
DayS3Color =DayS3 != DayS3[1] ? na : color.green

//Plotting daywise Support levels
plot(DayS1R1 ? DayS1 : na, title = "D-S1" , color = DayS1Color, style = plot.style_line, linewidth =1)
plot(DayS2R2 ? DayS2 : na, title = "D-S2" , color = DayS2Color, style = plot.style_line, linewidth =1)
plot(DayS3R3 ? DayS3 : na, title = "D-S3" , color = DayS3Color, style = plot.style_line, 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)

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

//Plotting daywise Resistance levels
plot(DayS1R1 ? DayR1 : na, title = "D-R1" , color = DayR1Color, style = plot.style_line, linewidth =1)
plot(DayS2R2 ? DayR2 : na, title = "D-R2" , color = DayR2Color, style = plot.style_line, linewidth =1)
plot(DayS3R3 ? DayR3 : na, title = "D-R3" , color = DayR3Color, style = plot.style_line, 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 = "W-P" , color = WeeklyPivotColor, style = plot.style_circles, linewidth =1)


// Getting Weely Support levels
//WS1 = security(syminfo.tickerid, "W", S1[1],barmerge.gaps_off, barmerge.lookahead_on)
//WS1 = security(syminfo.tickerid, 'D', high[1])
//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)

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

//Plotting Weely Support levels
//plot(WeeklyS1R1 ? WS1 : na, title = "W-S1" , color = WS1Color, style = plot.style_cross, linewidth =1)
//plot(WeeklyS2R2 ? WS2 : na, title = "W-S2" , color = WS2Color, style = plot.style_cross, linewidth =1)
//plot(WeeklyS3R3 ? WS3 : na, title = "W-S3" , color = WS3Color, style = plot.style_cross, linewidth =1)

// Getting Weely 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)

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

//Plotting Weely Resistance levels
//plot(WeeklyS1R1 ? WR1 : na , title = "W-R1" , color = WR1Color, style = plot.style_cross, linewidth =1)
//plot(WeeklyS2R2 ? WR2 : na , title = "W-R2" , color = WR2Color, style = plot.style_cross, linewidth =1)
//plot(WeeklyS3R3 ? WR3 : na , title = "W-R3" , color = WR3Color, style = plot.style_cross, 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  Monhly Pivot
//plot(MonthlyPivotInclude? MPivot:na, title = " M-P" , color = MonthlyPivotColor, style = plot.style_line, linewidth =1)

// Getting  Monhly 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 Montly Support levels
//MS1Color =MS1 != MS1[1] ? na : color.green
//MS2Color =MS2 != MS2[1] ? na : color.green
//MS3Color =MS3 != MS3[1] ? na : color.green

//Plotting  Monhly Support levels
//plot(MonthlyS1R1 ? MS1 : na, title = "M-S1" , color = MS1Color, style = plot.style_circles, linewidth =1)
//plot(MonthlyS2R2 ? MS2 : na, title = "M-S2" , color = MS2Color, style = plot.style_circles, linewidth =1)
//plot(MonthlyS3R3 ? MS3 : na, title = "M-S3" , color = MS3Color, style = plot.style_circles, linewidth =1)

// Getting  Monhly 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] ? na : color.red
//MR2Color =MR2 != MR2[1] ? na : color.red
//MR3Color =MR3 != MR3[1] ? na : color.red

//Plotting  Monhly Resistance levels
//plot(MonthlyS1R1 ? MR1 : na , title = "M-R1", color = MR1Color, style = plot.style_circles , linewidth =1)
//plot(MonthlyS2R2 ? MR2 : na , title = "M-R2" , color = MR2Color, style = plot.style_circles, linewidth =1)
//plot(MonthlyS3R3 ? MR3 : na, title = "M-R3" , color = MR3Color, style = plot.style_circles, linewidth =1)

//*****************************INDICATORs**************************

//SMA
PlotSMA = input(title = "Plot SMA?", type=input.bool, defval=true)
SMALength = input(title="SMA Length", type=input.integer, defval=50)
SMASource = input(title="SMA Source", type=input.source, defval=close)
SMAvg = sma (SMASource, SMALength)
plot(PlotSMA ? SMAvg : na,  color= color.orange, title="SMA")

//EMA
PlotEMA = input(title = "Plot EMA?", type=input.bool, defval=true)
EMALength = input(title="EMA Length", type=input.integer, defval=50)
EMASource = input(title="EMA Source", type=input.source, defval=close)
EMAvg = ema (EMASource, EMALength)
plot(PlotEMA ? EMAvg : na,  color= color.red, title="EMA")

//EMA
//PlotEMA1 = input(title = "Plot EMA?", type=input.bool, defval=true)
//EMALength1 = input(title="EMA Length", type=input.integer, defval=20)
//EMASource1 = input(title="EMA Source", type=input.source, defval=close)
//EMAvg1 = ema (EMASource1, EMALength1)
//plot(PlotEMA1 ? EMAvg : na,  color= color.blue, title="EMA")

//VWAP
PlotVWAP = input(title = "Plot VWAP?", type=input.bool, defval=true)
VWAPSource = input(title="VWAP Source", type=input.source, defval=close)
VWAPrice = vwap (VWAPSource)
plot(PlotVWAP ? VWAPrice : na,  color= color.teal,linewidth = 2, title="VWAP")
//SuperTrend
PlotSTrend = input(title = "Plot Super Trend?", type=input.bool, defval=true)
InputFactor=input(3, minval=1,maxval = 100, title="Factor")
InputLength=input(10, minval=1,maxval = 100, title="Lenght")
BasicUpperBand=hl2-(InputFactor*atr(InputLength))
BasicLowerBand=hl2+(InputFactor*atr(InputLength))
FinalUpperBand=0.0
FinalLowerBand=0.0
FinalUpperBand:=close[1]>FinalUpperBand[1]? max(BasicUpperBand,FinalUpperBand[1]) : BasicUpperBand
FinalLowerBand:=close[1]<FinalLowerBand[1]? min(BasicLowerBand,FinalLowerBand[1]) : BasicLowerBand
IsTrend=0.0
IsTrend:= close > FinalLowerBand[1] ? 1: close< FinalUpperBand[1]? -1: nz(IsTrend[1],1)
STrendline = IsTrend==1? FinalUpperBand: FinalLowerBand
linecolor = IsTrend == 1 ? color.green : color.red
Plotline = (PlotSTrend? STrendline: na)
plot(Plotline, color = linecolor , style = plot.style_line , linewidth = 1,title = "SuperTrend")
PlotShapeUp = cross(close,STrendline) and close>STrendline
PlotShapeDown = cross(STrendline,close) and close<STrendline
//plotshape(PlotSTrend?  PlotShapeUp: na, "Up Arrow", shape.triangleup,location.belowbar,color.green,0,0)
//plotshape(PlotSTrend?  PlotShapeDown: na , "Down Arrow", shape.triangledown , location.abovebar, color.red,0,0)
//BB
//@version=4
//study(shorttitle="BB", title="Bollinger Bands", overlay=true, resolution="")
length = input(20, minval=1)
src = input(close, title="Source")
mult = input(2.0, minval=0.001, maxval=50, title="StdDev")
basis = sma(src, length)
dev = mult * stdev(src, length)
upper = basis + dev
lower = basis - dev
offset = input(0, "Offset", type = input.integer, minval = -500, maxval = 500)
plot(basis, "Basis", color=#872323, offset = offset)
p1 = plot(upper, "Upper", color=color.teal, offset = offset)
p2 = plot(lower, "Lower", color=color.teal, offset = offset)
fill(p1, p2, title = "Background", color=#198787, transp=98)
//HL
//D_High = security(syminfo.tickerid, 'D', high) 
//D_Low = security(syminfo.tickerid, 'D', low[1]) 
//D_Close =  security(tickerid, 'D', close[1]) 
//D_Open =  security(tickerid, 'D', open[1]) 
//DHColour = D_High != D_High ? na : color.red
//DLColour = D_Low != D_Low[1] ? na : color.blue
//TCColour = DayTC != DayTC[1] ? na : color.blue


//plot(DailyHigh ? D_High : na, title="Daily High",style=plot.style_line,linewidth=1) 
//plot(DailyLow ? D_Low : na, title="Daily Low",style=plot.style_line, linewidth=1)
//D_High = security(syminfo.tickerid, 'D', high[1]) 
//D_Low = security(syminfo.tickerid, 'D', low[1]) 
//D_Close =  security(tickerid, 'D', close[1]) 
//D_Open =  security(tickerid, 'D', open[1]) 
//plot(DailyHigh ? D_High : na, title="Daily High",style=plot.style_line,linewidth=1) 
//plot(DailyLow ? D_Low : na, title="Daily Low",style=plot.style_line, linewidth=1)



//plot(isintraday ? D_High : na, title="Daily High",style=line, color=blue,linewidth=1) 
//plot(isintraday ? D_Low : na, title="Daily Low",style=line, color=blue,linewidth=1) 

//D W M 
show_pd = input(title="Show Prev Day H/L", defval=true)
show_pw = input(title="Show Prev Week H/L", defval=true)
show_pm = input(title="Show Prev Month H/L", defval=true)

[pdh, pdl] = security(syminfo.ticker, "D", [high[1], low[1]], lookahead=true)
plot(show_pd ? pdh : na, color=color.purple)
plot(show_pd ? pdl : na, color=color.purple)

[pwh, pwl] = security(syminfo.ticker, "W", [high[1], low[1]], lookahead=true)
plot(show_pw ? pwh : na, color=color.purple, linewidth = 2)
plot(show_pw ? pwl : na, color=color.purple, linewidth = 2)

[pmh, pml] = security(syminfo.ticker, "M", [high[1], low[1]], lookahead=true)
plot(show_pm ? pmh : na, color=color.red, linewidth = 3)
plot(show_pm ? pml : na, color=color.red, linewidth = 3)


//LABEL
//on_off = input(title="On/Off ?", type=input.bool, defval=true)
//position_option = input(title="Position ?", defval="Above", options=["Above", "Below"])

//l = label.new(bar_index, na, 'Price '+tostring(close), 
 // color=close >= open ? color.green : color.red, 
 // textcolor=color.white,
 // style=label.style_labeldown, yloc=yloc.abovebar)

//label.delete(l[1])

// Plot Buy and Sell Labels
//plotshape(MPivot, title = "Buy Signal", text ="BUY", textcolor = color.white, style=shape.labelup, size = size.normal, location=location.belowbar, color = color.green, transp = 0)
//plotshape(PlotSMA, title = "Sell Signal", text ="SELL", textcolor = color.white, style=shape.labeldown, size = size.normal, location=location.abovebar, color = color.red, transp = 0)


//Plot Buy Signal
//buySignal = ST_Trend == 1 and ST_Trend[1] == -1
//buySignal = MPivot
//plotshape(buySignal, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
//plotshape(buySignal, title="BUY", text="BUY", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)

plotshape(MPivot,style=shape.labeldown,location=location.absolute,color=color.blue,textcolor=color.black,show_last=1,text="M.Pivot",offset = 15,transp=30)
plotshape(MBC,style=shape.labeldown,location=location.absolute,color=color.blue,textcolor=color.black,show_last=1,text="M.TC",offset = 15,transp=30)
plotshape(MTC,style=shape.labeldown,location=location.absolute,color=color.blue,textcolor=color.black,show_last=1,text="M.BC",offset = 15,transp=30)
plotshape(SMAvg,style=shape.labeldown,location=location.absolute,color=color.yellow,textcolor=color.black,show_last=1,text="sma",offset = 11,transp=30)
plotshape(EMAvg,style=shape.labeldown,location=location.absolute,color=color.red,textcolor=color.black,show_last=1,text="ema",offset = 11,transp=30)
plotshape(WkPivot,style=shape.labeldown,location=location.absolute,color=color.blue,textcolor=color.black,show_last=1,text="W.Pivot",offset = 15,transp=30)
plotshape(WBC,style=shape.labeldown,location=location.absolute,color=color.blue,textcolor=color.black,show_last=1,text="W.BC",offset = 15,transp=30)
plotshape(WTC,style=shape.labeldown,location=location.absolute,color=color.blue,textcolor=color.black,show_last=1,text="W.TC",offset = 15,transp=30)
plotshape(DayPivot,style=shape.labeldown,location=location.absolute,color=color.blue,textcolor=color.black,show_last=1,text="D.Pivot",offset = 25,transp=30)
plotshape(DayBC,style=shape.labeldown,location=location.absolute,color=color.blue,textcolor=color.black,show_last=1,text="D.BC",offset = 30,transp=30)
plotshape(DayTC,style=shape.labeldown,location=location.absolute,color=color.blue,textcolor=color.black,show_last=1,text="D.TC",offset = 35,transp=30)
plotshape(upper,style=shape.labeldown,location=location.absolute,color=color.green,textcolor=color.black,show_last=1,text="UBB",offset = 8,transp=30)
plotshape(lower,style=shape.labeldown,location=location.absolute,color=color.green,textcolor=color.black,show_last=1,text="LBB",offset = 8,transp=30)
plotshape(basis,style=shape.labeldown,location=location.absolute,color=color.green,textcolor=color.black,show_last=1,text="MBB",offset = 8,transp=30)
plotshape(STrendline,style=shape.labeldown,location=location.absolute,color=color.red,textcolor=color.black,show_last=1,text="S.T.",offset = 15,transp=30)
plotshape(VWAPrice,style=shape.labeldown,location=location.absolute,color=color.black,textcolor=color.black,show_last=1,text="VWAP",offset = 15,transp=30)
plotshape(pdh,style=shape.labeldown,location=location.absolute,color=color.purple,textcolor=color.black,show_last=1,text="PD.HIGH",offset = 10,transp=30)
plotshape(pdl,style=shape.labeldown,location=location.absolute,color=color.purple,textcolor=color.black,show_last=1,text="PD.LOW",offset = 10,transp=30)
plotshape(pwh,style=shape.labeldown,location=location.absolute,color=color.purple,textcolor=color.black,show_last=1,text="PW.HIGH",offset = 10,transp=30)
plotshape(pwl,style=shape.labeldown,location=location.absolute,color=color.purple,textcolor=color.black,show_last=1,text="PW.LOW",offset = 10,transp=30)
//plotshape(pmh,style=shape.labeldown,location=location.absolute,color=color.purple,textcolor=color.black,show_last=1,text="PM.HIGH",offset = 10,transp=30)
//plotshape(pml,style=shape.labeldown,location=location.absolute,color=color.purple,textcolor=color.black,show_last=1,text="PM.LOW",offset = 10,transp=30)


