ATR Pivots
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tradeblinks
//Based on my own ideas and ideas from satymahajan.

//@version=5
indicator('ATR Pivots', shorttitle='ATR Pivots', overlay=true)
// Candle Pattern
InsideCandle = input(title='Inside Candle', defval=false,group='plot')
IC = high < high[1] and low > low[1] ? 1 : 0
plotshape(InsideCandle ? IC : na, title='Inside Candle', text='IC', color=color.new(color.aqua, 60), style=shape.diamond, textcolor=color.new( color.aqua,50), location=location.belowbar)
// Input Options
atr_length = input(14, 'ATR Length')
_Index = input.string('Nifty', 'Dash for Index', options=['BankNifty', 'Nifty'], group='Dash Settings')
t1 = _Index == 'Nifty' ? 'NSE:NIFTY': 'NSE:BANKNIFTY'
ticker1 = _Index == 'Nifty' ? 'NSE:NIFTY1!':'NSE:BANKNIFTY1!'
ATR_Levels = input(title='ATR all Fib Levels', defval=false,group='plot')

// Data
ticker = ticker.new(syminfo.prefix, syminfo.ticker, session=session.regular)
previous_close = request.security(ticker, 'D', close[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
atr = request.security(ticker, 'D', ta.atr(atr_length)[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
period_high = request.security(ticker, 'D', high[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
period_low = request.security(ticker, 'D', low[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
range_1 = period_high - period_low
tr_percent_of_atr = range_1 / atr * 100
lower_0236 = previous_close - atr * 0.236
upper_0236 = previous_close + atr * 0.236
lower_0382 = previous_close - atr * 0.382
upper_0382 = previous_close + atr * 0.382
lower_0500 = previous_close - atr * 0.5
upper_0500 = previous_close + atr * 0.5
lower_0618 = previous_close - atr * 0.618
upper_0618 = previous_close + atr * 0.618
lower_0786 = previous_close - atr * 0.786
upper_0786 = previous_close + atr * 0.786
lower_1000 = previous_close - atr
upper_1000 = previous_close + atr

// Add levels
plot(lower_1000, color=color.new(color.red, 0), linewidth=1, title='-100', style=plot.style_circles)
plot(ATR_Levels? lower_0786 : na, color=color.new(color.red, 0), linewidth=1, title='-78.6', style=plot.style_cross)
plot(lower_0618, color=color.new(color.red, 0), linewidth=1, title='-61.8', style=plot.style_circles)
plot(ATR_Levels? lower_0500 : na, color=color.new(color.red, 0), linewidth=1, title='-50.0', style=plot.style_cross)
plot(ATR_Levels? lower_0382 : na, color=color.new(color.red, 0), linewidth=1, title='-38.2', style=plot.style_cross)
plot(lower_0236, color=color.new(color.red, 0), linewidth=1, title='-23.6', style=plot.style_circles)
plot(upper_0236, color=color.new(color.green, 0), linewidth=1, title='23.6', style=plot.style_circles)
plot(ATR_Levels? upper_0382 : na, color=color.new(color.green, 0), linewidth=1, title='38.2', style=plot.style_cross)
plot(ATR_Levels? upper_0500 : na, color=color.new(color.green, 0), linewidth=1, title='50.0', style=plot.style_cross)
plot(upper_0618, color=color.new(color.green, 0), linewidth=1, title='61.8', style=plot.style_circles)
plot(ATR_Levels? upper_0786 : na, color=color.new(color.green, 0), linewidth=1, title='78.6', style=plot.style_cross)
plot(upper_1000, color=color.new(color.green, 0), linewidth=1, title='100', style=plot.style_circles)
plot(previous_close, color=color.new(color.aqua, 0), linewidth=1, title='Previous Close', style=plot.style_circles)
// PDH / PDL
PHPL = input(title='PDH/PDL', defval=true,group='plot')
PH = high
PL = low
PDH = request.security(syminfo.tickerid, 'D', PH[1], barmerge.gaps_off, barmerge.lookahead_on)
PDL = request.security(syminfo.tickerid, 'D', PL[1], barmerge.gaps_off, barmerge.lookahead_on)
PDHColour = PDH != PDH[1] ? na : color.black
PDLColour = PDL != PDL[1] ? na : color.black
plot(PHPL ? PDH : na, title='PDH', color=PDHColour, style=plot.style_line, linewidth=2)
plot(PHPL ? PDL : na, title='PDL', color=PDLColour, style=plot.style_line, linewidth=2)
//EMA Cloud
EMA_Cloud = input(title='EMA Cloud', defval=false,group='plot')
EMA = input.color(color.black)
len = input.int(200, minval=1, title=" EMA Length")
src = close
ema = ta.ema(src, len)
tema=plot(ema, title="EMA", color=EMA, linewidth=1)
FEMA = input.color(color.aqua,title="Fast EMA",group='EMA Cloud')
len1 = input.int(5, minval=1, title=" Fast EMA Length",group='EMA Cloud')
src1 = close
ema1 = ta.ema(src1, len1)
fema1=plot(EMA_Cloud ? ema1 : na, title="Fast EMA", color=FEMA, linewidth=1)
SEMA = input.color(color.blue,title="Slow EMA",group='EMA Cloud')
len2 = input.int(21, minval=1, title=" Slow EMA Length",group='EMA Cloud')
src2 = close
ema2 = ta.ema(src2, len2)
fema2=plot(EMA_Cloud ? ema2 : na, title="Slow EMA", color=SEMA, linewidth=1)
Bull = ema1 > ema2
Bear = ema1 < ema2
cloud = Bull ? input.color(color.new(color.green,95),"Bullish",group='EMA Cloud') : Bear ? input.color(color.new(color.red,95),"Bearish",group='EMA Cloud') : na
fill(fema1,fema2, color = cloud)
//ADR side of script below
text_ADR1_high = 'ADR1 Upper'
text_ADR2_high = 'ADR2 Upper'
text_ADR1_low = 'ADR1 Lower'
text_ADR2_low = 'ADR2 Lower'
//***Start of Inputs
ADR = input(title='ADR', defval=true,group='plot')
var labels_enabled = input.bool(defval=true, title='Show labels')
var adr_1 = input.int(title='ADR 1 Period', defval = 10, minval = 1, group='ADR')
var adr_2 = input.int(title='ADR 2 Period', defval = 5, minval = 1, group='ADR')
var color_ADR1_high = input.color(defval=color.new(color.red  ,0), title=text_ADR1_high, group='ADR')
var color_ADR2_high = input.color(defval=color.new(color.red,0), title=text_ADR2_high, group='ADR')
var color_ADR1_low = input.color(defval=color.new(color.green  ,0),   title=text_ADR1_low, group='ADR')
var color_ADR2_low = input.color(defval=color.new(color.green,0),  title=text_ADR2_low, group='ADR')
adrUppercolorfill = input.color(color.red, "Upper Zone Fill",group='ADR',tooltip="Color of zone between Upper Adr 1 and 2")
adrlowercolorfill = input.color(color.green, "Lower Zone Fill",group='ADR',tooltip="Color of zone between Lower Adr 1 and 2")
var plot_history = input.bool(defval=true, group='ADR',title='Historical levels')
//***Start of local functions definiton***
draw_line(_x1, _y1, _x2, _y2, _xloc, _extend, _color, _style, _width) =>
    dline = line.new(x1=_x1, y1=_y1, x2=_x2, y2=_y2, xloc=_xloc, extend=_extend, color=_color, style=_style, width=_width)
    line.delete(dline[1])

//If security is futures - replace the ticker with continuous contract
tickerid_func() =>
    tickerid = syminfo.tickerid
    if syminfo.type == 'futures'
        tickerid := syminfo.root + '1!'

//Function to calculate ADR levels
//Parameters:
//  * lengthInput - ADR period
//  * hi_low - true-->High, else-->Low 
adr_func(lengthInput,hi_low) =>
    float result = 0
    float adr = 0
    open_ = request.security(tickerid_func(), "D", open, barmerge.gaps_off, barmerge.lookahead_on)
	adr_High = request.security(tickerid_func(), "D", ta.sma(high[1], lengthInput), barmerge.gaps_off, barmerge.lookahead_on)
    adr_Low = request.security(tickerid_func(), "D", ta.sma(low[1], lengthInput), barmerge.gaps_off, barmerge.lookahead_on)
    adr := (adr_High - adr_Low)  
    if hi_low//High
        result := adr/2 + open_
    else    //Low
        result := open_ - adr/2    

transp_func() =>
    transp_0 = 0

//***End of local functions definiton***
//***Start of getting data  
start_time = request.security(tickerid_func(), "D", time_close[1],barmerge.gaps_off,barmerge.lookahead_on)
adr1_high = adr_func(adr_1,true)
adr1_low = adr_func(adr_1,false)
adr2_high = adr_func(adr_2,true)
adr2_low = adr_func(adr_2,false)
//***End of getting data
float _adr1_high = na
float _adr1_low = na
float _adr2_high = na
float _adr2_low = na
var show_chart = false
if timeframe.isintraday
    show_chart := true
//Plot all days
if show_chart and plot_history
    _adr1_high := adr1_high
    _adr1_low  := adr1_low
    _adr2_high := adr2_high
    _adr2_low  := adr2_low

ADR1H = plot(ADR ? _adr1_high : na, title=text_ADR1_high, color=color_ADR1_high, style=plot.style_circles,linewidth = 0)
ADR2H = plot(ADR ? _adr2_high : na, title=text_ADR2_high, color=color_ADR2_high, style=plot.style_circles,linewidth = 0)
ADR1L = plot(ADR ?_adr1_low : na, title=text_ADR1_low, color=color_ADR1_low, style=plot.style_circles,linewidth = 0)
ADR2L = plot(ADR ?_adr2_low : na, title=text_ADR2_low, color=color_ADR2_low, style=plot.style_circles,linewidth = 0)
fill(ADR1H,ADR2H,color.new(adrUppercolorfill,80))
fill(ADR1L,ADR2L,color.new(adrlowercolorfill,80))

if show_chart and not plot_history
    draw_line1 = line.new(start_time, adr1_high, time, adr1_high, xloc.bar_time, extend.none, color_ADR1_high, line.style_dotted, 2)
    line.delete(draw_line1[1])  
    draw_line2 = line.new(start_time, adr2_high, time, adr2_high, xloc.bar_time, extend.none, color_ADR2_high, line.style_dotted, 2)
    line.delete(draw_line2[1])   
    draw_line3 = line.new(start_time, adr1_low, time, adr1_low, xloc.bar_time, extend.none, color_ADR1_low, line.style_dotted, 2)
    line.delete(draw_line3[1])
    draw_line4 = line.new(start_time, adr2_low, time, adr2_low, xloc.bar_time, extend.none, color_ADR2_low, line.style_dotted, 2)  
    line.delete(draw_line4[1]) 
    linefill.new(draw_line1, draw_line2, color.new(adrUppercolorfill,80))
    linefill.new(draw_line3, draw_line4, color.new(adrlowercolorfill, 80))
//Dash
normalcolor =    color.new(color.aqua,0 ) 
colorchangefirst =  color.new(color.green,0 ) 
Bullish =    color.new(color.green,0 ) 
Bearish =    color.new(color.red,0 ) 
Top_Right = position.top_right
Bottom_Right = position.bottom_right
DashPlacement = input.string(Top_Right, "Dash Location",options = [Top_Right, Bottom_Right],group='Dash Settings')
//Day ATR
t1atr = request.security(t1, 'D', ta.atr(atr_length)[1])
t1ATR = math.round(t1atr,2)
t1dtr = request.security(t1,'D', math.round(high - low,2))
t1atrp = math.round(t1dtr/t1atr * 100,2)
// 1min ATR
TimeFrame =  input.timeframe("1", "Timeframe for ATR Intraday", tooltip="Time you want Indtradat ATR based on",group='Dash Settings')
atr1 = request.security(ticker1, TimeFrame, ta.atr(14))
atrindex = math.round(atr1,2)

dtr_color = color.gray
if t1atrp <=23.6
    dtr_color := color.gray
else if t1atrp >= 78.6
    dtr_color := color.red
else
    dtr_color := color.green

//RSI
rsiVal = input(defval=14, title='RSI Input',group='Dash Settings')
rsiMax = input(defval=55, title='RSI Top',group='Dash Settings')
rsiMin = input(defval=45, title='RSI Bottom',group='Dash Settings')
indexRSI= request.security(_Index,"",ta.rsi(close, rsiVal))
rsiIndexColor = indexRSI >= rsiMax ? Bullish : indexRSI <= rsiMin ? Bearish : normalcolor

//Draw Table
var table ATR = table.new(DashPlacement, 4, 6,frame_color=color.new(color.black, 0), frame_width = 1, border_color= color.new(color.black, 40), border_width = 1)
if barstate.islast
    table.cell(ATR,1,1,text = _Index == 'Nifty' ? 'NIFTY ATR Dash' : 'BANKNIFTY ATR Dash',text_color=#e53238,bgcolor=color.new(color.black,90))
    table.cell(ATR,1,2,text='ATR   |  DTR  |   DTR% ',bgcolor=color.new(color.black,90))
    table.cell(ATR,1,3, str.tostring(t1ATR, '#') + '  |  ' + str.tostring(t1dtr, '#')+ '  |  ' + str.tostring(t1atrp, '#.#')+"%",text_color=color.new(dtr_color,0),bgcolor=color.new(dtr_color,70))
    table.cell(ATR,1,4, text = _Index == 'Nifty' ? TimeFrame + "min ATR : " + str.tostring(atrindex,format.mintick) :TimeFrame + "min ATR : " + str.tostring(atrindex,format.mintick),text_color=_Index == 'Nifty' ? color.new(atr1 > 11? colorchangefirst:normalcolor,0) :color.new(atr1 > 30? colorchangefirst:normalcolor,0),bgcolor=_Index == 'Nifty' ? color.new(atr1 > 11? colorchangefirst:normalcolor,70) :color.new(atr1 > 30? colorchangefirst:normalcolor,70))
    table.cell(ATR,1,5, text = _Index == 'Nifty' ? 'RSI : ' + str.tostring(indexRSI,format.mintick) : 'RSI : ' + str.tostring(indexRSI,format.mintick),text_color=color.new(rsiIndexColor,0),bgcolor= color.new(rsiIndexColor,70))
    
//Table Logo
var table logo = table.new(position.top_center, 1, 1)
if barstate.islast
    table.cell(logo, 0, 0, "tradeblinks",text_color =#e53238)

//END 
