RS Stage Analysis
// 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("RS Stage Analysis",precision = 5,overlay = true,max_bars_back = 1260)

autodraw_lookback_period=input.int(defval = 504,title = "No of days look back for auto draw resistance & support",minval = 63,maxval = 1260,step = 21)
gap_between_2_lines=input.int(defval = 15,title = "Days gap between 2 same stage lines",minval = 5,maxval =63,step = 1)

display_stage_3_support_line = input.bool(defval =  true,title = "Stage 3 Support Line",group = "lines")
display_stage_1_resistance_line = input.bool(defval =  false,title = "Stage 1 Resistance Line",group = "lines")
display_stage_1_proper_resistance_line = input.bool(defval =  true,title = "Proper Stage 1 Resistance Line",group = "lines")
display_stage_2_resistance_line = input.bool(defval =  true,title = "Stage 2 Resistance Line",group = "lines")
display_stage_2_proper_resistance_line = input.bool(defval =  true,title = "Proper Stage 2 Resistance Line",group = "lines")

color_stage_3_support_line=input.color(defval = color.green,title = "Stage 3 Support Line Color",group = "colors")
color_stage_1_resistance_line=input.color(defval = color.aqua,title = "Stage 1 Resistance Line Color",group = "colors")
color_stage_1_proper_resistance_line=input.color(defval = color.navy,title = "Proper Stage 1 Resistance Line Color",group = "colors")
color_stage_2_resistance_line=input.color(defval = color.orange,title = "Stage 1 Resistance Line Color",group = "colors")
color_stage_2_proper_resistance_line=input.color(defval = color.red,title = "Proper Stage 2 Resistance Line Color",group = "colors")


i_nifty50 = input.symbol("NSE:CNX500", "Symbol")
NIFTY50 = request.security(i_nifty50, timeframe.period, close)

var stages = array.new_float()

sma200=ta.ema(close,200)
//plot(ema189,title = 'EMA189',color=color.gray)

sma150=ta.vwma(close,150)
//plot(vwmah189,title = 'VWMAH189',color=color.rgb(146, 194, 233))

sma100=ta.vwma(close,100)
//plot(vwma189,title = 'VWMA189',color=color.rgb(126, 199, 132))


rs=(close/NIFTY50)
rs21=ta.ema(rs,21)
rs42=ta.ema(rs,42)
rs63=ta.ema(rs,63)
rs72=ta.ema(rs,72)
rs84=ta.ema(rs,84)
rs126=ta.ema(rs,126)
rs147=ta.ema(rs,147)
rs168=ta.ema(rs,168)
rs252=ta.ema(rs,252)

hh7=ta.highest(high,7)
hh15=ta.highest(high,15)

var stage1_line_bar_index = 0
var stage1_proper_line_bar_index = 0
var stage3_line_bar_index = 0
var stage2_line_bar_index = 0
var stage2_proper_line_bar_index = 0

color ccolor = na

if(rs>=rs84 and rs < rs126)
    //stage 1
    array.push(stages,1)
    ccolor:=color.new(color.aqua,90)
else if(rs < rs42 and rs >= rs72 and rs >= rs84 and rs >= rs126 and rs >= rs147 and rs >= rs168 and rs >= rs252 and (rs42 > rs63 or rs < rs63) and rs63 > rs126 and close >= math.max(sma200,sma150,sma100))
    //stage 3
    array.push(stages,3)
    ccolor:=color.new(color.yellow,90)
else if(rs >= rs168 and rs >= rs147 and rs>=rs126 and rs >= rs252 and close>=math.max(sma200,sma150,sma100) and (rs21 >= rs42 or rs42 >= rs63))
    //stage 2
    array.push(stages,2.1)
    ccolor:=color.new(color.green,70)
else if(rs>=rs126 and rs >= rs147 and (close>=sma200 or close>=math.max(sma100,sma150)) and (rs21 >= rs42 or rs42 >= rs63))
    //stage 2
    array.push(stages,2)
    ccolor:=color.new(color.green,90)
else if((rs<rs63 and rs < rs126) or (rs63 < rs126 and rs < rs126))
    //stage 4
    array.push(stages,4)
    ccolor:=color.new(color.red,90)
else if((math.max(close,open)>=math.max(sma200,sma150) or math.max(close,open)>=math.max(sma100,sma150)) and rs >= rs72 and rs < rs126)
    //stage 1
    array.push(stages,1)
    ccolor:=color.new(color.aqua,90)
else if(rs >= rs84 and rs >= rs72 and (math.max(close,open)>=math.max(sma200,sma150) or math.max(close,open)>=math.max(sma100,sma150)))
    //stage 1
    array.push(stages,1)
    ccolor:=color.new(color.aqua,90)
else
    array.push(stages,0)
bgcolor(ccolor)

// if(bar_index == 0)
//     label.new(x=bar_index,y = high,text= str.tostring(array.size(stages)) + " = " + str.tostring(bar_index), xloc=xloc.bar_index, yloc=yloc.price)

// if(bar_index == last_bar_index)
//     label.new(x=bar_index,y = high,text= str.tostring(array.size(stages)) + " = " + str.tostring(bar_index), xloc=xloc.bar_index, yloc=yloc.price)

if((bar_index >= (last_bar_index - autodraw_lookback_period) or ((last_bar_index - autodraw_lookback_period) < 0 and bar_index >= 130)) and bar_index >=6) //only start auto draw for last 2 years

    //stage 3 support line code start
    if(display_stage_3_support_line==true)
        if(array.get(stages,bar_index)==3 and array.get(stages,(bar_index - 1))==3 and array.get(stages,(bar_index - 2))==2.1 and array.get(stages,(bar_index - 3))==2.1 and array.get(stages,(bar_index - 4))==2.1 and array.get(stages,(bar_index - 5))==2.1 and array.get(stages,(bar_index - 6))==2.1)
            if(bar_index >= (stage3_line_bar_index + gap_between_2_lines))
                line.new(x1=bar_index - 1,y1=hh15,x2=bar_index,y2=hh15,xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color_stage_3_support_line)
                stage3_line_bar_index := bar_index

        if(array.get(stages,bar_index)==2.1 and array.get(stages,(bar_index - 1))==3 and array.get(stages,(bar_index - 2))==2.1 and array.get(stages,(bar_index - 3))==2.1 and array.get(stages,(bar_index - 4))==2.1 and array.get(stages,(bar_index - 5))==2.1 and array.get(stages,(bar_index - 6))==2.1)
            if(bar_index >= (stage3_line_bar_index + gap_between_2_lines))
                line.new(x1=bar_index - 1,y1=hh15,x2=bar_index,y2=hh15,xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color_stage_3_support_line)
                stage3_line_bar_index := bar_index
    //stage 3 support line code end

    //stage 2 resistance line code start
    if(display_stage_2_resistance_line==true)
        if((array.get(stages,bar_index)==1 or array.get(stages,bar_index)==0) and array.get(stages,(bar_index - 1))==2 and array.get(stages,(bar_index - 2))==2)
            if(bar_index >= (stage2_line_bar_index + gap_between_2_lines))
                line.new(x1=bar_index - 1,y1=hh15,x2=bar_index,y2=hh15,xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color_stage_2_resistance_line)
                stage2_line_bar_index := bar_index
    //stage 2 resistance line code end

    //proper stage 2 resistance line code start
    if(display_stage_2_proper_resistance_line ==true)
        check_stage_2=0
        check_stage_2_1=0

        if(array.get(stages,bar_index) == 2 or array.get(stages,bar_index) == 2.1) 
            check_stage_2 := check_stage_2 + 1

        if(array.get(stages,bar_index - 1) == 2 or array.get(stages,bar_index - 1) == 2.1) 
            check_stage_2 := check_stage_2 + 1

        if(array.get(stages,bar_index - 2) == 2 or array.get(stages,bar_index - 2) == 2.1) 
            check_stage_2 := check_stage_2 + 1

        if(array.get(stages,bar_index - 3) == 2 or array.get(stages,bar_index - 3) == 2.1) 
            check_stage_2 := check_stage_2 + 1

        if(array.get(stages,bar_index - 4) == 2 or array.get(stages,bar_index - 4) == 2.1) 
            check_stage_2 := check_stage_2 + 1

        if(array.get(stages,bar_index - 5) == 2 or array.get(stages,bar_index - 5) == 2.1) 
            check_stage_2 := check_stage_2 + 1
        
        if(array.get(stages,bar_index - 6) == 2 or array.get(stages,bar_index - 6) == 2.1) 
            check_stage_2 := check_stage_2 + 1

        //state 2.1 checking
        if(array.get(stages,bar_index - 1) == 2.1) 
            check_stage_2_1 := check_stage_2_1 + 1

        if(array.get(stages,bar_index - 2) == 2.1) 
            check_stage_2_1 := check_stage_2_1 + 1

        if(array.get(stages,bar_index - 3) == 2.1) 
            check_stage_2_1 := check_stage_2_1 + 1

        if(array.get(stages,bar_index - 4) == 2.1) 
            check_stage_2_1 := check_stage_2_1 + 1

        if(array.get(stages,bar_index - 5) == 2.1) 
            check_stage_2_1 := check_stage_2_1 + 1
        
        if(array.get(stages,bar_index - 6) == 2.1) 
            check_stage_2_1 := check_stage_2_1 + 1

        if(check_stage_2 >= 5 and check_stage_2_1 >= 2 and (array.get(stages,bar_index) == 2 or array.get(stages,bar_index) == 0 or array.get(stages,bar_index) == 1 or array.get(stages,bar_index) == 4)) 
            if(bar_index >= (stage2_proper_line_bar_index + gap_between_2_lines))
                line.new(x1=bar_index - 6,y1=hh15,x2=bar_index,y2=hh15,xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color_stage_2_proper_resistance_line)
                stage2_proper_line_bar_index := bar_index
    //proper stage 2 resistance line code end

    //stage 1 resistance line code start - 1 - stage 1 and stage 0 count >= 2 in last 5 days. minimum 1 stage 1 days required. no stage 2 in last 5 days and no stage 1 in previouse 6th days.
    if(display_stage_1_resistance_line==true)
        check_pass_count=0
        check_stage_1=0
        check_stage_2=0
        
        //day 1
        if(array.get(stages,bar_index) == 1 or array.get(stages,bar_index) == 0) 
            check_pass_count := check_pass_count + 1

        if(array.get(stages,bar_index) == 1)
            check_stage_1 := check_stage_1 + 1

        if(array.get(stages,bar_index) == 2)
            check_stage_2 := check_stage_2 + 1

        //day 2
        if(array.get(stages,bar_index - 1) == 1 or array.get(stages,bar_index - 1) == 0) 
            check_pass_count := check_pass_count + 1
            
        if(array.get(stages,bar_index - 1) == 1)
            check_stage_1 := check_stage_1 + 1
        
        if(array.get(stages,bar_index - 1) == 2)
            check_stage_2 := check_stage_2 + 1

        //day 3
        if(array.get(stages,bar_index - 2) == 1 or array.get(stages,bar_index - 2) == 0) 
            check_pass_count := check_pass_count + 1
        
        if(array.get(stages,bar_index - 2) == 1)
            check_stage_1 := check_stage_1 + 1

        if(array.get(stages,bar_index - 2) == 2)
            check_stage_2 := check_stage_2 + 1

        //day 4
        if(array.get(stages,bar_index - 3) == 1 or array.get(stages,bar_index - 3) == 0) 
            check_pass_count := check_pass_count + 1

        if(array.get(stages,bar_index - 3) == 1)
            check_stage_1 := check_stage_1 + 1

        if(array.get(stages,bar_index - 3) == 2)
            check_stage_2 := check_stage_2 + 1
        
        //day 5
        if(array.get(stages,bar_index - 4) == 1 or array.get(stages,bar_index - 4) == 0)
            check_pass_count := check_pass_count + 1

        if(array.get(stages,bar_index - 4) == 1)
            check_stage_1 := check_stage_1 + 1

        if(array.get(stages,bar_index - 4) == 2)
            check_stage_2 := check_stage_2 + 1
        
        if(check_pass_count >= 2 and check_stage_1 >= 1 and check_stage_2==0 and array.get(stages,bar_index - 5) != 1)
            if(bar_index >= (stage1_line_bar_index + gap_between_2_lines))
                line.new(x1=bar_index - 4,y1=hh15,x2=bar_index - 3,y2=hh15,xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color_stage_1_resistance_line)
                stage1_line_bar_index := bar_index
    //stage 1 resistance line code start - 1 - stage 1 and stage 0 count >= 2 in last 5 days. minimum 1 stage 1 days required. no stage 2 in last 5 days and no stage 1 in previouse 6th days.

    //stage 1 proper resistance line code start - in last 7 days total 5 days in stage 1/0 and no stage 2 in last 7 days.
    if(display_stage_1_proper_resistance_line==true and bar_index >= 132)
        check_pass_count=0
        check_stage_2=0
        check_stage_1=0
        
        //day 1
        if(array.get(stages,bar_index) == 1 or array.get(stages,bar_index) == 0) 
            check_pass_count := check_pass_count + 1

        if(array.get(stages,bar_index) == 2)
            check_stage_2 := check_stage_2 + 1

        if(array.get(stages,bar_index) == 1)
            check_stage_1 := check_stage_1 + 1

        //day 2
        if(array.get(stages,bar_index - 1) == 1 or array.get(stages,bar_index - 1) == 0) 
            check_pass_count := check_pass_count + 1
        
        if(array.get(stages,bar_index - 1) == 2)
            check_stage_2 := check_stage_2 + 1

        if(array.get(stages,bar_index - 1) == 1)
            check_stage_1 := check_stage_1 + 1

        //day 3
        if(array.get(stages,bar_index - 2) == 1 or array.get(stages,bar_index - 2) == 0) 
            check_pass_count := check_pass_count + 1
        
        if(array.get(stages,bar_index - 2) == 2)
            check_stage_2 := check_stage_2 + 1

        if(array.get(stages,bar_index - 2) == 1)
            check_stage_1 := check_stage_1 + 1

        //day 4
        if(array.get(stages,bar_index - 3) == 1 or array.get(stages,bar_index - 3) == 0) 
            check_pass_count := check_pass_count + 1

        if(array.get(stages,bar_index - 3) == 2)
            check_stage_2 := check_stage_2 + 1

        if(array.get(stages,bar_index - 3) == 1)
            check_stage_1 := check_stage_1 + 1
        
        //day 5
        if(array.get(stages,bar_index - 4) == 1 or array.get(stages,bar_index - 4) == 0)
            check_pass_count := check_pass_count + 1

        if(array.get(stages,bar_index - 4) == 2)
            check_stage_2 := check_stage_2 + 1

        if(array.get(stages,bar_index - 4) == 1)
            check_stage_1 := check_stage_1 + 1

        //day 6
        if(array.get(stages,bar_index - 5) == 1 or array.get(stages,bar_index - 5) == 0)
            check_pass_count := check_pass_count + 1

        if(array.get(stages,bar_index - 5) == 2)
            check_stage_2 := check_stage_2 + 1

        if(array.get(stages,bar_index - 5) == 1)
            check_stage_1 := check_stage_1 + 1

        //day 7
        if(array.get(stages,bar_index - 6) == 1 or array.get(stages,bar_index - 6) == 0)
            check_pass_count := check_pass_count + 1

        if(array.get(stages,bar_index - 6) == 2)
            check_stage_2 := check_stage_2 + 1

        if(array.get(stages,bar_index - 6) == 1)
            check_stage_1 := check_stage_1 + 1
        
        if(check_pass_count >= 5 and check_stage_1 >= 1)
            if(bar_index >= (stage1_proper_line_bar_index + gap_between_2_lines))
                line.new(x1=bar_index - 6,y1=hh15,x2=bar_index - 5,y2=hh15,xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color_stage_1_proper_resistance_line)
                stage1_proper_line_bar_index := bar_index
    //stage 1 proper resistance line code start - in last 7 days total 5 days in stage 1/0 and no stage 2 in last 7 days.