BBD Master
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Kent_RichProFit_93

//@version=5

indicator(title='BBD Master', overlay=false, precision=2)

//10Oct21 : revision 1
//16Nov21 : revision 2, added source input, candle turn green/turn red alerts and indicator descriptions



////////////////////////////////////////
//Input
////////////////////////////////////////


gr1 = 'Table'
ind = input.bool(title='Show Table', tooltip ='Indicator Name, Status, Alerts', defval=true, inline='00', group=gr1)
alert = input.bool(title='Show alerts', defval=true, inline='00', group=gr1)

text_size = input.string(title='Table Text Size:', options=['AUTO', 'TINY', 'SMALL', 'NORMAL', 'LARGE', 'HUGE'], defval='AUTO', inline='01', group=gr1)
w = input.int(title='/ Table Width:', defval=9, minval=4, maxval=20, inline='01', group=gr1)

gr2 ='BBD Master'
bbd_src = input.source(hlc3,"BBD Source", inline='03', group=gr2, tooltip='Recommend to use default source')
bbd_avg = input.int(title='BBD Moving Average =', defval=5, minval=3, maxval=20, inline='03b', group=gr2)


gr3='Indicator Descriptions Label'
method_label = input.bool(title='Show Indicator Brief Description', defval=false, inline='04', group=gr3)


////////////////////////////////////////
//Indicator Descriptions
////////////////////////////////////////

ind_l = method_label? label.new(bar_index+1, 0, 'BBD Master :  '
  +'\n Green candle : inflow of fund, Red candle  : outflow of fund'
  +'\n Technical Rebound = BBD <0, candle turns from red to green, moving up towards 0'
  +'\n Uptrend = BBD crossover 0, continue to move up'
  +'\n Downtrend = BBD >0, candle turns from green to red, moving down towards 0'
  +'\n Consolidation or Downtrend = BBD crossunder 0, continue to move down',
  color=  color.new(color.purple,80),
  textcolor= color.black,
  size = size.small,
  style=label.style_label_left, textalign=text.align_left) :na

label.delete(ind_l[1])



/////////////////////////////////
//Function Y = alpha*X + (1-alpha)*Y[1] 

Y(X, N, M) =>
    alpha = M / N

    S2 = 0.0
    S2 := nz(S2[1]) - nz(X[N]) + X

    M2 = 0.0
    M2 := na(X[N]) ? na : S2 / N

    A2 = 0.0
    A2 := na(A2[1]) ? M2 : alpha * X + (1 - alpha) * nz(A2[1])
    A2

bbd_raw = Y(bbd_src, 5, 1) - Y(bbd_src, 13, 1)
bbd = (bbd_raw - Y(bbd_raw, 3, 1)) * 100

bbd_color = bbd >= nz(bbd[1]) ? color.green : color.red

plotcandle(bbd, bbd, nz(bbd[1]), nz(bbd[1]), title='BBD Master', color=bbd_color, wickcolor=bbd_color, bordercolor=bbd_color)

bbd_bbl = ta.sma(bbd, bbd_avg)
plot(bbd_bbl, title='BBD Average Line', color=color.new(color.fuchsia, 0), linewidth=1)




////////////////////////////////////////
//BBD cross over 0 Alert
bbd_co = ta.crossover(bbd, 0)
bbd_co_c = bbd_co ? color.new(#FC0FC0, 40) : na
bbd_co_t = bbd_co ? 0 : na
plotcandle(bbd_co ? bbd : na, bbd_co ? bbd : na, bbd_co ? bbd[1] : na, bbd_co ? bbd[1] : na, title='BBD crossover 0, candle alert', color=color.yellow, wickcolor=color.yellow, bordercolor=color.yellow)
plotshape(alert?bbd_co_t:na, title='BBD crossover 0, Text Alert', text='0', style=shape.triangleup, location=location.absolute, color=color.new(color.black, 0), size=size.tiny)

bbd_cu = ta.crossunder(bbd, 0)


////////////////////////////////////////
//Turn Green or Turn Red Labels
bbd_turn_g = not bbd_co and bbd[2]>bbd[1] and bbd>bbd[1]
bbd_turn_r = bbd[2]<bbd[1] and bbd<bbd[1]

bbd_turn_g_t = bbd_turn_g ? bbd:na
plotshape(alert?bbd_turn_g_t:na, title='BBD turn Green', text='G', style=shape.triangleup, location=location.absolute, color=color.new(color.green, 0), size=size.tiny)

bbd_turn_r_t = bbd_turn_r ? bbd:na
plotshape(alert?bbd_turn_r_t:na, title='BBD turn Red', text='R', style=shape.triangleup, location=location.absolute, color=color.new(color.red, 0), size=size.tiny)



////////////////////////////////////////
z0 = hline(0, 'zero line', linestyle=hline.style_solid, color=color.blue, linewidth=2)

////////////////////////////////////////
//Table

var table Qtable = table.new(position.middle_right, 5, 5, border_width=1)

textsize = size.auto
if text_size == 'TINY'
    textsize := size.tiny
    textsize
if text_size == 'SMALL'
    textsize := size.small
    textsize
if text_size == 'NORMAL'
    textsize := size.normal
    textsize
if text_size == 'LARGE'
    textsize := size.large
    textsize
if text_size == 'HUGE'
    textsize := size.huge
    textsize


f_fillCell_10(_table, _column, _row, _label) =>
    _cellText_10 = ind ? _label : na
    _cellColor_10 = ind ? color.new(color.yellow, 0) : na
    table.cell(_table, _column, _row, _cellText_10, bgcolor=_cellColor_10, text_color=color.black, width=w)
    table.cell_set_text_size(Qtable, 1, 0, textsize)

if barstate.islast
    f_fillCell_10(Qtable, 1, 0, 'BBD Master ')


bbd_g_can = not bbd_turn_g and bbd>=bbd[1]
bbd_r_can = not bbd_turn_r and bbd<bbd[1]

bbd_stat_text = bbd_g_can ? 'Green candle' : bbd_r_can ?'Red candle' : bbd_turn_g ? 'turn Green' : bbd_turn_r ? 'turn Red' : bbd_co ? 'crossover 0' : bbd_co ? 'crossunder 0' : na
bbd_stat_color = bbd_g_can ?color.new(color.green,80) : bbd_r_can ?color.new(color.red,80) : bbd_turn_g ?color.new(color.green,80) : bbd_turn_r ? color.new(color.red,80) : bbd_co ? color.yellow: bbd_co ? color.new(color.red,80):na

f_fillCell_11(_table, _column, _row, _value) =>
    _cellText_11 = ind ? _value : na
    _cellColor_11 = ind ? bbd_stat_color : na
    table.cell(_table, _column, _row, _cellText_11, bgcolor=_cellColor_11, text_color=color.black, width=w)
    table.cell_set_text_size(Qtable, 1, 1, textsize)

if barstate.islast
    f_fillCell_11(Qtable, 1, 1, bbd_stat_text)



f_fillCell12(_table, _column, _row, _value) =>
    _cellText12 = ind ? str.tostring(_value, '#.##') : na
    _cellColor12 = ind  ? bbd_stat_color : na
    table.cell(_table, _column, _row, _cellText12, bgcolor=_cellColor12, text_color=color.black, width=w)
    table.cell_set_text_size(Qtable, 1, 2, textsize)

if barstate.islast
    f_fillCell12(Qtable, 1, 2, bbd)


// bbd_co_text = bbd_co ? 'crossover 0' : bbd_co ? 'crossunder 0' : 'No Alert'
// f_fillCell_12(_table, _column, _row, _value) =>
//     _cellText_12 = ind ? _value : na
//     _cellColor_12 = ind ? bbd_co ? bbd_co_c : bbd_cu ? color.new(color.red, 80) : color.new(color.gray, 80) : na
//     table.cell(_table, _column, _row, _cellText_12, bgcolor=_cellColor_12, text_color=color.black, width=w)

// if barstate.islast
//     f_fillCell_12(Qtable, 1, 2, bbd_co_text)
    // table.cell_set_text_size(Qtable, 1, 2, textsize)


