EMAs Distances
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Create by seba34e

//@version=5
indicator('EMAs Distances',  overlay=true)

// Inputs and variables
ema1 = input.int (    8, title = 'EMA 1', minval=1, tooltip='EMA #1')
ema2 = input.int (   20, title = 'EMA 2', minval=1, tooltip='EMA #2')
ema3 = input.int (   50, title = 'EMA 3', minval=1, tooltip='EMA #3')
ema4 = input.int (  200, title = 'EMA 4', minval=1, tooltip='EMA #4')
src  = input     (close, title = 'Source', tooltip='Source')
tablePosition = input.string(title = 'Table Position', defval="Top right", options = ["Top right", "Top left", "Bottom right", "Bottom left"], tooltip='Position of the table')

// Get EMAs
xEMA1 = ta.ema (src, ema1)
xEMA2 = ta.ema (src, ema2)
xEMA3 = ta.ema (src, ema3)
xEMA4 = ta.ema (src, ema4)

// Ploting EMAs
plot(xEMA1, color=color.new(color.blue, 0), linewidth=2, title='EMA 1')
plot(xEMA2, color=color.new(color.orange, 0), linewidth=2, title='EMA 2')
plot(xEMA3, color=color.new(color.gray, 0), linewidth=2, title='EMA 3')
plot(xEMA4, color=color.new(color.purple, 0), linewidth=2, title='EMA 4')

// Prepare table
var bgcolor = color.new(color.black, 50)
var table emaTable = table.new(tablePosition == "Top right" ? position.top_right : tablePosition == "Top left" ? position.top_left : tablePosition == "Bottom right" ? position.bottom_right : position.bottom_left, 5, 6, border_width=4)
fill_Cell(_table, _column, _row, _title, _value, _bgcolor, _txtcolor) =>
    _cellText = _title + '\n' + _value
    table.cell(_table, _column, _row, _cellText, bgcolor=_bgcolor, text_color=_txtcolor, text_halign=text.align_right)

// Prepare cells
if barstate.islast
    fill_Cell(emaTable, 0, 0, 'Distance EMA ' + str.tostring (ema1) , str.tostring(close - xEMA1, "0.00") + '\n ' + str.tostring(1-xEMA1/close, "0.00%") , bgcolor, color.blue)
    fill_Cell(emaTable, 1, 0, 'Distance EMA ' + str.tostring (ema2) , str.tostring(close - xEMA2, "0.00") + '\n ' + str.tostring(1-xEMA2/close, "0.00%") , bgcolor, color.orange)
    fill_Cell(emaTable, 0, 1, 'Distance EMA ' + str.tostring (ema3) , str.tostring(close - xEMA3, "0.00") + '\n ' + str.tostring(1-xEMA3/close, "0.00%") , bgcolor, color.gray)
    fill_Cell(emaTable, 1, 1, 'Distance EMA ' + str.tostring (ema4) , str.tostring(close - xEMA4, "0.00") + '\n ' + str.tostring(1-xEMA4/close, "0.00%") , bgcolor, color.purple)
    
