Patrick Walker Flat Base
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jadeja_rajdeep

//@version=5
indicator("Patrick Walker Flat Base",overlay = true,max_bars_back = 1260)

lookback=input.int(defval =  20,title =  "Look back periods",minval =  20,maxval =  252,step =  1,tooltip = "Will look back no of days to find the occurrences")
maxbars=input.int(defval =  252,title =  "Max Bars Back",minval = 20,maxval =  252,step =  1,tooltip = "This will help to draw recent resistance if its not found in lookback periods, Max Bars Back always atleast double the value of Look back periods. it will create loop deviding the lookback periods. for example lookback is 20 and maxbars is 252 it will loop 13 times with 20 days loop. and draw only recent one.")

gap_21_50=input.int(defval = 10,title =  "Gap Between EMA 21 - 50 <=",minval = 1,maxval = 50,step =  1,tooltip = "its used in top resistance if gap is <= then only the resistance is draws on chart.")

vavgday=input.int(defval = 36,title =  "Volume sma days",minval = 1,maxval = 252,step =  1,tooltip = "its used in down day resistance")

display_bottom_resistance=input.bool(true, "Display Bottom Resistance",group = "display")
display_top_resistance=input.bool(true, "Display Top Resistance",group = "display")
display_top_resistance2=input.bool(true, "Display Top Resistance 2",group = "display")
display_downday_resistance=input.bool(true, "Display Down Days Resistance",group = "display")
display_pivot5_resistance=input.bool(true, "Display 5 Days Pivot Point",group = "display")
display_pivot10_resistance=input.bool(true, "Display 10 Days Pivot Point",group = "display")
display_pivot20_resistance=input.bool(true, "Display 20 Days Pivot Point",group = "display")
display_high252_resistance=input.bool(true, "Display 252 Days High Resistance",group = "display")
display_high252_recent=input.bool(true, "Display Only Recent 252 Days High Resistance",group = "display")
display_rs_table=input.bool(true, "Display RS Table",group = "display")

color_top_resistance_line=input.color(defval = color.green,title = "Top Resistance Line Color",group =  "Colors")
color_top_resistance_support=input.color(defval = color.red,title = "Top Resistance Support Line Color",group =  "Colors")

color_top_resistance2_line=input.color(defval = color.lime,title = "Top Resistance Line 2 Color",group =  "Colors")
color_top_resistance2_support=input.color(defval = color.red,title = "Top Resistance Support Line 2 Color",group =  "Colors")

color_bottom_resistance_line=input.color(defval = color.green,title = "Bottom Resistance Line Color",group =  "Colors")
color_bottom_resistance_support=input.color(defval = color.red,title = "Bottom Resistance Support Line Color",group =  "Colors")

color_downday_resistance_line=input.color(defval = #e91e63,title = "DownDay Resistance Line Color",group =  "Colors")
color_downday_resistance_support=input.color(defval = color.red,title = "DownDay Resistance Support Line Color",group =  "Colors")

color_pivot5_resistance_line=input.color(defval =color.aqua,title = "5Day Pivot Point Resistance Line Color",group =  "Colors")
color_pivot5_resistance_support=input.color(defval = color.yellow,title = "5Day Pivot Point Resistance Support Line Color",group =  "Colors")

color_pivot10_resistance_line=input.color(defval =color.aqua,title = "10Day Pivot Point Resistance Line Color",group =  "Colors")
color_pivot10_resistance_support=input.color(defval = color.yellow,title = "10Day Pivot Point Resistance Support Line Color",group =  "Colors")

color_pivot20_resistance_line=input.color(defval =color.aqua,title = "20Day Pivot Point Resistance Line Color",group =  "Colors")
color_pivot20_resistance_support=input.color(defval = color.yellow,title = "20Day Pivot Point Resistance Support Line Color",group =  "Colors")

color_high252_resistance_line=input.color(defval =color.aqua,title = "252 Days High Resistance Line Color",group =  "Colors")
color_high252_resistance_support=input.color(defval = color.yellow,title = "252 Days High Resistance Support Line Color",group =  "Colors")

//displaying rs21 and rs50 line in table
i_nifty50 = input.symbol("NIFTY", "Symbol")
NIFTY50 = request.security(i_nifty50, 'D', close)
NIFTY50_21 = request.security(i_nifty50, 'D', close[20])
NIFTY50_50 = request.security(i_nifty50, 'D', close[49])
NIFTY50_200 = request.security(i_nifty50, 'D', close[199])

if(display_rs_table==true)
    rsline=(close/NIFTY50)
    rsline21=ta.ema(rsline,21)
    rsline50=ta.ema(rsline,50)
    rsline200=ta.ema(rsline,200)

    rs21=close / close[20]
    rs50=close / close[49]
    rs200=close / close[199]
    rs21_index=NIFTY50 / NIFTY50_21
    rs50_index=NIFTY50 / NIFTY50_50
    rs200_index=NIFTY50 / NIFTY50_200

    rs_21=rs21/rs21_index
    rs_50=rs50/rs50_index
    rs_200=rs200/rs200_index

    if barstate.islast
        var rstable = table.new(position = position.top_center,columns = 6, rows = 2)
        rsline21text=(rsline >= rsline21 ? "Yes" : "No")
        rsline50text=(rsline >= rsline50 ? "Yes" : "No")
        rsline200text=(rsline >= rsline200 ? "Yes" : "No")
        
        rsline21textcolor=(rsline >= rsline21 ? color.green : color.red)
        rsline50textcolor=(rsline >= rsline50 ? color.green : color.red)
        rsline200textcolor=(rsline >= rsline200 ? color.green : color.red)

        rs21textcolor=(rs_21 >= 1 ? color.green : color.red)
        rs50textcolor=(rs_50 >= 1 ? color.green : color.red)
        rs200textcolor=(rs_200 >= 1 ? color.green : color.red)

        table.cell(rstable,0,0,"RsLine > EMA21",text_halign = text.align_right,text_size = size.small)
        table.cell(rstable,1,0,rsline21text,text_color=rsline21textcolor,text_halign = text.align_left,text_size = size.small)

        table.cell(rstable,2,0,"RsLine > EMA50",text_halign = text.align_right,text_size = size.small)
        table.cell(rstable,3,0,rsline50text,text_color=rsline50textcolor,text_halign = text.align_left,text_size = size.small)

        table.cell(rstable,4,0,"RsLine > EMA200",text_halign = text.align_right,text_size = size.small)
        table.cell(rstable,5,0,rsline200text,text_color=rsline200textcolor,text_halign = text.align_left,text_size = size.small)

        table.cell(rstable,0,1,"RS21",text_halign = text.align_right,text_size = size.small)
        table.cell(rstable,1,1, str.tostring(math.round(rs_21,2)) ,text_color=rs21textcolor,text_halign = text.align_left,text_size = size.small)

        table.cell(rstable,2,1,"RS50",text_halign = text.align_right,text_size = size.small)
        table.cell(rstable,3,1,str.tostring(math.round(rs_50,2)),text_color=rs50textcolor,text_halign = text.align_left,text_size = size.small)

        table.cell(rstable,4,1,"RS200",text_halign = text.align_right,text_size = size.small)
        table.cell(rstable,5,1,str.tostring(math.round(rs_200,2)),text_color=rs200textcolor,text_halign = text.align_left,text_size = size.small)
//displaying rs21 and rs50 line in table

var line top_resistance = na
var line top_resistance_support = na
var label top_resistance_label = na

var line top_resistance2 = na
var line top_resistance2_support = na
var label top_resistance2_label = na

var line bottom_resistance = na
var line bottom_resistance_support = na
var label bottom_resistance_label = na

var line downday_resistance = na
var line downday_resistance_support = na
var label downday_resistance_label = na

var line pivot5_resistance = na
var line pivot5_resistance_support = na
var label pivot5_resistance_label = na

var line pivot10_resistance = na
var line pivot10_resistance_support = na
var label pivot10_resistance_label = na

var line pivot20_resistance = na
var line pivot20_resistance_support = na
var label pivot20_resistance_label = na

var line high252_resistance = na
var line high252_resistance_support = na
var label high252_resistance_label = na

var hh = 0.0
var hh_bar_index = 0

ema8=ta.ema(close,8)
ema21=ta.ema(close,21)
sma50=ta.sma(close,50)
ema200=ta.sma(close,200)
smav36=ta.sma(volume,vavgday)

plot(ema8,title = "EMA8",color=color.green)
plot(ema21,title = "EMA21",color=color.aqua)
plot(sma50,title = "SMA50",color=#e91e63)
plot(ema200,title = "EMA200",color=#9c27b0)

hh5=ta.highest(high,5) 
hc5=ta.highest(close,4)

hh10=ta.highest(high,10) 
hc10=ta.highest(close,9)

high252days=ta.highest(high,252)
hc15=ta.highest(close,15)
hh15=ta.highest(high,15)

if(bar_index == last_bar_index)
    maxbar_steps=math.ceil(maxbars/lookback)

    //bottom resistance code start 
    //removed close < ema21 condition
    if(display_bottom_resistance==true)
        for y= 1 to maxbar_steps
            found_recent=0
            for x = 1 to lookback*y
                if(ema8[x] < ema21[x] and close[x] < sma50[x] and high[x] >= sma50[x] and high[x] >= hh)
                    hh := high[x]
                    hh_bar_index := last_bar_index - x
                    line.delete(bottom_resistance)
                    bottom_resistance := line.new(x1=hh_bar_index,y1=high[x],x2=hh_bar_index + 1,y2=high[x],xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color_bottom_resistance_line)
                    label.delete(bottom_resistance_label)
                    bottom_resistance_label := label.new(x=hh_bar_index,y = high[x],text= str.tostring(math.round(high[x],2)), xloc=xloc.bar_index, yloc=yloc.price,color = color_bottom_resistance_line)
                    line.delete(bottom_resistance_support)
                    bottom_resistance_support := line.new(x1=hh_bar_index,y1=low[x],x2=hh_bar_index + 1,y2=low[x],xloc=xloc.bar_index,extend=extend.right,style=line.style_dashed,color=color_bottom_resistance_support)
                    found_recent := 1
            if(found_recent==1)
                break
    //bottom resistance code end

    hh := 0
    hh_bar_index := 0
    
    //top resistance code start
    if(display_top_resistance==true)
        for y= 1 to maxbar_steps
            found_recent=0
            for x = ((lookback*y) + 3) to 3
                if((ema8[x] >= sma50[x] or ema21[x] >= sma50[x]) and ema8[x] >= ema21[x] and low[x] > ema8[x] and (((ema21[x] - sma50[x]) * 100) / sma50[x]) <= gap_21_50)

                    check_pass_top = 1
                    if(ema200[x] > 0 and ema21[x] >= ema200[x] and ema200[x] < ema200[x + 1])
                        check_pass_top := 0

                    if(ema8[x + 1] < ema21[x + 1] or ema8[x + 2] < ema21[x + 2] or ema8[x + 3] < ema21[x + 3])
                        check_pass_top := 0

                    if(check_pass_top == 1 and high[x] > close[x - 1] and high[x] > close[x - 2] and high[x] > close[x - 3])
                        hh := high[x]
                        hh_bar_index := last_bar_index - x
                        line.delete(top_resistance)
                        top_resistance := line.new(x1=hh_bar_index,y1=high[x],x2=hh_bar_index + 1,y2=high[x],xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color_top_resistance_line)
                        label.delete(top_resistance_label)
                        top_resistance_label := label.new(x=hh_bar_index,y = high[x],text= str.tostring(math.round(high[x],2)), xloc=xloc.bar_index, yloc=yloc.price,color = color_top_resistance_line)
                        line.delete(top_resistance_support)
                        top_resistance_support := line.new(x1=hh_bar_index,y1=low[x],x2=hh_bar_index + 1,y2=low[x],xloc=xloc.bar_index,extend=extend.right,style=line.style_dashed,color=color_top_resistance_support)
                        found_recent := 1
            if(found_recent==1)
                break
    //top resistance code end

    hh := 0
    hh_bar_index := 0

    //top resistance 2 code start
    if(display_top_resistance2==true)
        for y= 1 to maxbar_steps
            found_recent=0
            for x = 1 to lookback*y

                check_pass_top = 1
                if(ema200[x] > 0 and ema21[x] >= ema200[x] and ema200[x] < ema200[x + 1])
                    check_pass_top := 0
                
                if(ema8[x + 1] < ema21[x + 1] or ema8[x + 2] < ema21[x + 2] or ema8[x + 3] < ema21[x + 3])
                    check_pass_top := 0

                if(check_pass_top == 1 and (open[x] < ema21[x] or close[x] < ema21[x]) and high[x] >= ema8[x]  and ema8[x] >= ema21[x] and ema21[x] >= sma50[x]  and (((ema21[x] - sma50[x]) * 100) / sma50[x]) <= gap_21_50 and (high[x] >= hh or high[x + 1] >= hh))
                    hh := math.max(high[x],high[x + 1]) 
                    hh_bar_index := last_bar_index - x
                    line.delete(top_resistance2)
                    top_resistance2 := line.new(x1=hh_bar_index,y1=hh,x2=hh_bar_index + 1,y2=hh,xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color_top_resistance2_line)
                    label.delete(top_resistance2_label)
                    top_resistance2_label := label.new(x=hh_bar_index,y = hh,text= str.tostring(math.round(hh,2)), xloc=xloc.bar_index, yloc=yloc.price,color = color_top_resistance2_line)
                    line.delete(top_resistance2_support)
                    top_resistance2_support := line.new(x1=hh_bar_index,y1=low[x],x2=hh_bar_index + 1,y2=low[x],xloc=xloc.bar_index,extend=extend.right,style=line.style_dashed,color=color_top_resistance2_support)
                    found_recent := 1
            if(found_recent==1)
                break
    //top resistance 2 code end

    //down days resistance code start
    if(display_downday_resistance==true)
        for y= 1 to maxbars
            avg=(high[y] + low[y])/2
            if(close[y] >= avg and close[y] < open[y] and (close[y] >= ema21[y] or close[y] >= sma50[y] or volume[y] >= smav36[y]))
                hh_bar_index := last_bar_index - y
                line.delete(downday_resistance)
                downday_resistance := line.new(x1=hh_bar_index,y1=high[y],x2=hh_bar_index + 1,y2=high[y],xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color_downday_resistance_line)
                label.delete(downday_resistance_label)
                downday_resistance_label := label.new(x=hh_bar_index,y = high[y],text= str.tostring(math.round(high[y],2)), xloc=xloc.bar_index, yloc=yloc.price,color = color_downday_resistance_line)
                line.delete(downday_resistance_support)
                downday_resistance_support := line.new(x1=hh_bar_index,y1=low[y],x2= hh_bar_index + 1,y2=low[y],xloc=xloc.bar_index,extend=extend.right,style=line.style_dashed,color=color_downday_resistance_support)
                break
    //down days resistance code end

    //5 days pivot point code start
    if(display_pivot5_resistance==true)
        for y= 0 to maxbars
            if(high[y + 4] >= hh5[y + 4] and high[y + 4] > hc5[y])
                line.delete(pivot5_resistance)
                pivot5_resistance := line.new(x1=last_bar_index - (y + 4) ,y1=high[y + 4],x2=last_bar_index - (y + 3),y2=high[y + 4],xloc=xloc.bar_index,extend=extend.right,style=line.style_dashed,color=color_pivot5_resistance_line)
                label.delete(pivot5_resistance_label)
                pivot5_resistance_label := label.new(x=last_bar_index - (y + 4),y = high[y + 4],text= str.tostring(math.round(high[y + 4],2)), xloc=xloc.bar_index, yloc=yloc.price,color =color_pivot5_resistance_line)
                line.delete(pivot5_resistance_support)
                pivot5_resistance_support := line.new(x1=last_bar_index - (y + 4),y1=low[y + 4],x2=last_bar_index - (y + 3),y2=low[y + 4],xloc=xloc.bar_index,extend=extend.right,style=line.style_dashed,color=color_pivot5_resistance_support)
                break
    //5 days pivot point code end

    //10 days pivot point code start starting from 0
    if(display_pivot10_resistance==true)
        for y= 0 to maxbars
            if(high[y + 9] >= hh10[y + 9] and high[y + 9] > hc10[y])
                line.delete(pivot10_resistance)
                pivot10_resistance := line.new(x1=last_bar_index - (y + 9) ,y1=high[y + 9],x2=last_bar_index - (y + 8),y2=high[y + 9],xloc=xloc.bar_index,extend=extend.right,style=line.style_dashed,color=color_pivot10_resistance_line)
                label.delete(pivot10_resistance_label)
                pivot10_resistance_label := label.new(x=last_bar_index - (y + 9),y = high[y + 9],text= str.tostring(math.round(high[y + 9],2)), xloc=xloc.bar_index, yloc=yloc.price,color = color_pivot10_resistance_line)
                line.delete(pivot10_resistance_support)
                pivot10_resistance_support := line.new(x1=last_bar_index - (y + 9),y1=low[y + 9],x2=last_bar_index - (y + 8),y2=low[y + 9],xloc=xloc.bar_index,extend=extend.right,style=line.style_dashed,color=color_pivot10_resistance_support)
                break
    //10 days pivot point code end

    //20 days pivot point code start
    if(display_pivot20_resistance==true)
        for y= 0 to maxbars
            if(high[y + 19] >= hh10[y + 19] and high[y + 19] > hc10[y])
                line.delete(pivot20_resistance)
                pivot20_resistance := line.new(x1=last_bar_index - (y + 19) ,y1=high[y + 19],x2=last_bar_index - (y + 18),y2=high[y + 19],xloc=xloc.bar_index,extend=extend.right,style=line.style_dashed,color=color_pivot20_resistance_line)
                label.delete(pivot20_resistance_label)
                pivot20_resistance_label := label.new(x=last_bar_index - (y + 19),y = high[y + 19],text= str.tostring(math.round(high[y + 19],2)), xloc=xloc.bar_index, yloc=yloc.price,color = color_pivot20_resistance_line)
                line.delete(pivot20_resistance_support)
                pivot20_resistance_support := line.new(x1=last_bar_index - (y + 19),y1=low[y + 19],x2=last_bar_index - (y + 18),y2=low[y + 19],xloc=xloc.bar_index,extend=extend.right,style=line.style_dashed,color=color_pivot20_resistance_support)
                break
    //20 days pivot point code end

    //252 days high resitance if not crossed for next 15 days / 3 week code start
    if(display_high252_resistance==true and last_bar_index > 15)
        high252_bars = 1260
        if(last_bar_index < 1260)
            high252_bars := last_bar_index
        
        if(display_high252_recent==true)
            high252_bars :=504
            if(last_bar_index < 504)
                high252_bars := last_bar_index

        
        for y = 15  to high252_bars
            if(high[y] == high252days[y] and hc15[y - 15] < high252days[y] and hh15[y - 15] <= high[y])
                if(display_high252_recent==true)
                    line.delete(high252_resistance)
                    high252_resistance := line.new(x1=last_bar_index - y,y1=high[y],x2=last_bar_index - (y - 1),y2=high[y],xloc=xloc.bar_index,extend=extend.right,style=line.style_dashed,color=color_high252_resistance_line)
                    label.delete(high252_resistance_label)
                    high252_resistance_label := label.new(x=last_bar_index - y,y = high[y],text= str.tostring(math.round(high[y],2)), xloc=xloc.bar_index, yloc=yloc.price,color = color_high252_resistance_line)
                    line.delete(high252_resistance_support)
                    high252_resistance_support := line.new(x1=last_bar_index - y,y1=low[y],x2=last_bar_index - (y - 1),y2=low[y],xloc=xloc.bar_index,extend=extend.right,style=line.style_dashed,color=color_high252_resistance_support)
                    break
                else
                    line.new(x1=last_bar_index - y,y1=high[y],x2=last_bar_index - (y - 1),y2=high[y],xloc=xloc.bar_index,extend=extend.right,style=line.style_dashed,color=color_high252_resistance_line)
                    label.new(x=last_bar_index - y,y = high[y],text= str.tostring(math.round(high[y],2)), xloc=xloc.bar_index, yloc=yloc.price,color = color_high252_resistance_line)
                    line.new(x1=last_bar_index - y,y1=low[y],x2=last_bar_index - (y - 1),y2=low[y],xloc=xloc.bar_index,extend=extend.right,style=line.style_dashed,color=color_high252_resistance_support)
    //252 days high resitance if not crossed for next 15 days / 3 week code ends
    