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

//@version=5
indicator(title="EMA Price Distance Tracker", overlay=true)

// Indenting Classs
indent_1 = " "
indent_2 = "  "
indent_3 = "   "
indent_4 = "    "
indent_5 = "     "
indent_6 = "      "
indent_7 = "       "
indent_8 = "        "
indent_9 = "         "
indent_10 = "          "

// Group Titles
group_one_title = "EMA Settings"
group_two_title = "EMA Distance Settings"

// Colors
blue = color.new(#00A5FF,0)
green = color.new(#2DBD85,0)
gray_80 =  color.new(#7F7F7F,80)
gray_60 =  color.new(#7F7F7F,60)
gray_40 =  color.new(#7F7F7F,40)
white = color.new(#ffffff,0)

// Strategy Settings
EMAtimeframe = input.timeframe(defval="", title="Timeframe", group=group_one_title)
EMAlength = input.int(defval=200, minval=1, title="Length", group=group_one_title)
EMAtype = input.string(defval="EMA", options = ["EMA", "SMA", "RMA", "WMA"], title="Type", group=group_one_title)
EMAsource = input.source(defval=close, title="Source", group=group_one_title)

low_level = input.int(defval=10, title="Low EMA Distance",  group=group_two_title)
mid_level = input.int(defval=20, title="Medium EMA Distance",  group=group_two_title)
high_level = input.int(defval=30, title="High EMA Distance",  group=group_two_title)

// Calulate EMA
EMA = switch EMAtype
    "EMA" => ta.ema(EMAsource, EMAlength)
    "SMA" => ta.sma(EMAsource, EMAlength)
    "RMA" => ta.rma(EMAsource, EMAlength)
    "WMA" => ta.wma(EMAsource, EMAlength)
    => na
EMA_ = EMAtimeframe == timeframe.period ? EMA : request.security(syminfo.ticker, EMAtimeframe, EMA[1], lookahead = barmerge.lookahead_on)

// EMA Distance Settings
hline(0, title="Zero Line", color=white, linestyle=hline.style_solid)
hline(low_level, title="Low Level Line",color=gray_80, linestyle=hline.style_solid)
hline(mid_level, title="Mid Level Line",color=gray_60, linestyle=hline.style_solid)
hline(high_level, title="High Level Line",color=gray_40, linestyle=hline.style_solid)

// Calculate Price's Distance from EMA
distance_from_EMA = ((close - EMA_)/close)*100
if distance_from_EMA < 0
    distance_from_EMA := distance_from_EMA * -1

plot(distance_from_EMA, title="Distance from EMA", color=blue, linewidth=2)