Volume based support resistance with Swing
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Ankit_1618

//@version=4
study("Volume based support resistance with Swing", overlay=true)

length = input(20)
mid_point = hlc3

basis_vol = sma(volume, length)
sd2_vol = basis_vol + stdev(volume, length)

// plot(volume, style=plot.style_columns, color=close>open ? color.green : close<open ? color.red : color.orange)
// plot(basis_vol, color=color.red)
// plot(sd2_vol, color=color.blue)


up_fractal_volume = volume > basis_vol 
extreme_volume = volume > basis_vol and volume > sd2_vol

var hv_sr = 0.
var ev_sr = 0.

if up_fractal_volume
    hv_sr := mid_point

if extreme_volume
    ev_sr := mid_point
    
plot(hv_sr, color= hv_sr!=hv_sr[1] ? na : color.gray)
plot(ev_sr, color= ev_sr!=ev_sr[1] ? na : color.blue, linewidth=2)
plot(mid_point, color=color.purple, style=plot.style_circles)




//SWING Levels
// based on William's Fractal indicator

n = 2
up_fractal = high
down_fractal = low

up = ((up_fractal[n+2]  < up_fractal[n]) and (up_fractal[n+1]  < up_fractal[n]) and (up_fractal[n-1] < up_fractal[n]) and (up_fractal[n-2] < up_fractal[n]))
         or ((up_fractal[n+3]  < up_fractal[n]) and (up_fractal[n+2]  < up_fractal[n]) and (up_fractal[n+1] == up_fractal[n]) and (up_fractal[n-1] < up_fractal[n]) and (up_fractal[n-2] < up_fractal[n]))
         or ((up_fractal[n+4]  < up_fractal[n]) and (up_fractal[n+3]  < up_fractal[n]) and (up_fractal[n+2] == up_fractal[n]) and (up_fractal[n+1] <= up_fractal[n]) and (up_fractal[n-1] < up_fractal[n]) and (up_fractal[n-2] < up_fractal[n]))
         or ((up_fractal[n+5] < up_fractal[n]) and (up_fractal[n+4]  < up_fractal[n]) and (up_fractal[n+3] == up_fractal[n]) and (up_fractal[n+2] == up_fractal[n]) and (up_fractal[n+1] <= up_fractal[n]) and (up_fractal[n-1] < up_fractal[n]) and (up_fractal[n-2] < up_fractal[n]))
         or ((up_fractal[n+6] < up_fractal[n]) and (up_fractal[n+5] < up_fractal[n]) and (up_fractal[n+4] == up_fractal[n]) and (up_fractal[n+3] <= up_fractal[n]) and (up_fractal[n+2] == up_fractal[n]) and (up_fractal[n+1] <= up_fractal[n]) and (up_fractal[n-1] < up_fractal[n]) and (up_fractal[n-2] < up_fractal[n]))

down = ((down_fractal[n+2]  > down_fractal[n]) and (down_fractal[n+1]  > down_fractal[n]) and (down_fractal[n-1] > down_fractal[n]) and (down_fractal[n-2] > down_fractal[n]))
         or ((down_fractal[n+3]  > down_fractal[n]) and (down_fractal[n+2]  > down_fractal[n]) and (down_fractal[n+1] == down_fractal[n]) and (down_fractal[n-1] > down_fractal[n]) and (down_fractal[n-2] > down_fractal[n]))
         or ((down_fractal[n+4]  > down_fractal[n]) and (down_fractal[n+3]  > down_fractal[n]) and (down_fractal[n+2] == down_fractal[n]) and (down_fractal[n+1] >= down_fractal[n]) and (down_fractal[n-1] > down_fractal[n]) and (down_fractal[n-2] > down_fractal[n]))
         or ((down_fractal[n+5] > down_fractal[n]) and (down_fractal[n+4]  > down_fractal[n]) and (down_fractal[n+3] == down_fractal[n]) and (down_fractal[n+2] == down_fractal[n]) and (down_fractal[n+1] >= down_fractal[n]) and (down_fractal[n-1] > down_fractal[n]) and (down_fractal[n-2] > down_fractal[n]))
         or ((down_fractal[n+6] > down_fractal[n]) and (down_fractal[n+5] > down_fractal[n]) and (down_fractal[n+4] == down_fractal[n]) and (down_fractal[n+3] >= down_fractal[n]) and (down_fractal[n+2] == down_fractal[n]) and (down_fractal[n+1] >= down_fractal[n]) and (down_fractal[n-1] > down_fractal[n]) and (down_fractal[n-2] > down_fractal[n]))

LowerHigh = up and up_fractal[n] < valuewhen(up, up_fractal[n], 1)
HigherHigh = up and up_fractal[n] > valuewhen(up, up_fractal[n], 1)
EqualHigh = up and up_fractal[n] == valuewhen(up, up_fractal[n], 1)
LowerLow = down and down_fractal[n] < valuewhen(down, down_fractal[n], 1)
HigherLow = down and down_fractal[n] > valuewhen(down, down_fractal[n], 1)
EqualLow = down and down_fractal[n] == valuewhen(down, down_fractal[n], 1)

plotshape(LowerHigh, style=shape.labeldown, text='lh', location=location.abovebar, offset=-2, color=na,textcolor=color.red, transp=0)
plotshape(HigherHigh, style=shape.labeldown, text='hh', location=location.abovebar, offset=-2, color=na,textcolor=color.green, transp=0)
plotshape(EqualHigh, style=shape.labeldown, text='EH', location=location.abovebar, offset=-2, color=na,textcolor=color.gray, transp=0)
plotshape(LowerLow, style=shape.labelup, text='ll', location=location.belowbar, offset=-2, color=na,textcolor=color.red, transp=0)
plotshape(HigherLow, style=shape.labelup, text='hl', location=location.belowbar, offset=-2, color=na,textcolor=color.green, transp=0)
plotshape(EqualLow, style=shape.labelup, text='EL', location=location.belowbar, offset=-2, color=na,textcolor=color.gray, transp=0)







