EMA Deviation Rebound
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zamansiz74

//@version=5
indicator("EMA Deviation Rebound", overlay=true)

periods = input.int(defval = 20, title="Periods")
length = input.int(defval = 10, title="Length")

var float avghighest = 0.0
var float avglowest = 0.0
var float xyz1 = 0.0
var float xyz2 = 0.0
var colorValue = color.black

// Calculation of the scatter
deviation = ta.stdev(close, periods)

// Highest and lowest spread
highestDeviation = ta.highest(deviation, periods)
lowestDeviation = ta.lowest(deviation, periods)

if bar_index % periods == 0
    xyz1 := close  + highestDeviation
    xyz2 := close  - lowestDeviation

ema1 = ta.ema((xyz1+xyz2)/2,length)

ema2 = ta.ema(ema1, length)
u_double_prime_approx = ema1 - ema2

colorValue := low > ema1 and open < close ? color.green : high < ema1 and open > close ? color.red : colorValue

// Plot 
plot(ema1, "ED", color=colorValue, linewidth=4)



