Downtrends [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('Downtrends [theEccentricTrader]', overlay = true)


//////////// downtrends ////////////


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

onePartDowntrend = shPrice < ta.valuewhen(shPrice, shPrice, 1)
twoPartDowntrend = onePartDowntrend and ta.valuewhen(shPrice, shPrice, 1) < ta.valuewhen(shPrice, shPrice, 2)
threePartDowntrend = twoPartDowntrend and ta.valuewhen(shPrice, shPrice, 2) < ta.valuewhen(shPrice, shPrice, 3)
fourPartDowntrend = threePartDowntrend and ta.valuewhen(shPrice, shPrice, 3) < ta.valuewhen(shPrice, shPrice, 4)
fivePartDowntrend = fourPartDowntrend and ta.valuewhen(shPrice, shPrice, 4) < ta.valuewhen(shPrice, shPrice, 5)
sixPartDowntrend = fivePartDowntrend and ta.valuewhen(shPrice, shPrice, 5) < ta.valuewhen(shPrice, shPrice, 6)
sevenPartDowntrend = sixPartDowntrend and ta.valuewhen(shPrice, shPrice, 6) < ta.valuewhen(shPrice, shPrice, 7)
eightPartDowntrend = sevenPartDowntrend and ta.valuewhen(shPrice, shPrice, 7) < ta.valuewhen(shPrice, shPrice, 8)
ninePartDowntrend = eightPartDowntrend and ta.valuewhen(shPrice, shPrice, 8) < ta.valuewhen(shPrice, shPrice, 9)
tenPartDowntrend = ninePartDowntrend and ta.valuewhen(shPrice, shPrice, 9) < ta.valuewhen(shPrice, shPrice, 10)
elevenPartDowntrend = tenPartDowntrend and ta.valuewhen(shPrice, shPrice, 10) < ta.valuewhen(shPrice, shPrice, 11)
twelvePartDowntrend = elevenPartDowntrend and ta.valuewhen(shPrice, shPrice, 11) < ta.valuewhen(shPrice, shPrice, 12)
thirteenPartDowntrend = twelvePartDowntrend and ta.valuewhen(shPrice, shPrice, 12) < ta.valuewhen(shPrice, shPrice, 13)
fourteenPartDowntrend = thirteenPartDowntrend and ta.valuewhen(shPrice, shPrice, 13) < ta.valuewhen(shPrice, shPrice, 14)
fifteenPartDowntrend = fourteenPartDowntrend and ta.valuewhen(shPrice, shPrice, 14) < ta.valuewhen(shPrice, shPrice, 15)
sixteenPartDowntrend = fifteenPartDowntrend and ta.valuewhen(shPrice, shPrice, 15) < ta.valuewhen(shPrice, shPrice, 16)
seventeenPartDowntrend = sixteenPartDowntrend and ta.valuewhen(shPrice, shPrice, 16) < ta.valuewhen(shPrice, shPrice, 17)
eighteenPartDowntrend = seventeenPartDowntrend and ta.valuewhen(shPrice, shPrice, 17) < ta.valuewhen(shPrice, shPrice, 18)
nineteenPartDowntrend = eighteenPartDowntrend and ta.valuewhen(shPrice, shPrice, 18) < ta.valuewhen(shPrice, shPrice, 19)
twentyPartDowntrend = nineteenPartDowntrend and ta.valuewhen(shPrice, shPrice, 19) < ta.valuewhen(shPrice, shPrice, 20)


//////////// plots ////////////


plotshape(shPrice and onePartDowntrend and not twoPartDowntrend and high >= high[1] ? onePartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '1', textcolor = color.red)
plotshape(shPrice and twoPartDowntrend and not threePartDowntrend and high >= high[1] ? twoPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '2', textcolor = color.red)
plotshape(shPrice and threePartDowntrend and not fourPartDowntrend and high >= high[1] ? threePartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '3', textcolor = color.red)
plotshape(shPrice and fourPartDowntrend and not fivePartDowntrend and high >= high[1] ? fourPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '4', textcolor = color.red)
plotshape(shPrice and fivePartDowntrend and not sixPartDowntrend and high >= high[1] ? fivePartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '5', textcolor = color.red)
plotshape(shPrice and sixPartDowntrend and not sevenPartDowntrend and high >= high[1] ? sixPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '6', textcolor = color.red)
plotshape(shPrice and sevenPartDowntrend and not eightPartDowntrend and high >= high[1] ? sevenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '7', textcolor = color.red)
plotshape(shPrice and eightPartDowntrend and not ninePartDowntrend and high >= high[1] ? eightPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '8', textcolor = color.red)
plotshape(shPrice and ninePartDowntrend and not tenPartDowntrend and high >= high[1] ? ninePartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '9', textcolor = color.red)
plotshape(shPrice and tenPartDowntrend and not elevenPartDowntrend and high >= high[1] ? tenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '10', textcolor = color.red)
plotshape(shPrice and elevenPartDowntrend and not twelvePartDowntrend and high >= high[1] ? elevenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '11', textcolor = color.red)
plotshape(shPrice and twelvePartDowntrend and not thirteenPartDowntrend and high >= high[1] ? twelvePartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '12', textcolor = color.red)
plotshape(shPrice and thirteenPartDowntrend and not fourteenPartDowntrend and high >= high[1] ? thirteenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '13', textcolor = color.red)
plotshape(shPrice and fourteenPartDowntrend and not fifteenPartDowntrend and high >= high[1] ? fourteenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '14', textcolor = color.red)
plotshape(shPrice and fifteenPartDowntrend and not sixteenPartDowntrend and high >= high[1] ? fifteenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '15', textcolor = color.red)
plotshape(shPrice and sixteenPartDowntrend and not seventeenPartDowntrend and high >= high[1] ? sixteenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '16', textcolor = color.red)
plotshape(shPrice and seventeenPartDowntrend and not eighteenPartDowntrend and high >= high[1] ? seventeenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '17', textcolor = color.red)
plotshape(shPrice and eighteenPartDowntrend and not nineteenPartDowntrend and high >= high[1] ? eighteenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '18', textcolor = color.red)
plotshape(shPrice and nineteenPartDowntrend and not twentyPartDowntrend and high >= high[1] ? nineteenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '19', textcolor = color.red)
plotshape(shPrice and twentyPartDowntrend and high >= high[1] ? twentyPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '20', textcolor = color.red)

plotshape(shPrice and onePartDowntrend and not twoPartDowntrend and high < high[1] ? onePartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '1', textcolor = color.red, offset = -1)
plotshape(shPrice and twoPartDowntrend and not threePartDowntrend and high < high[1] ? twoPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '2', textcolor = color.red, offset = -1)
plotshape(shPrice and threePartDowntrend and not fourPartDowntrend and high < high[1] ? threePartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '3', textcolor = color.red, offset = -1)
plotshape(shPrice and fourPartDowntrend and not fivePartDowntrend and high < high[1] ? fourPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '4', textcolor = color.red, offset = -1)
plotshape(shPrice and fivePartDowntrend and not sixPartDowntrend and high < high[1] ? fivePartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '5', textcolor = color.red, offset = -1)
plotshape(shPrice and sixPartDowntrend and not sevenPartDowntrend and high < high[1] ? sixPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '6', textcolor = color.red, offset = -1)
plotshape(shPrice and sevenPartDowntrend and not eightPartDowntrend and high < high[1] ? sevenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '7', textcolor = color.red, offset = -1)
plotshape(shPrice and eightPartDowntrend and not ninePartDowntrend and high < high[1] ? eightPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '8', textcolor = color.red, offset = -1)
plotshape(shPrice and ninePartDowntrend and not tenPartDowntrend and high < high[1] ? ninePartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '9', textcolor = color.red, offset = -1)
plotshape(shPrice and tenPartDowntrend and not elevenPartDowntrend and high < high[1] ? tenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '10', textcolor = color.red, offset = -1)
plotshape(shPrice and elevenPartDowntrend and not twelvePartDowntrend and high < high[1] ? elevenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '11', textcolor = color.red, offset = -1)
plotshape(shPrice and twelvePartDowntrend and not thirteenPartDowntrend and high < high[1] ? twelvePartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '12', textcolor = color.red, offset = -1)
plotshape(shPrice and thirteenPartDowntrend and not fourteenPartDowntrend and high < high[1] ? thirteenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '13', textcolor = color.red, offset = -1)
plotshape(shPrice and fourteenPartDowntrend and not fifteenPartDowntrend and high < high[1] ? fourteenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '14', textcolor = color.red, offset = -1)
plotshape(shPrice and fifteenPartDowntrend and not sixteenPartDowntrend and high < high[1] ? fifteenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '15', textcolor = color.red, offset = -1)
plotshape(shPrice and sixteenPartDowntrend and not seventeenPartDowntrend and high < high[1] ? sixteenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '16', textcolor = color.red, offset = -1)
plotshape(shPrice and seventeenPartDowntrend and not eighteenPartDowntrend and high < high[1] ? seventeenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '17', textcolor = color.red, offset = -1)
plotshape(shPrice and eighteenPartDowntrend and not nineteenPartDowntrend and high < high[1] ? eighteenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '18', textcolor = color.red, offset = -1)
plotshape(shPrice and nineteenPartDowntrend and not twentyPartDowntrend and high < high[1] ? nineteenPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '19', textcolor = color.red, offset = -1)
plotshape(shPrice and twentyPartDowntrend and high < high[1] ? twentyPartDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '20', textcolor = color.red, offset = -1)


