0% found this document useful (0 votes)
232 views21 pages

Wedge and Flag Finder (Multi - Zigzag)

Uploaded by

ser77.ru77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
232 views21 pages

Wedge and Flag Finder (Multi - Zigzag)

Uploaded by

ser77.ru77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 21

//@version=5

indicator("Wedge and Flag Finder (Multi - zigzag) + S/R ", max_lines_count=500,


max_labels_count=500, overlay=true, max_bars_back = 2000)
import HeWhoMustNotBeNamed/zigzag/14 as zg
import HeWhoMustNotBeNamed/arrays/1 as pa
import HeWhoMustNotBeNamed/utils/1 as utils

maxBarsBack = 2000
max_bars_back(open, maxBarsBack)
max_bars_back(high, maxBarsBack)
max_bars_back(low, maxBarsBack)
max_bars_back(close, maxBarsBack)

wedgeSize = input.int(5, 'Wedge Length', options=[5,6], group='Generic',


tooltip='Defines how many pivots a wedge should comply. Options are 5 and
6 with 5 being default')
theme = input.string('Dark', title='Theme', options=['Light', 'Dark'],
group='Generic',
tooltip='Chart theme settings. Line and label colors are generted based on
the theme settings. If dark theme is selected, '+
'lighter colors are used and if light theme is selected, darker colors are
used.')
avoidOverlap = input.bool(true, 'Suppress Overlap', group='Generic',
tooltip='Avoids plotting wedge if there is an existing wedge at starting
point. This does not avoid nesting wedges (Wedge within wedge)')
drawZigzag = input.bool(true, 'Draw Zigzag', group='Generic', tooltip='Draw zigzag
lines and mark pivots within wedge')

showZigZag1 = input.bool(true, title='L1', group='Zigzag', inline='z1')


zigzag1Length = input.int(5, step=5, minval=3, title='', group='Zigzag',
inline='z1')

showZigZag2 = input.bool(true, title='L2', group='Zigzag', inline='z1')


zigzag2Length = input.int(8, step=5, minval=3, title='', group='Zigzag',
inline='z1')

showZigZag3 = input.bool(true, title='L3', group='Zigzag', inline='z2')


zigzag3Length = input.int(13, step=5, minval=3, title='', group='Zigzag',
inline='z2')

showZigZag4 = input.bool(true, title='L4', group='Zigzag', inline='z2')


zigzag4Length = input.int(21, step=5, minval=3, title='', group='Zigzag',
inline='z2')

applyAngleDiff = input.bool(false, 'Angle Difference', inline='ad', group='Angles')


minAngleDiff = input.int(5, '', minval=0, maxval=10, inline='ad', step=5,
group='Angles')
maxAngleDiff = input.int(20, '', minval=10, maxval=90, inline='ad', step=5,
group='Angles', tooltip='Show patterns where angle between trend lines falls under
the selected range')

applyAngleLimit = input.bool(false, 'Angle Range ', inline='ar', group='Angles')


minAngleRange = input.int(10, '', minval=0, maxval=20, inline='ar', step=5,
group='Angles')
maxAngleRange = input.int(60, '', minval=20, maxval=90, inline='ar', step=5,
group='Angles', tooltip='Show patterns where at least one of the trend line is
within the given angle range')
var themeColors = theme=="Dark"? array.from(
color.rgb(251, 244, 109),
color.rgb(141, 186, 81),
color.rgb(74, 159, 245),
color.rgb(255, 153, 140),
color.rgb(255, 149, 0),
color.rgb(0, 234, 211),
color.rgb(167, 153, 183),
color.rgb(255, 210, 113),
color.rgb(119, 217, 112),
color.rgb(95, 129, 228),
color.rgb(235, 146, 190),
color.rgb(198, 139, 89),
color.rgb(200, 149, 149),
color.rgb(196, 182, 182),
color.rgb(255, 190, 15),
color.rgb(192, 226, 24),
color.rgb(153, 140, 235),
color.rgb(206, 31, 107),
color.rgb(251, 54, 64),
color.rgb(194, 255, 217),
color.rgb(255, 219, 197),
color.rgb(121, 180, 183)
) : array.from(
color.rgb(61, 86, 178),
color.rgb(57, 163, 136),
color.rgb(250, 30, 14),
color.rgb(169, 51, 58),
color.rgb(225, 87, 138),
color.rgb(62, 124, 23),
color.rgb(244, 164, 66),
color.rgb(134, 72, 121),
color.rgb(113, 159, 176),
color.rgb(170, 46, 230),
color.rgb(161, 37, 104),
color.rgb(189, 32, 0),
color.rgb(16, 86, 82),
color.rgb(200, 92, 92),
color.rgb(63, 51, 81),
color.rgb(114, 106, 149),
color.rgb(171, 109, 35),
color.rgb(247, 136, 18),
color.rgb(51, 71, 86),
color.rgb(12, 123, 147),
color.rgb(195, 43, 173)
)

maxPatternsReference = 10

var aBarArray = array.new_int()


var bBarArray = array.new_int()
var cBarArray = array.new_int()
var dBarArray = array.new_int()
var eBarArray = array.new_int()
var fBarArray = array.new_int()
var xBarArray = array.new_int()
f_angle(a, b, loopback) =>
rad2degree = 180 / math.pi
ang = rad2degree * math.atan((a - b)/(2*math.sum(ta.tr,
loopback+1)/(loopback+1)))
ang

add_new_wedge(a,b,c,d,e,f,aBar,bBar,cBar,dBar,eBar,fBar,l1Angle, l2Angle, zgColor,


wedgeSize=5)=>
dir = a>b? 1: -1

pa.unshift(fBarArray, fBar, maxPatternsReference)


pa.unshift(aBarArray, aBar, maxPatternsReference)
pa.unshift(bBarArray, bBar, maxPatternsReference)
pa.unshift(cBarArray, cBar, maxPatternsReference)
pa.unshift(dBarArray, dBar, maxPatternsReference)
pa.unshift(eBarArray, eBar, maxPatternsReference)

if(drawZigzag)
ab = line.new(bBar, b, aBar, a)
bc = line.new(cBar, c, bBar, b)
cd = line.new(dBar, d, cBar, c)
de = line.new(eBar, e, dBar, d)

aLbl = label.new(aBar, a, '5', style=label.style_none, yloc=dir>0?


yloc.abovebar:yloc.belowbar, textcolor=zgColor)
bLbl = label.new(bBar, b, '4', style=label.style_none, yloc=dir<0?
yloc.abovebar:yloc.belowbar, textcolor=zgColor)
cLbl = label.new(cBar, c, '3', style=label.style_none, yloc=dir>0?
yloc.abovebar:yloc.belowbar, textcolor=zgColor)
dLbl = label.new(dBar, d, '2', style=label.style_none, yloc=dir<0?
yloc.abovebar:yloc.belowbar, textcolor=zgColor)
eLbl = label.new(eBar, e, '1', style=label.style_none, yloc=dir>0?
yloc.abovebar:yloc.belowbar, textcolor=zgColor)
if(wedgeSize == 6)
ef = line.new(fBar, f, eBar, e)
fLbl = label.new(fBar, f, '0', style=label.style_none, yloc=dir<0?
yloc.abovebar:yloc.belowbar, textcolor=zgColor)

wedgeLine(l1StartX, l1StartY, l1EndX, l1EndY, l2StartX, l2StartY, l2EndX, l2EndY,


zgColor)=>
l1t = line.new(l1StartX, l1StartY, l1EndX, l1EndY, color=zgColor,
extend=extend.both)
l2t = line.new(l2StartX, l2StartY, l2EndX, l2EndY, color=zgColor,
extend=extend.both)

startBar = math.min(l2StartX, l1StartX)


endBar = l1EndX
l1Start = line.get_price(l1t, startBar)
l1End = line.get_price(l1t, endBar)
l2Start = line.get_price(l2t,startBar)
l2End = line.get_price(l2t, endBar)

line.set_extend(l1t, extend.none)
line.set_extend(l2t, extend.none)
line.set_x1(l1t, startBar)
line.set_y1(l1t, l1Start)
line.set_x2(l1t, endBar)
line.set_y2(l1t, l1End)

line.set_x1(l2t, startBar)
line.set_y1(l2t, l2Start)
line.set_x2(l2t, endBar)
line.set_y2(l2t, l2End)

l1Angle = f_angle(l1End, l1Start, endBar-startBar)


l2Angle = f_angle(l2End, l2Start, endBar-startBar)

l1Diff = math.abs(l1Start-l1End)
l2Diff = math.abs(l2Start-l2End)

[l1t, l2t, l1Angle, l2Angle, l1Diff, l2Diff]

find_wedge(a,b,c,d,e,f,aBar,bBar,cBar,dBar,eBar,fBar,zigzagpivots, zigzagpivotbars,
fIndex, wedgeSize=6)=>
existingPattern = false
lastPivot = wedgeSize == 6? f : e
lastPivotBar = wedgeSize == 6? fBar : eBar
llastPivot = wedgeSize == 6? e : d
for i=0 to array.size(aBarArray)==0? na: array.size(aBarArray)-1
commonPivots = (array.get(aBarArray, i) == aBar ? 1 : 0) +
(array.get(bBarArray, i) == bBar ? 1 : 0) +
(array.get(cBarArray, i) == cBar ? 1 : 0) +
(array.get(dBarArray, i) == dBar ? 1 : 0) +
(array.get(eBarArray, i) == eBar ? 1 : 0) +
(wedgeSize == 6 and array.get(fBarArray, i) == fBar ? 1 :
0)

if(commonPivots >=2) or (avoidOverlap and lastPivotBar <


array.get(aBarArray,i) and lastPivotBar > array.get(fBarArray, i))
existingPattern := true
break

if(not existingPattern)
aRatio = math.abs(a-b)/math.abs(b-c)
bRatio = math.abs(b-c)/math.abs(c-d)
cRatio = math.abs(c-d)/math.abs(d-e)
dRatio = math.abs(d-e)/math.abs(e-f)

zgColor = array.pop(themeColors)
[l1t, l2t, l1Angle, l2Angle, l1Diff, l2Diff] = wedgeLine(eBar, e, aBar, a,
wedgeSize == 6?fBar:dBar, wedgeSize == 6?f:d, bBar, b, zgColor)

isType1Wedge = aRatio >=1 and bRatio < 1 and cRatio >= 1 and (dRatio < 1 or
wedgeSize==5) and l1Diff < l2Diff
isType2Wedge = aRatio <1 and bRatio >= 1 and cRatio < 1 and (dRatio >=1 or
wedgeSize==5) and l1Diff > l2Diff

angleDiff = math.abs(l1Angle-l2Angle)
angleDiffInRange = (angleDiff >= minAngleDiff and angleDiff <=
maxAngleDiff) or not applyAngleDiff
angleInRange = (math.max(math.abs(l1Angle), math.abs(l2Angle)) >=
minAngleRange and math.min(math.abs(l1Angle), math.abs(l2Angle)) <= maxAngleRange)
or not applyAngleLimit
isWedge = (isType1Wedge or isType2Wedge) and angleDiffInRange and
angleInRange
if(isWedge)
for i = aBar to lastPivotBar
l = low[bar_index-i]
h = high[bar_index-i]
l1Price = line.get_price(l1t, i)
l2Price = line.get_price(l2t, i)
if(h < math.min(l1Price, l2Price) or l > math.max(l1Price,
l2Price))
isWedge := false
break
if(i == cBar and (l1Price > h or l1Price < l))
isWedge := false
break
if(i == dBar and (l2Price > h or l2Price < l))
isWedge := false
break

if isWedge
wedgeType = isType1Wedge? 1 : 2
add_new_wedge(a,b,c,d,e,f,aBar,bBar,cBar,dBar,eBar,fBar,l1Angle,
l2Angle, zgColor, wedgeSize)
length = array.size(zigzagpivots)
xIndexes = utils.get_trend_series(zigzagpivots, fIndex, length)
isFlag = false
for i=array.size(xIndexes)-1 to 0
xIndex = array.get(xIndexes, i)
x = array.get(zigzagpivots, xIndex)
xBar = array.get(zigzagpivotbars, xIndex)

flagRatio = math.abs(lastPivot-line.get_price(wedgeSize == 6?
l1t:l2t, lastPivotBar))/math.abs(x-lastPivot)

isFlag := flagRatio < 0.618 and (lastPivot-x)/math.abs(lastPivot-x)


== (lastPivot-(wedgeSize == 6?b:a))/math.abs(lastPivot-(wedgeSize == 6?b:a))
if(isFlag)
lFlag = line.new(xBar, x, lastPivotBar, lastPivot,
color=zgColor, extend=extend.none)
dir = x > lastPivot? 1 : -1
label.new(xBar, x, 'Flag', style=dir>0?
label.style_label_down:label.style_label_up, yloc=yloc.price, color=zgColor,
textcolor=color.black)
break
if(not isFlag)
dir = lastPivot > llastPivot? 1 : -1
label.new(fBar, f, 'Wedge', style=dir>0?
label.style_label_down:label.style_label_up, yloc=yloc.price, color=zgColor,
textcolor=color.black)

alert('New '+(isFlag?'Flag':'Wedge')+' pattern found')


array.unshift(themeColors, zgColor)
true
else
line.delete(l1t)
line.delete(l2t)
array.push(themeColors, zgColor)
false

scan_patterns(startIndex, newPivot, zigzagpivots, zigzagpivotbars,


zigzagpivotratios, zigzagpivotdirs, lastABar)=>
length = array.size(zigzagpivots)
numberOfPivots=5
newLastABar = lastABar
if(length >= startIndex+numberOfPivots+1 and newPivot)
a = array.get(zigzagpivots, startIndex)
aBar = array.get(zigzagpivotbars, startIndex)
aIndex = startIndex
lastDir = array.get(zigzagpivotdirs, startIndex)
if(aBar!=lastABar)
newLastABar := aBar
b = array.get(zigzagpivots, startIndex+1)
bBar = array.get(zigzagpivotbars, startIndex+1)
c = array.get(zigzagpivots, startIndex+2)
cBar = array.get(zigzagpivotbars, startIndex+2)
d = array.get(zigzagpivots, startIndex+3)
dBar = array.get(zigzagpivotbars, startIndex+3)
e = array.get(zigzagpivots, startIndex+4)
eBar = array.get(zigzagpivotbars, startIndex+4)
f = e
fBar = eBar
if(wedgeSize == 6)
f := array.get(zigzagpivots, startIndex+5)
fBar := array.get(zigzagpivotbars, startIndex+5)
find_wedge(a,b,c,d,e,f,aBar,bBar,cBar,dBar,eBar,fBar,zigzagpivots,
zigzagpivotbars, startIndex+wedgeSize-1,wedgeSize)
newLastABar

startIndex = 0
if(showZigZag1)
[zigzagpivots, zigzagpivotbars, zigzagpivotdirs, zigzagpivotratios, _, _, _, _,
_, newPivot, doublePivot] = zg.czigzag(zigzag1Length)

var lastABar = 0
lastABar := scan_patterns(startIndex+1, doublePivot, zigzagpivots,
zigzagpivotbars, zigzagpivotratios, zigzagpivotdirs, lastABar)
lastABar := scan_patterns(startIndex, newPivot, zigzagpivots, zigzagpivotbars,
zigzagpivotratios, zigzagpivotdirs, lastABar)

if(showZigZag2)
[zigzagpivots, zigzagpivotbars, zigzagpivotdirs, zigzagpivotratios, _, _, _, _,
_, newPivot, doublePivot] = zg.czigzag(zigzag2Length)

var lastABar = 0
lastABar := scan_patterns(startIndex+1, doublePivot, zigzagpivots,
zigzagpivotbars, zigzagpivotratios, zigzagpivotdirs, lastABar)
lastABar := scan_patterns(startIndex, newPivot, zigzagpivots, zigzagpivotbars,
zigzagpivotratios, zigzagpivotdirs, lastABar)

if(showZigZag3)
[zigzagpivots, zigzagpivotbars, zigzagpivotdirs, zigzagpivotratios, _, _, _, _,
_, newPivot, doublePivot] = zg.czigzag(zigzag3Length)

var lastABar = 0
lastABar := scan_patterns(startIndex+1, doublePivot, zigzagpivots,
zigzagpivotbars, zigzagpivotratios, zigzagpivotdirs, lastABar)
lastABar := scan_patterns(startIndex, newPivot, zigzagpivots, zigzagpivotbars,
zigzagpivotratios, zigzagpivotdirs, lastABar)

if(showZigZag4)
[zigzagpivots, zigzagpivotbars, zigzagpivotdirs, zigzagpivotratios, _, _, _, _,
_, newPivot, doublePivot] = zg.czigzag(zigzag4Length)

var lastABar = 0
lastABar := scan_patterns(startIndex+1, doublePivot, zigzagpivots,
zigzagpivotbars, zigzagpivotratios, zigzagpivotdirs, lastABar)
lastABar := scan_patterns(startIndex, newPivot, zigzagpivots, zigzagpivotbars,
zigzagpivotratios, zigzagpivotdirs, lastABar)

pivotLeg = input.int(10, "Pivot Leg")


isBullish = input.bool(true, "Bullish")
isBearish = input.bool(true, "Bearish")

ph = ta.pivothigh(high, pivotLeg, pivotLeg)


pl = ta.pivotlow(low, pivotLeg, pivotLeg)

var allPH = array.new_float(3)


var allPL = array.new_float(3)

var allPHIndex = array.new_int(3)


var allPLIndex = array.new_int(3)

if not na(ph)
allPH.push(ph)
allPHIndex.push(bar_index[pivotLeg])
allPHIndex.shift()
allPH.shift()

if not na(pl)
allPL.push(pl)
allPLIndex.push(bar_index[pivotLeg])
allPL.shift()
allPLIndex.shift()

drawLine(X1, Y1, X2, Y2, COLOR=color.lime)=>


line.new(X1, Y1, X2, Y2, color=COLOR, width = 2)

drawLabel(X, Y, TEXT, STYLE="UP", COLOR=color.lime)=>


label.new(X, Y, TEXT, color=COLOR,
style=STYLE == "UP" ? label.style_label_up : label.style_label_down,
textcolor = COLOR == color.lime ? color.black : color.white)

//#region ///////////////////// VAI ĐẦU


VAI ////////////////////////////////////////////

isRight = allPH.last() > allPL.last() and allPH.last() < allPH.get(1)


isLeft = allPH.first() > allPL.first() and allPH.first() > allPL.get(1)
isHead = allPH.get(1) > allPH.first() and allPH.get(1) > allPH.last()

var line leftA = na


var line leftB = na

var line headA = na


var line headB = na

var line rightA = na

var label leftLabel = na


var label headLabel = na
var label rightLabel = na

isHS = isLeft and


isHead and
isRight and
allPH.last() < allPH.get(1) and
allPH.last() > allPL.last() and
allPHIndex.last() > allPLIndex.last() and
allPH.get(1) > allPH.first() and
allPHIndex.get(1) > allPLIndex.get(1) and
allPH.first() > allPL.first() and
allPHIndex.first() > allPLIndex.first() and
allPHIndex.first() < allPLIndex.get(1) and
allPLIndex.last() > allPHIndex.get(1) and
allPL.last() > allPL.first()

var isHSCreated = false


var neckIndex = 0
var neckPrice = 0.0

var rsIndex = 0
var rsPrice = 0.0
var headPrice = 0.0

if isBearish and isHS and not isHS[1]


isHSCreated := true
neckIndex := allPLIndex.last()
neckPrice := allPL.last()

rsIndex := allPHIndex.last()
rsPrice := allPH.last()
headPrice := allPH.get(1)

leftA := drawLine(allPLIndex.first(), allPL.first(), allPHIndex.first(),


allPH.first(), color.red)
leftB := drawLine(allPHIndex.first(), allPH.first(), allPLIndex.get(1),
allPL.get(1), color.red)

headA := drawLine(allPLIndex.get(1), allPL.get(1), allPHIndex.get(1),


allPH.get(1), color.red)
headB := drawLine(allPHIndex.get(1), allPH.get(1), allPLIndex.get(2),
allPL.get(2), color.red)

rightA := drawLine(allPLIndex.get(2), allPL.get(2), allPHIndex.get(2),


allPH.get(2), color.red)

leftLabel := drawLabel(allPHIndex.first(), allPH.first(), "Left Shoulder",


"DOWN", color.red)
headLabel := drawLabel(allPHIndex.get(1), allPH.get(1), "Head", "DOWN",
color.red)
rightLabel := drawLabel(allPHIndex.last(), allPH.last(), "Right Shoulder",
"DOWN", color.red)

shortCondition = ta.crossunder(close, neckPrice) and isHSCreated and isBearish

var line HSNeckLineA = na


var line HSNeckLineB = na

plotshape(shortCondition ? high : na, style=shape.labeldown, text="Sell",


color=color.red, textcolor = color.white,
size=size.large, location=location.absolute)

if shortCondition
alert("Confirm H & S Sell in : " + syminfo.ticker,
alert.freq_once_per_bar_close)

if shortCondition
HSNeckLineA := drawLine(neckIndex, neckPrice, bar_index, neckPrice, color.red)
HSNeckLineA.set_style(line.style_dotted)

HSNeckLineB := drawLine(rsIndex, rsPrice, bar_index, neckPrice, color.red)


HSNeckLineB.set_style(line.style_dotted)

if shortCondition[1] or ta.crossover(close, headPrice)


isHSCreated := false

//#endregion

//#region ///////////////////// VAI ĐẦU VAI


NGƯỢC ////////////////////////////////////////////

isIVRight = allPL.last() > allPL.get(1) and allPL.last() < allPH.last()


isIVLeft = allPL.first() < allPH.get(1) and allPL.first() < allPH.first()
isIVHead = allPL.first() > allPL.get(1) and allPL.get(1) < allPL.last()

var line ivLeftA = na


var line ivLeftB = na

var line ivHeadA = na


var line ivHeadB = na

var line ivRightA = na


var line ivRightB = na

var label ivLeftLabel = na


var label ivHeadLabel = na
var label ivRightLabel = na

isIVHS = isIVLeft and


isIVHead and
isIVRight and
(allPH.get(2) > allPL.first()) and
(allPH.last() > allPH.get(1)) and
(allPH.last() < allPH.first()) and
(allPHIndex.first() < allPLIndex.first()) and
(allPLIndex.first() < allPHIndex.get(1)) and
(allPHIndex.get(1) < allPLIndex.get(1)) and
(allPLIndex.last() > allPHIndex.last()) and
(allPHIndex.last() > allPLIndex.get(1)) and
(allPL.last() < allPH.get(1))

var isIVHSCreated = false


var ivRSNeckIndex = 0
var ivRSNeckPrice = 0.0

var ivRSIndex = 0
var ivRSPrice = 0.0
var ivHeadPrice = 0.0

if isBullish and isIVHS and not isIVHS[1]


isIVHSCreated := true

ivRSNeckIndex := allPHIndex.last()
ivRSNeckPrice := allPH.last()

ivRSIndex := allPLIndex.last()
ivRSPrice := allPL.last()
ivHeadPrice := allPL.get(1)

ivLeftA := drawLine(allPHIndex.first(), allPH.first(), allPLIndex.first(),


allPL.first())
ivLeftB := drawLine(allPLIndex.first(), allPL.first(), allPHIndex.get(1),
allPH.get(1))

ivHeadA := drawLine(allPHIndex.get(1), allPH.get(1), allPLIndex.get(1),


allPL.get(1))
ivHeadB := drawLine(allPLIndex.get(1), allPL.get(1), allPHIndex.get(2),
allPH.get(2))

ivRightA := drawLine(allPHIndex.get(2), allPH.get(2), allPLIndex.get(2),


allPL.get(2))

ivLeftLabel := drawLabel(allPLIndex.first(), allPL.first(), "Left Shoulder")


ivHeadLabel := drawLabel(allPLIndex.get(1), allPL.get(1), "Head")
ivRightLabel := drawLabel(allPLIndex.last(), allPL.last(), "Right Shoulder")

longCondition = ta.crossover(close, ivRSNeckPrice) and isIVHSCreated and isBullish

var line ivHSNeckLineA = na


var line ivHSNeckLineB = na

plotshape(longCondition ? low : na, style=shape.labelup, text="Buy",


color=color.lime, textcolor = color.black,
size=size.large, location=location.absolute)

if longCondition
alert("Confirm H & S Buy in : " + syminfo.ticker,
alert.freq_once_per_bar_close)

if longCondition
ivHSNeckLineA := drawLine(ivRSNeckIndex, ivRSNeckPrice, bar_index,
ivRSNeckPrice)
ivHSNeckLineA.set_style(line.style_dotted)

ivHSNeckLineB := drawLine(ivRSIndex, ivRSPrice, bar_index, ivRSNeckPrice)


ivHSNeckLineB.set_style(line.style_dotted)

if longCondition or ta.crossunder(close, ivHeadPrice)


isIVHSCreated := false

//#endregion

///////////////////////////////////////////////// HAI ĐỈNH VÀ PHẦN CỔ


NECKLINE///////////////

length = input.int(10, step=5, minval=5)


showZigzag = input(false)
showPivots = input(true)
showStats = input(true)
bullishColor = input(color.green)
bullTrapColor = input(color.orange)
bearishColor = input(color.red)
bearTrapColor = input(color.lime)
textColor = input(color.black)

MaxRiskPerReward = input.int(30, step=5, minval=5, maxval=100)


DisplayRiskPerReward = input(true)
var zigzagvalues = array.new_float(0)
var zigzagindexes = array.new_int(0)
var zigzagdir = array.new_int(0)

var doubleTopBottomValues = array.new_float(3)


var doubleTopBottomIndexes = array.new_int(3)
var doubleTopBottomDir = array.new_int(3)

int max_array_size = 10
max_bars_back(high, 1000)
max_bars_back(low, 1000)

var lineArray = array.new_line(0)


var labelArray = array.new_label(0)
pivots(length) =>
float ph = ta.highestbars(high, length) == 0 ? high : na
float pl = ta.lowestbars(low, length) == 0 ? low : na
dir = 0
iff_1 = pl and na(ph) ? -1 : dir[1]
dir := ph and na(pl) ? 1 : iff_1
[dir, ph, pl]

add_to_array(value, index, dir) =>


mult = array.size(zigzagvalues) < 2 ? 1 : dir * value > dir *
array.get(zigzagvalues, 1) ? 2 : 1
array.unshift(zigzagindexes, index)
array.unshift(zigzagvalues, value)
array.unshift(zigzagdir, dir * mult)
if array.size(zigzagindexes) > max_array_size
array.pop(zigzagindexes)
array.pop(zigzagvalues)
array.pop(zigzagdir)

add_to_zigzag(dir, dirchanged, ph, pl, index) =>


value = dir == 1 ? ph : pl
if array.size(zigzagvalues) == 0 or dirchanged
add_to_array(value, index, dir)
else if dir == 1 and value > array.get(zigzagvalues, 0) or dir == -1 and value
< array.get(zigzagvalues, 0)
array.shift(zigzagvalues)
array.shift(zigzagindexes)
array.shift(zigzagdir)
add_to_array(value, index, dir)

zigzag(length) =>
[dir, ph, pl] = pivots(length)
dirchanged = ta.change(dir)
if ph or pl
add_to_zigzag(dir, dirchanged, ph, pl, bar_index)

calculate_double_pattern() =>
doubleTop = false
doubleTopConfirmation = 0
doubleBottom = false
doubleBottomConfirmation = 0
if array.size(zigzagvalues) >= 4
index = array.get(zigzagindexes, 1)
value = array.get(zigzagvalues, 1)
highLow = array.get(zigzagdir, 1)

lindex = array.get(zigzagindexes, 2)
lvalue = array.get(zigzagvalues, 2)
lhighLow = array.get(zigzagdir, 2)

llindex = array.get(zigzagindexes, 3)
llvalue = array.get(zigzagvalues, 3)
llhighLow = array.get(zigzagdir, 3)
risk = math.abs(value - llvalue)
reward = math.abs(value - lvalue)
riskPerReward = risk * 100 / (risk + reward)

if highLow == 1 and llhighLow == 2 and lhighLow < 0 and riskPerReward <


MaxRiskPerReward
doubleTop := true
doubleTop
if highLow == -1 and llhighLow == -2 and lhighLow > 0 and riskPerReward <
MaxRiskPerReward
doubleBottom := true
doubleBottom

if doubleTop or doubleBottom
array.set(doubleTopBottomValues, 0, value)
array.set(doubleTopBottomValues, 1, lvalue)
array.set(doubleTopBottomValues, 2, llvalue)

array.set(doubleTopBottomIndexes, 0, index)
array.set(doubleTopBottomIndexes, 1, lindex)
array.set(doubleTopBottomIndexes, 2, llindex)

array.set(doubleTopBottomDir, 0, highLow)
array.set(doubleTopBottomDir, 1, lhighLow)
array.set(doubleTopBottomDir, 2, llhighLow)

[doubleTop, doubleBottom]

get_crossover_info(doubleTop, doubleBottom) =>


index = array.get(doubleTopBottomIndexes, 0)
value = array.get(doubleTopBottomValues, 0)
highLow = array.get(doubleTopBottomDir, 0)
lindex = array.get(doubleTopBottomIndexes, 1)
lvalue = array.get(doubleTopBottomValues, 1)
lhighLow = array.get(doubleTopBottomDir, 1)

llindex = array.get(doubleTopBottomIndexes, 2)
llvalue = array.get(doubleTopBottomValues, 2)
llhighLow = array.get(doubleTopBottomDir, 2)

latestDoubleTop = false
latestDoubleBottom = false
latestDoubleTop := doubleTop ? true : doubleBottom ? false : latestDoubleTop[1]
latestDoubleBottom := doubleBottom ? true : doubleTop ? false :
latestDoubleBottom[1]

doubleTopConfirmation = 0
doubleBottomConfirmation = 0
doubleTopConfirmation := latestDoubleTop ? ta.crossunder(low, lvalue) ? 1 :
ta.crossover(high, llvalue) ? -1 : 0 : 0
doubleBottomConfirmation := latestDoubleBottom ? ta.crossover(high, lvalue) ? 1
: ta.crossunder(low, llvalue) ? -1 : 0 : 0
[doubleTopConfirmation, doubleBottomConfirmation]

draw_double_pattern(doubleTop, doubleBottom, doubleTopConfirmation,


doubleBottomConfirmation) =>
index = array.get(doubleTopBottomIndexes, 0)
value = array.get(doubleTopBottomValues, 0)
highLow = array.get(doubleTopBottomDir, 0)

lindex = array.get(doubleTopBottomIndexes, 1)
lvalue = array.get(doubleTopBottomValues, 1)
lhighLow = array.get(doubleTopBottomDir, 1)

llindex = array.get(doubleTopBottomIndexes, 2)
llvalue = array.get(doubleTopBottomValues, 2)
llhighLow = array.get(doubleTopBottomDir, 2)

isBullish = true
isBullish := doubleTop or doubleBottom ? doubleTop : isBullish[1]

risk = math.abs(value - llvalue)


reward = math.abs(value - lvalue)
riskPerReward = risk * 100 / (risk + reward)

base = line.new(x1=index, y1=value, x2=llindex, y2=llvalue, color=doubleTop ?


bearishColor : bullishColor, width=2, style=line.style_solid)
l1 = line.new(x1=index, y1=value, x2=lindex, y2=lvalue, color=doubleTop ?
bearishColor : bullishColor, width=2, style=line.style_dotted)
l2 = line.new(x1=lindex, y1=lvalue, x2=llindex, y2=llvalue, color=doubleTop ?
bearishColor : bullishColor, width=2, style=line.style_dotted)
labelText = (doubleTop ? 'Double Top' : 'Double Bottom') +
(DisplayRiskPerReward ? ' RR - ' + str.tostring(riskPerReward) : '')

baseLabel = label.new(x=index, y=value, text=labelText, yloc=doubleTop ?


yloc.abovebar : yloc.belowbar, color=doubleTop ? bearishColor : bullishColor,
style=doubleTop ? label.style_label_down : label.style_label_up,
textcolor=textColor, size=size.normal)

if not(doubleTop or doubleBottom)
line.delete(base)
line.delete(l1)
line.delete(l2)
label.delete(baseLabel)

var doubleTopCount = 0
var doubleBottomCount = 0
doubleTopCount := doubleTop ? nz(doubleTopCount[1], 0) + 1 :
nz(doubleTopCount[1], 0)
doubleBottomCount := doubleBottom ? nz(doubleBottomCount[1], 0) + 1 :
nz(doubleBottomCount[1], 0)

if line.get_x2(base) == line.get_x2(base[1])
line.delete(base[1])
line.delete(l1[1])
line.delete(l2[1])
label.delete(baseLabel[1])
doubleTopCount := doubleTop ? doubleTopCount - 1 : doubleTopCount
doubleBottomCount := doubleBottom ? doubleBottomCount - 1 :
doubleBottomCount
doubleBottomCount

if barstate.islast
lres = line.new(x1=bar_index, y1=lvalue, x2=lindex, y2=lvalue,
color=isBullish ? bearishColor : bullishColor, width=2, style=line.style_dashed,
extend=extend.left)
lsup = line.new(x1=bar_index, y1=llvalue, x2=llindex, y2=llvalue,
color=isBullish ? bullishColor : bearishColor, width=2, style=line.style_dashed,
extend=extend.left)
lsup

doubleTopConfirmationCount = doubleTopConfirmation > 0 ? 1 : 0


doubleBottomConfirmationCount = doubleBottomConfirmation > 0 ? 1 : 0
doubleTopInvalidationCount = doubleTopConfirmation < 0 ? 1 : 0
doubleBottomInvalidationCount = doubleBottomConfirmation < 0 ? 1 : 0

if doubleTopConfirmation != 0 or doubleBottomConfirmation != 0
if doubleTopConfirmation > 0 or doubleBottomConfirmation > 0
lresbreak = line.new(x1=lindex, y1=lvalue, x2=bar_index, y2=lvalue,
color=isBullish ? bearishColor : bullishColor, width=2, style=line.style_dashed)
if line.get_x1(lresbreak[1]) == line.get_x1(lresbreak)
doubleTopConfirmationCount := 0
doubleBottomConfirmationCount := 0
doubleTopInvalidationCount := 0
doubleBottomInvalidationCount := 0
line.delete(lresbreak)
lresbreak := lresbreak[1]
lresbreak
else if doubleTopConfirmation < 0 or doubleBottomConfirmation < 0
lsupbreak = line.new(x1=llindex, y1=llvalue, x2=bar_index, y2=llvalue,
color=isBullish ? bullishColor : bearishColor, width=2, style=line.style_dashed)
if line.get_x1(lsupbreak[1]) == line.get_x1(lsupbreak)
doubleTopInvalidationCount := 0
doubleBottomInvalidationCount := 0
doubleTopConfirmationCount := 0
doubleBottomConfirmationCount := 0
line.delete(lsupbreak)
lsupbreak := lsupbreak[1]
lsupbreak
doubleTopConfirmationCount := nz(doubleTopConfirmationCount[1], 0) +
doubleTopConfirmationCount
doubleBottomConfirmationCount := nz(doubleBottomConfirmationCount[1], 0) +
doubleBottomConfirmationCount
doubleTopInvalidationCount := nz(doubleTopInvalidationCount[1], 0) +
doubleTopInvalidationCount
doubleBottomInvalidationCount := nz(doubleBottomInvalidationCount[1], 0) +
doubleBottomInvalidationCount
[doubleTopCount, doubleBottomCount, doubleTopConfirmationCount,
doubleBottomConfirmationCount, doubleTopInvalidationCount,
doubleBottomInvalidationCount]

zigzag(length)

[doubleTop, doubleBottom] = calculate_double_pattern()


[doubleTopConfirmation, doubleBottomConfirmation] = get_crossover_info(doubleTop,
doubleBottom)
[doubleTopCount, doubleBottomCount, doubleTopConfirmationCount,
doubleBottomConfirmationCount, doubleTopInvalidationCount,
doubleBottomInvalidationCount] = draw_double_pattern(doubleTop, doubleBottom,
doubleTopConfirmation, doubleBottomConfirmation)

var stats = table.new(position=position.top_right, columns=5, rows=5,


border_width=1)
if barstate.islast and showStats
colorWorst = color.rgb(255, 153, 51)
colorBest = color.rgb(51, 204, 51)
colorBad = color.rgb(255, 204, 153)
colorGood = color.rgb(204, 255, 204)
colorNeutral = color.rgb(255, 255, 204)

dtConfirmationPercent = doubleTopConfirmationCount + doubleTopInvalidationCount


== 0 ? 0.5 : doubleTopConfirmationCount / (doubleTopConfirmationCount +
doubleTopInvalidationCount)
dbConfirmationPercent = doubleBottomConfirmationCount +
doubleBottomInvalidationCount == 0 ? 0.5 : doubleBottomConfirmationCount /
(doubleBottomConfirmationCount + doubleBottomInvalidationCount)

dtColor = dtConfirmationPercent >= 0.8 ? colorBest : dtConfirmationPercent >=


0.6 ? colorGood : dtConfirmationPercent >= 0.4 ? colorNeutral :
dtConfirmationPercent >= 0.2 ? colorBad : colorWorst
dbColor = dbConfirmationPercent >= 0.8 ? colorBest : dbConfirmationPercent >=
0.6 ? colorGood : dbConfirmationPercent >= 0.4 ? colorNeutral :
dbConfirmationPercent >= 0.2 ? colorBad : colorWorst
table.cell(table_id=stats, column=0, row=0, text='', bgcolor=color.black,
text_color=color.white)
table.cell(table_id=stats, column=0, row=1, text='Double Top',
bgcolor=color.black, text_color=color.white)
table.cell(table_id=stats, column=0, row=2, text='Double Bottom',
bgcolor=color.black, text_color=color.white)

table.cell(table_id=stats, column=1, row=0, text='Count', bgcolor=color.black,


text_color=color.white)
table.cell(table_id=stats, column=2, row=0, text='Confirmation',
bgcolor=color.black, text_color=color.white)
table.cell(table_id=stats, column=3, row=0, text='Invalidation',
bgcolor=color.black, text_color=color.white)
table.cell(table_id=stats, column=1, row=1, text=str.tostring(doubleTopCount),
bgcolor=dtColor)
table.cell(table_id=stats, column=1, row=2,
text=str.tostring(doubleBottomCount), bgcolor=dbColor)

table.cell(table_id=stats, column=2, row=1,


text=str.tostring(doubleTopConfirmationCount), bgcolor=dtColor)
table.cell(table_id=stats, column=3, row=1,
text=str.tostring(doubleTopInvalidationCount), bgcolor=dtColor)

table.cell(table_id=stats, column=2, row=2,


text=str.tostring(doubleBottomConfirmationCount), bgcolor=dbColor)
table.cell(table_id=stats, column=3, row=2,
text=str.tostring(doubleBottomInvalidationCount), bgcolor=dbColor)

if barstate.islast and array.size(zigzagindexes) > 1


lastHigh = 0.0
lastLow = 0.0
for x = 0 to array.size(zigzagindexes) - 1 by 1
i = array.size(zigzagindexes) - 1 - x
index = array.get(zigzagindexes, i)
value = array.get(zigzagvalues, i)
highLow = array.get(zigzagdir, i)
index_offset = bar_index - index

labelText = highLow == 2 ? 'HH' : highLow == 1 ? 'LH' : highLow == -1 ?


'HL' : 'LL'
labelColor = highLow == 2 ? bullishColor : highLow == 1 ? bullTrapColor :
highLow == -1 ? bearTrapColor : bearishColor
labelStyle = highLow > 0 ? label.style_label_down : label.style_label_up
// labelLocation = highLow > 0? yloc.abovebar : yloc.belowbar
labelLocation = yloc.price
if showPivots
l = label.new(x=index, y=value, text=labelText, xloc=xloc.bar_index,
yloc=labelLocation, style=labelStyle, size=size.tiny, color=labelColor,
textcolor=textColor)
array.unshift(labelArray, l)
if array.size(labelArray) > 100
label.delete(array.pop(labelArray))
if i < array.size(zigzagindexes) - 1 and showZigzag
indexLast = array.get(zigzagindexes, i + 1)
valueLast = array.get(zigzagvalues, i + 1)
l = line.new(x1=index, y1=value, x2=indexLast, y2=valueLast,
color=labelColor, width=2, style=line.style_solid)

array.unshift(lineArray, l)
if array.size(lineArray) > 100
line.delete(array.pop(lineArray))
//alertcondition(doubleBottom, 'Double Bottom', 'Probable double bottom observed
for {{ticker}} on {{interval}} timeframe')
//alertcondition(doubleBottomConfirmation > 0, 'Double Bottom Confirmation',
'Double bottom confirmation observed for {{ticker}} on {{interval}} timeframe')
//alertcondition(doubleBottomConfirmation < 0, 'Double Bottom Invalidation',
'Double bottom invalidation observed for {{ticker}} on {{interval}} timeframe')
//alertcondition(doubleTop, 'Double Top', 'Probable double top observed for
{{ticker}} on {{interval}} timeframe')
//alertcondition(doubleTopConfirmation > 0, 'Double Top Confirmation', 'Double top
confirmation observed for {{ticker}} on {{interval}} timeframe')
//alertcondition(doubleTopConfirmation < 0, 'Double Top Invalidation', 'Double top
invalidation observed for {{ticker}} on {{interval}} timeframe')

//---------------------------------------------------------------------------------
----------------

Bars = input.int( 10 , "Number Of Bars For Swing" , 5 , confirm = true)


NoOfPivots = input.int(2 , "No of Tests for a Valid S and R Level" , options =
[2,3,4] , confirm = true )

PivotHigh = ta.pivothigh( Bars , Bars)

var bool InsideBarAtSupport = false

var float RZoneH = na


var float RZoneL = na

var line l1 = na
var line l2 = na

IndexOfPivot = ta.valuewhen(PivotHigh > 0 , bar_index - Bars ,0)


HighAtPivot = ta.valuewhen(PivotHigh > 0, high[Bars] , 0 )
UpperAtPivot = ta.valuewhen(PivotHigh > 0, math.max(close[Bars] , open[Bars]) , 0 )

IndexOfPivot1 = ta.valuewhen(PivotHigh> 0 , bar_index - Bars ,1)


HighAtPivot1 = ta.valuewhen(PivotHigh > 0, high[Bars] , 1 )
UpperAtPivot1 = ta.valuewhen(PivotHigh> 0 , math.max(close[Bars] , open[Bars]) ,
1 )

IndexOfPivot2 = ta.valuewhen(PivotHigh > 0, bar_index - Bars ,2)


HighAtPivot2 = ta.valuewhen(PivotHigh > 0, high[Bars] , 2)
UpperAtPivot2 = ta.valuewhen(PivotHigh > 0, math.max(close[Bars] , open[Bars]) , 2)

IndexOfPivot3 = ta.valuewhen(PivotHigh > 0, bar_index - Bars ,3)


HighAtPivot3 = ta.valuewhen(PivotHigh > 0, high[Bars] , 3)
UpperAtPivot3 = ta.valuewhen(PivotHigh > 0, math.max(close[Bars] , open[Bars]) ,
3 )

BreakOutCandle = ta.valuewhen( high > HighAtPivot2 , bar_index , 0 )

bool alertcon = na
candle=math.abs(high-low)
body = math.abs(open - close)

EvnCan = body > candle * 0.6 and open > close and body[1] < candle[1] * 0.3 and
body[2] > candle[2] * 0.6 and open[2] < close[2]
BInBar = high < high[1] and low > low[1] and body[1] > candle[1] * 0.4

EvnCanHigh = ta.valuewhen(EvnCan , high[1] , 0)


BInBarHigh = ta.valuewhen(BInBar , high[1] , 0)

if PivotHigh > 0
if NoOfPivots == 2 and HighAtPivot < HighAtPivot1 and HighAtPivot >
UpperAtPivot1
RZoneH := HighAtPivot1
RZoneL := UpperAtPivot1
alertcon := true
l1 := line.new( IndexOfPivot1 , HighAtPivot1 , bar_index , HighAtPivot1 ,
color = color.red)
l2 := line.new( IndexOfPivot1 , UpperAtPivot1 , bar_index , UpperAtPivot1 ,
color = color.red)

if NoOfPivots == 3 and HighAtPivot < HighAtPivot2 and HighAtPivot >


UpperAtPivot2 and HighAtPivot1 < HighAtPivot2 and HighAtPivot1 > UpperAtPivot2
alertcon := true
RZoneH := HighAtPivot2
RZoneL := UpperAtPivot2
l1 := line.new( IndexOfPivot2 , HighAtPivot2 , bar_index , HighAtPivot2 ,
color = color.red)
l2 := line.new( IndexOfPivot2 , UpperAtPivot2 , bar_index , UpperAtPivot2 ,
color = color.red)

if NoOfPivots == 4 and HighAtPivot < HighAtPivot3 and HighAtPivot >


UpperAtPivot3 and HighAtPivot1 < HighAtPivot3 and HighAtPivot1 > UpperAtPivot3 and
HighAtPivot2 < HighAtPivot3 and HighAtPivot2 > UpperAtPivot3
RZoneH := HighAtPivot3
RZoneL := UpperAtPivot3
alertcon := true
l1 := line.new( IndexOfPivot3 , HighAtPivot3 , bar_index , HighAtPivot3,
color = color.red )
l2 := line.new( IndexOfPivot3 , UpperAtPivot3 , bar_index , UpperAtPivot3,
color = color.red )

if EvnCan and (EvnCanHigh < RZoneH and EvnCanHigh > RZoneL)


label.new(bar_index[1] , high[1] , "Evening Star" , color = color.orange ,
textcolor = color.white)

if BInBar and (BInBarHigh < RZoneH and BInBarHigh > RZoneL)


label.new(bar_index[1] , high[1] , "Inside Bar" , color = color.rgb(224, 67,
5), textcolor = color.white )

bperc = ((high[1] - low[1]) / 100) * 5


bpercc = ((high[1] - low[1]) / 100) * 60

BodyPerc = (body/candle)*100
tbc = low > (low[1] - bperc) and low < (low[1] + bperc) and (close > (low[1] +
bpercc )) and (open[1] > close[1]) and (close[1] - low[1] < bperc) and (open - low
< bperc) and (BodyPerc > 60) and ( BodyPerc[1] > 60 )
ttc = high > (high[1] - bperc) and high < (high[1] + bperc) and (close < (high[1]
- bpercc )) and (open[1] < close[1]) and ((high[1] - close[1]) < bperc) and
(( high - open) < bperc) and (BodyPerc > 60) and ( BodyPerc[1] > 60 )
TTopH = ta.valuewhen(ttc , math.max(high , high[1] ), 0)

if ttc and ( TTopH < RZoneH and TTopH > RZoneL)


label.new(bar_index[1] , high[1] , "Tweezer Top" , color = color.rgb(255, 0,
208) , textcolor = color.white)

line.set_x2(l1 , bar_index)
line.set_x2(l2 , bar_index)
linefill.new(l1, l2 , color = color.new(color.red, 80))

plot(PivotHigh, style=plot.style_cross, linewidth=3, color= color.red, offset=-


Bars)

PivotLow = ta.pivotlow( Bars , Bars)

var line l4 = na
var line l3 = na

IndexOfPivotl = ta.valuewhen(PivotLow > 0 , bar_index - Bars ,0)


LowAtPivot = ta.valuewhen(PivotLow > 0, low[Bars] , 0 )
LowerAtPivot = ta.valuewhen(PivotLow > 0, math.min(close[Bars] , open[Bars]) , 0 )

IndexOfPivotl1 = ta.valuewhen(PivotLow > 0, bar_index - Bars ,1)


LowAtPivot1 = ta.valuewhen(PivotLow > 0, low[Bars] , 1 )
LowerAtPivotl = ta.valuewhen(PivotLow> 0 , math.min(close[Bars] , open[Bars]) , 1 )

IndexOfPivotl2 = ta.valuewhen(PivotLow > 0, bar_index - Bars ,2)


LowAtPivot2 = ta.valuewhen(PivotLow > 0, low[Bars] , 2)
LowerAtPivot2 = ta.valuewhen(PivotLow > 0, math.min(close[Bars] , open[Bars]) , 2)

IndexOfPivotl3 = ta.valuewhen(PivotLow > 0, bar_index - Bars ,3)


LowAtPivot3 = ta.valuewhen(PivotLow > 0, low[Bars] , 3)
LowerAtPivot3 = ta.valuewhen(PivotLow> 0 , math.min(close[Bars] , open[Bars]) , 3 )

var float SZoneH = na


var float SZoneL = na

if PivotLow> 0
if NoOfPivots == 2 and LowAtPivot > LowAtPivot1 and LowAtPivot < LowerAtPivotl
alertcon := true
SZoneL := LowAtPivot1
SZoneH := LowerAtPivotl

l4 := line.new( IndexOfPivotl1 , LowAtPivot1 , bar_index , LowAtPivot1 ,


color = color.green)
l3 := line.new( IndexOfPivotl1 , LowerAtPivotl , bar_index ,
LowerAtPivotl , color = color.green)
if NoOfPivots == 3 and LowAtPivot > LowAtPivot2 and LowAtPivot < LowerAtPivot2
and LowAtPivot1 > LowAtPivot2 and LowAtPivot1 < LowerAtPivot2
alertcon := true
SZoneL := LowAtPivot2
SZoneH := LowerAtPivot2

l4 := line.new( IndexOfPivotl2 , LowAtPivot2 , bar_index , LowAtPivot2 ,


color = color.green)
l3 := line.new( IndexOfPivotl2 , LowerAtPivot2 , bar_index ,
LowerAtPivot2 , color = color.green)
if NoOfPivots == 4 and LowAtPivot > LowAtPivot3 and LowAtPivot < LowerAtPivot3
and LowAtPivot1 > LowAtPivot3 and LowAtPivot1 < LowerAtPivot3 and LowAtPivot2 >
LowAtPivot3 and LowAtPivot2 < LowerAtPivot3
alertcon := true
SZoneL := LowAtPivot3
SZoneH := LowerAtPivot3

l4 := line.new( IndexOfPivotl3 , LowAtPivot3 , bar_index , LowAtPivot3,


color = color.green )
l3 := line.new( IndexOfPivotl3 , LowerAtPivot3 , bar_index , LowerAtPivot3,
color = color.green )

MorCan = body > candle * 0.6 and open < close and body[1] < candle[1] * 0.3 and
body[2] > candle[2] * 0.6 and open[2] > close[2]
BullInBar = high < high[1] and low > low[1] and body[1] > candle[1] * 0.4

MorCanLow = ta.valuewhen(MorCan , low[1] , 0)


BullInBarLow = ta.valuewhen(BullInBar , low[1] , 0)

if MorCan and ( MorCanLow < SZoneH and MorCanLow > SZoneL)


label.new(bar_index[1] , low[1] , "Morning Star" ,color =
color.orange,textcolor = color.white , style = label.style_label_up)

if BullInBar and ( BullInBarLow < SZoneH and BullInBarLow > SZoneL)


label.new(bar_index[1] , low[1] , "Inside Bar" , color = color.rgb(255, 72,
0),textcolor = color.white , style = label.style_label_up)
InsideBarAtSupport := true

TBotL = ta.valuewhen(tbc , math.max(low , low[1] ), 0)

if tbc and ( TBotL < SZoneH and TBotL > SZoneL)


label.new(bar_index[1] , low[1] , "Tweezer Bottom" , color = color.rgb(255, 0,
208) , textcolor = color.white , style = label.style_label_up)

line.set_x2(l4 , bar_index)
line.set_x2(l3 , bar_index)
linefill.new(l4, l3 , color = color.new(color.green, 80))

plot(PivotLow, style=plot.style_cross, linewidth=3, color= color.rgb(0, 208, 52),


offset=-Bars)
alertcondition(condition = alertcon , title = "Support And Resistance Zone
Alert" ,message = "SR Zone Has Been Detected")

TableR = table.new(position = position.top_right , columns = 5 , rows = 3 ,


frame_color = color.white , frame_width = 2 ,border_color = color.white ,
border_width = 2, bgcolor = color.rgb(49, 49, 49))

//

You might also like