Bank Nifty Scalping
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DanSib

//@version=5
indicator(title="Bank Nifty Scalping", shorttitle = "DS", overlay=true)

//===== MULTI EMA 20,50,100,200 =====
showmema = input(true, title="Show Multi EMA 20/50/100/200", group = "EMA")
plot((showmema ? ta.ema(close, 20) : na), "EMA 20", style=plot.style_line, color=color.gray , linewidth=1)
plot((showmema ? ta.ema(close, 50) : na), "EMA 50", style=plot.style_line, color=color.maroon , linewidth=1, display=display.none)
plot((showmema ? ta.ema(close, 100) : na), "EMA 100", style=plot.style_line, color=color.blue, linewidth=1, display=display.none)
plot((showmema ? ta.ema(close, 200) : na), "EMA 200", style=plot.style_line, color=color.black, linewidth=2)

//===== 9 EMA 5 Minutes =====
showema = input(true, title="Show 9 EMA 5 Mins", group = "EMA")
out = request.security(syminfo.tickerid, "5" , ta.ema(close, 9))
plot(showema ? out : na, "9 EMA", color=color.orange , offset = 0)

//===== VWAP =====
showVWAP = input.bool(title = "Show VWAP", defval=true, group = "VWAP")
VWAPSource = input.source(title="VWAP Source", defval=hl2, group = "VWAP")
VWAPrice = ta.vwap(VWAPSource)
plot(showVWAP ? VWAPrice : na,  color= color.teal, title="VWAP", linewidth = 2)

//===== Super Trend 1 =====
showST = input(true, "Show SuperTrend Indicator 1", group = "Super Trend 1")
Period=input(title="ATR Period", defval=10)
Multiplier=input(title="ATR Multiplier", defval=2)

//Super Trend ATR
Up=hl2-(Multiplier*ta.atr(Period))
Dn=hl2+(Multiplier*ta.atr(Period))
var TUp = close
TUp:=close[1]>TUp[1]? math.max(Up,TUp[1]) : Up
var TDown = close
TDown:=close[1]<TDown[1]? math.min(Dn,TDown[1]) : Dn
var Trend = -1
Trend := close > TDown[1] ? 1: close< TUp[1]? -1: nz(Trend[1],1)

Tsl1 = Trend==1? TUp: TDown
Tsl2 = Trend==1? TDown: TUp

linecolor = Trend == 1 ? color.green : color.red

plot(showST ?Tsl1 : na, color = linecolor, style = plot.style_line , linewidth = 2,title = "SuperTrend 1")

//===== Super Trend 2 =====
showST2 = input(false, "Show SuperTrend Indicator 2", group = "Super Trend 2")
Period2=input(title="ATR Period", defval=10)
Multiplier2=input(title="ATR Multiplier", defval=3)

//Super Trend ATR
Up2=hl2-(Multiplier2*ta.atr(Period2))
Dn2=hl2+(Multiplier2*ta.atr(Period2))
var TUp2 = close
TUp2:=close[1]>TUp2[1]? math.max(Up2,TUp2[1]) : Up2
var TDown2 = close
TDown2:=close[1]<TDown2[1]? math.min(Dn2,TDown2[1]) : Dn2
var Trend2 = -1
Trend2 := close > TDown2[1] ? 1: close< TUp2[1]? -1: nz(Trend2[1],1)

Tsl12 = Trend2==1? TUp2: TDown2
Tsl22 = Trend2==1? TDown2: TUp2

linecolor2 = Trend2 == 1 ? color.green : color.red

plot(showST2 ?Tsl12 : na, color = linecolor2, style = plot.style_line , linewidth = 1,title = "SuperTrend 2")


//===== VWMA =====
showVWMA = input(true, "Show VWMA", group = "VWMA")
len = input.int(20, "Length", minval=1)
vsrc = input(close, "Source")
ma = ta.vwma(vsrc, len)
offset = input.int(0, "Offset", minval = -500, maxval = 500)
plot(showVWMA ? ma : na, title="VWMA", color=#2962FF, offset = offset)

//===== Camarilla Pivot =====
showcam = input.bool(true, title="Show Camarilla R3/R4/S3/S4", group = "Camarilla")

tc2 = input.string(title = "Camarilla Pivot Resolution", defval="D", options =["D","W","M"],group = "Camarilla")

//Get previous day/week bar and avoiding realtime calculation by taking the previous to current bar
[sopen,shigh,slow,sclose] = request.security(syminfo.tickerid, tc2, [open[1],high[1],low[1],close[1]], barmerge.gaps_off, barmerge.lookahead_on)
r = shigh-slow

//Calculate pivots
center=(sclose)
h1=sclose + r*(1.1/12)
h2=sclose + r*(1.1/6)
h3=sclose + r*(1.1/4)
h4=sclose + r*(1.1/2)
h5=(shigh/slow)*sclose
l1=sclose - r*(1.1/12)
l2=sclose - r*(1.1/6)
l3=sclose - r*(1.1/4)
l4=sclose - r*(1.1/2)
l5=sclose - (h5-sclose)

//Colors (<ternary conditional operator> expression prevents continuous lines on history)
c4=sopen != sopen[1] ? na : color.fuchsia
c3=sopen != sopen[1] ? na : color.fuchsia

//Plotting Camarilla
plot((showcam ? h4 : na), title="Camarilla R4",color=c4,style=plot.style_circles, linewidth=2)
plot((showcam ? h3 : na), title="Camarilla R3",color=c3, style=plot.style_circles, linewidth=2)
plot((showcam ? l3 : na), title="Camarilla S3",color=c3, style=plot.style_circles, linewidth=2)
plot((showcam ? l4 : na), title="Camarilla S4",color=c4, style=plot.style_circles, linewidth=2)

//===== CPR - 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)

CPRPlot = input.bool(title = "Show CPR", defval=true, group = "CPR")
DayS1R1 = input.bool(title = "Show Daiy S1/R1", defval=true, group = "CPR")
DayS2R2 = input.bool(title = "Show Daiy S2/R2", defval=false, group = "CPR")
DayS3R3 = input.bool(title = "Show Daiy S3/R3", defval=false, group = "CPR")

// Getting daywise CPR/Level 1, 2 & 3
[DayPivot, DayBC, DayTC, DayS1, DayS2, DayS3, DayR1, DayR2, DayR3]=request.security(syminfo.tickerid, "D", [pivot[1],BC[1],TC[1],S1[1],S2[1],S3[1],R1[1],R2[1],R3[1]], barmerge.gaps_off, barmerge.lookahead_on)

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

//Plotting daywise CPR
plot(CPRPlot ? DayPivot : na, title = "CPR_CP" , color = CPColour, style=plot.style_circles, linewidth =2)
plot(CPRPlot ? DayBC : na , title = "CPR_BC" , color = BCColour, style = plot.style_line, linewidth =1)
plot(CPRPlot ? DayTC : na , title = "CPR_TC" , color = TCColour, style = plot.style_line, linewidth =1)

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

//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 = "CPR-R1" , color = DayR1Color, style = plot.style_line, linewidth =1)
plot(DayS2R2 ? DayR2 : na, title = "CPR-R2" , color = DayR2Color, style = plot.style_line, linewidth =1)
plot(DayS3R3 ? DayR3 : na, title = "CPR-R3" , color = DayR3Color, style = plot.style_line, linewidth =1)

//===== Previous day High/Low =====
PH = high
PL= low
showPDHL = input.bool(true, title="Show PDH/PDL", group = "CPR")
[PDH,PDL] = request.security(syminfo.tickerid, 'D', [PH[1],PL[1]], lookahead=barmerge.lookahead_on)
//PDH = request.security(syminfo.tickerid, 'D', PH[1], lookahead=barmerge.lookahead_on)
//PDL = request.security(syminfo.tickerid, 'D', PL[1], lookahead=barmerge.lookahead_on)

plot(showPDHL ? PDH : na, title = "PDH", color=color.green, style=plot.style_circles, linewidth = 2, trackprice = true)
plot(showPDHL ? PDL : na, title = "PDL", color=color.red, style=plot.style_circles, linewidth = 2, trackprice = true)

//===== Fibonacci Pivot Points =====
showFibPiv = input(false, "Show Fibonacci Pivot Points", group = "Fib Pivot Points")
fpt = input.string(title = "Fibonacci Pivot Resolution", defval="D", options =["D","W","M"],group = "Fib Pivot Points")

//Get previous day/week bar and avoiding realtime calculation by taking the previous to current bar
[fopen,fhigh,flow,fclose] = request.security(syminfo.tickerid, fpt, [open[1],high[1],low[1],close[1]], barmerge.gaps_off, barmerge.lookahead_on)
fr = fhigh-flow
fp = (fhigh+flow+fclose)/3

fib_L1 = input(title="Insert Fib Level 1", defval=0.382)
fib_L2 = input(title="Insert Fib Level 2", defval=0.618)
fib_L3 = input(title="Insert Fib Level 3", defval=1.000)

//Calculate Fib pivots
fR1 = fp+(fr*fib_L1)
fS1 = fp-(fr*fib_L1)
fR2 = fp+(fr*fib_L2)
fS2 = fp-(fr*fib_L2)
fR3 = fp+(fr*fib_L3) 
fS3 = fp-(fr*fib_L3) 

//Colors (<ternary conditional operator> expression prevents continuous lines on history)
fc1=fopen != fopen[1] ? na : color.red
fc2=fopen != fopen[1] ? na : color.green

//Plotting Fibonacci Pivot Points
plot(showFibPiv ? fS1 : na, color=fc1, title="Fib-S1", style = plot.style_line, linewidth = 1) 
plot(showFibPiv ? fS2 : na, color=fc1, title="Fib-S2", style = plot.style_line, linewidth = 1) 
plot(showFibPiv ? fS3 : na, color=fc1, title="Fib-S3", style = plot.style_line, linewidth = 1) 
plot(showFibPiv ? fR1 : na, color=fc2, title="Fib-R1", style = plot.style_line, linewidth = 1) 
plot(showFibPiv ? fR2 : na, color=fc2, title="Fib-R2", style = plot.style_line, linewidth = 1) 
plot(showFibPiv ? fR3 : na, color=fc2, title="Fib-R3", style = plot.style_line, linewidth = 1)


//===== Elder Impulse =====
showEI = input(true, title="Show Elder Impulse", group = "Elder Impulse")

source = close

// MACD Options
macd_length_fast   = input.int(defval=12, minval=1, title="MACD Fast Length")
macd_length_slow   = input.int(defval=26, minval=1, title="MACD Slow Length")
macd_length_signal = input.int(defval=9,  minval=1, title="MACD Signal Length")
// Calculate MACD
macd_ma_fast       = ta.ema(source, macd_length_fast)
macd_ma_slow       = ta.ema(source, macd_length_slow)
macd               = macd_ma_fast - macd_ma_slow
macd_signal        = ta.ema(macd, macd_length_signal)
macd_histogram     = macd - macd_signal

// EMA Option
ema_length         = input.int(defval=13, minval=1, title="EMA Length")
// Calculate EMA
ema                = ta.ema(source, ema_length)

// Calculate Elder Impulse
elder_bulls        = (ema[0] > ema[1]) and (macd_histogram[0] > macd_histogram[1])
elder_bears        = (ema[0] < ema[1]) and (macd_histogram[0] < macd_histogram[1])
elder_color        = elder_bulls
                     ? color.green //  If Bulls Control Trend and Momentum
                     : elder_bears
                       ? color.red //  If Bears Control Trend and Mementum
                       : color.blue // If Neither Bulls or Bears Control the Market

barcolor(showEI ? elder_color : na, title = "Elder Impulse Bar Color")

// ====== Volume ======
ShowHighVolume = input(true, 'Show Strong Volume', group = "Strong Volume")
Averageval1 = input.int(title='Average Volume BankNifty: (in K)', defval=40, minval=1,group = "Strong Volume")
Averageval2 = input.int(title='Average Volume Nifty: (in K)', defval=100, minval=1,group = "Strong Volume")

Averageval = syminfo.ticker == 'BANKNIFTY1!' ? Averageval1 * 1000 : syminfo.ticker == 'NIFTY1!' ? Averageval2 * 1000 : ta.sma(volume, 20)
varstrong = volume >= Averageval
plotshape(ShowHighVolume ? varstrong : na,"Volume Shape", style=shape.square, location=location.bottom, color=color.blue)

//===== Inside Bar =====
showIB = input(false, "Show Inside Bar", group = "Inside Bar")

isInside() =>
    bodyStatus = (close >= open) ? 1 : -1
    isInsidePattern = high < high[1] and low > low[1]
    
    isInsidePattern ? bodyStatus : 0

// When is bullish
plotshape(showIB ? isInside() == 1 : na, style=shape.triangleup,
                 location=location.abovebar, color=color.green)

// When is bearish
plotshape(showIB ? isInside() == -1 : na, style=shape.triangledown,
                 location=location.belowbar, color=color.red)

isInsideBarMade = isInside() == 1 or isInside() == -1

alertcondition(showIB ? isInsideBarMade : na, title='Inside Bar', message='Inside Bar formed')