Bearish ABCD Harmonic Patterns [theEccentricTrader]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © theEccentricTrader
//@version=5


indicator('Bearish ABCD Harmonic Patterns [theEccentricTrader]', overlay = true, max_lines_count = 500, max_labels_count = 250)


//////////// shsl //////////// 


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)

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)

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)
shRangeRatioZero = ta.valuewhen(shPrice, shRangeRatio, 0)

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')

bearishABCD = shPrice and returnLineUptrend and uptrend
 and slRangeRatioZero >= 61.8 - bcLowerTolerance and slRangeRatioZero <= 78.6 + bcUpperTolerance
 and shRangeRatio >= 127.2 - cdLowerTolerance and shRangeRatio <= 161.8 + cdUpperTolerance


//////////// lines //////////// 


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 bearishABCD 
    lineOne = line.new(slPriceBarIndexOne, slPriceOne, shPriceBarIndexOne, shPriceOne, color = patternColor, width = 2)
    lineTwo = line.new(shPriceBarIndexOne, shPriceOne, troughBarIndex, trough, color = patternColor, width = 2)
    lineThree = line.new(troughBarIndex, trough, shPriceBarIndex, shPrice, color = patternColor, width = 2)
    lineFour = line.new(shPriceBarIndexOne, shPriceOne, shPriceBarIndex, shPrice, color = patternColor, style = line.style_dashed)
    lineFive = line.new(slPriceBarIndexOne, slPriceOne, troughBarIndex, trough, color = patternColor, style = line.style_dashed)
    labelOne = label.new(slPriceBarIndexOne, slPriceOne, color = color.rgb(54, 58, 69, 100), style = label.style_label_up,
     text = 'A', textcolor = labelColor)
    labelTwo = label.new(shPriceBarIndexOne, shPriceOne, color = color.rgb(54, 58, 69, 100), style = label.style_label_down,
     text = 'B', textcolor = labelColor)
    labelThree = 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)
    labelFour = label.new(shPriceBarIndex, shPrice, color = color.rgb(54, 58, 69, 100), style = label.style_label_down,
     text = 'D (' + str.tostring(math.round(shRangeRatio, 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 + (shPriceOne - slPriceOne) : na, showProjections ? bar_index : na, 
     showProjections ? peak + (shPriceOne - slPriceOne) : na, color = color.green, style = line.style_dashed)
    patternLowerProjection = line.new(showProjections ? troughBarIndex : na, showProjections ? trough - (shPriceOne - slPriceOne) : na, showProjections ? bar_index : na, 
     showProjections ? trough - (shPriceOne - 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, 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) 
    if array.size(myLabelArray) >= 250
        firstLabel = array.remove(myLabelArray, 0) 
        label.delete(firstLabel)

if bearishABCD  
    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 + (shPriceOne - slPriceOne) : na)
    line.set_xy2(currentPatternUpperProjection, showProjections ? bar_index : na, showProjections ? peak + (shPriceOne - slPriceOne) : na)
    line.set_xy1(currentPatternLowerProjection, showProjections ? troughBarIndex : na, showProjections ? trough - (shPriceOne - slPriceOne) : na)
    line.set_xy2(currentPatternLowerProjection, showProjections ? bar_index : na, showProjections ? trough - (shPriceOne - slPriceOne) : na)
    alert('Bearish ABCD')

    





