You are on page 1of 7

// This source code is subject to the terms of the Mozilla Public License 2.

0 at
https://mozilla.org/MPL/2.0/
// © LonesomeTheBlue

//@version=4
study("Support Resistance - Dynamic", overlay = true, max_bars_back = 600)
rb = input(10, title = "Period for Pivot Points", minval = 10)
prd = input(284, title = "Loopback Period", minval = 100, maxval = 500)
nump = input(2, title ="S/R strength", minval = 1)
ChannelW = input(10, title = "Channel Width %", minval = 5)
linestyle = input('Dashed', title = "Line Style", options = ['Solid', 'Dotted',
'Dashed'])
linecolor = input('Blue', title = "Line Color", options = ['Blue', 'Red', 'Lime',
'White', 'Black'])
drawhl = input(true, title = "Draw Highest/Lowest Pivots in Period")
showpp = input(false, title = "Show Point Points")

ph = pivothigh(rb, rb)
pl = pivotlow(rb, rb)
plotshape(ph and showpp, text="[H]", style=shape.labeldown, color=color.white,
textcolor=color.black, location=location.abovebar, transp=0, offset = -rb)
plotshape(pl and showpp, text="[L]", style=shape.labelup, color=color.white,
textcolor=color.black, location=location.belowbar, transp=0, offset = -rb)

// S/R levels
float sr1 = na, float sr2 = na, float sr3 = na, float sr4 = na, float sr5 = na,
float sr6 = na, float sr7 = na, float sr8 = na, float sr9 = na, float sr10 = na
float sr11 = na, float sr12 = na, float sr13 = na, float sr14 = na, float sr15 =
na, float sr16 = na, float sr17 = na, float sr18 = na, float sr19 = na, float sr20
= na

// if number of bars is less then the loop then pine highest() fundtion brings
'na'. we need highest/lowest to claculate channel size
// so you cannot see S/R until the number of bars is equal/greater then the
"Loopback Period"
prdhighest = highest(prd)
prdlowest = lowest(prd)
cwidth = (prdhighest - prdlowest) * ChannelW / 100

//availability of the PPs


a1 = true, a2 = true, a3 = true, a4 = true, a5 = true, a6 = true, a7 = true, a8 =
true, a9 = true, a10 = true
a11 = true, a12 = true, a13 = true, a14 = true, a15 = true, a16 = true, a17 = true,
a18 = true, a19 = true, a20 = true
a21 = true, a22 = true, a23 = true, a24 = true, a25 = true, a26 = true, a27 = true,
a28 = true, a29 = true, a30 = true
a31 = true, a32 = true, a33 = true, a34 = true, a35 = true, a36 = true, a37 = true,
a38 = true, a39 = true, a40 = true

ppavailable(ind)=> // is PP available?
_ret = ind == 1 and a1 ? true : ind == 2 and a2 ? true : ind == 3
and a3 ? true : ind == 4 and a4 ? true : ind == 5 and a5 ? true :
ind == 6 and a6 ? true : ind == 7 and a7 ? true : ind == 8
and a8 ? true : ind == 9 and a9 ? true : ind == 10 and a10 ? true :
ind == 11 and a11 ? true : ind == 12 and a12 ? true : ind == 13
and a13 ? true : ind == 14 and a14 ? true : ind == 15 and a15 ? true :
ind == 16 and a16 ? true : ind == 17 and a17 ? true : ind == 18
and a18 ? true : ind == 19 and a19 ? true : ind == 20 and a20 ? true :
ind == 21 and a21 ? true : ind == 22 and a22 ? true : ind == 23
and a23 ? true : ind == 24 and a24 ? true : ind == 25 and a25 ? true :
ind == 26 and a26 ? true : ind == 27 and a27 ? true : ind == 28
and a28 ? true : ind == 29 and a29 ? true : ind == 30 and a30 ? true :
ind == 31 and a31 ? true : ind == 32 and a32 ? true : ind == 33
and a33 ? true : ind == 34 and a34 ? true : ind == 35 and a35 ? true :
ind == 36 and a36 ? true : ind == 37 and a37 ? true : ind == 38
and a38 ? true : ind == 39 and a39 ? true : ind == 40 and a40 ? true :
false

set_a(t1, a1)=>
_ret = not t1 ? false : a1

// last privot points have more priority to be support/resistance, so we start from


them
// if we met new Pivot Point then we calculate all supports/resistances again
u1 = 0.0, u1 := nz(u1[1])
d1 = 0.0, d1 := nz(d1[1])
highestph = 0.0
lowestpl = 0.0
highestph := highestph[1]
lowestpl := lowestpl[1]
if ph or pl
//old S/Rs not valid anymore
sr1 := na, sr2 := na, sr3 := na, sr4 := na, sr5 := na, sr6 := na, sr7 := na,
sr8 := na, sr9 := na, sr10 := na
sr11 := na, sr12 := na, sr13 := na, sr14 := na, sr15 := na, sr16 := na, sr17 :=
na, sr18 := na, sr19 := na, sr20 := na
highestph := prdlowest
lowestpl := prdhighest
countpp = 0 // keep position of the PP
for x = 0 to prd
if na(close[x])
break
if not na(ph[x]) or not na(pl[x]) // is it PP?
highestph := max(highestph, nz(ph[x], prdlowest), nz(pl[x], prdlowest))
lowestpl := min(lowestpl, nz(ph[x], prdhighest), nz(pl[x], prdhighest))
countpp := countpp + 1
if ppavailable(countpp) // if PP is not used in a channel
upl = (ph[x] ? high[x+rb] : low[x+rb]) + cwidth
dnl = (ph[x] ? high[x+rb] : low[x+rb]) - cwidth
u1 := countpp == 1 ? upl : u1
d1 := countpp == 1 ? dnl : d1
// to keep the PPs which will be in current channel
t1 = true, t2 = true, t3 = true, t4 = true, t5 = true, t6 =
true, t7 = true, t8 = true, t9 = true, t10 = true
t11 = true, t12 = true, t13 = true, t14 = true, t15 = true, t16 =
true, t17 = true, t18 = true, t19 = true, t20 = true
t21 = true, t22 = true, t23 = true, t24 = true, t25 = true, t26 =
true, t27 = true, t28 = true, t29 = true, t30 = true
t31 = true, t32 = true, t33 = true, t34 = true, t35 = true, t36 =
true, t37 = true, t38 = true, t39 = true, t40 = true
cnt = 0 // keep which pivot point we are on
tpoint = 0 // number of PPs in the channel
for xx = 0 to prd
if na(close[xx])
break
if not na(ph[xx]) or not na(pl[xx])
chg = false
cnt := cnt + 1
if ppavailable(cnt) // if PP not used in other channels
if not na(ph[xx])
if high[xx+rb] <= upl and high[xx+rb] >= dnl // PP
is in the channel?
tpoint := tpoint + 1
chg := true

if not na(pl[xx])
if low[xx+rb] <= upl and low[xx+rb] >= dnl // PP
is in the channel?
tpoint := tpoint + 1
chg := true
// set if PP is used in the channel
t1 := iff(chg and cnt == 1, false, t1), t2 := iff(chg and
cnt == 2, false, t2), t3 := iff(chg and cnt == 3, false, t3), t4 := iff(chg and cnt
== 4, false, t4), t5 := iff(chg and cnt == 5, false, t5)
t6 := iff(chg and cnt == 6, false, t6), t7 := iff(chg and
cnt == 7, false, t7), t8 := iff(chg and cnt == 8, false, t8), t9 := iff(chg and cnt
== 9, false, t9), t10 := iff(chg and cnt == 10, false, t10)
t11 := iff(chg and cnt == 11, false, t11), t12 := iff(chg
and cnt == 12, false, t12), t13 := iff(chg and cnt == 13, false, t13), t14 :=
iff(chg and cnt == 14, false, t14), t15 := iff(chg and cnt == 15, false, t15)
t16 := iff(chg and cnt == 16, false, t16), t17 := iff(chg
and cnt == 17, false, t17), t18 := iff(chg and cnt == 18, false, t18), t19 :=
iff(chg and cnt == 19, false, t19), t20 := iff(chg and cnt == 20, false, t20)
t21 := iff(chg and cnt == 21, false, t21), t22 := iff(chg
and cnt == 22, false, t22), t23 := iff(chg and cnt == 23, false, t23), t24 :=
iff(chg and cnt == 24, false, t24), t25 := iff(chg and cnt == 25, false, t25)
t26 := iff(chg and cnt == 26, false, t26), t27 := iff(chg
and cnt == 27, false, t27), t28 := iff(chg and cnt == 28, false, t28), t29 :=
iff(chg and cnt == 29, false, t29), t30 := iff(chg and cnt == 30, false, t30)
t31 := iff(chg and cnt == 31, false, t31), t32 := iff(chg
and cnt == 32, false, t32), t33 := iff(chg and cnt == 33, false, t33), t34 :=
iff(chg and cnt == 34, false, t34), t35 := iff(chg and cnt == 35, false, t35)
t36 := iff(chg and cnt == 36, false, t36), t37 := iff(chg
and cnt == 37, false, t37), t38 := iff(chg and cnt == 38, false, t38), t39 :=
iff(chg and cnt == 39, false, t39), t40 := iff(chg and cnt == 40, false, t40)

if tpoint >= nump // met enough PP in the channel? mark the PP as


used for a channel and set the SR level
a1 := set_a(t1, a1), a2 := set_a(t2, a2), a3 := set_a(t3, a3),
a4 := set_a(t4, a4), a5 := set_a(t5, a5), a6 := set_a(t6, a6), a7 := set_a(t7, a7),
a8 := set_a(t8, a8), a9 := set_a(t9, a9), a10 := set_a(t10, a10)
a11 := set_a(t11, a11), a12 := set_a(t12, a12), a13 :=
set_a(t13, a13), a14 := set_a(t14, a14), a15 := set_a(t15, a15), a16 := set_a(t16,
a16), a17 := set_a(t17, a17), a18 := set_a(t18, a18), a19 := set_a(t19, a19),
a20 := set_a(t20, a20)
a21 := set_a(t21, a21), a22 := set_a(t22, a22), a23 :=
set_a(t23, a23), a24 := set_a(t24, a24), a25 := set_a(t25, a25), a26 := set_a(t26,
a26), a27 := set_a(t27, a27), a28 := set_a(t28, a28), a29 := set_a(t29, a29),
a30 := set_a(t30, a30)
a31 := set_a(t31, a31), a32 := set_a(t32, a32), a33 :=
set_a(t33, a33), a34 := set_a(t34, a34), a35 := set_a(t35, a35), a36 := set_a(t36,
a36), a37 := set_a(t37, a37), a38 := set_a(t38, a38), a39 := set_a(t39, a39),
a40 := set_a(t40, a40)

sr1 := countpp == 1 ? ph[x] ? high[x+rb] : low[x+rb] : sr1


sr2 := countpp == 2 ? ph[x] ? high[x+rb] : low[x+rb] : sr2
sr3 := countpp == 3 ? ph[x] ? high[x+rb] : low[x+rb] : sr3
sr4 := countpp == 4 ? ph[x] ? high[x+rb] : low[x+rb] : sr4
sr5 := countpp == 5 ? ph[x] ? high[x+rb] : low[x+rb] : sr5
sr6 := countpp == 6 ? ph[x] ? high[x+rb] : low[x+rb] : sr6
sr7 := countpp == 7 ? ph[x] ? high[x+rb] : low[x+rb] : sr7
sr8 := countpp == 8 ? ph[x] ? high[x+rb] : low[x+rb] : sr8
sr9 := countpp == 9 ? ph[x] ? high[x+rb] : low[x+rb] : sr9
sr10 := countpp == 10 ? ph[x] ? high[x+rb] : low[x+rb] : sr10
sr11 := countpp == 11 ? ph[x] ? high[x+rb] : low[x+rb] : sr11
sr12 := countpp == 12 ? ph[x] ? high[x+rb] : low[x+rb] : sr12
sr13 := countpp == 13 ? ph[x] ? high[x+rb] : low[x+rb] : sr13
sr14 := countpp == 14 ? ph[x] ? high[x+rb] : low[x+rb] : sr14
sr15 := countpp == 15 ? ph[x] ? high[x+rb] : low[x+rb] : sr15
sr16 := countpp == 16 ? ph[x] ? high[x+rb] : low[x+rb] : sr16
sr17 := countpp == 17 ? ph[x] ? high[x+rb] : low[x+rb] : sr17
sr18 := countpp == 18 ? ph[x] ? high[x+rb] : low[x+rb] : sr18
sr19 := countpp == 19 ? ph[x] ? high[x+rb] : low[x+rb] : sr19
sr20 := countpp == 20 ? ph[x] ? high[x+rb] : low[x+rb] : sr20

setline( level) =>


LineStyle = linestyle == 'Solid' ? line.style_solid :
linestyle == 'Dotted' ? line.style_dotted :
line.style_dashed
LineColor = linecolor == 'Blue' ? color.blue :
linecolor == 'Red' ? color.red :
linecolor == 'Lime' ? color.lime :
linecolor == 'White' ? color.white :
color.black
_ret = line.new(bar_index - prd , level, bar_index , level, color = LineColor,
width = 2, style = LineStyle, extend = extend.right)

if ph or pl
line highest_ = na, highest_ := highest_[1]
line lowest_ = na, lowest_ := lowest_[1]
line.delete(highest_)
line.delete(lowest_)
if drawhl
highest_ := line.new(bar_index - prd , highestph, bar_index , highestph,
color = color.blue, style = line.style_dashed, width = 1, extend = extend.right)
lowest_ := line.new(bar_index - prd , lowestpl, bar_index , lowestpl, color
= color.blue, style = line.style_dashed, width = 1, extend = extend.right)

line l1 = na, line l2 = na, line l3 = na, line l4 = na, line l5 = na, line l6 =
na, line l7 = na, line l8 = na, line l9 = na, line l10 = na
line l11 = na, line l12 = na, line l13 = na, line l14 = na, line l15 = na, line
l16 = na, line l17 = na, line l18 = na, line l19 = na, line l20 = na

l1 := l1[1], l2 := l2[1], l3 := l3[1], l4 := l4[1], l5 := l5[1], l6 := l6[1],


l7 := l7[1], l8 := l8[1], l9 := l9[1], l10 := l10[1]
l11 := l11[1], l12 := l12[1], l13 := l13[1], l14 := l14[1], l15 := l15[1],
l16 := l16[1], l17 := l17[1], l18 := l18[1], l19 := l19[1], l20 := l20[1]
line.delete(l1), line.delete(l2), line.delete(l3), line.delete(l4),
line.delete(l5), line.delete(l6), line.delete(l7), line.delete(l8),
line.delete(l9), line.delete(l10)
line.delete(l11), line.delete(l12), line.delete(l13), line.delete(l14),
line.delete(l15), line.delete(l16), line.delete(l17), line.delete(l18),
line.delete(l19), line.delete(l20)

l1 := sr1 ? setline(sr1) : na
l2 := sr2 ? setline(sr2) : na
l3 := sr3 ? setline(sr3) : na
l4 := sr4 ? setline(sr4) : na
l5 := sr5 ? setline(sr5) : na
l6 := sr6 ? setline(sr6) : na
l7 := sr7 ? setline(sr7) : na
l8 := sr8 ? setline(sr8) : na
l9 := sr9 ? setline(sr9) : na
l10 := sr10 ? setline(sr10) : na
l11 := sr11 ? setline(sr11) : na
l12 := sr12 ? setline(sr12) : na
l13 := sr13 ? setline(sr13) : na
l14 := sr14 ? setline(sr14) : na
l15 := sr15 ? setline(sr15) : na
l16 := sr16 ? setline(sr16) : na
l17 := sr17 ? setline(sr17) : na
l18 := sr18 ? setline(sr18) : na
l19 := sr19 ? setline(sr19) : na
l20 := sr20 ? setline(sr20) : na

// Label for S/R


chper = time - time[1]
chper := change(chper) > 0 ? chper[1] : chper

float lbl1 = na, float lbl2 = na, float lbl3 = na, float lbl4 = na, float lbl5 =
na, float lbl6 = na, float lbl7 = na, float lbl8 = na, float lbl9 = na, float lbl10
= na
float lbl11 = na, float lbl12 = na, float lbl13 = na, float lbl14 = na, float lbl15
= na, float lbl16 = na, float lbl17 = na, float lbl18 = na, float lbl19 = na, float
lbl20 = na

// set new labels if changed


lbl1 := (ph or pl) ? sr1 : lbl1[1]
lbl2 := (ph or pl) ? sr2 : lbl2[1]
lbl3 := (ph or pl) ? sr3 : lbl3[1]
lbl4 := (ph or pl) ? sr4 : lbl4[1]
lbl5 := (ph or pl) ? sr5 : lbl5[1]
lbl6 := (ph or pl) ? sr6 : lbl6[1]
lbl7 := (ph or pl) ? sr7 : lbl7[1]
lbl8 := (ph or pl) ? sr8 : lbl8[1]
lbl9 := (ph or pl) ? sr9 : lbl9[1]
lbl10 := (ph or pl) ? sr10 : lbl10[1]
lbl11 := (ph or pl) ? sr11 : lbl11[1]
lbl12 := (ph or pl) ? sr12 : lbl12[1]
lbl13 := (ph or pl) ? sr13 : lbl13[1]
lbl14 := (ph or pl) ? sr14 : lbl14[1]
lbl15 := (ph or pl) ? sr15 : lbl15[1]
lbl16 := (ph or pl) ? sr16 : lbl16[1]
lbl17 := (ph or pl) ? sr17 : lbl17[1]
lbl18 := (ph or pl) ? sr18 : lbl18[1]
lbl19 := (ph or pl) ? sr19 : lbl19[1]
lbl20 := (ph or pl) ? sr20 : lbl20[1]

// define and delete old labels


label lb1 = na, label lb2 = na, label lb3 = na, label lb4 = na, label lb5 = na,
label lb6 = na, label lb7 = na, label lb8 = na, label lb9 = na, label lb10 = na
label lb11 = na, label lb12 = na, label lb13 = na, label lb14 = na, label lb15 =
na, label lb16 = na, label lb17 = na, label lb18 = na, label lb19 = na, label lb20
= na
label.delete(lb1[1]), label.delete(lb2[1]), label.delete(lb3[1]),
label.delete(lb4[1]), label.delete(lb5[1]), label.delete(lb6[1]),
label.delete(lb7[1]), label.delete(lb8[1]), label.delete(lb9[1]),
label.delete(lb10[1])
label.delete(lb11[1]), label.delete(lb12[1]), label.delete(lb13[1]),
label.delete(lb14[1]), label.delete(lb15[1]), label.delete(lb16[1]),
label.delete(lb17[1]), label.delete(lb18[1]), label.delete(lb19[1]),
label.delete(lb20[1])
label hlabel = na
label llabel = na
label.delete(hlabel[1])
label.delete(llabel[1])

// set labels
lb1 := lbl1 ? label.new(x = time + chper * 30, y = lbl1, text = tostring(lbl1),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb2 := lbl2 ? label.new(x = time + chper * 30, y = lbl2, text = tostring(lbl2),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb3 := lbl3 ? label.new(x = time + chper * 30, y = lbl3, text = tostring(lbl3),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb4 := lbl4 ? label.new(x = time + chper * 30, y = lbl4, text = tostring(lbl4),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb5 := lbl5 ? label.new(x = time + chper * 30, y = lbl5, text = tostring(lbl5),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb6 := lbl6 ? label.new(x = time + chper * 30, y = lbl6, text = tostring(lbl6),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb7 := lbl7 ? label.new(x = time + chper * 30, y = lbl7, text = tostring(lbl7),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb8 := lbl8 ? label.new(x = time + chper * 30, y = lbl8, text = tostring(lbl8),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb9 := lbl9 ? label.new(x = time + chper * 30, y = lbl9, text = tostring(lbl9),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb10 := lbl10 ? label.new(x = time + chper * 30, y = lbl10, text = tostring(lbl10),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb11 := lbl11 ? label.new(x = time + chper * 30, y = lbl11, text = tostring(lbl11),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb12 := lbl12 ? label.new(x = time + chper * 30, y = lbl12, text = tostring(lbl12),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb13 := lbl13 ? label.new(x = time + chper * 30, y = lbl13, text = tostring(lbl13),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb14 := lbl14 ? label.new(x = time + chper * 30, y = lbl14, text = tostring(lbl14),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb15 := lbl15 ? label.new(x = time + chper * 30, y = lbl15, text = tostring(lbl15),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb16 := lbl16 ? label.new(x = time + chper * 30, y = lbl16, text = tostring(lbl16),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb17 := lbl17 ? label.new(x = time + chper * 30, y = lbl17, text = tostring(lbl17),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb18 := lbl18 ? label.new(x = time + chper * 30, y = lbl18, text = tostring(lbl18),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb19 := lbl19 ? label.new(x = time + chper * 30, y = lbl19, text = tostring(lbl19),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
lb20 := lbl20 ? label.new(x = time + chper * 30, y = lbl20, text = tostring(lbl20),
color = color.lime, textcolor=color.black, style=label.style_labeldown, xloc =
xloc.bar_time, yloc=yloc.price) : na
hlabel := drawhl ? label.new(x = time + chper * 60, y = highestph, text = "Highest
PH " + tostring(highestph), color = color.silver, textcolor=color.black,
style=label.style_labeldown, xloc = xloc.bar_time, yloc=yloc.price) : na
llabel := drawhl ? label.new(x = time + chper * 60, y = lowestpl, text = "Lowest PL
" + tostring(lowestpl), color = color.silver, textcolor=color.black,
style=label.style_labeldown, xloc = xloc.bar_time, yloc=yloc.price) : na

You might also like