Koncorde Plus
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © OskarGallard

//@version=5
indicator("Koncorde Plus", "Koncorde [+]")
f_htf_ohlc(_htf) =>
    var htf_o = 0.
    var htf_h = 0.
    var htf_l = 0.
    htf_c = close
    var htf_ox = 0.
    var htf_hx = 0.
    var htf_lx = 0.
    var htf_cx = 0.

    if ta.change(time(_htf))
        htf_ox := htf_o
        htf_o := open
        htf_hx := htf_h
        htf_h := high
        htf_lx := htf_l
        htf_l := low
        htf_cx := htf_c[1]
        htf_cx
    else
        htf_h := math.max(high, htf_h)
        htf_l := math.min(low, htf_l)
        htf_l

    [htf_ox, htf_hx, htf_lx, htf_cx, htf_o, htf_h, htf_l, htf_c]

show_K = input.bool(true,    "═══════ Show Koncorde ═══════")
g_pattern = "Pattern 🐳 🆚 🐟"
show_espejo    = input.bool(true,  "Mirror Pattern",   inline="p1", group=g_pattern)
show_oso       = input.bool(false, "Bear Hug Pattern", inline="p1", group=g_pattern)
show_cruces    = input.bool(false, "Crossing Pattern", inline="p2", group=g_pattern)
show_primavera = input.bool(false, "Spring Pattern",   inline="p2", group=g_pattern)
show_cero      = input.bool(true,  "Zero Pattern",     inline="p3", group=g_pattern)
show_arpon     = input.bool(true,  "Harpoon Pattern",  inline="p3", group=g_pattern)
tip_chg    = 'Performance (Change), displays values from the beginning of the month, week, year etc (So = Start of)\n\nDistance from/to MA - Multi TimeFrame Price Distance from/to MA'
i_chg      = input.bool(true, "Statistic Panel of Performance (Change)", inline='oth', group=g_pattern, tooltip=tip_chg)
i_textSize = input.string('Small', 'Text Size', options=['Tiny', 'Small', 'Normal'], inline='oth', group=g_pattern)
textSize   = i_textSize == 'Small' ? size.small : i_textSize == 'Normal' ? size.normal : size.tiny

tprice      = input.source(ohlc4, "RSI, BB & Stoch Source", group="Trend")
srcMfi      = input.source(hlc3,  "MFI Source", group="Trend")
len_mfi_rsi = input.int(14,       "MFI & RSI Length", 1, group="Trend")
length_bb   = input.int(25,       "BB Length", 1, inline="bb", group="Trend")
mult_bb     = input.float(2.0,    "BB Multiplier", 1, inline="bb", group="Trend")
length_ma1  = input.int(15,       "MA Length", 1, inline="ma1", group="Trend")
type_ma1    = input.string("EMA", "Type", inline="ma1", group="Trend", options=["SMA", "EMA", "Wilder", "WMA", "VWMA", "ALMA", "LSMA", "HMA", "VAMA", "JMA", "RSS_WMA"])
// Volume Type
voltype = input.string("Default", "Volume Type (if VWMA)", options=["Default", "Tick"], group = "Trend")
// VAMA factor
factor = input.float(0.67, "Factor (if VAMA)", minval = 0.01, step = 0.1, group = "Trend")
// ALMA Offset and Sigma
offs  = input.float(0.85, "Offset (if ALMA)", step=0.01, minval=0, group = "Trend")
sigma = input.int(6,      "Sigma (if ALMA)", minval=0, group = "Trend")
// JMA 
phaseJ = input.int(50, "Phase (if JMA)", group = "Trend")
powerJ = input.int(2,  "Power (if JMA)", group = "Trend")
// LSMA Offset
loff = input.int(0, "Offset (if LSMA)", minval=0, group = "Trend")

m_pvi_nvi   = input.int(15, "PVI & NVI Length", 1, group="Volume")
longitudPVI = input.int(90, "Min-Max Period [PVI]", 2, group="Volume")
longitudNVI = input.int(90, "Min-Max Period [NVI]", 2, group="Volume")

show_DVDI = input.bool(false, "═════ Show Dual Volume Divergence Index ═════")
// Mode Selection
mode = input.string("Oscillator Mode", "Mode Selection", inline="extra2", options=["Cloud Mode", "Oscillator Mode", "Bar Counter Mode"])
// Tick Volume Toggle
usetick = input.bool(false, "Use Tick Volume", inline="extra2")
show_bar_dvdi = input.bool(false, "Show Bars Color", inline="extra2")
src = input.source(close, "Source") // Source 
// Sampling Period
per = input.int(55, "Sampling Period", 1)
smper = input.int(1, "Smoothing Period", 1)

group_macd = "Moving Average Convergence Divergence"
show_macd  = input.bool(true,    "═════ Show MACD ═════", group = group_macd)
macd_src   = input.source(close, "Source", inline="macd1", group = group_macd)
show_macd2 = input.bool(false,   "Show Bars Color", inline="macd1", group = group_macd)
macd_fast  = input.int(12,       "Length: Fast", 1, inline = "macd2", group = group_macd)
macd_slow  = input.int(26,       "Slow",         2, inline = "macd2", group = group_macd)
macd_sig   = input.int(9,        "Signal",       1, inline = "macd2", group = group_macd)

show_delta = input.bool(false, "═══════ Show Volume Delta Arrows ═══════", group="Volume Delta Arrows")
v_type = input.string("Cumulative Volume Delta", "Volume Type", group="Volume Delta Arrows", options = ["Cumulative Volume Delta", "Delta+AccDist", "Delta+OBV", "Delta+PVT", "OBV+PVT+AccDist", "OBV+PVT"])
fast_len   = input.int(5,           "Fast MA", 1, inline="delta1", group="Volume Delta Arrows")
type_fast  = input.string("Wilder", "Type", inline="delta1", group="Volume Delta Arrows", options=["SMA", "EMA", "Wilder", "WMA", "ALMA", "LSMA", "HMA", "JMA", "RSS_WMA"])
slow_len   = input.int(21,          "Slow MA", 1, inline="delta2", group="Volume Delta Arrows")
type_slow  = input.string("EMA",    "Type", inline="delta2", group="Volume Delta Arrows", options=["SMA", "EMA", "Wilder", "WMA", "ALMA", "LSMA", "HMA", "JMA", "RSS_WMA"])

group_tsv = "Enhanced Time Segmented Volume"
show_tsv = input.bool(false, "Show Time Segmented Volume [TSV]", inline = "s1", group = group_tsv)
len_tsv = input.int(13, "Length", 3, inline = "s1", group = group_tsv)
len_ma  = input.int(7,  "MA Length",  1, inline = "ma_tsv", group = group_tsv)
type_ma = input.string("SMA", "Type", inline = "ma_tsv", group = group_tsv, options=["SMA", "EMA", "Wilder", "WMA", "ALMA", "LSMA", "HMA", "JMA", "RSS_WMA"])

//__________________________________________________________
//_______________Definitions of Koncorde Plus_______________
pvi_media = ta.ema(ta.pvi, m_pvi_nvi)
pvi_max   = ta.highest(pvi_media, longitudPVI)
pvi_min   = ta.lowest(pvi_media, longitudPVI)
osc_pos   = (ta.pvi - pvi_media)*100 / (pvi_max - pvi_min)

nvi_media = ta.ema(ta.nvi, m_pvi_nvi)
nvi_max   = ta.highest(nvi_media, longitudNVI)
nvi_min   = ta.lowest(nvi_media, longitudNVI)
osc_neg   = (ta.nvi - nvi_media)*100 / (nvi_max - nvi_min)

// VAMA - Volume Adjusted Moving Average of @allanster
vama(_src,_len,_fct,_rul,_nvb) =>                             // vama(source,length,factor,rule,sample)
    tvb = 0, tvb := _nvb == 0 ? nz(tvb[1]) + 1 : _nvb         // total volume bars used in sample            
    tvs = _nvb == 0 ? ta.cum(volume) : math.sum(volume, _nvb) // total volume in sample
    v2i = volume / ((tvs / tvb) * _fct)                       // ratio of volume to increments of volume                                                  
    wtd = _src*v2i                                            // weighted prices 
    nmb = 1                                                   // initialize number of bars summed back               
    wtdSumB = 0.0                                             // initialize weighted prices summed back
    v2iSumB = 0.0                                             // initialize ratio of volume to increments of volume summed back
    for i = 1 to _len * 10                                    // set artificial cap for strict to VAMA length * 10 to help reduce edge case timeout errors
        strict = _rul ? false : i == _len                     // strict rule N bars' v2i ratios >= vama length, else <= vama length
        wtdSumB := wtdSumB + nz(wtd[i-1])                     // increment number of bars' weighted prices summed back
        v2iSumB := v2iSumB + nz(v2i[i-1])                     // increment number of bars' v2i's summed back
        if v2iSumB >= _len or strict                          // if chosen rule met
            break                                             // break (exit loop)
        nmb := nmb + 1                                        // increment number of bars summed back counter
    nmb                                                       // number of bars summed back to fulfill volume requirements or vama length
    wtdSumB                                                   // number of bars' weighted prices summed back
    v2iSumB                                                   // number of bars' v2i's summed back
    vama = (wtdSumB - (v2iSumB - _len) * _src[nmb]) / _len    // volume adjusted moving average

// JMA - Jurik Moving Average of @everget
jma(src, length, power, phase) => 
    phaseRatio = phase < -100 ? 0.5 : phase > 100 ? 2.5 : phase / 100 + 1.5
    beta = 0.45 * (length - 1) / (0.45 * (length - 1) + 2)
    alpha = math.pow(beta, power)
    Jma = 0.0
    e0 = 0.0
    e0 := (1 - alpha) * src + alpha * nz(e0[1])
    e1 = 0.0
    e1 := (src - e0) * (1 - beta) + beta * nz(e1[1])
    e2 = 0.0
    e2 := (e0 + phaseRatio * e1 - nz(Jma[1])) * math.pow(1 - alpha, 2) + math.pow(alpha, 2) * nz(e2[1])
    Jma := e2 + nz(Jma[1])
    Jma

// ALMA - Arnaud Legoux Moving Average of @kurtsmock
enhanced_alma(_series, _length, _offset, _sigma) =>
    length      = int(_length) // Floating point protection
    numerator   = 0.0
    denominator = 0.0 
    m = _offset * (length - 1)
    s = length / _sigma
    for i=0 to length-1
        weight       = math.exp(-((i-m)*(i-m)) / (2 * s * s))
        numerator   := numerator   + weight * _series[length - 1 - i]
        denominator := denominator + weight
    numerator / denominator

// Tick Volume
tick     = syminfo.mintick
rng      = close - open
tickrng  = tick
tickrng := math.abs(rng) < tick ? nz(tickrng[1]) : rng
tickvol  = math.abs(tickrng)/tick
volumen  = nz(volume) == 0 ? tickvol : volume

enhanced_vwma(_series, _length) =>
    vol  = voltype == "Default" ? volumen : tickvol
    vmp  = _series * vol
    VWMA = math.sum(vmp, _length) / math.sum(vol, _length)

// RSS_WMA of @RedKTrader
f_LazyLine(_data, _length) =>
    w1 = 0,     w2 = 0,     w3 = 0
    L1 = 0.0,   L2 = 0.0,   L3 = 0.0
    w = _length / 3

    if _length > 2
        w2 := math.round(w)
        w1 := math.round((_length - w2) / 2)
        w3 := int((_length - w2) / 2)

        L1 := ta.wma(_data, w1)
        L2 := ta.wma(L1, w2)
        L3 := ta.wma(L2, w3)
        
    else
        L3 := _data
        
    L3 

ma(_type, _source, _length) => 
    switch _type
        "SMA"  => ta.sma (_source, _length)
        "EMA"  => ta.ema (_source, _length)
        "WMA"  => ta.wma (_source, _length)
        "VWMA" => enhanced_vwma(_source, _length)
        "LSMA" => ta.linreg(_source, _length, loff) // Least Squares
        "ALMA" => enhanced_alma(_source, _length, offs, sigma) 
        "HMA"  => ta.hma(_source, _length)
        "VAMA" => vama(_source, _length, factor, true, 0)
        "JMA"  => jma(_source, _length, powerJ, phaseJ)
        "Wilder" => wild = _source, wild := nz(wild[1]) + (_source - nz(wild[1])) / _length // Wilder's moving average
        "RSS_WMA" => f_LazyLine(_source, _length)
        
f_bb(source, length) =>
    basis = ta.sma(source, length)  
    dev   = mult_bb * ta.stdev(source, length)  
    upper = basis + dev  // Upper Band 
    lower = basis - dev  // Lower Band
    avg_up_low = math.avg(upper, lower) 
    diff_up_low = upper - lower
    (source - avg_up_low) / diff_up_low * 100

f_stoch(src, length, smoothFastD) =>
    ll = ta.lowest(low, length)
    hh = ta.highest(high, length)
    k = 100 * (src - ll) / (hh - ll)
    ta.sma(k, smoothFastD) // enhanced_alma(k, smoothFastD, 0.85, 6) 

rsi_valor = ta.rsi(tprice, len_mfi_rsi)
mfi_valor = ta.mfi(srcMfi, len_mfi_rsi)
osc_bb    = f_bb(tprice, length_bb)
stoc      = f_stoch(tprice, 21, 3)

tendencia = (rsi_valor + mfi_valor + osc_bb + stoc/3) / 2 //math.avg(rsi_valor, mfi_valor, osc_bb, stoc)
tiburones = ta.sma(osc_neg, 1)
pececillos = tendencia + osc_pos

ma_trend = ma(type_ma1, tendencia, length_ma1) //ta.ema(tendencia, length_ma1)

char_up = "▲", char_dn = "▼", char_arp = "⇗", char_sur = "⇘"

patron_espejo = (pececillos < 0 and tiburones > 0)  
patron_oso = (pececillos > 0 and tiburones < 0) and pececillos >= tendencia 
patron_cero = ta.crossover(tendencia, 0)

cross_up_trend = ta.crossover(tendencia, ma_trend)  
cross_dn_trend = ta.crossunder(tendencia, ma_trend) 

patron_primavera = (ta.crossover(tendencia, ma_trend) and pececillos > tendencia) //or (ta.crossover(pececillos, tendencia) and pececillos >0)

cross_up_shark  = ta.crossover(tiburones, ma_trend) and (pececillos < 0 and tiburones > 0)
cross_dn_shark  = ta.crossunder(tiburones, 0) and pececillos > tendencia
cross_up_shark2 = ta.crossover(tiburones, ma_trend) and tendencia < ma_trend 

plot(show_K ? pececillos : na, "Minnows", pececillos > 0 ? #05FF68 : #009929, 1, plot.style_columns)  
plot(show_K ? tiburones  : na, "Shark",   tiburones  > 0 ? #00B0F6 : #003785, 1, plot.style_columns)
plot(show_K ? tendencia  : na, "Trend", tendencia > tendencia[1] ? #8D4925 : #320000, 2, plot.style_line)
plot(show_K ? tendencia  : na, "Trend", tendencia > tendencia[1] ? color.new(#8D4925, 70) : color.new(#320000, 45), 1, plot.style_columns)
plot(show_K ? ma_trend   : na, "MA of Trend", #FF0000, 2, plot.style_line) 
//hline(show_K ? 0 : na, "Center Line", #FAF5A5, hline.style_dotted)

plotchar(show_cruces ? cross_up_trend : na, "Crossover",  char_up, location.bottom, color.new(color.teal, 60), size=size.tiny)
plotchar(show_cruces ? cross_dn_trend : na, "Crossunder", char_dn, location.top,    color.new(color.red, 60),  size=size.tiny)

plotchar(show_cero ? patron_cero : na, "Zero Pattern", char_up, location.bottom, color.new(#FFCC99, 25), size=size.tiny)
plotchar(show_primavera ? patron_primavera : na, "Spring Pattern", char_up, location.bottom, color.new(color.lime, 35), size=size.tiny)

plotchar(show_arpon ? cross_up_shark  : na, "Harpoon Pattern",      char_arp, location.bottom, color.new(color.aqua, 10), size=size.tiny)
plotchar(show_arpon ? cross_dn_shark  : na, "Exit Harpoon Pattern", char_sur, location.top,    color.new(#FF0062, 10),    size=size.tiny)
plotchar(show_arpon ? cross_up_shark2 : na, "Weak Harpoon Pattern", char_arp, location.bottom, color.new(color.blue, 50), size=size.tiny)

bgcolor(show_espejo and patron_espejo ? color.new(color.aqua, 85) : na, title="Mirror Pattern")
bgcolor(show_oso and patron_oso ? color.new(color.red, 85) : na, title="Bear Hug Pattern")

//______________________________________________________________________________
// Dual Volume Divergence Index - DVDI of @DonovanWall
// https://www.tradingview.com/script/p2tfpKK3-Dual-Volume-Divergence-Index-DW/
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
// Definitions
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
// Dual Volume Divergence Index
roc_x = ta.roc(src, 1) 
dvdi(x, t1, t2, v)=>
    pvi   = x
    pvi  := v > v[1] ? nz(pvi[1]) + roc_x : nz(pvi[1])
    psig  = ta.ema(pvi, t1)
    PDIV  = ta.ema(pvi - psig, t2)
    nvi   = x
    nvi  := v < v[1] ? nz(nvi[1]) - roc_x : nz(nvi[1])
    nsig  = ta.ema(nvi, t1)
    NDIV  = ta.ema(nvi - nsig, t2)
    DVDIO = PDIV - NDIV
    [PDIV, NDIV, DVDIO]

vol = usetick ? tickvol : volume
[pdiv, ndiv, dvdio] = dvdi(src, per, smper, vol)

// DVDI Bar Counter
cross_up = ta.barssince(ta.crossover(dvdio, 0))
cross_dn = ta.barssince(ta.crossunder(dvdio, 0))
pdbars = dvdio > 0 ? cross_up + 1 : 0
ndbars = dvdio < 0 ? cross_dn + 1 : 0

// Divergence Color
color1   = (pdiv > ndiv) and (pdiv > 0) ? #05ffa6 : (pdiv > ndiv) and (pdiv <= 0) ? #00945f : 
           (ndiv > pdiv) and (ndiv > 0) ? #ff0a70 : (ndiv > pdiv) and (ndiv <= 0) ? #990040 : #cccccc
color2   = (dvdio > 0) and (dvdio > dvdio[1]) ? #05ffa6 : (dvdio > 0) and (dvdio <= dvdio[1]) ? #00945f : 
           (dvdio < 0) and (dvdio < dvdio[1]) ? #ff0a70 : (dvdio < 0) and (dvdio >= dvdio[1]) ? #990040 : #cccccc
color3   = (pdbars > 0) and (src > src[1]) ? #05ffa6 : (pdbars > 0) and (src <= src[1]) ? #00945f : 
           (ndbars > 0) and (src < src[1]) ? #ff0a70 : (ndbars > 0) and (src >= src[1]) ? #990040 : #cccccc
barcolor = mode=="Cloud Mode" ? color1 : mode=="Oscillator Mode" ? color2 : color3

color1_60   = (pdiv > ndiv) and (pdiv > 0) ? color.new(#05ffa6, 60) : (pdiv > ndiv) and (pdiv <= 0) ? color.new(#00945f, 60) : 
           (ndiv > pdiv) and (ndiv > 0) ? color.new(#ff0a70, 60) : (ndiv > pdiv) and (ndiv <= 0) ? color.new(#990040, 60) : color.new(#cccccc, 60)
color2_60   = (dvdio > 0) and (dvdio > dvdio[1]) ? color.new(#05ffa6, 60) : (dvdio > 0) and (dvdio <= dvdio[1]) ? color.new(#00945f, 60) : 
           (dvdio < 0) and (dvdio < dvdio[1]) ? color.new(#ff0a70, 60) : (dvdio < 0) and (dvdio >= dvdio[1]) ? color.new(#990040, 60) : color.new(#cccccc, 60)
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
// Plots
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
// Index Center
centplot = plot(0, "Index Center", display=display.none, editable=false)

// Dual Index Plots
pplot = plot(show_DVDI and mode=="Cloud Mode" ? pdiv : na, "PVI Divergence", #05FFA6)
nplot = plot(show_DVDI and mode=="Cloud Mode" ? ndiv : na, "NVI Divergence", #FF0A70)

// Dual Index Fill
fill(pplot, nplot, color1_60, "Cloud Fill")

//Oscillator Plot
oplot = plot(show_DVDI and mode=="Oscillator Mode" ? dvdio : na, "DVDI Oscillator", color2)

// Oscillator Fill
fill(oplot, centplot, color2_60, "Oscillator Fill")

// Bar Counter Plot
pdbarsplot = plot(show_DVDI and mode=="Bar Counter Mode" ? pdbars : na, "Positive Dominant Bar Count", #05FFA6)
ndbarsplot = plot(show_DVDI and mode=="Bar Counter Mode" ? ndbars : na, "Negative Dominant Bar Count", #FF0A70)

// Bar Counter Fill
fill(pdbarsplot, centplot, color.new(#05FFA6, 60), "Positive Dominant Bar Count Fill")
fill(ndbarsplot, centplot, color.new(#FF0A70, 60), "Negative Dominant Bar Count Fill")

// Bar Colors
barcolor(show_bar_dvdi ? barcolor : na, title="DVDI Bars Color")

//____________________________________________________________________________
// Enhanced Time Segmented Volume of @StephXAGs                                             
// https://www.tradingview.com/script/672uPH9q-Enhanced-Time-Segmented-Volume/
t = math.sum(close > close[1] ? volume * (close - close[1]) : close < close[1] ? volume * (close - close[1]) : 0, len_tsv)
m = ma(type_ma, t, len_ma)

PAL      = t > m and t > 0 
PAL_fail = t < m and t > 0
PAS      = t < m and t < 0
PAS_fail = t > m and t < 0

plotshape(show_tsv ? PAL : na, "Price Action Long",  shape.square, location.bottom, color.new(#04E604, 40), size=size.auto)
plotshape(show_tsv ? PAS : na, "Price Action Short", shape.square, location.bottom, color.new(#E60404, 40), size=size.auto)
plotshape(show_tsv ? PAL_fail : na, "Price Action Long - FAILURE",  shape.xcross, location.bottom, color.new(#027302, 60), size=size.auto)
plotshape(show_tsv ? PAS_fail : na, "Price Action Short - FAILURE", shape.xcross, location.bottom, color.new(#730202, 60), size=size.auto)

//____________________________________________Delta Arrows____________________________________________
tw = high - math.max(open, close) 
bw = math.min(open, close) - low 
body = math.abs(close - open) 

f_rate(cond) =>
    ret = 0.5 * (tw + bw + (cond ? 2 * body : 0)) / (tw + bw + body) 
    ret := nz(ret) == 0 ? 0.5 : ret
    ret
    
delta_up   = volume * f_rate(open <= close) 
delta_down = volume * f_rate(open > close)
delta = close >= open ? delta_up : -delta_down
cum_delta = switch v_type
    "Cumulative Volume Delta" => ta.cum(delta) 
    "OBV+PVT" => math.avg(ta.obv, ta.pvt)
    "OBV+PVT+AccDist" => math.avg(ta.obv, ta.pvt, ta.accdist)  
    "Delta+AccDist" => math.avg(delta, ta.accdist)
    "Delta+OBV" => math.avg(delta, ta.obv)
    "Delta+PVT" => math.avg(delta, ta.pvt)

ma_fast   = ma(type_fast, cum_delta, fast_len) 
ma_slow   = ma(type_slow, cum_delta, slow_len) 
delta_osc = ma_fast - ma_slow

sc1 = delta_osc >= 0
sc2 = delta_osc < 0
sc3 = delta_osc >= delta_osc[1]
sc4 = delta_osc < delta_osc[1]
col_delta_up = sc1 and sc3 ? #8080FF : sc1 and sc4 ? #40407F : color.silver
col_delta_dn = sc2 and sc4 ? #FF8080 : sc2 and sc3 ? #7F4040 : color.silver

plotshape(show_delta and delta_osc >= 0, "Up Delta",   shape.triangleup,   location.bottom, col_delta_up)
plotshape(show_delta and delta_osc <  0, "Down Delta", shape.triangledown, location.bottom, col_delta_dn)

//____________________________________________MACD____________________________________________
[macd_line, macd_signal, _] = ta.macd(macd_src, macd_fast, macd_slow, macd_sig)

cian = color.new(#38F5E9, 0)
cian_oscuro = color.new(#1C7A74, 0)
pink = color.new(#F7709D, 0)
pink_oscuro = color.new(#7B384E, 0)
color_up = macd_line >= macd_signal ? cian : cian_oscuro
color_dn = macd_line <= macd_signal ? pink : pink_oscuro
color_macd = macd_line >= 0 ? color_up : color_dn
char_dot = "◍" 

plotchar(show_macd ? macd_line : na, "MACD", char_dot, location.top, color_macd)
barcolor(show_macd2 ? color_macd : na, title="MACD Bars Color")

//______________________________________________________________________________
// Performance Table
//______________________________________________________________________________
[_, _, _, Cd , _, _, _, _] = f_htf_ohlc('D') 
[_, _, _, Cw , _, _, _, _] = f_htf_ohlc('W') 
[_, _, _, Cm , _, _, _, _] = f_htf_ohlc('M')
[_, _, _, C3m, _, _, _, _] = f_htf_ohlc('3M')
[_, _, _, C6m, _, _, _, _] = f_htf_ohlc('6M')
[_, _, _, Cy , _, _, _, _] = f_htf_ohlc('12M')

var table change = table.new(position.top_right, 4, 2, border_width=3)
if barstate.islast and i_chg 
    table.cell(change, 0, 0, 'CHG %', text_color=color.blue, bgcolor=color.new(color.blue, 80), text_halign=text.align_left, text_size=textSize)
    table.cell(change, 0, 1, '     ', text_color=color.blue, bgcolor=color.new(color.blue, 80), text_halign=text.align_left, text_size=textSize)

    table.cell(change, 1, 0, str.tostring((close / Cd - 1) * 100, '#.##')  + '%\nSoD ', text_color=close / Cd - 1 > 0  ? #00FF98 : #FF9800, bgcolor=close / Cd  - 1 > 0 ? color.new(color.green, 80) : color.new(color.red, 80), text_size=textSize, tooltip='SoD : Start of the Day')
    table.cell(change, 2, 0, str.tostring((close / Cw - 1) * 100, '#.##')  + '%\nSoW' , text_color=close / Cw - 1 > 0  ? #00FF98 : #FF9800, bgcolor=close / Cw  - 1 > 0 ? color.new(color.green, 80) : color.new(color.red, 80), text_size=textSize, tooltip='SoW : Start of the Week')
    table.cell(change, 3, 0, str.tostring((close / Cm - 1) * 100, '#.##')  + '%\nSoM' , text_color=close / Cm - 1 > 0  ? #00FF98 : #FF9800, bgcolor=close / Cm  - 1 > 0 ? color.new(color.green, 80) : color.new(color.red, 80), text_size=textSize, tooltip='SoM : Start of the Month')
    table.cell(change, 1, 1, str.tostring((close / C3m - 1) * 100, '#.##') + '%\nSo3M', text_color=close / C3m - 1 > 0 ? #00FF98 : #FF9800, bgcolor=close / C3m - 1 > 0 ? color.new(color.green, 80) : color.new(color.red, 80), text_size=textSize, tooltip='So3M : Start of the Quarter')
    table.cell(change, 2, 1, str.tostring((close / C6m - 1) * 100, '#.##') + '%\nSo6M', text_color=close / C6m - 1 > 0 ? #00FF98 : #FF9800, bgcolor=close / C6m - 1 > 0 ? color.new(color.green, 80) : color.new(color.red, 80), text_size=textSize, tooltip='So6M : Start of the HalfYear')
    table.cell(change, 3, 1, str.tostring((close / Cy - 1) * 100, '#.##')  + '%\nSoY' , text_color=close / Cy - 1 > 0  ? #00FF98 : #FF9800, bgcolor=close / Cy  - 1 > 0 ? color.new(color.green, 80) : color.new(color.red, 80), text_size=textSize, tooltip='SoY : Start of the Year')
