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


//////////// return line downtrends ////////////


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

onePartReturnLineDowntrend = slPrice < ta.valuewhen(slPrice, slPrice, 1)
twoPartReturnLineDowntrend = onePartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 1) < ta.valuewhen(slPrice, slPrice, 2)
threePartReturnLineDowntrend = twoPartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 2) < ta.valuewhen(slPrice, slPrice, 3)
fourPartReturnLineDowntrend = threePartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 3) < ta.valuewhen(slPrice, slPrice, 4)
fivePartReturnLineDowntrend = fourPartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 4) < ta.valuewhen(slPrice, slPrice, 5)
sixPartReturnLineDowntrend = fivePartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 5) < ta.valuewhen(slPrice, slPrice, 6)
sevenPartReturnLineDowntrend = sixPartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 6) < ta.valuewhen(slPrice, slPrice, 7)
eightPartReturnLineDowntrend = sevenPartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 7) < ta.valuewhen(slPrice, slPrice, 8)
ninePartReturnLineDowntrend = eightPartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 8) < ta.valuewhen(slPrice, slPrice, 9)
tenPartReturnLineDowntrend = ninePartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 9) < ta.valuewhen(slPrice, slPrice, 10)
elevenPartReturnLineDowntrend = tenPartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 10) < ta.valuewhen(slPrice, slPrice, 11)
twelvePartReturnLineDowntrend = elevenPartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 11) < ta.valuewhen(slPrice, slPrice, 12)
thirteenPartReturnLineDowntrend = twelvePartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 12) < ta.valuewhen(slPrice, slPrice, 13)
fourteenPartReturnLineDowntrend = thirteenPartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 13) < ta.valuewhen(slPrice, slPrice, 14)
fifteenPartReturnLineDowntrend = fourteenPartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 14) < ta.valuewhen(slPrice, slPrice, 15)
sixteenPartReturnLineDowntrend = fifteenPartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 15) < ta.valuewhen(slPrice, slPrice, 16)
seventeenPartReturnLineDowntrend = sixteenPartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 16) < ta.valuewhen(slPrice, slPrice, 17)
eighteenPartReturnLineDowntrend = seventeenPartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 17) < ta.valuewhen(slPrice, slPrice, 18)
nineteenPartReturnLineDowntrend = eighteenPartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 18) < ta.valuewhen(slPrice, slPrice, 19)
twentyPartReturnLineDowntrend = nineteenPartReturnLineDowntrend and ta.valuewhen(slPrice, slPrice, 19) < ta.valuewhen(slPrice, slPrice, 20)


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


plotshape(slPrice and onePartReturnLineDowntrend and not twoPartReturnLineDowntrend and low <= low[1] ? onePartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '1', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and twoPartReturnLineDowntrend and not threePartReturnLineDowntrend and low <= low[1] ? twoPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '2', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and threePartReturnLineDowntrend and not fourPartReturnLineDowntrend and low <= low[1] ? threePartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '3', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and fourPartReturnLineDowntrend and not fivePartReturnLineDowntrend and low <= low[1] ? fourPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '4', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and fivePartReturnLineDowntrend and not sixPartReturnLineDowntrend and low <= low[1] ? fivePartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '5', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and sixPartReturnLineDowntrend and not sevenPartReturnLineDowntrend and low <= low[1] ? sixPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '6', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and sevenPartReturnLineDowntrend and not eightPartReturnLineDowntrend and low <= low[1] ? sevenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '7', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and eightPartReturnLineDowntrend and not ninePartReturnLineDowntrend and low <= low[1] ? eightPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '8', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and ninePartReturnLineDowntrend and not tenPartReturnLineDowntrend and low <= low[1] ? ninePartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '9', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and tenPartReturnLineDowntrend and not elevenPartReturnLineDowntrend and low <= low[1] ? tenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '10', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and elevenPartReturnLineDowntrend and not twelvePartReturnLineDowntrend and low <= low[1] ? elevenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '11', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and twelvePartReturnLineDowntrend and not thirteenPartReturnLineDowntrend and low <= low[1] ? twelvePartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '12', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and thirteenPartReturnLineDowntrend and not fourteenPartReturnLineDowntrend and low <= low[1] ? thirteenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '13', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and fourteenPartReturnLineDowntrend and not fifteenPartReturnLineDowntrend and low <= low[1] ? fourteenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '14', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and fifteenPartReturnLineDowntrend and not sixteenPartReturnLineDowntrend and low <= low[1] ? fifteenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '15', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and sixteenPartReturnLineDowntrend and not seventeenPartReturnLineDowntrend and low <= low[1] ? sixteenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '16', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and seventeenPartReturnLineDowntrend and not eighteenPartReturnLineDowntrend and low <= low[1] ? seventeenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '17', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and eighteenPartReturnLineDowntrend and not nineteenPartReturnLineDowntrend and low <= low[1] ? eighteenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '18', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and nineteenPartReturnLineDowntrend and not twentyPartReturnLineDowntrend and low <= low[1] ? nineteenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '19', textcolor = color.red, location = location.belowbar)
plotshape(slPrice and twentyPartReturnLineDowntrend and low <= low[1] ? twentyPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '20', textcolor = color.red, location = location.belowbar)

plotshape(slPrice and onePartReturnLineDowntrend and not twoPartReturnLineDowntrend and low > low[1] ? onePartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '1', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and twoPartReturnLineDowntrend and not threePartReturnLineDowntrend and low > low[1] ? twoPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '2', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and threePartReturnLineDowntrend and not fourPartReturnLineDowntrend and low > low[1] ? threePartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '3', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and fourPartReturnLineDowntrend and not fivePartReturnLineDowntrend and low > low[1] ? fourPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '4', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and fivePartReturnLineDowntrend and not sixPartReturnLineDowntrend and low > low[1] ? fivePartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '5', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and sixPartReturnLineDowntrend and not sevenPartReturnLineDowntrend and low > low[1] ? sixPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '6', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and sevenPartReturnLineDowntrend and not eightPartReturnLineDowntrend and low > low[1] ? sevenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '7', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and eightPartReturnLineDowntrend and not ninePartReturnLineDowntrend and low > low[1] ? eightPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '8', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and ninePartReturnLineDowntrend and not tenPartReturnLineDowntrend and low > low[1] ? ninePartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '9', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and tenPartReturnLineDowntrend and not elevenPartReturnLineDowntrend and low > low[1] ? tenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '10', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and elevenPartReturnLineDowntrend and not twelvePartReturnLineDowntrend and low > low[1] ? elevenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '11', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and twelvePartReturnLineDowntrend and not thirteenPartReturnLineDowntrend and low > low[1] ? twelvePartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '12', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and thirteenPartReturnLineDowntrend and not fourteenPartReturnLineDowntrend and low > low[1] ? thirteenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '13', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and fourteenPartReturnLineDowntrend and not fifteenPartReturnLineDowntrend and low > low[1] ? fourteenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '14', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and fifteenPartReturnLineDowntrend and not sixteenPartReturnLineDowntrend and low > low[1] ? fifteenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '15', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and sixteenPartReturnLineDowntrend and not seventeenPartReturnLineDowntrend and low > low[1] ? sixteenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '16', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and seventeenPartReturnLineDowntrend and not eighteenPartReturnLineDowntrend and low > low[1] ? seventeenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '17', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and eighteenPartReturnLineDowntrend and not nineteenPartReturnLineDowntrend and low > low[1] ? eighteenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '18', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and nineteenPartReturnLineDowntrend and not twentyPartReturnLineDowntrend and low > low[1] ? nineteenPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '19', textcolor = color.red, location = location.belowbar, offset = -1)
plotshape(slPrice and twentyPartReturnLineDowntrend and low > low[1] ? twentyPartReturnLineDowntrend : na, 
 style = shape.triangledown, color = color.red, text = '20', textcolor = color.red, location = location.belowbar, offset = -1)




