Distance from EMA Levels
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mrbongiolo

//@version=5
indicator("Distance from EMA Levels", "EMA Distance Levels", overlay = true)

// Inputs

i_ema_timeframe = input.timeframe("5", "Timeframe", group = "EMA")
i_ema_source = input.source(close, "Source", group = "EMA")
i_ema_period = input.int(12, "Period", group = "EMA")

i_level_one_distance = input.int(5, "Distance %", group = "Level 1")
i_level_two_distance = input.int(2, "Distance %", group = "Level 2")
i_level_three_distance = input.int(-2, "Distance %", group = "Level 3")
i_level_four_distance = input.int(-5, "Distance %", group = "Level 4")

// Levels

_ema = ta.ema(i_ema_source, i_ema_period)
ema = request.security(syminfo.tickerid, i_ema_timeframe, _ema)

level_one = ema * (i_level_one_distance / 100 + 1)
level_two = ema * (i_level_two_distance / 100 + 1)
level_three = ema * (i_level_three_distance / 100 + 1)
level_four = ema * (i_level_four_distance / 100 + 1)

// Plotting

plot(ema, "EMA", #ffffff, linewidth = 2)
plot(level_one, "Level 1", #388e3c)
plot(level_two, "Level 2", #81c784)
plot(level_three, "Level 3", #f06292)
plot(level_four, "Level 4", #c2185b)