Support/Resistant Zone (Simple)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/Mpivotlow/2.0/
// © TrendCrypto2022

//@version=5
indicator('Support/Resistant Zone (Simple)',          overlay=true)

// Input Pivot
number      = input.float(15, "Number bars to detect Pivot High/Low", minval=3,  maxval=30, step=1,  group='Detect Pivot High/Low')
alert1      =  input.bool    (title="Alert when price test Support Zone",       defval=true,        group="Set up Alert")
alert2      =  input.bool   (title="Alert when price test Resistant Zone",      defval=true,        group="Set up Alert")

//Pivot High/Low and Resistant Zone
// Calculator Pivot High and Resistant Zone
pivothigh       = ta.pivothigh(high, number, number)
pivothighv1     = ta.valuewhen(pivothigh, high[number], 0), pivothighb1 = ta.valuewhen(pivothigh, bar_index[number], 0),      pivothighv1low = ta.valuewhen(pivothigh, close[number]>open[number] ? close[number] : open[number], 0)
pivothighv2     = ta.valuewhen(pivothigh, high[number], 1), pivothighb2 = ta.valuewhen(pivothigh, bar_index[number], 1),      pivothighv2low = ta.valuewhen(pivothigh, close[number]>open[number] ? close[number] : open[number], 1)
pivothighv3     = ta.valuewhen(pivothigh, high[number], 2), pivothighb3 = ta.valuewhen(pivothigh, bar_index[number], 2),      pivothighv3low = ta.valuewhen(pivothigh, close[number]>open[number] ? close[number] : open[number], 2)

// Draw Resistant Zone
var line linepivothighv1 = na
line.delete(linepivothighv1)
linepivothighv1         := line.new(pivothighb1, pivothighv1, bar_index, pivothighv1, width = 1, color = close > pivothighv1 ? color.lime : color.red)
var line linepivothighv1low = na
line.delete(linepivothighv1low)
linepivothighv1low      := line.new(pivothighb1, pivothighv1low, bar_index, pivothighv1low, width = 1, color = close > pivothighv1low ? color.lime : color.red)
linefill.new(linepivothighv1, linepivothighv1low, color = close > math.max(pivothighv1low, pivothighv1) ? color.new(color.lime, transp =90) : color.new(color.red, transp =90))

var line linepivothighv2 = na
line.delete(linepivothighv2)
linepivothighv2         := line.new(pivothighb2, pivothighv2, bar_index, pivothighv2, width = 1, color = close > pivothighv2 ? color.lime : color.red)
var line linepivothighv2low = na
line.delete(linepivothighv2low)
linepivothighv2low      := line.new(pivothighb2, pivothighv2low, bar_index, pivothighv2low, width = 1, color = close > pivothighv2low ? color.lime : color.red)
linefill.new(linepivothighv2, linepivothighv2low, color = close > math.max(pivothighv2low, pivothighv2) ? color.new(color.lime, transp =90) : color.new(color.red, transp =90))

var line linepivothighv3 = na
line.delete(linepivothighv3)
linepivothighv3         := line.new(pivothighb3, pivothighv3, bar_index, pivothighv3, width = 1, color = close > pivothighv3 ? color.lime : color.red)
var line linepivothighv3low = na
line.delete(linepivothighv3low)
linepivothighv3low      := line.new(pivothighb3, pivothighv3low, bar_index, pivothighv3low, width = 1, color = close > pivothighv3low ? color.lime : color.red)
linefill.new(linepivothighv3, linepivothighv3low, color = close > math.max(pivothighv3low, pivothighv3) ? color.new(color.lime, transp =90) : color.new(color.red, transp =90))


// Label Resistant Zone
var label Label_pivothighv1 = na
label.delete(Label_pivothighv1)
Label_pivothighv1       := label.new(x=time + 120, y=pivothighv1, text='' + str.tostring(pivothighv1) + '', color=color.new(#000000, 100), textcolor = color.yellow, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)

var label Label_pivothighv2 = na
label.delete(Label_pivothighv2)
Label_pivothighv2       := label.new(x=time + 120, y=pivothighv2, text='' + str.tostring(pivothighv2) + '', color=color.new(#000000, 100), textcolor = color.yellow, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)

var label Label_pivothighv3 = na
label.delete(Label_pivothighv3)
Label_pivothighv3       := label.new(x=time + 120, y=pivothighv3, text='' + str.tostring(pivothighv3) + '', color=color.new(#000000, 100), textcolor = color.yellow, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)

var label Label_pivothighv1low = na
label.delete(Label_pivothighv1low)
Label_pivothighv1low    := label.new(x=time + 120, y=pivothighv1low, text='' + str.tostring(pivothighv1low) + '', color=color.new(#000000, 100), textcolor = color.yellow, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)

var label Label_pivothighv2low = na
label.delete(Label_pivothighv2low)
Label_pivothighv2low    := label.new(x=time + 120, y=pivothighv2low, text='' + str.tostring(pivothighv2low) + '', color=color.new(#000000, 100), textcolor = color.yellow, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)

var label Label_pivothighv3low = na
label.delete(Label_pivothighv3low)
Label_pivothighv3low    := label.new(x=time + 120, y=pivothighv3low, text='' + str.tostring(pivothighv3low) + '', color=color.new(#000000, 100), textcolor = color.yellow, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)

// Calculator Pivot Low and Support Zone
pivotlow        = ta.pivotlow(low, number, number)
pivotlowv1      = ta.valuewhen(pivotlow, low[number], 0), pivotlowb1 = ta.valuewhen(pivotlow, bar_index[number], 0),   pivotlowv1high = ta.valuewhen(pivotlow, close[number]>open[number] ? open[number] : close[number], 0)
pivotlowv2      = ta.valuewhen(pivotlow, low[number], 1), pivotlowb2 = ta.valuewhen(pivotlow, bar_index[number], 1),   pivotlowv2high = ta.valuewhen(pivotlow, close[number]>open[number] ? open[number] : close[number], 1)
pivotlowv3      = ta.valuewhen(pivotlow, low[number], 2), pivotlowb3 = ta.valuewhen(pivotlow, bar_index[number], 2),   pivotlowv3high = ta.valuewhen(pivotlow, close[number]>open[number] ? open[number] : close[number], 2)

// Draw Support Zone
var line linepivotlowv1 = na
line.delete(linepivotlowv1)
linepivotlowv1          := line.new(pivotlowb1, pivotlowv1, bar_index, pivotlowv1, width = 1, color = close > pivotlowv1 ? color.lime : color.red)
var line linepivotlowv1high = na
line.delete(linepivotlowv1high)
linepivotlowv1high      := line.new(pivotlowb1, pivotlowv1high, bar_index, pivotlowv1high, width = 1, color = close > pivotlowv1high ? color.lime : color.red)
linefill.new(linepivotlowv1, linepivotlowv1high, color = close > math.max(pivotlowv1high, pivotlowv1) ? color.new(color.lime, transp =90) : color.new(color.red, transp =90))

var line linepivotlowv2 = na
line.delete(linepivotlowv2)
linepivotlowv2      := line.new(pivotlowb2, pivotlowv2, bar_index, pivotlowv2, width = 1, color = close > pivotlowv2 ? color.lime : color.red)
var line linepivotlowv2high = na
line.delete(linepivotlowv2high)
linepivotlowv2high  := line.new(pivotlowb2, pivotlowv2high, bar_index, pivotlowv2high, width = 1, color = close > pivotlowv2high ? color.lime : color.red)
linefill.new(linepivotlowv2, linepivotlowv2high, color = close > math.max(pivotlowv2high, pivotlowv2) ? color.new(color.lime, transp =90) : color.new(color.red, transp =90))

var line linepivotlowv3 = na
line.delete(linepivotlowv3)
linepivotlowv3          := line.new(pivotlowb3, pivotlowv3, bar_index, pivotlowv3, width = 1, color = close > pivotlowv3 ? color.lime : color.red)
var line linepivotlowv3high = na
line.delete(linepivotlowv3high)
linepivotlowv3high      := line.new(pivotlowb3, pivotlowv3high, bar_index, pivotlowv3high, width = 1, color = close > pivotlowv3high ? color.lime : color.red)
linefill.new(linepivotlowv3, linepivotlowv3high, color = close > math.max(pivotlowv3high, pivotlowv3) ? color.new(color.lime, transp =90) : color.new(color.red, transp =90))

// Label Support Zone
var label Label_pivotlowv1  = na
label.delete(Label_pivotlowv1)
Label_pivotlowv1            := label.new(x=time + 120, y=pivotlowv1, text='' + str.tostring(pivotlowv1) + '', color=color.new(#000000, 100), textcolor = color.yellow, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)

var label Label_pivotlowv2  = na
label.delete(Label_pivotlowv2)
Label_pivotlowv2            := label.new(x=time + 120, y=pivotlowv2, text='' + str.tostring(pivotlowv2) + '', color=color.new(#000000, 100), textcolor = color.yellow, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)

var label Label_pivotlowv3  = na
label.delete(Label_pivotlowv3)
Label_pivotlowv3            := label.new(x=time + 120, y=pivotlowv3, text='' + str.tostring(pivotlowv3) + '', color=color.new(#000000, 100), textcolor = color.yellow, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)

var label Label_pivotlowv1high = na
label.delete(Label_pivotlowv1high)
Label_pivotlowv1high        := label.new(x=time + 120, y=pivotlowv1high, text='' + str.tostring(pivotlowv1high) + '', color=color.new(#000000, 100), textcolor = color.yellow, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)

var label Label_pivotlowv2high = na
label.delete(Label_pivotlowv2high)
Label_pivotlowv2high        := label.new(x=time + 120, y=pivotlowv2high, text='' + str.tostring(pivotlowv2high) + '', color=color.new(#000000, 100), textcolor = color.yellow, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)

var label Label_pivotlowv3high = na
label.delete(Label_pivotlowv3high)
Label_pivotlowv3high        := label.new(x=time + 120, y=pivotlowv3high, text='' + str.tostring(pivotlowv3high) + '', color=color.new(#000000, 100), textcolor = color.yellow, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)


// Set up alerts when price test Support/Resistant Zone
price_closebefore1 = close[1]

// Alert price test Support Zone
_testlong(_x, _y)     =>
    var int trend   = na
    trend :=  price_closebefore1 > math.max(_x,_y) ? 1 : price_closebefore1 < math.min(_x,_y) ? -1 : trend[1]
    if trend == 1           and alert1 ==true and low < math.max(_x,_y) and close > math.min(_x,_y)
        alert   (''+ str.tostring(syminfo.ticker) +' test Support Zone. Price: '+ str.tostring(close) +'.', alert.freq_once_per_bar)

_testlong(pivothighv1, pivothighv1low)
_testlong(pivothighv2, pivothighv2low)
_testlong(pivothighv3, pivothighv3low)
_testlong(pivotlowv1, pivotlowv1high)
_testlong(pivotlowv2, pivotlowv2high)
_testlong(pivotlowv3, pivotlowv3high)

// Alert price test Resistant Zone
_testshort(_c, _d)     =>
    var int trendshort   = na
    trendshort :=  price_closebefore1 < math.min(_c,_d) ? -1 : price_closebefore1 > math.max(_c,_d) ? 1 : trendshort[1]
    if trendshort == -1     and alert1 ==true and high > math.min(_c,_d) and close < math.max(_c,_d)
        alert   (''+ str.tostring(syminfo.ticker) +' test Resistant Zone. Price: '+ str.tostring(close) +'.', alert.freq_once_per_bar)

_testshort(pivothighv1, pivothighv1low)
_testshort(pivothighv2, pivothighv2low)
_testshort(pivothighv3, pivothighv3low)
_testshort(pivotlowv1, pivotlowv1high)
_testshort(pivotlowv2, pivotlowv2high)
_testshort(pivotlowv3, pivotlowv3high)


