ETS Price Deviation Reversal Areas
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © EasyTradingSignals
// This indicator tracks the degree to which price moves away from an average and triggers potential direction changes based on standard deviation levels
// It also tries to avoid giving signals when prices are in a very small range
// Change this code as you want to, but please let the community know and send me a message if you found something to share! Thanks! 
//@version=5
indicator(title='ETS Price Deviation Reversal Areas', shorttitle='Price Deviation Reversals', overlay=false)
source = close
windowsize = input(title='Window Size', defval=20)
// Reversals

delen = input.int(6, minval=1, title="EMA Length") //5
delend = input.int(7, minval=1, title="EMA of Primary Length") //6
amp = 1.5  //input(type=input.float, defval=1.5, minval=-9.00, maxval=9, step=0.1, title="Amplify")
dedo = (100 - ta.ema(close, delen) / close * 100) * amp // This is a percentage of the position of the current price in relation to the moving average
ded = ta.ema(dedo, delend)

delength = input.int(13, minval=1) //14

demult = input.float(0.5, minval=0.001, maxval=50, step=0.05, title="Level 1")
debasis = ta.sma(ded, delength)
dedev = demult * ta.stdev(ded, delength) // This is a standard deviation from the moving average with a multiplier
deupper = debasis + dedev
delower = debasis - dedev

demult2 = input.float(1.2, minval=0.001, maxval=50, step=0.05, title="Level 2") //1.5
debasis2 = ta.sma(ded, delength)
dedev2 = demult2 * ta.stdev(ded, delength) // This is a standard deviation from the moving average with a multiplier
deupper2 = debasis2 + dedev2
delower2 = debasis2 - dedev2

demultb = input.float(1.6, minval=0.001, maxval=50, step=0.05, title="Level 3")
debasisb = ta.sma(ded, delength)
dedevb = demultb * ta.stdev(ded, delength) // This is a standard deviation from the moving average with a multiplier
deupperb = debasisb + dedevb
delowerb = debasisb - dedevb


plot(deupper, color=color.blue)
plot(deupperb, color=color.navy)
plot(debasis, color=color.black)
plot(ded, color=color.green, linewidth=2)
plot(delower, color=color.orange)
plot(delowerb, color=color.red)

plot(deupper2, color=color.aqua)
plot(debasis2, color=color.black)
plot(delower2, color=color.purple)

//////////////////////////////////////////

per = input.int(title="Period", defval=13)
demax = high > high[1] ? high - high[1] : 0
demin = low < low[1] ? low[1] - low : 0
demax_av = ta.sma(demax, per)
demin_av = ta.sma(demin, per)
dmark = ta.ema(demax_av / (demax_av + demin_av) * 100, 3)

//////////////////////////////////////////

sp = input.int(8, minval=1, title='Min Span')
span = (math.abs(delowerb) + deupperb) * 100 > sp ? true : false // span tracks the size of the price changes


co = (ded <= delower and ded[1] >= delower[1]) and span ? ded : (ded <= delowerb and ded[1] >= delowerb[1]) ? ded : na
cu = (ded >= deupper and ded[1] <= deupper[1]) and span ? ded : (ded >= deupperb and ded[1] <= deupperb[1]) ? ded : na
plot(cu, title="Cross", color=color.green, linewidth=5, style=plot.style_circles)
plot(co, title="Cross", color=color.orange, linewidth=5, style=plot.style_circles)


cu2 = (ded >= delower2 and ded[1] <= delower2[1]) and span ? ded : na
co2 = (ded <= deupper2 and ded[1] >= deupper2[1]) and span ? ded : na
plot(cu2, title="Cross", color=color.blue, linewidth=3, style=plot.style_circles)
plot(co2, title="Cross", color=color.red, linewidth=3, style=plot.style_circles)

//// Alerts
alertcondition(cu, title='PDR Outer Up', message='Potential reversal upwards')
alertcondition(co, title='PDR Outer Down', message='Potential reversal downwards')

alertcondition(cu2, title='PDR Early Up', message='Potential early reversal upwards')
alertcondition(co2, title='PDR Early Down', message='Potential early reversal downwards')

