Significance Condensed
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

// © LvNThL

//@version=5

// *EDIT* NOTE *EDIT*

// For the sake of clarity, after using Significance for some time, I have collapsed the overall asthetic to be more easily readible for the trader.
// I have removed the Auto-Trendline function and 1 ema, along with all zone fills and inter-high/low ranges. These modifications have made it easier for me to use this indicator.
// I would like to share this revision to Significance, to well, be straight-forwardly, more isolated, focused, and clear on the significance of levels as we approach them.
// I am working on a isolated auto-trendline code to accompany this script, one that works right.
// Feel free to message me if you have a script you want and I'll see what I can do!

// *EDIT* DESCRIPTION *EDIT*

// Indicator Name: "Significance Condensed"

// This is a "Multi-Indicator", which includes:

// *EDIT*
// Custom Candlesticks with Bearish/Bullish Engulfing Body Fills, otherwise hollow.
// 2 EMA's with user inputs.
// Continuous plots of high/low values with up to 3 overlapping timeframes.
// Two Tables: Bottom_Left displays TickerId and Timeframe Period, Bottom_Right displays Current Price (Red/Green for DownDay/UpDay), Daily Percentile Change (Red/Green for DownDay/UpDay), Volume: Daily Volume Total and Current Bar Volume, and RSI: Value. Respectively.
// *EDIT*

//CANDLESTICK DISCLAIMER

// If you would like to use the custom candlestick plots, (hollow, else engulfing), that come along with this indicator,
// be sure to disable the Candlestick Body, Wick, and Border under Chart Appearance; and then enable "Candlesticks Active" in the indicator settings.

indicator("Significance Condensed", overlay=true, precision=4, format=format.price, max_labels_count = 500, max_bars_back = 500, max_lines_count = 500)

[o,h,l,c,v] = request.security(syminfo.tickerid, "", [open, high, low, close, volume], lookahead=barmerge.lookahead_on)
[o1,o1_1,h1,h1_1,l1,l1_1,c1,c1_1,v1] = request.security(syminfo.tickerid, input.timeframe("D", title="Timeframe #1", group="High/Low Timeframes"), [open, open[1], high, high[1], low, low[1], close, close[1], volume], lookahead=barmerge.lookahead_on)
[o4,o4_1,h4,h4_1,l4,l4_1,c4,c4_1,v4] = request.security(syminfo.tickerid, input.timeframe("W", title="Timeframe #2", group="High/Low Timeframes"), [open, open[1], high, high[1], low, low[1], close, close[1], volume], lookahead=barmerge.lookahead_on)
[o2,o2_1,h2,h2_1,l2,l2_1,c2,c2_1,v2] = request.security(syminfo.tickerid, input.timeframe("M", title="Timeframe #3", group="High/Low Timeframes", tooltip = string("This Timeframe is coordinated with the Spread Math shown on the Table")), [open, open[1], high, high[1], low, low[1], close, close[1], volume], lookahead=barmerge.lookahead_on)

[o3,h3,l3,c3] = request.security(syminfo.tickerid, "D", [open, high, low, close], lookahead=barmerge.lookahead_on)
[c5] = request.security(syminfo.tickerid, input.timeframe("", title = "User Ema Timeframe:", group = "User Ema Input"), [close], lookahead=barmerge.lookahead_on, gaps = barmerge.gaps_off)
[_volume] = request.security(syminfo.tickerid, "", [volume], lookahead=barmerge.lookahead_on)


var int _emainput1 = na
var int _emainput2 = na

var int _emainput3 = na
var int _emainput4 = na

string _timeframe1input = na
string _timeframe2input = na
string _timeframe3input = na

if timeframe.ismonthly
    _emainput1 := 6
    _emainput2 := 12
else if timeframe.isweekly
    _emainput1 := 26
    _emainput2 := 52
else if timeframe.period == "10D"
    _emainput1 := 26
    _emainput2 := 52
else if timeframe.period == "5D"
    _emainput1 := 26
    _emainput2 := 52
else if timeframe.isdaily
    _emainput1 := 180
    _emainput2 := 360  
else if timeframe.period == "4H"
    _emainput1 := 180
    _emainput2 := 360  
else if timeframe.isintraday
    _emainput1 := 7
    _emainput2 := 30

_ema1 = ta.ema(close, int(_emainput1))
_ema2 = ta.ema(close, int(_emainput2))
_ema3 = ta.ema(c5, input.int(50, title = "User Ema #1", minval = 1, maxval = 4999, step = 1, group = "User Ema Input"))
_ema4 = ta.ema(c5, input.int(100, title = "User Ema #2", minval = 1, maxval = 4999, step = 1, group = "User Ema Input"))
_ema5 = ta.ema(c5, input.int(200, title = "User Ema #3", minval = 1, maxval = 4999, step = 1, group = "User Ema Input"))

_rsi = ta.rsi(hlc3,input.int(14, title="RSI Length", minval=1, step=1, group="RSI Settings"))

var float _highlowavg1 = na
var float _highlowavg2 = na
var float _highlowavg3 = na

var float _bottomquartile = na
var float _topquartile = na
var float _bottomoctile = na
var float _topoctile = na
var float _wbottomquartile = na
var float _wtopquartile = na
var float _wbottomoctile = na
var float _wtopoctile = na
var float _mbottomquartile = na
var float _mtopquartile = na
var float _mbottomoctile = na
var float _mtopoctile = na
var float _bottomsharp = na
var float _topsharp = na
var float _wbottomsharp = na
var float _wtopsharp = na
var float _mbottomsharp = na
var float _mtopsharp = na

var float _dailyvolume = na
var float _open = na
var float _high = na
var float _low = na
var float _close = na

var float y_open = na
var float y_high = na
var float y_low = na
var float y_close = na

var float w_open = na
var float w_high = na
var float w_low = na
var float w_close = na

var float m_open = na
var float m_high = na
var float m_low = na
var float m_close = na
var float d_high = na
var float d_low = na

var float _currentprice = na

float _dailychange = na
float _dailychangepos = ((close/o3)-1)
float _dailychangeneg = (-(o3/close)+1)

var label _lbl1 = na
var label _lbl2 = na
var label _lbl3 = na
var label _lbl4 = na
var label _lbl5 = na
var label _lbl6 = na
var label _lbl7 = na
var label _lbl8 = na
var label _lbl9 = na
var label _lblema1 = na
var label _lblema2 = na
var label _lblema3 = na
var label _lblema4 = na
var label _lblema5 = na

bool _candlesticks = input.bool(false, title="Candlesticks Active", group="Switches")
bool _highlowswitch1 = input.bool(true, title="High/Low #1", group="Switches")
bool _highlowswitch2 = input.bool(true, title="High/Low #2", group="Switches")
bool _highlowswitch3 = input.bool(true, title="High/Low #3", group="Switches")

bool _above = close >= o3
bool _below = close < o3

bool _ema1switch = input.bool(true, title="EMA #1", group="Switches")
bool _ema2switch = input.bool(true, title="EMA #2", group="Switches")
bool _ema3switch = input.bool(true, title="User EMA #1", group="Switches")
bool _ema4switch = input.bool(true, title="User EMA #2", group="Switches")
bool _ema5switch = input.bool(true, title="User EMA #3", group="Switches")

color _green = open<=close?color.new(#27ad75, 0):na
color _red = open>=close?color.new(#fa5252, 0):na
color _greenw = open<=close?color.new(#27ad75, 50):na
color _redw = open>=close?color.new(#fa5252, 50):na
color _bullishengulf = open<close and open[1]>close[1] and close>open[1]?color.new(#27ad75, 0):na
color _bearishengulf = open>close and open[1]<close[1] and close<open[1]?color.new(#fa5252, 0):na

var string s_dailyvolume = " Daily Volume:  "
var string s_currentprice = " Current Price:  "
var string _volumetext = na

var table _tbl1 = na
var table _tbl2 = na
var line _ln1 = na

_currentprice := c1
_dailyvolume := v1

_open := o1
_high := h1
_low := l1
_close := c1

y_open := o1_1
y_high := h1_1
y_low := l1_1
y_close := c1_1

w_open := o4
w_high := h4
w_low := l4
w_close := c4

m_open := o2
m_high := h2
m_low := l2
m_close := c2

d_high := h3
d_low := l3

if _dailyvolume >= 1000000000000
    _volumetext := str.tostring(str.format("{0,number, ###.## T }", _dailyvolume / 1000000000000) + " + " + str.tostring(str.format("{0,number, ###,###,###,###,###.## }", volume)))
else if _dailyvolume >= 1000000000
    _volumetext := str.tostring(str.format("{0,number, ###.## B }", _dailyvolume / 1000000000) + " + " + str.tostring(str.format("{0,number, ###,###,###,###,###.## }", volume)))
else if _dailyvolume >= 1000000
    _volumetext := str.tostring(str.format("{0,number, ###.## M }", _dailyvolume / 1000000) + " + " + str.tostring(str.format("{0,number, ###,###,###,###,###.## }", volume)))
else if _dailyvolume >= 1000
    _volumetext := str.tostring(str.format("{0,number, ###.## k }", _dailyvolume / 1000) + " + " + str.tostring(str.format("{0,number, ###,###,###,###,###.## }", volume)))
else
    _volumetext := str.tostring(str.format("{0,number, ###,###,###,###,### }", _dailyvolume) + " + " + str.tostring(str.format("{0,number, ###,###,###,###,###.## }", volume)))

dhi = plot(not timeframe.isdwm?d_high:na, title="Persistent Daily High", color=color.rgb(255,0,0,50))
dlw = plot(not timeframe.isdwm?d_low:na, title="Persistent Daily Low" , color=color.rgb(0,255,0,50))

hi = plot(_highlowswitch1?m_high:na, title="High Line #1", color=color.rgb(255,0,0,50))
lw = plot(_highlowswitch1?m_low:na, title="Low Line #1" , color=color.rgb(0,255,0,50))

whi = plot(_highlowswitch2?w_high:na, title="High Line #2", color=color.rgb(255,0,0,50))
wlw = plot(_highlowswitch2?w_low:na, title="Low Line #2", color=color.rgb(0,255,0,50))

mhi = plot(_highlowswitch3?_high:na, title="High Line #3", color=color.rgb(255,0,0,50))
mlw = plot(_highlowswitch3?_low:na, title="Low Line #3", color=color.rgb(0,255,0,50))

float _spread = (m_high - m_low)
float _upside = (((m_high - close)/100) / _spread) * 100
float _downside = (((close - m_low)/100) / _spread) * 100

if barstate.islast

    if _above
        _dailychange := _dailychangepos
    if _below
        _dailychange := _dailychangeneg

    //TABLES
    
    // _tbl1 := table.new(position.bottom_left, 50, 50, bgcolor=color.rgb(0,0,0,100), frame_color=na, frame_width=1, border_color=color.new(color.white, 25), border_width=1)
    _tbl2 := table.new(position.top_right, 50, 50, bgcolor=color.rgb(0,0,0,100), frame_color=na, frame_width=1, border_color=color.new(color.white, 25), border_width=1)

    table.cell(_tbl2, 0, 0, text= " " + string(syminfo.ticker) + " " + string(timeframe.period) + " ",
     text_color=color.new(color.white, 0), text_size=size.small)

    table.cell(_tbl2, 1, 0, text= " " + str.tostring(str.format("{0,number, $#,###,###.##########}", _currentprice)) + " ",
     text_color=close>=_open?color.rgb(0,255,0,0):close<=_open?color.rgb(255,0,0,0):color.white, text_size=size.small)

    table.cell(_tbl2, 2, 0, text=_dailychange?_below?" " + str.tostring(str.format("{0,number,###.### % ▼}", _dailychange)) + " "
     :_dailychange?_above? " " + str.tostring(str.format("{0,number,###.### % ▲}", _dailychange)) + " "
     :_dailychange == 0? " " + str.tostring(str.format("{0,number,###.### % -}", _dailychange)) + " ":na:na:na,
     text_color=_above?color.rgb(0,255,0,0):_below?color.rgb(255,0,0,0):color.white, text_size=size.small)

    table.cell(_tbl2, 3, 0, text= " " + string("RSI:")  + " " +  str.tostring(str.format("{0,number,##.##}", _rsi)) + " ",
     text_color=color.new(color.white, 0), text_size=size.small)

    table.cell(_tbl2, 1, 1, text= " " + str.tostring(str.format("{0,number, $###,###.########}", _spread)) + " " + string("Spread") + " ",
     text_color=color.new(color.white, 0), bgcolor = color.rgb(0,255,0,85), text_size=size.tiny)

    table.cell(_tbl2, 1, 2, text= " " + str.tostring(str.format("{0,number, #.#%}", _upside)) + " " + string("Upside") + " ",
     text_color=color.new(color.white, 0), text_size=size.tiny)

    table.cell(_tbl2, 1, 3, text= " " + str.tostring(str.format("{0,number, #.#%}", _downside)) + " " + string("Downside") + " ",
     text_color=color.new(color.white, 0), text_size=size.tiny)

    table.cell(_tbl2, 2, 1, text= " " + str.tostring(str.format("{0,number}", _emainput1)) + " " + string("EMA") + " ",
     text_color=color.new(color.white, 0), bgcolor = color.rgb(255,255,0,85), text_size=size.tiny)
     
    table.cell(_tbl2, 2, 2, text= " " + str.tostring(str.format("{0,number}", _emainput2)) + " " + string("EMA") + " ",
     text_color=color.new(color.white, 0), bgcolor = color.rgb(255,255,0,85), text_size=size.tiny)
     
    table.cell(_tbl2, 2, 3, text= " " + string("Volume:") + " " + _volumetext + " ",
     text_color=color.new(color.white, 0), text_size=size.tiny)
    

    
    //LABELS

    _lbl2 := label.new(_highlowswitch1?bar_index:na, h1, str.tostring(str.format("{0,number, $#,###,###.########## }", h1)),
     color=color.rgb(0,0,0,100),textcolor=color.white, style=label.style_label_left, size=size.small)
    label.delete(_lbl2[1])

    _lbl3 := label.new(_highlowswitch1?bar_index:na, l1, str.tostring(str.format("{0,number, $#,###,###.########## }", l1)),
     color=color.rgb(0,0,0,100),textcolor=color.white, style=label.style_label_left, size=size.small)
    label.delete(_lbl3[1])

    _lbl4 := label.new(_highlowswitch2?bar_index:na, h4, str.tostring(str.format("{0,number, $#,###,###.########## }", h4)),
     color=color.rgb(0,0,0,100),textcolor=color.white, style=label.style_label_left, size=size.small)
    label.delete(_lbl4[1])

    _lbl5 := label.new(_highlowswitch2?bar_index:na, l4, str.tostring(str.format("{0,number, $#,###,###.########## }", l4)),
     color=color.rgb(0,0,0,100),textcolor=color.white, style=label.style_label_left, size=size.small)
    label.delete(_lbl5[1])

    _lbl6 := label.new(_highlowswitch3?bar_index:na, h2, str.tostring(str.format("{0,number, $#,###,###.########## }", h2)),
     color=color.rgb(0,0,0,100),textcolor=color.white, style=label.style_label_left, size=size.small)
    label.delete(_lbl6[1])

    _lbl7 := label.new(_highlowswitch3?bar_index:na, l2, str.tostring(str.format("{0,number, $#,###,###.########## }", l2)),
     color=color.rgb(0,0,0,100),textcolor=color.white, style=label.style_label_left, size=size.small)
    label.delete(_lbl7[1])

    _lblema1 := label.new(_ema3switch?bar_index:na, _ema3, close<0.0000?str.tostring(str.format("{0,number, $#,###,###.############## }", _ema3)):str.tostring(str.format("{0,number, $#,###,###.#### }", _ema3)),
     color=color.rgb(0,0,0,100),textcolor=color.white, style=label.style_label_left, size=size.small)
    label.delete(_lblema1[1])
    
    _lblema2 := label.new(_ema4switch?bar_index:na, _ema4, close<0.0000?str.tostring(str.format("{0,number, $#,###,###.############## }", _ema4)):str.tostring(str.format("{0,number, $#,###,###.#### }", _ema4)),
     color=color.rgb(0,0,0,100),textcolor=color.white, style=label.style_label_left, size=size.small)
    label.delete(_lblema2[1])

    _lblema3 := label.new(_ema5switch?bar_index:na, _ema5, close<0.0000?str.tostring(str.format("{0,number, $#,###,###.############## }", _ema5)):str.tostring(str.format("{0,number, $#,###,###.#### }", _ema5)),
     color=color.rgb(0,0,0,100),textcolor=color.white, style=label.style_label_left, size=size.small)
    label.delete(_lblema3[1])

    _lblema4 := label.new(_ema1switch?bar_index:na, _ema1, close<0.0000?str.tostring(str.format("{0,number, $#,###,###.############## }", _ema1)):str.tostring(str.format("{0,number, $#,###,###.#### }", _ema1)),
     color=color.rgb(0,0,0,100),textcolor=color.white, style=label.style_label_left, size=size.small)
    label.delete(_lblema4[1])

    _lblema5 := label.new(_ema2switch?bar_index:na, _ema2, close<0.0000?str.tostring(str.format("{0,number, $#,###,###.############## }", _ema2)):str.tostring(str.format("{0,number, $#,###,###.#### }", _ema2)),
     color=color.rgb(0,0,0,100),textcolor=color.white, style=label.style_label_left, size=size.small)
    label.delete(_lblema5[1])

    _lbl8 := label.new(not timeframe.isdwm?bar_index:na, d_high, str.tostring(str.format("{0,number, $#,###,###.########## }", d_high)),
     color=color.rgb(0,0,0,100),textcolor=color.white, style=label.style_label_left, size=size.small)
    label.delete(_lbl8[1])
    _lbl9 := label.new(not timeframe.isdwm?bar_index:na, d_low, str.tostring(str.format("{0,number, $#,###,###.########## }", d_low)),
     color=color.rgb(0,0,0,100),textcolor=color.white, style=label.style_label_left, size=size.small)
    label.delete(_lbl9[1])

plot(_ema1switch?_ema1:na, title="EMA #1", color=#00fff9, linewidth=1)
plot(_ema2switch?_ema2:na, title="EMA #2", color=#00fff9, linewidth=1)

plot(_ema3switch?_ema3:na, title="User EMA #1", color=#ff0000, linewidth=1)
plot(_ema4switch?_ema4:na, title="User EMA #2", color=#ff0000, linewidth=1)
plot(_ema5switch?_ema5:na, title="User EMA #3", color=#ff0000, linewidth=1)

_h1 = plot(high, display = display.none)
_l1 = plot(low, display = display.none)
_cl1 = plot(close, display = display.none)

fill(hi,_h1, color = color.rgb(255,0,127.5,80))
fill(lw,_l1, color = color.rgb(0,127.5,255,80))
fill(_h1, _cl1, color = _highlowswitch3?color.rgb(255,0,127.5,80):na)
fill(_l1,_cl1, color = _highlowswitch3?color.rgb(0,127.5,255,80):na)

plotcandle(_candlesticks?open:na, high, low, close, title="Green Candles", color=_bullishengulf, wickcolor=_greenw, bordercolor=_green)
plotcandle(_candlesticks?open:na, high, low, close, title="Red Candles", color=_bearishengulf, wickcolor=_redw, bordercolor=_red)
