-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBearish Bat Harmonic Patterns
150 lines (128 loc) · 11 KB
/
Bearish Bat Harmonic Patterns
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
Bullish and Bearish Bat Patterns
• Bullish bat patterns are fundamentally composed of three troughs and two peaks, with the second peak being lower than the first peak and the third trough being lower than the second but higher than the first.
• Bearish bat patterns are fundamentally composed of three peaks and two troughs, with the second trough being higher than the first trough and the third peak being higher than the second but lower than the first.
The most commonly recognised ratio measurements used by traders today are as follows:
• Wave 1 of the pattern, generally referred to as XA, has no specific ratio requirements.
• Wave 2 of the pattern, generally referred to as AB, should retrace by at least 38.2%, but no further than 50.0% of the range set by wave 1.
• Wave 3 of the pattern, generally referred to as BC, should retrace by at least 38.2%, but no further than 88.6% of the range set by wave 2.
• Wave 4 of the pattern, generally referred to as CD, should extend to at least 161.8%, but no further than 261.8% of the range set by wave 3.
• The last measure, generally referred to as AD, is that of wave 4 as a ratio of the range set by wave 1, which should retrace to 88.6%.
########################################################################################################################################
//@version=5
indicator('Bearish Bat Harmonic Patterns', overlay = true, max_lines_count = 500, max_labels_count = 250)
shPrice = close[1] >= open[1] and close < open and high >= high[1] and barstate.isconfirmed ? high :
close[1] >= open[1] and close < open and high <= high[1] and barstate.isconfirmed ? high[1] : na
shPriceBarIndex = close[1] >= open[1] and close < open and high >= high[1] and barstate.isconfirmed ? bar_index :
close[1] >= open[1] and close < open and high <= high[1] and barstate.isconfirmed ? bar_index - 1 : na
peak = ta.valuewhen(shPrice, shPrice, 0)
peakBarIndex = ta.valuewhen(shPrice, shPriceBarIndex, 0)
shPriceOne = ta.valuewhen(shPrice, shPrice, 1)
shPriceBarIndexOne = ta.valuewhen(shPriceBarIndex, shPriceBarIndex, 1)
shPriceTwo = ta.valuewhen(shPrice, shPrice, 2)
shPriceBarIndexTwo = ta.valuewhen(shPriceBarIndex, shPriceBarIndex, 2)
slPrice = close[1] < open[1] and close >= open and low <= low[1] and barstate.isconfirmed ? low :
close[1] < open[1] and close >= open and low >= low[1] and barstate.isconfirmed ? low[1] : na
slPriceBarIndex = close[1] < open[1] and close >= open and low <= low[1] and barstate.isconfirmed ? bar_index :
close[1] < open[1] and close >= open and low >= low[1] and barstate.isconfirmed ? bar_index - 1 : na
trough = ta.valuewhen(slPrice, slPrice, 0)
troughBarIndex = ta.valuewhen(slPrice, slPriceBarIndex, 0)
slPriceOne = ta.valuewhen(slPrice, slPrice, 1)
slPriceBarIndexOne = ta.valuewhen(slPriceBarIndex, slPriceBarIndex, 1)
slPriceTwo = ta.valuewhen(slPrice, slPrice, 2)
slPriceBarIndexTwo = ta.valuewhen(slPriceBarIndex, slPriceBarIndex, 2)
returnLineUptrend = ta.valuewhen(shPrice, shPrice, 0) > ta.valuewhen(shPrice, shPrice, 1)
uptrend = ta.valuewhen(slPrice, slPrice, 0) > ta.valuewhen(slPrice, slPrice, 1)
downtrend = ta.valuewhen(shPrice, shPrice, 0) < ta.valuewhen(shPrice, shPrice, 1)
returnLineDowntrend = ta.valuewhen(slPrice, slPrice, 0) < ta.valuewhen(slPrice, slPrice, 1)
slRangeRatio = (peak - trough) / (peak - trough[1]) * 100
shRangeRatio = (peak - trough) / (peak[1] - trough) * 100
slRangeRatioZero = ta.valuewhen(slPrice, slRangeRatio, 0)
slRangeRatioOne = ta.valuewhen(slPrice, slRangeRatio, 1)
shRangeRatioZero = ta.valuewhen(shPrice, shRangeRatio, 0)
shRangeRatioOne = ta.valuewhen(shPrice, shRangeRatio, 1)
adRangeRatio = (shPrice - slPriceOne) / (shPriceTwo - slPriceOne) * 100
abLowerTolerance = input(defval = 10, title = 'AB Lower Tolerance (%)', group = 'Tolerances')
abUpperTolerance = input(defval = 10, title = 'AB Upper Tolerance (%)', group = 'Tolerances')
bcLowerTolerance = input(defval = 10, title = 'BC Lower Tolerance (%)', group = 'Tolerances')
bcUpperTolerance = input(defval = 10, title = 'BC Upper Tolerance (%)', group = 'Tolerances')
cdLowerTolerance = input(defval = 10, title = 'CD Lower Tolerance (%)', group = 'Tolerances')
cdUpperTolerance = input(defval = 10, title = 'CD Upper Tolerance (%)', group = 'Tolerances')
adLowerTolerance = input(defval = 10, title = 'AD Lower Tolerance (%)', group = 'Tolerances')
adUpperTolerance = input(defval = 10, title = 'AD Upper Tolerance (%)', group = 'Tolerances')
bearishBat = shPrice and returnLineUptrend and downtrend[1] and uptrend
and shRangeRatioOne >= 38.2 - abLowerTolerance and shRangeRatioOne <= 50.0 + abUpperTolerance
and slRangeRatioZero >= 38.2 - bcLowerTolerance and slRangeRatioZero <= 88.6 + bcUpperTolerance
and shRangeRatio >= 161.8 - cdLowerTolerance and shRangeRatio <= 261.8 + cdUpperTolerance
and adRangeRatio >= 88.6 - adLowerTolerance and adRangeRatio <= 88.6 + adUpperTolerance
patternColor = input(defval = color.blue, title = 'Pattern Color', group = 'Line Coloring')
labelColor = input(defval = color.blue, title = 'Label Color', group = 'Label Coloring')
showProjections = input(defval = true, title = 'Show Projections', group = 'Lines')
selectProjectionExtend = input.string(title = 'Extend Current Projection Lines', defval = 'None', options = ['None', 'Right', 'Left', 'Both'], group = "Line Extension")
extendProjectionLines = selectProjectionExtend == 'None' ? extend.none : selectProjectionExtend == 'Right' ? extend.right :
selectProjectionExtend == 'Left' ? extend.left : selectProjectionExtend == 'Both' ? extend.both : na
var currentPatternPeak = line.new(na, na, na, na, extend = extendProjectionLines, color = color.green, style = line.style_dashed)
var currentPatternTrough = line.new(na, na, na, na, extend = extendProjectionLines, color = color.red, style = line.style_dashed)
var currentPatternUpperProjection = line.new(na, na, na, na, extend = extendProjectionLines, color = color.green, style = line.style_dashed)
var currentPatternLowerProjection = line.new(na, na, na, na, extend = extendProjectionLines, color = color.red, style = line.style_dashed)
if bearishBat
lineOne = line.new(shPriceBarIndexTwo, shPriceTwo, slPriceBarIndexOne, slPriceOne, color = patternColor, width = 2)
lineTwo = line.new(slPriceBarIndexOne, slPriceOne, shPriceBarIndexOne, shPriceOne, color = patternColor, width = 2)
lineThree = line.new(shPriceBarIndexOne, shPriceOne, troughBarIndex, trough, color = patternColor, width = 2)
lineFour = line.new(troughBarIndex, trough, shPriceBarIndex, shPrice, color = patternColor, width = 2)
lineFive = line.new(shPriceBarIndexTwo, shPriceTwo, shPriceBarIndexOne, shPriceOne, color = patternColor, style = line.style_dashed)
lineSix = line.new(shPriceBarIndexTwo, shPriceTwo, shPriceBarIndex, shPrice, color = patternColor, style = line.style_dashed)
lineSeven = line.new(shPriceBarIndexOne, shPriceOne, shPriceBarIndex, shPrice, color = patternColor, style = line.style_dashed)
lineEight = line.new(slPriceBarIndexOne, slPriceOne, troughBarIndex, trough, color = patternColor, style = line.style_dashed)
labelOne = label.new(shPriceBarIndexTwo, shPriceTwo, color = color.rgb(54, 58, 69, 100), style = label.style_label_down,
text = 'X', textcolor = labelColor)
labelTwo = label.new(slPriceBarIndexOne, slPriceOne, color = color.rgb(54, 58, 69, 100), style = label.style_label_up,
text = 'A', textcolor = labelColor)
labelThree = label.new(shPriceBarIndexOne, shPriceOne, color = color.rgb(54, 58, 69, 100), style = label.style_label_down,
text = 'B (' + str.tostring(math.round(shRangeRatioOne, 2)) + ')', textcolor = labelColor)
labelFour = label.new(troughBarIndex, trough, color = color.rgb(54, 58, 69, 100), style = label.style_label_up,
text = 'C (' + str.tostring(math.round(slRangeRatioZero, 2)) + ')', textcolor = labelColor)
labelFive = label.new(shPriceBarIndex, shPrice, color = color.rgb(54, 58, 69, 100), style = label.style_label_down,
text = 'D (' + str.tostring(math.round(shRangeRatio, 2)) + ')\n(' + str.tostring(math.round(adRangeRatio, 2)) + ')', textcolor = labelColor)
patternPeak = line.new(showProjections ? troughBarIndex : na, showProjections ? peak : na, showProjections ? bar_index : na,
showProjections ? peak : na, color = color.green, style = line.style_dashed)
patternTrough = line.new(showProjections ? troughBarIndex : na, showProjections ? trough : na, showProjections ? bar_index : na,
showProjections ? trough : na, color = color.red, style = line.style_dashed)
patternUpperProjection = line.new(showProjections ? troughBarIndex : na, showProjections ? peak + (shPriceTwo - slPriceOne) : na, showProjections ? bar_index : na,
showProjections ? peak + (shPriceTwo - slPriceOne) : na, color = color.green, style = line.style_dashed)
patternLowerProjection = line.new(showProjections ? troughBarIndex : na, showProjections ? trough - (shPriceTwo - slPriceOne) : na, showProjections ? bar_index : na,
showProjections ? trough - (shPriceTwo - slPriceOne) : na, color = color.red, style = line.style_dashed)
var myLineArray = array.new_line()
array.push(myLineArray, lineOne)
array.push(myLineArray, lineTwo)
array.push(myLineArray, lineThree)
array.push(myLineArray, lineFour)
array.push(myLineArray, lineFive)
array.push(myLineArray, lineSix)
array.push(myLineArray, lineSeven)
array.push(myLineArray, lineEight)
array.push(myLineArray, patternPeak)
array.push(myLineArray, patternTrough)
array.push(myLineArray, patternUpperProjection)
array.push(myLineArray, patternLowerProjection)
if array.size(myLineArray) >= 500
firstLine = array.remove(myLineArray, 0)
line.delete(firstLine)
var myLabelArray = array.new_label()
array.push(myLabelArray, labelOne)
array.push(myLabelArray, labelTwo)
array.push(myLabelArray, labelThree)
array.push(myLabelArray, labelFour)
array.push(myLabelArray, labelFive)
if array.size(myLabelArray) >= 250
firstLabel = array.remove(myLabelArray, 0)
label.delete(firstLabel)
if bearishBat
line.set_xy1(currentPatternPeak, showProjections ? troughBarIndex : na, showProjections ? peak : na)
line.set_xy2(currentPatternPeak, showProjections ? bar_index : na, showProjections ? peak : na)
line.set_xy1(currentPatternTrough, showProjections ? troughBarIndex : na, showProjections ? trough : na)
line.set_xy2(currentPatternTrough, showProjections ? bar_index : na, showProjections ? trough : na)
line.set_xy1(currentPatternUpperProjection, showProjections ? troughBarIndex : na, showProjections ? peak + (shPriceTwo - slPriceOne) : na)
line.set_xy2(currentPatternUpperProjection, showProjections ? bar_index : na, showProjections ? peak + (shPriceTwo - slPriceOne) : na)
line.set_xy1(currentPatternLowerProjection, showProjections ? troughBarIndex : na, showProjections ? trough - (shPriceTwo - slPriceOne) : na)
line.set_xy2(currentPatternLowerProjection, showProjections ? bar_index : na, showProjections ? trough - (shPriceTwo - slPriceOne) : na)
alert('Bearish Bat')