SMAs 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('SMAs Distances', overlay=true)

// Inputs and variables
src  = input     (close, title = 'Source MA', tooltip='Source', group= "SMA")

sma1 = input.int (    8, title = 'SMA 1', minval=1, tooltip='SMA #1', inline="SMA1")
sma2 = input.int (   20, title = 'SMA 2', minval=1, tooltip='SMA #2', inline="SMA2")
sma3 = input.int (   50, title = 'SMA 3', minval=1, tooltip='SMA #3', inline="SMA3")
sma4 = input.int (  100, title = 'SMA 4', minval=1, tooltip='SMA #4', inline="SMA4")
sma5 = input.int (  200, title = 'SMA 4', minval=1, tooltip='SMA #5', inline="SMA5")

alertSma1 = input.float (1.00, title = "* Alert on +/- (%)",  inline="SMA1")
alertSma2 = input.float (1.00, title = "* Alert on +/- (%)",  inline="SMA2")
alertSma3 = input.float (1.00, title = "* Alert on +/- (%)",  inline="SMA3")
alertSma4 = input.float (1.00, title = "* Alert on +/- (%)",  inline="SMA4")
alertSma5 = input.float (1.00, title = "* Alert on +/- (%)",  inline="SMA5")

HighLevel = input.float (80.00, title = "RSI High Level", tooltip = "RSI High Level", group = "RSI")
LowLevel =  input.float (20.00, title = "RSI Low Level" , tooltip = "RSI Low Level" , group = "RSI")
srcRSI   =  input       (close, title = 'Source RSI'    , tooltip = "RSI Source"    , group = "RSI")
RSILengh =  input.int   (   14, title = 'RSI Lenght'    , tooltip = "RSI Lengh"     , group = "RSI", minval=1)

tablePosition = input.string(title = 'Table Position', defval="Top right", options = ["Top right", "Top left", "Bottom right", "Bottom left"], tooltip='Position of the table')

xRSI = ta.rsi(srcRSI, RSILengh)

// Get EMAs
xSMA1 = ta.sma (src, sma1)
xSMA2 = ta.sma (src, sma2)
xSMA3 = ta.sma (src, sma3)
xSMA4 = ta.sma (src, sma4)
xSMA5 = ta.sma (src, sma5)

// Ploting EMAs
plot(xSMA1, color=color.new(color.yellow, 0), linewidth=2, title='SMA 1')
plot(xSMA2, color=color.new(color.blue  , 0), linewidth=2, title='SMA 2')
plot(xSMA3, color=color.new(color.purple, 0), linewidth=2, title='SMA 3')
plot(xSMA4, color=color.new(color.maroon, 0), linewidth=2, title='SMA 4')
plot(xSMA5, color=color.new(color.red   , 0), linewidth=2, title='SMA 5')

// 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 SMA ' + str.tostring (sma1) ,  str.tostring(close - xSMA1, "0.00") + '\n ' + str.tostring(1-xSMA1/close, "0.00%") , bgcolor, color.yellow)
    fill_Cell(emaTable, 1, 0, 'Distance SMA ' + str.tostring (sma2) ,  str.tostring(close - xSMA2, "0.00") + '\n ' + str.tostring(1-xSMA2/close, "0.00%") , bgcolor, color.blue)
    fill_Cell(emaTable, 0, 1, 'Distance SMA ' + str.tostring (sma3) ,  str.tostring(close - xSMA3, "0.00") + '\n ' + str.tostring(1-xSMA3/close, "0.00%") , bgcolor, color.purple)
    fill_Cell(emaTable, 1, 1, 'Distance SMA ' + str.tostring (sma4) ,  str.tostring(close - xSMA4, "0.00") + '\n ' + str.tostring(1-xSMA4/close, "0.00%") , bgcolor, color.maroon)
    fill_Cell(emaTable, 0, 2, 'Distance SMA ' + str.tostring (sma5) ,  str.tostring(close - xSMA5, "0.00") + '\n ' + str.tostring(1-xSMA5/close, "0.00%") , bgcolor, color.red)
    fill_Cell(emaTable, 1, 2, 'RSI\n' + str.tostring (xRSI, "0.00") , xRSI > HighLevel ? "Overbought" : xRSI < LowLevel ? "Oversold" : "Neutral"   , bgcolor, color.white)

alertcondition (math.abs((xSMA1/close*100)-100) >= alertSma1, title = "Alerta on SMA 1", message="Alerta on SMA 1") 
alertcondition (math.abs((xSMA2/close*100)-100) >= alertSma2, title = "Alerta on SMA 2", message="Alerta on SMA 2") 
alertcondition (math.abs((xSMA3/close*100)-100) >= alertSma3, title = "Alerta on SMA 3", message="Alerta on SMA 3") 
alertcondition (math.abs((xSMA4/close*100)-100) >= alertSma4, title = "Alerta on SMA 4", message="Alerta on SMA 4") 
alertcondition (math.abs((xSMA5/close*100)-100) >= alertSma5, title = "Alerta on SMA 5", message="Alerta on SMA 5") 

    
