Choppy Market EMA Identification
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Fraggl

//@version=5
indicator("Choppy Market EMA Identification", overlay=true)

src = input(close, 'Source', group="Strategy Config")

ema_len = input.int(200, minval=1, title='EMA Length', group="Strategy Config")
max_cross = input.int(1, title='EMA Crosses to invalidate')
lookback = input.int(6, minval=1, title='EMA Touch Lookback', group="Strategy Config")
ema_col = input.color(color.white, group="Strategy Config")

fun_numbersOfEmaCross(numbers, ema) =>
    int crosses = 0
    cr = ta.cross(close,ema)
    for candleNo = 1 to numbers+1
        if cr[candleNo]
            crosses := crosses+1
    [crosses]

ema = ta.ema(src, ema_len)
[C] = fun_numbersOfEmaCross(lookback, ema)
maColor = if C < max_cross
    ema_col
else
    color.new(color.white, 100)

plot(ema, title='TR EMA', color=maColor, linewidth=2)
