BTMM|TDI
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bullcatcherJC

//@version=5
indicator("BTMM|TDI", overlay=false)

// USER Interface
RSI_input = input.int(title="RSI", defval=21, minval=5, group='TDI Components', inline="RSI")
RSI_c = input.color(title="Color", defval=#000000, group='TDI Components', inline="RSI")
TL_input = input.int(title="TrendLine", defval=7, minval=3, group='TDI Components', inline="TL")
TL_c = input.color(title="Color", defval=#FF0000, group='TDI Components', inline="TL")
BL_input = input.int(title="BaseLine", defval=34, minval=14, group='TDI Components', inline="BL")
BL_c = input.color(title="Color", defval=color.yellow, group='TDI Components', inline="BL")
VB_input = input.float(title="Volatility Bands", defval=1.6185, minval=1.0, group='TDI Components', inline="VB")
VB_c = input.color(title="Color", defval=#b2ebf2, group='TDI Components', inline="VB")
BU_c = input.color(title="Bullish", defval=#0064FF, group='Color Scheme', inline="CS")
BE_c = input.color(title="Bearish", defval=#FF0000, group='Color Scheme', inline="CS")
is_showDataTBL = input.bool(title='Show Data Table', defval=false)

// Position Size Calculator
is_showLotSize = input.bool(title="Activate Lot Size Module", defval=false)
i_accountCurrency = input.string('USD', title='Account Currency', options=['GBP', 'USD', 'EUR', 'AUD', 'NZD', 'JPY', 'CAD', 'CHF'])
Capital = input.float(defval=50000, title="Account Balance", minval=0.0)
RiskPerTrade = input.float(defval=1.0, title="Risk % on Balance", minval=0.0) / 100
PipsRisked = input.float(defval=30, title="Pips at Risk", minval=0.1)

// Convert to Base Account Currency
sec01 = i_accountCurrency == syminfo.currency ? syminfo.ticker : syminfo.currency + i_accountCurrency
sec03 = i_accountCurrency == syminfo.currency ? 1 : request.security(sec01, '1', close)
pip_Movement = syminfo.mintick == 0.00001 ? 10 : syminfo.mintick == 0.001 ? 1000 : 100
AmountRisked = math.round(Capital * RiskPerTrade)
Lotsize = AmountRisked / (PipsRisked * pip_Movement) / sec03

int mintime = time
if time[1]
    mintime := math.min(mintime, time - time[1])
    mintime
txt = 'Currency = ' + i_accountCurrency + '\nBalance = ' + str.tostring(Capital,'#,###') + '\nRisk = ' + str.tostring(RiskPerTrade*100) + '%' + '\nAmount = ' + str.tostring(AmountRisked,'#,###') + '\nPips = ' + str.tostring(PipsRisked,'#.#') + '\nLot Size = ' + str.tostring(Lotsize,'#.##')
if is_showLotSize
    var label psclabel = na
    label.delete(psclabel)
    psclabel := label.new(x=time + 15*mintime, y=40, xloc=xloc.bar_time, text=txt, color=color.black, textcolor=color.white, textalign=text.align_left, size=size.normal)
    psclabel


// TDI Setup

r = ta.rsi(close, RSI_input)
r_plot = ta.sma(r, 2)
r_tl = ta.sma(r, TL_input)
r_gbl = ta.sma(r, BL_input)

SD = VB_input * ta.stdev(r, BL_input)
VB_UP = r_gbl + SD
VB_DOWN = r_gbl - SD


// Higher TimeFrame RSI vs TL Bias

var HTF3 = timeframe.isintraday and 3 * timeframe.multiplier <= 960 ? str.tostring(3 * timeframe.multiplier, '') : '3D'
r_bias = r_plot - r_tl > 0 ? BU_c : BE_c
HTF_c = request.security(syminfo.tickerid, HTF3, r_bias)
bgcolor(color.new(HTF_c, 85))


// Plots

UL = 70, MID = 50, LL = 30
hline(MID, 'Balance Level', color=color.white, linestyle=hline.style_dotted)
hline(UL, 'Upper Level', color=color.white, linestyle=hline.style_dotted)
hline(LL, 'Lower Level', color=color.white, linestyle=hline.style_dotted)

plot(r_plot, 'RSI', color=RSI_c, linewidth=2)
plot(r_tl, 'RSI TrendLine', color=TL_c, linewidth=2)
plot(r_gbl, 'Market Baseline', color=BL_c, linewidth=2)
plot(VB_UP, 'Volatility Upper', color=VB_c, linewidth=2)
plot(VB_DOWN, 'Volatility Lower', color=VB_c, linewidth=2)


// MACD Zero Lag Trend Bias

dema(period) => 2 * ta.ema(close, period) - ta.ema(ta.ema(close, period), period)
mzl = dema(12) - dema(26)

plotshape(mzl < 0, title='Trend DN', style=shape.labeldown, location=location.top, color=BE_c)
plotshape(mzl > 0, title='Trend UP', style=shape.labelup, location=location.bottom, color=BU_c)


// BTMM EMA Position vs Closing Price Scoring

y1 = ta.ema(close, 13), y2 = ta.ema(close, 50), y3 = ta.ema(close, 200), y4 = ta.ema(close, 800)

p_vs_y1 = close>y1 ? 1 : close<y1 ? -1 : 0
p_vs_y2 = close>y2 ? 1 : close<y2 ? -1 : 0
p_vs_y3 = close>y3 ? 1 : close<y3 ? -1 : 0
p_vs_y4 = close>y4 ? 1 : close<y4 ? -1 : 0
y1_vs_y2 = y1>y2 ? 1 : y1<y2 ? -1 : 0
y1_vs_y3 = y1>y3 ? 1 : y1<y3 ? -1 : 0
y1_vs_y4 = y1>y4 ? 1 : y1<y4 ? -1 : 0
y2_vs_y3 = y2>y3 ? 1 : y2<y3 ? -1 : 0
y2_vs_y4 = y2>y4 ? 1 : y2<y4 ? -1 : 0
y3_vs_y4 = y3>y4 ? 1 : y3<y4 ? -1 : 0

EMA_score = p_vs_y1 + p_vs_y2 + p_vs_y3 + p_vs_y4 + y1_vs_y2 + y1_vs_y3 + y1_vs_y4 + y2_vs_y3 + y2_vs_y4 + y3_vs_y4
EMA_score_C = EMA_score>0 ? BU_c : EMA_score<0 ? BE_c : color.yellow

// DataTable + Color Scheme Definitions

r_plotc = r_plot > 50 ? BU_c : BE_c 
r_tlc = r_plot > r_tl ? BU_c : BE_c
r_gblc = r_gbl > r_gbl[1] ? BU_c : BE_c

if is_showDataTBL
    var main_table = table.new(position.middle_right, 5, 5, frame_color=#000000, frame_width=2, border_color=#000000, border_width=1)
    table.cell(main_table, 0, 0, 'TF', text_color=color.white, text_size=size.small, bgcolor=color.new(color.black, 50))
    table.cell(main_table, 1, 0, 't3', text_color=color.white, text_size=size.small, bgcolor=color.new(color.black, 50))
    table.cell(main_table, 2, 0, 't2', text_color=color.white, text_size=size.small, bgcolor=color.new(color.black, 50))
    table.cell(main_table, 3, 0, 't1', text_color=color.white, text_size=size.small, bgcolor=color.new(color.black, 50))
    table.cell(main_table, 4, 0, 't0', text_color=color.white, text_size=size.small, bgcolor=color.new(color.black, 50))
    table.cell(main_table, 0, 1, 'R', text_color=color.white, text_size=size.small, bgcolor=color.new(color.black, 50))
    table.cell(main_table, 0, 2, 'TL', text_color=color.white, text_size=size.small, bgcolor=color.new(color.black, 50))
    table.cell(main_table, 0, 3, 'BL', text_color=color.white, text_size=size.small, bgcolor=color.new(color.black, 50))
    table.cell(main_table, 0, 4, 'EMA', text_color=color.white, text_size=size.small, bgcolor=color.new(color.black, 50))

    for i = 1 to 4
        table.cell(main_table, i, 1, str.tostring(r_plot[4-i], '#.#'), text_color=#000000, text_size=size.small, bgcolor=color.new(r_plotc[4-i], 50))
        table.cell(main_table, i, 2, str.tostring(r_tl[4-i], '#.#'), text_color=#000000, text_size=size.small, bgcolor=color.new(r_tlc[4-i], 50))
        table.cell(main_table, i, 3, str.tostring(r_gbl[4-i], '#.#'), text_color=#000000, text_size=size.small, bgcolor=color.new(r_gblc[4-i], 50))
        table.cell(main_table, i, 4, str.tostring(EMA_score[4-i], '#'), text_color=#000000, text_size=size.small, bgcolor=color.new(EMA_score_C[4-i], 50))
