-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBearish Butterfly Harmonic Patterns
150 lines (128 loc) · 11.1 KB
/
Bearish Butterfly 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 Butterfly Patterns
• Bullish butterfly patterns are fundamentally composed of two troughs and three peaks, with the second trough being higher than the first trough. And the third peak being higher than both the second and first peaks, while the second peak is lower than the first.
• Bearish butterfly patterns are fundamentally composed of two peaks and three troughs, with the second peak being lower than the first peak. And the third trough being lower than both the second and first troughs, while the second trough is higher 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 78.6% 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 224% 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 extend to 127%.
########################################################################################################################################
//@version=5
indicator('Bearish Butterfly Harmonic Patterns [theEccentricTrader]', 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 = (shPriceOne - trough) / (shPriceOne - slPriceTwo) * 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')
bearishButterfly = slPrice and returnLineDowntrend and uptrend[1] and downtrend
and slRangeRatioOne >= 78.6 - abLowerTolerance and slRangeRatioOne <= 78.6 + abUpperTolerance
and shRangeRatioZero >= 38.2 - bcLowerTolerance and shRangeRatioZero <= 88.6 + bcUpperTolerance
and slRangeRatio >= 161.8 - cdLowerTolerance and slRangeRatio <= 224.0 + cdUpperTolerance
and adRangeRatio >= 127.0 - adLowerTolerance and adRangeRatio <= 127.0 + 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 bearishButterfly
lineOne = line.new(slPriceBarIndexTwo, slPriceTwo, shPriceBarIndexOne, shPriceOne, color = patternColor, width = 2)
lineTwo = line.new(shPriceBarIndexOne, shPriceOne, slPriceBarIndexOne, slPriceOne, color = patternColor, width = 2)
lineThree = line.new(slPriceBarIndexOne, slPriceOne, peakBarIndex, peak, color = patternColor, width = 2)
lineFour = line.new(peakBarIndex, peak, slPriceBarIndex, slPrice, color = patternColor, width = 2)
lineFive = line.new(slPriceBarIndexTwo, slPriceTwo, slPriceBarIndexOne, slPriceOne, color = patternColor, style = line.style_dashed)
lineSix = line.new(slPriceBarIndexTwo, slPriceTwo, slPriceBarIndex, slPrice, color = patternColor, style = line.style_dashed)
lineSeven = line.new(slPriceBarIndexOne, slPriceOne, slPriceBarIndex, slPrice, color = patternColor, style = line.style_dashed)
lineEight = line.new(shPriceBarIndexOne, shPriceOne, peakBarIndex, peak, color = patternColor, style = line.style_dashed)
labelOne = label.new(slPriceBarIndexTwo, slPriceTwo, color = color.rgb(54, 58, 69, 100), style = label.style_label_up,
text = 'X', textcolor = labelColor)
labelTwo = label.new(shPriceBarIndexOne, shPriceOne, color = color.rgb(54, 58, 69, 100), style = label.style_label_down,
text = 'A', textcolor = labelColor)
labelThree = label.new(slPriceBarIndexOne, slPriceOne, color = color.rgb(54, 58, 69, 100), style = label.style_label_up,
text = 'B (' + str.tostring(math.round(slRangeRatioOne, 2)) + ')', textcolor = labelColor)
labelFour = label.new(peakBarIndex, peak, color = color.rgb(54, 58, 69, 100), style = label.style_label_down,
text = 'C (' + str.tostring(math.round(shRangeRatioZero, 2)) + ')', textcolor = labelColor)
labelFive = label.new(slPriceBarIndex, slPrice, color = color.rgb(54, 58, 69, 100), style = label.style_label_up,
text = 'D (' + str.tostring(math.round(slRangeRatio, 2)) + ')\n(' + str.tostring(math.round(adRangeRatio, 2)) + ')', textcolor = labelColor)
patternPeak = line.new(showProjections ? peakBarIndex : na, showProjections ? peak : na, showProjections ? bar_index : na,
showProjections ? peak : na, color = color.green, style = line.style_dashed)
patternTrough = line.new(showProjections ? peakBarIndex : na, showProjections ? trough : na, showProjections ? bar_index : na,
showProjections ? trough : na, color = color.red, style = line.style_dashed)
patternUpperProjection = line.new(showProjections ? peakBarIndex : na, showProjections ? peak + (shPriceOne - slPriceTwo) : na, showProjections ? bar_index : na,
showProjections ? peak + (shPriceOne - slPriceTwo) : na, color = color.green, style = line.style_dashed)
patternLowerProjection = line.new(showProjections ? peakBarIndex : na, showProjections ? trough - (shPriceOne - slPriceTwo) : na, showProjections ? bar_index : na,
showProjections ? trough - (shPriceOne - slPriceTwo) : 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 bearishButterfly
line.set_xy1(currentPatternPeak, showProjections ? peakBarIndex : na, showProjections ? peak : na)
line.set_xy2(currentPatternPeak, showProjections ? bar_index : na, showProjections ? peak : na)
line.set_xy1(currentPatternTrough, showProjections ? peakBarIndex : na, showProjections ? trough : na)
line.set_xy2(currentPatternTrough, showProjections ? bar_index : na, showProjections ? trough : na)
line.set_xy1(currentPatternUpperProjection, showProjections ? peakBarIndex : na, showProjections ? peak + (shPriceOne - slPriceTwo) : na)
line.set_xy2(currentPatternUpperProjection, showProjections ? bar_index : na, showProjections ? peak + (shPriceOne - slPriceTwo) : na)
line.set_xy1(currentPatternLowerProjection, showProjections ? peakBarIndex : na, showProjections ? trough - (shPriceOne - slPriceTwo) : na)
line.set_xy2(currentPatternLowerProjection, showProjections ? bar_index : na, showProjections ? trough - (shPriceOne - slPriceTwo) : na)
alert('Bearish Butterfly')