TDMACD Divergence Multi Timeframe and Alert [5ema]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vn-5ema
// Reused some functions from (I believe made by @kingthies, ©paaax, @QuantNomad, @ChaosTrader)
// @paaax: The table position function (https://www.tradingview.com/script/Jp37jHwT-PX-MTF-Overview/).
// @kingthies: The divergence function (https://www.tradingview.com/script/CaMohiJM-rsi-Divergence-Pine-v4/).
// @QuantNomad: The function calculated value and array screener for 40+ instruments (https://www.tradingview.com/script/zN8rwPFF-Screener-for-40-instruments/).
// @ChaosTrader: The function calculated value TDMACD (https://www.tradingview.com/script/oJYIelLW-TDMacd/).

//@version=5
indicator(title="TDMACD Divergence Multi Timeframe and Alert [5ema]", shorttitle = "tdMACD [5ema]")
// macd 1
source = input.source(defval = close, title = "Source of MACD")
fastLength = input.int(5, minval=1,title = "Fast length of MACD" )
slowLength = input.int(20, minval=1, title = "Slow length of MACD")
signalLength = input.int(30, minval=1, title = "Signal length of MACD")

// Input divergence
righBar = input.int(defval = 1, minval = 1, title = "Number righbar", tooltip = "Recommend 1-3, use to returns price of the pivot low & high point.")
leftBar = input.int(defval = 5, minval = 2,  title = "Number leftbar", tooltip = "Recommend 5, use to returns price of the pivot low & high point.")
rangeUpper = input.int(defval = 60, minval = 2, title = "Number range upper", tooltip = "Recommend 60, use to compare the low & high point.")
rangeLower = input.int(defval = 5, minval = 2, title = "Number range lower", tooltip = "Recommend 5, use to compare the low & high point.")

// Plot macd
opplotmacd = input.timeframe("", title = "Time frame", group = "set up macd on chart")
showchart = input.string(defval = "All", options = ["Divergence", "Crosser", "All", "Off"], title = "Show Signal", group = "set up macd on chart")


/////// This bool symbool refer from @QuantNomad /////
// Bool Symbols
u01 = input.bool(true,  title = "Time frame 1     ", group = "Time frame following", inline = "s01")
u02 = input.bool(true,  title = "Time frame 2     ", group = "Time frame following", inline = "s02")
u03 = input.bool(true,  title = "Time frame 3     ", group = "Time frame following", inline = "s03")
u04 = input.bool(true,  title = "Time frame 4     ", group = "Time frame following", inline = "s04")
u05 = input.bool(true,  title = "Time frame 5     ", group = "Time frame following", inline = "s05")
u06 = input.bool(true,  title = "Time frame 6     ", group = "Time frame following", inline = "s06")

// Input Symbols
s01 = input.timeframe("", title = "",  group = "Time frame following", inline = "s01")
s02 = input.timeframe("5", title = "",  group = "Time frame following", inline = "s02")
s03 = input.timeframe("15", title = "", group = "Time frame following", inline = "s03")
s04 = input.timeframe("60", title = "",  group = "Time frame following", inline = "s04")
s05 = input.timeframe("240", title = "",  group = "Time frame following", inline = "s05")
s06 = input.timeframe("D", title = "",  group = "Time frame following", inline = "s06")
//////////

// Divergence
plotBull = input.bool(title="Regular Bullish ", defval=true, group = "Show divergence on chart", tooltip = "Show regular Bullish divergence on chart. The table will display >>.")
plotBear = input.bool(title="Regular Bearish", defval=true, group = "Show divergence on chart", tooltip = "Show regular Bearish divergence on chart. The table will display <<.")
plotHiddenBull = input.bool(title="Hidden Bullish  ", defval=false, group = "Show divergence on chart", tooltip = "Show hidden Bullish divergence on chart. The table will display >.")
plotHiddenBear = input.bool(title="Hidden Bearish ", defval=false, group = "Show divergence on chart", tooltip = "Show hidden Bearish divergence on chart. The table will display <.")

// Set up table
boolTable = input.bool(defval = true,  title = "Show table         ", group = "Set up table", inline ="tb")
pTable = input.string("Bottom Right", title="", inline ="tb", options=["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"], group="Set up table", tooltip = "On table will appear the shapes: divergence (>, <), cross (x), strong signal ⦿, review signal (〇) with the color green (bull) or bear (red).")
/////// This function reused from ©paaax /////
getPosition(pTable) =>
    ret = position.top_left
    if pTable == "Top Center"
        ret := position.top_center
    if pTable == "Top Right"
        ret := position.top_right
    if pTable == "Middle Left"
        ret := position.middle_left
    if pTable == "Middle Center"
        ret := position.middle_center
    if pTable == "Middle Right"
        ret := position.middle_right
    if pTable == "Bottom Left"
        ret := position.bottom_left
    if pTable == "Bottom Center"
        ret := position.bottom_center
    if pTable == "Bottom Right"
        ret := position.bottom_right
    ret
//////////
// Set up color of table
table_boder = input(color.new(color.white, 100), group = "Set up table", title = "Border Color ")
table_frame = input(#151715, group = "Set up table", title ="Frame Color ")
table_bg = input(color.black, group = "Set up table", title ="Back Color ")
table_text = input(color.white, group = "Set up table", title ="Text Color ")

// Alert
onlyStrongAlert = input.bool(title="Only strong Signal", defval=true, group = "Set up Alert", tooltip = "Select this if want to alert only strong signal.")
onlyreviewSignalAlert = input.bool(title="Only review Signal", defval=true, group = "Set up Alert", tooltip = "Select this if want to alert only review Signal.")

/////// This divergence function refer from @kingthies/////
// Calculate macd
fastMA = ta.ema(source, fastLength)
slowMA = ta.ema(source, slowLength)
macd()=> 
    macd = fastMA - slowMA
signalmacd()=>
    signalmacd = ta.ema(macd(), signalLength)

crossBuy() =>
    crossBuy = ta.crossover(macd(), signalmacd())

crossSell() =>
    crossSell = ta.crossunder(macd(), signalmacd())

plFindsn()=> 
    plFind = na(ta.pivotlow(signalmacd(), leftBar, righBar)) ? false : true
phFindsn()=> 
    phFind = na(ta.pivothigh(signalmacd(), leftBar, righBar)) ? false : true

Omacd()=> 
    Omacd = macd()

plFind()=> 
    plFind = na(ta.pivotlow(Omacd(), leftBar, righBar)) ? false : true
phFind()=> 
    phFind = na(ta.pivothigh(Omacd(), leftBar, righBar)) ? false : true

    
// macd divergence
inRange(Cdt) =>
    bars = ta.barssince(Cdt == true)
    rangeLower <= bars and bars <= rangeUpper
// Regular bullish macd
OmacdHL() =>
    OmacdHL = Omacd()[righBar] > ta.valuewhen(plFind(), Omacd()[righBar], 1) and inRange(plFind()[1])
// Hidden bullish macd
OmacdLL() =>
    OmacdLL = Omacd()[righBar] < ta.valuewhen(plFind(), Omacd()[righBar], 1) and inRange(plFind()[1])
//Hidden bearish
OmacdHH () =>
    OmacdHH = Omacd()[righBar] > ta.valuewhen(phFind(), Omacd()[righBar], 1) and inRange(phFind()[1])
// Regular bearish macd
OmacdLH() =>
    OmacdLH = Omacd()[righBar] < ta.valuewhen(phFind(), macd()[righBar], 1) and inRange(phFind()[1])

// Regular bullish price
priceLL() =>
    priceLL = low[righBar] < ta.valuewhen(plFind(), low[righBar], 1)
// Hidden bullish price
priceHL() =>
    priceHL = low[righBar] > ta.valuewhen(plFind(), low[righBar], 1)
//Regular bearish price
priceHH() =>
    priceHH = high[righBar] > ta.valuewhen(phFind(), high[righBar], 1)
//Hidden bearish price
priceLH() =>
    priceLH = high[righBar] < ta.valuewhen(phFind(), high[righBar], 1)

//////////
// Screener function
scr_macd() =>
    macd = macd()
    signalmacd = signalmacd()
    crossBuy = crossBuy()
    crossSell = crossSell()
    plFindsn = plFindsn()
    phFindsn = phFindsn()
    Omacd = macd()
    plFind = plFind()
    phFind = phFind()
    OmacdHL = OmacdHL()
    OmacdLL = OmacdLL()
    OmacdHH = OmacdHH()
    OmacdLH = OmacdLH()
    priceLL = priceLL()
    priceHH = priceHH()
    priceLH = priceLH()
    priceHL = priceHL()
    BullCdt = plotBull and priceLL and OmacdHL
    hiddenBullCdt = plotHiddenBull and priceHL and OmacdLL
    BearCdt = plotBear and priceHH and OmacdLH
    hiddenBearCdt = plotHiddenBear and priceLH and OmacdHH

    [macd, signalmacd, crossBuy, crossSell, plFindsn, phFindsn, Omacd, plFind, phFind, OmacdHL, OmacdLL, OmacdHH, OmacdLH, priceLL,  priceHH, priceLH, priceHL, BullCdt, hiddenBullCdt, BearCdt, hiddenBearCdt]


/////// This function refer from @QuantNomad /////
// Security call
[macd, signalmacd, crossBuy, crossSell, plFindsn, phFindsn, Omacd, plFind, phFind, OmacdHL, OmacdLL, OmacdHH, OmacdLH, priceLL,  priceHH, priceLH, priceHL, BullCdt, hiddenBullCdt, BearCdt, hiddenBearCdt] = request.security(timeframe = opplotmacd, symbol = syminfo.tickerid, expression =  scr_macd())
[macd01, signalmacd01, crossBuy01, crossSell01,  plFindsn01, phFindsn01, Omacd01, plFind01, phFind01, OmacdHL01, OmacdLL01, OmacdHH01, OmacdLH01, priceLL01,  priceHH01, priceLH01, priceHL01, BullCdt01, hiddenBullCdt01, BearCdt01, hiddenBearCdt01] = request.security(timeframe = s01, symbol = syminfo.tickerid, expression =  scr_macd())
[macd02, signalmacd02, crossBuy02, crossSell02, plFindsn02, phFindsn02,  Omacd02, plFind02, phFind02, OmacdHL02, OmacdLL02, OmacdHH02, OmacdLH02, priceLL02,  priceHH02, priceLH02, priceHL02, BullCdt02, hiddenBullCdt02, BearCdt02, hiddenBearCdt02] = request.security(timeframe = s02, symbol = syminfo.tickerid, expression =  scr_macd())
[macd03, signalmacd03, crossBuy03, crossSell03, plFindsn03, phFindsn03,Omacd03, plFind03, phFind03, OmacdHL03, OmacdLL03, OmacdHH03, OmacdLH03, priceLL03,  priceHH03, priceLH03, priceHL03, BullCdt03, hiddenBullCdt03, BearCdt03, hiddenBearCdt03] = request.security(timeframe = s03, symbol = syminfo.tickerid, expression =  scr_macd())
[macd04, signalmacd04, crossBuy04, crossSell04, plFindsn04, phFindsn04, Omacd04, plFind04, phFind04, OmacdHL04, OmacdLL04, OmacdHH04, OmacdLH04, priceLL04,  priceHH04, priceLH04, priceHL04, BullCdt04, hiddenBullCdt04, BearCdt04, hiddenBearCdt04] = request.security(timeframe = s04, symbol = syminfo.tickerid, expression =  scr_macd())
[macd05, signalmacd05, crossBuy05, crossSell05, plFindsn05, phFindsn05, Omacd05, plFind05, phFind05, OmacdHL05, OmacdLL05, OmacdHH05, OmacdLH05, priceLL05,  priceHH05, priceLH05, priceHL05, BullCdt05, hiddenBullCdt05, BearCdt05, hiddenBearCdt05] = request.security(timeframe = s05, symbol = syminfo.tickerid, expression =  scr_macd())
[macd06, signalmacd06, crossBuy06, crossSell06, plFindsn06, phFindsn06, Omacd06, plFind06, phFind06, OmacdHL06, OmacdLL06, OmacdHH06, OmacdLH06, priceLL06,  priceHH06, priceLH06, priceHL06, BullCdt06, hiddenBullCdt06, BearCdt06, hiddenBearCdt06] = request.security(timeframe = s06, symbol = syminfo.tickerid, expression =  scr_macd())

// Arrays //
s_arr   = array.new_string(0)
u_arr   = array.new_bool(0)
macd_arr  = array.new_float(0)
signalmacd_arr  = array.new_float(0)
crossBuy_arr = array.new_bool(0)
crossSell_arr = array.new_bool(0)
plFindsn_arr = array.new_bool(0)
phFindsn_arr = array.new_bool(0)
Omacd_arr  = array.new_float(0)
plFind_arr = array.new_bool(0)
phFind_arr = array.new_bool(0)
OmacdHL_arr = array.new_bool(0)
OmacdLL_arr = array.new_bool(0)
OmacdHH_arr = array.new_bool(0)
OmacdLH_arr = array.new_bool(0)
priceLL_arr = array.new_bool(0)
priceHH_arr = array.new_bool(0)
priceLH_arr = array.new_bool(0)
priceHL_arr = array.new_bool(0)
BullCdt_arr = array.new_bool(0)
hiddenBullCdt_arr = array.new_bool(0)
BearCdt_arr = array.new_bool(0)
hiddenBearCdt_arr = array.new_bool(0)


// Add Symbols 
array.push(s_arr, s01)
array.push(s_arr, s02)
array.push(s_arr, s03)
array.push(s_arr, s04)
array.push(s_arr, s05)
array.push(s_arr, s06)


// Flags //
array.push(u_arr, u01)
array.push(u_arr, u02)
array.push(u_arr, u03)
array.push(u_arr, u04)
array.push(u_arr, u05)
array.push(u_arr, u06)



array.push(macd_arr, macd01)
array.push(macd_arr, macd02)
array.push(macd_arr, macd03)
array.push(macd_arr, macd04)
array.push(macd_arr, macd05)
array.push(macd_arr, macd06)


array.push(signalmacd_arr, signalmacd01)
array.push(signalmacd_arr, signalmacd02)
array.push(signalmacd_arr, signalmacd03)
array.push(signalmacd_arr, signalmacd04)
array.push(signalmacd_arr, signalmacd05)
array.push(signalmacd_arr, signalmacd06)


array.push(crossBuy_arr, crossBuy01)
array.push(crossBuy_arr, crossBuy02)
array.push(crossBuy_arr, crossBuy03)
array.push(crossBuy_arr, crossBuy04)
array.push(crossBuy_arr, crossBuy05)
array.push(crossBuy_arr, crossBuy06)

array.push(crossSell_arr, crossSell01)
array.push(crossSell_arr, crossSell02)
array.push(crossSell_arr, crossSell03)
array.push(crossSell_arr, crossSell04)
array.push(crossSell_arr, crossSell05)
array.push(crossSell_arr, crossSell06)


array.push(plFindsn_arr, plFindsn01)
array.push(plFindsn_arr, plFindsn02)
array.push(plFindsn_arr, plFindsn03)
array.push(plFindsn_arr, plFindsn04)
array.push(plFindsn_arr, plFindsn05)
array.push(plFindsn_arr, plFindsn06)


array.push(phFindsn_arr, phFindsn01)
array.push(phFindsn_arr, phFindsn02)
array.push(phFindsn_arr, phFindsn03)
array.push(phFindsn_arr, phFindsn04)
array.push(phFindsn_arr, phFindsn05)
array.push(phFindsn_arr, phFindsn06)


array.push(Omacd_arr, Omacd01)
array.push(Omacd_arr, Omacd02)
array.push(Omacd_arr, Omacd03)
array.push(Omacd_arr, Omacd04)
array.push(Omacd_arr, Omacd05)
array.push(Omacd_arr, Omacd06)


array.push(plFind_arr, plFind01)
array.push(plFind_arr, plFind02)
array.push(plFind_arr, plFind03)
array.push(plFind_arr, plFind04)
array.push(plFind_arr, plFind05)
array.push(plFind_arr, plFind06)


array.push(phFind_arr, phFind01)
array.push(phFind_arr, phFind02)
array.push(phFind_arr, phFind03)
array.push(phFind_arr, phFind04)
array.push(phFind_arr, phFind05)
array.push(phFind_arr, phFind06)


array.push(hiddenBearCdt_arr, hiddenBearCdt01)
array.push(hiddenBearCdt_arr, hiddenBearCdt02)
array.push(hiddenBearCdt_arr, hiddenBearCdt03)
array.push(hiddenBearCdt_arr, hiddenBearCdt04)
array.push(hiddenBearCdt_arr, hiddenBearCdt05)
array.push(hiddenBearCdt_arr, hiddenBearCdt06)


array.push(BearCdt_arr, BearCdt01)
array.push(BearCdt_arr, BearCdt02)
array.push(BearCdt_arr, BearCdt03)
array.push(BearCdt_arr, BearCdt04)
array.push(BearCdt_arr, BearCdt05)
array.push(BearCdt_arr, BearCdt06)


array.push(hiddenBullCdt_arr, hiddenBullCdt01)
array.push(hiddenBullCdt_arr, hiddenBullCdt02)
array.push(hiddenBullCdt_arr, hiddenBullCdt03)
array.push(hiddenBullCdt_arr, hiddenBullCdt04)
array.push(hiddenBullCdt_arr, hiddenBullCdt05)
array.push(hiddenBullCdt_arr, hiddenBullCdt06)


array.push(BullCdt_arr, BullCdt01)
array.push(BullCdt_arr, BullCdt02)
array.push(BullCdt_arr, BullCdt03)
array.push(BullCdt_arr, BullCdt04)
array.push(BullCdt_arr, BullCdt05)
array.push(BullCdt_arr, BullCdt06)


array.push(priceHL_arr, priceHL01)
array.push(priceHL_arr, priceHL02)
array.push(priceHL_arr, priceHL03)
array.push(priceHL_arr, priceHL04)
array.push(priceHL_arr, priceHL05)
array.push(priceHL_arr, priceHL06)


array.push(priceLH_arr, priceLH01)
array.push(priceLH_arr, priceLH02)
array.push(priceLH_arr, priceLH03)
array.push(priceLH_arr, priceLH04)
array.push(priceLH_arr, priceLH05)
array.push(priceLH_arr, priceLH06)

array.push(priceHH_arr, priceHH01)
array.push(priceHH_arr, priceHH02)
array.push(priceHH_arr, priceHH03)
array.push(priceHH_arr, priceHH04)
array.push(priceHH_arr, priceHH05)
array.push(priceHH_arr, priceHH06)



array.push(priceLL_arr, priceLL01)
array.push(priceLL_arr, priceLL02)
array.push(priceLL_arr, priceLL03)
array.push(priceLL_arr, priceLL04)
array.push(priceLL_arr, priceLL05)
array.push(priceLL_arr, priceLL06)


array.push(OmacdLH_arr, OmacdLH01)
array.push(OmacdLH_arr, OmacdLH02)
array.push(OmacdLH_arr, OmacdLH03)
array.push(OmacdLH_arr, OmacdLH04)
array.push(OmacdLH_arr, OmacdLH05)
array.push(OmacdLH_arr, OmacdLH06)


array.push(OmacdHH_arr, OmacdHH01)
array.push(OmacdHH_arr, OmacdHH02)
array.push(OmacdHH_arr, OmacdHH03)
array.push(OmacdHH_arr, OmacdHH04)
array.push(OmacdHH_arr, OmacdHH05)
array.push(OmacdHH_arr, OmacdHH06)


array.push(OmacdLL_arr, OmacdLL01)
array.push(OmacdLL_arr, OmacdLL02)
array.push(OmacdLL_arr, OmacdLL03)
array.push(OmacdLL_arr, OmacdLL04)
array.push(OmacdLL_arr, OmacdLL05)
array.push(OmacdLL_arr, OmacdLL06)


array.push(OmacdHL_arr, OmacdHL01)
array.push(OmacdHL_arr, OmacdHL02)
array.push(OmacdHL_arr, OmacdHL03)
array.push(OmacdHL_arr, OmacdHL04)
array.push(OmacdHL_arr, OmacdHL05)
array.push(OmacdHL_arr, OmacdHL06)


/////// /////
//plot
var tbl = table.new(getPosition(pTable), 6, 7, frame_color=table_frame, frame_width=1, border_width=1, border_color= table_boder)
if barstate.islast and boolTable
    table.cell(tbl, 0, 0, "Tframe", text_halign = text.align_center, bgcolor = table_bg, text_color = table_text, text_size = size.small)
    table.cell(tbl, 1, 0, "MACD", text_halign = text.align_center, bgcolor = table_bg, text_color = table_text, text_size = size.small)
    table.cell(tbl, 2, 0, "Diver.", text_halign = text.align_center, bgcolor = table_bg, text_color = table_text, text_size = size.small)
    table.cell(tbl, 3, 0, "sEMA", text_halign = text.align_center, bgcolor = table_bg, text_color = table_text, text_size = size.small)
    table.cell(tbl, 4, 0, "Cross", text_halign = text.align_center, bgcolor = table_bg, text_color = table_text, text_size = size.small)
    table.cell(tbl, 5, 0, "Signal", text_halign = text.align_center, bgcolor = table_bg, text_color = table_text, text_size = size.small)

       
    for i = 0 to 5
        if array.get(u_arr, i)
            checkBull = array.get(plFind_arr, i) and  array.get(BullCdt_arr,i)
            checkhdBull = array.get(plFind_arr, i) and array.get(hiddenBullCdt_arr, i)
            checkBear = array.get(phFind_arr, i) and array.get(BearCdt_arr, i)
            checkhdBear = array.get(phFind_arr, i) and array.get(hiddenBearCdt_arr, i)

            plBull_sha   =  checkBull ? ">>" : na
            plhdBull_sha   =  checkhdBull ? ">"  : na
            phBear_sha   = checkBear ? "<<"  : na
            phhdBear_sha   =  checkhdBear ? "<" : na

            checkSignalBuy = array.get(plFind_arr, i) == true and array.get(macd_arr, i) < array.get(signalmacd_arr, i) and array.get(macd_arr, i) < 0
            checkSignalBuysn =  array.get(plFindsn_arr, i) == true and array.get(macd_arr, i) >= array.get(signalmacd_arr, i) and array.get(signalmacd_arr, i) < 0
            checkSignalSell = array.get(phFind_arr, i) == true  and array.get(macd_arr, i) > array.get(signalmacd_arr, i) and array.get(macd_arr, i) > 0
            checkSignalSellsn = array.get(phFindsn_arr, i) == true and array.get(macd_arr, i) <= array.get(signalmacd_arr, i) and array.get(signalmacd_arr, i) > 0

            signalcrossBuy = array.get(crossBuy_arr, i) == true
            signalcrossSell = array.get(crossSell_arr, i) == true


            sgstrongBuy = signalcrossBuy and (checkSignalBuysn or checkSignalBuy)
            sgstrongSell = signalcrossSell and (checkSignalSellsn or checkSignalSellsn)

            sgSell = checkBear or checkhdBear or checkSignalSell or checkSignalSellsn or signalcrossSell
            sgBuy = checkBull or checkhdBull or checkSignalBuy or checkSignalBuysn or signalcrossBuy
            


            table.cell(tbl, 0, i + 1, array.get(s_arr, i), text_halign = text.align_center,   bgcolor = table_bg, text_color = table_text, text_size = size.small)
            table.cell(tbl, 1, i + 1, str.tostring(array.get(macd_arr, i), "#.####"), text_halign = text.align_center,   bgcolor = table_bg, text_color = checkSignalBuy ? color.new(color.green, 0) : checkSignalSell ? color.new(color.red, 0) : table_text, text_size = size.small)
            table.cell(tbl, 2, i + 1, checkBull ? plBull_sha : checkBear ? phBear_sha : checkhdBull ? plhdBull_sha : checkhdBear ? phhdBear_sha : "", text_halign = text.align_center, bgcolor = table_bg, text_color = checkBull or checkhdBull ? color.new(color.green, 0) : checkBear or checkhdBear ? color.new(color.red, 0) : table_text , text_size = size.small)
            table.cell(tbl, 3, i + 1, str.tostring(array.get(signalmacd_arr, i), "#.####"), text_halign = text.align_center, bgcolor = table_bg, text_color = checkSignalBuysn ? color.new(color.green, 0) : checkSignalSellsn ? color.new(color.red, 0) : table_text , text_size = size.small)
            table.cell(tbl, 4, i + 1, (signalcrossBuy or signalcrossSell) ? "X" : "", text_color = sgBuy or sgstrongBuy ? color.new(color.green, 0) :  sgSell or sgstrongSell ? color.new(color.red, 0) : table_text , text_halign = text.align_center, bgcolor = table_bg, text_size = size.small)
            table.cell(tbl, 5, i + 1, (sgstrongBuy or sgstrongSell) ? "⦿" : (sgBuy or sgSell) ? "〇" : "", text_color = sgBuy or sgstrongBuy ? color.new(color.green, 0) :  sgSell or sgstrongSell ? color.new(color.red, 0) : table_text , text_halign = text.align_center, bgcolor = table_bg, text_size = size.small)
            

// For current macd chart

signalcrossBuy_cr = crossBuy == true
signalcrossSell_cr = crossSell == true

// Plot macd
plot(Omacd, title="MACD line", linewidth=1, color=ta.change(Omacd)<=0 ? color.new( color.red, 40) : color.new( color.green, 40), style = plot.style_histogram)
plot(signalmacd, title="Signal MACD line", linewidth=1, color=  ta.change(signalmacd)<=0 ? color.new( color.red, 0) : color.new( color.green, 0))
plot(showchart != "Crosser" and showchart != "Off" and plFind ? Omacd[righBar] : na, offset=-righBar, title="Regular Bullish", linewidth=1, color=BullCdt ? color.new(color.green, 0) : color.new(color.white, 100))
plot(showchart != "Crosser" and showchart != "Off" and plFind ? Omacd[righBar] : na, offset=-righBar, title="Hidden Bullish", linewidth=1, color=hiddenBullCdt ? color.new(color.green, 0) : color.new(color.white, 100) )
plot(showchart != "Crosser" and showchart != "Off" and phFind ? Omacd[righBar] : na, offset=-righBar, title="Regular Bearish", linewidth=1, color=BearCdt ? color.new(color.red, 0) : color.new(color.white, 100))
plot(showchart != "Crosser" and showchart != "Off" and phFind ? Omacd[righBar] : na, offset=-righBar, title="Hidden Bearish", linewidth=1, color=hiddenBearCdt ? color.new(color.red, 0) :  color.new(color.white, 100))
plotshape(showchart != "Divergence" and showchart != "Off" and (signalcrossBuy_cr or signalcrossSell_cr) ? signalmacd : na,  color = signalcrossBuy_cr ?color.new(color.green, 0) : color.new(color.red, 0), title = "Cross signal", style = shape.xcross, location=location.absolute)



crh_alert_sg = ""
//Signal strong BUY
crh_alert_sg := (u01 != false  and (crossBuy01  and ((plFind01  and macd01 < signalmacd01 and macd01 < 0) or (plFindsn01  and macd01 >= signalmacd01 and signalmacd01 < 0)))) ? crh_alert_sg + syminfo.ticker + "\n" + "Time frame: " + s01 + "\n" + "Signal: BUY" + "\n" : crh_alert_sg
crh_alert_sg := (u02 != false  and (crossBuy02  and ((plFind02  and macd02 < signalmacd02 and macd02 < 0) or (plFindsn02  and macd02 >= signalmacd02 and signalmacd02 < 0)))) ? crh_alert_sg + syminfo.ticker + "\n" + "Time frame: " + s02 + "\n" + "Signal: BUY" + "\n" : crh_alert_sg
crh_alert_sg := (u03 != false  and (crossBuy03  and ((plFind03  and macd03 < signalmacd03 and macd03 < 0) or (plFindsn03  and macd03 >= signalmacd03 and signalmacd03 < 0)))) ? crh_alert_sg + syminfo.ticker + "\n" + "Time frame: " + s03 + "\n" + "Signal: BUY" + "\n" : crh_alert_sg
crh_alert_sg := (u04 != false  and (crossBuy04  and ((plFind04  and macd04 < signalmacd04 and macd04 < 0) or (plFindsn04  and macd04 >= signalmacd04 and signalmacd04 < 0)))) ? crh_alert_sg + syminfo.ticker + "\n" + "Time frame: " + s04 + "\n" + "Signal: BUY" + "\n" : crh_alert_sg
crh_alert_sg := (u05 != false  and (crossBuy05  and ((plFind05  and macd05 < signalmacd05 and macd05 < 0) or (plFindsn05  and macd05 >= signalmacd05 and signalmacd05 < 0)))) ? crh_alert_sg + syminfo.ticker + "\n" + "Time frame: " + s05 + "\n" + "Signal: BUY" + "\n" : crh_alert_sg
crh_alert_sg := (u06 != false  and (crossBuy06  and ((plFind06  and macd06 < signalmacd06 and macd06 < 0) or (plFindsn06  and macd06 >= signalmacd06 and signalmacd06 < 0)))) ? crh_alert_sg + syminfo.ticker + "\n" + "Time frame: " + s06 + "\n" + "Signal: BUY" + "\n" : crh_alert_sg

//Signal strong SELL
crh_alert_sg := (u01 != false   and (crossSell01  and ((phFind01  and macd01 > signalmacd01 and macd01 > 0) or (phFindsn01  and macd01 <= signalmacd01 and signalmacd01 > 0)))) ? crh_alert_sg + syminfo.ticker + "\n" + "Time frame: " + s01 + "\n" + "Signal: SELL" + "\n" : crh_alert_sg
crh_alert_sg := (u02 != false   and (crossSell02  and ((phFind02  and macd02 > signalmacd02 and macd02 > 0) or (phFindsn02  and macd02 <= signalmacd02 and signalmacd02 > 0)))) ? crh_alert_sg + syminfo.ticker + "\n" + "Time frame: " + s02 + "\n" + "Signal: SELL" + "\n" : crh_alert_sg
crh_alert_sg := (u03 != false   and (crossSell03  and ((phFind03  and macd03 > signalmacd03 and macd03 > 0) or (phFindsn03  and macd03 <= signalmacd03 and signalmacd03 > 0)))) ? crh_alert_sg + syminfo.ticker + "\n" + "Time frame: " + s03 + "\n" + "Signal: SELL" + "\n" : crh_alert_sg
crh_alert_sg := (u04 != false   and (crossSell04  and ((phFind04  and macd04 > signalmacd04 and macd04 > 0) or (phFindsn04  and macd04 <= signalmacd04 and signalmacd04 > 0))))  ? crh_alert_sg + syminfo.ticker + "\n" + "Time frame: " + s04 + "\n" + "Signal: SELL" + "\n" : crh_alert_sg
crh_alert_sg := (u05 != false   and (crossSell05  and ((phFind05  and macd05 > signalmacd05 and macd05 > 0) or (phFindsn05  and macd05 <= signalmacd05 and signalmacd05 > 0)))) ? crh_alert_sg + syminfo.ticker + "\n" + "Time frame: " + s05 + "\n" + "Signal: SELL" + "\n" : crh_alert_sg
crh_alert_sg := (u06 != false   and (crossSell06  and ((phFind06  and macd06 > signalmacd06 and macd06 > 0) or (phFindsn06  and macd06 <= signalmacd06 and signalmacd06 > 0)))) ? crh_alert_sg + syminfo.ticker + "\n" + "Time frame: " + s06 + "\n" + "Signal: SELL" + "\n" : crh_alert_sg

// Send alert only if screener is not empty
if (crh_alert_sg != "") and onlyStrongAlert
    alert(crh_alert_sg + "\n(tdMACD - 5ema.vn)", freq = alert.freq_once_per_bar_close)


crh_alert_rd = ""
//Signal Regular divergence BUY
crh_alert_rd := (u01 != false   and ((crossBuy01 ) or (plFindsn01 and macd01 >= signalmacd01 and signalmacd01 < 0) or (plFind01 and macd01 < signalmacd01 and macd01 < 0) or (plFind01 and BullCdt01))) ? crh_alert_rd + syminfo.ticker + "\n" + "Time frame: " + s01 + "\n" + "Signal: Review BUY" + "\n" : crh_alert_rd
crh_alert_rd := (u02 != false   and ((crossBuy02 ) or (plFindsn02 and macd02 >= signalmacd02 and signalmacd02 < 0) or (plFind02 and macd02 < signalmacd02 and macd02 < 0) or (plFind02 and BullCdt02))) ? crh_alert_rd + syminfo.ticker + "\n" + "Time frame: " + s02 + "\n" + "Signal: Review BUY" + "\n" : crh_alert_rd
crh_alert_rd := (u03 != false   and ((crossBuy03 ) or (plFindsn03 and macd03 >= signalmacd03 and signalmacd03 < 0) or (plFind03 and macd03 < signalmacd03 and macd03 < 0) or (plFind03 and BullCdt03))) ? crh_alert_rd + syminfo.ticker + "\n" + "Time frame: " + s03 + "\n" + "Signal: Review BUY" + "\n" : crh_alert_rd
crh_alert_rd := (u04 != false   and ((crossBuy04 ) or (plFindsn04 and macd04 >= signalmacd04 and signalmacd04 < 0) or (plFind04 and macd04 < signalmacd04 and macd04 < 0) or (plFind04 and BullCdt04))) ? crh_alert_rd + syminfo.ticker + "\n" + "Time frame: " + s04 + "\n" + "Signal: Review BUY" + "\n" : crh_alert_rd
crh_alert_rd := (u05 != false   and ((crossBuy05 ) or (plFindsn05 and macd05 >= signalmacd05 and signalmacd05 < 0) or (plFind05 and macd05 < signalmacd05 and macd05 < 0) or (plFind05 and BullCdt05))) ? crh_alert_rd + syminfo.ticker + "\n" + "Time frame: " + s05 + "\n" + "Signal: Review BUY" + "\n" : crh_alert_rd
crh_alert_rd := (u06 != false   and ((crossBuy06 ) or (plFindsn05 and macd06 >= signalmacd06 and signalmacd06 < 0) or (plFind06 and macd06 < signalmacd06 and macd06 < 0) or (plFind06 and BullCdt06))) ? crh_alert_rd + syminfo.ticker + "\n" + "Time frame: " + s06 + "\n" + "Signal: Review BUY" + "\n" : crh_alert_rd

//Signal Regular divergence SELL
crh_alert_rd := (u01 != false   and ((crossSell01 ) or (phFindsn01 and macd01 <= signalmacd01 and signalmacd01 > 0) or (phFind01 and macd01 > signalmacd01 and macd01 > 0) or (phFind01  and BearCdt01))) ? crh_alert_rd + syminfo.ticker + "\n" + "Time frame: " + s01 + "\n" + "Signal: Review SELL" + "\n" : crh_alert_rd
crh_alert_rd := (u02 != false   and ((crossSell02 ) or (phFindsn02 and macd02 <= signalmacd02 and signalmacd02 > 0) or (phFind02 and macd02 > signalmacd02 and macd02 > 0) or (phFind02 and BearCdt02))) ? crh_alert_rd + syminfo.ticker + "\n" + "Time frame: " + s02 + "\n" + "Signal: Review SELL" + "\n" : crh_alert_rd
crh_alert_rd := (u03 != false   and ((crossSell03 ) or (phFindsn03 and macd03 <= signalmacd03 and signalmacd03 > 0) or (phFind03 and macd03 > signalmacd03 and macd03 > 0) or (phFind03 and BearCdt03))) ? crh_alert_rd + syminfo.ticker + "\n" + "Time frame: " + s03 + "\n" + "Signal: Review SELL" + "\n" : crh_alert_rd
crh_alert_rd := (u04 != false   and ((crossSell04 ) or (phFindsn04 and macd04 <= signalmacd04 and signalmacd04 > 0) or (phFind04 and macd04 > signalmacd04 and macd04 > 0) or (phFind04 and BearCdt04))) ? crh_alert_rd + syminfo.ticker + "\n" + "Time frame: " + s04 + "\n" + "Signal: Review SELL" + "\n" : crh_alert_rd
crh_alert_rd := (u05 != false   and ((crossSell05 ) or (phFindsn05 and macd05 <= signalmacd05 and signalmacd05 > 0) or (phFind05 and macd05 > signalmacd05 and macd05 > 0) or (phFind05 and BearCdt05))) ? crh_alert_rd + syminfo.ticker + "\n" + "Time frame: " + s05 + "\n" + "Signal: Review SELL" + "\n" : crh_alert_rd
crh_alert_rd := (u06 != false   and ((crossSell06 ) or (phFindsn06 and macd06 <= signalmacd06 and signalmacd06 > 0) or (phFind06 and macd06 > signalmacd06 and macd06 > 0) or (phFind06 and BearCdt06))) ? crh_alert_rd + syminfo.ticker + "\n" + "Time frame: " + s06 + "\n" + "Signal: Review SELL" + "\n" : crh_alert_rd

// Send alert only if screener is not empty
if (crh_alert_rd != "") and onlyreviewSignalAlert
    alert(crh_alert_rd + "\n(tdMACD - 5ema.vn)", freq = alert.freq_once_per_bar_close)



