Trend Momentum Divergence (TMD)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Shout out to a few people for there script code to work with.


//@version=5

indicator(title='Trend Momentum Divergence', shorttitle='TMD', timeframe='')
// PVSRA
// From MT4 source:


//   Situation "Climax"
//   Bars with volume >= 200% of the average volume of the 10 previous chart TFs, and bars
//   where the product of candle spread x candle volume is >= the highest for the 10 previous
//   chart time TFs.
//   Default Colors:  Bull bars are green and bear bars are red.

//   Situation "Volume Rising Above Average"
//   Bars with volume >= 150% of the average volume of the 10 previous chart TFs.
//   Default Colors:  Bull bars are blue and bear are blue-violet.

// We want to be able to override where we get the volume data for the candles.


bool overridesym = input(title='Override chart symbol?', defval=false)
string pvsra_sym = input.symbol(title='Symbol', defval='INDEX:BTCUSD')
bool setcandlecolors = input(true, title='Set PVSRA candle colors?')

pvsra_security(sresolution, sseries) =>
    request.security(overridesym ? pvsra_sym : syminfo.tickerid, sresolution, sseries[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, barmerge.lookahead_off)
pvsra_security_1 = pvsra_security('', volume)
pvsra_volume = overridesym == true ? pvsra_security_1 : volume
pvsra_security_2 = pvsra_security('', high)
pvsra_high = overridesym == true ? pvsra_security_2 : high
pvsra_security_3 = pvsra_security('', low)
pvsra_low = overridesym == true ? pvsra_security_3 : low
pvsra_security_4 = pvsra_security('', close)
pvsra_close = overridesym == true ? pvsra_security_4 : close
pvsra_security_5 = pvsra_security('', open)
pvsra_open = overridesym == true ? pvsra_security_5 : open


//label.new(overridesym ? 0 : na, low, text = "PVSRA Override: " + pvsra_sym, xloc = xloc.bar_index, yloc=yloc.belowbar,style=label.style_label_down, size=size.huge)

// The below math matches MT4 PVSRA indicator source
// average volume from last 10 candles
sum_1 = math.sum(pvsra_volume, 10)
sum_2 = math.sum(volume, 10)
av = overridesym == true ? sum_1 / 10 : sum_2 / 10
//climax volume on the previous candle
value2 = overridesym == true ? pvsra_volume * (pvsra_high - pvsra_low) : volume * (high - low)
// highest climax volume of the last 10 candles
hivalue2 = ta.highest(value2, 10)
// VA value determines the bar color. va = 0: normal. va = 1: climax.  va = 2: rising
iff_1 = pvsra_volume >= av * 1.5 ? 2 : 0
iff_2 = pvsra_volume >= av * 2 or value2 >= hivalue2 ? 1 : iff_1
iff_3 = volume >= av * 1.5 ? 2 : 0
iff_4 = volume >= av * 2 or value2 >= hivalue2 ? 1 : iff_3
va = overridesym == true ? iff_2 : iff_4

// Bullish or bearish coloring
isBull = overridesym == true ? pvsra_close > pvsra_open : close > open

CUColor = color.lime  // Climax up (bull) bull and bear both start with b so it would be weird hence up down
CDColor = color.red  // Climax down (bear)

AUColor = color.blue  //Avobe average up (bull)
ADColor = color.fuchsia  //Above average down (bear))

NUColor = #999999
NDColor = #4d4d4d

// candleColor = iff(climax,iff(isBull,CUColor,CDColor),iff(aboveA,iff(isBull,AUColor,ADColor),iff(isBull,NUColor,NDColor)))
iff_5 = va == 2 ? AUColor : NUColor
iff_6 = va == 1 ? CUColor : iff_5
iff_7 = va == 2 ? ADColor : NDColor
iff_8 = va == 1 ? CDColor : iff_7
candleColor = isBull ? iff_6 : iff_8
barcolor(setcandlecolors ? candleColor : na)

plot(pvsra_volume, style=plot.style_columns, linewidth=1, color=candleColor)

alertcondition(va > 0, title='Alert on Vector Candle', message='{{ticker}} Vector Candle on the {{interval}}')

grad(src)=>
    color out = switch int(src)
        0 => color.new(#1500FF , 20)
        1 => color.new(#1709F6 , 20)
        2 => color.new(#1912ED , 20)
        3 => color.new(#1B1AE5 , 20)
        4 => color.new(#1D23DC , 20)
        5 => color.new(#1F2CD3 , 20)
        6 => color.new(#2135CA , 20)
        7 => color.new(#233EC1 , 20)
        8 => color.new(#2446B9 , 20)
        9 => color.new(#264FB0 , 20)
        10 => color.new(#2858A7 , 20)
        11 => color.new(#2A619E , 20)
        12 => color.new(#2C6A95 , 20)
        13 => color.new(#2E728D , 20)
        14 => color.new(#307B84 , 20)
        15 => color.new(#32847B , 20)
        16 => color.new(#348D72 , 20)
        17 => color.new(#36956A , 20)
        18 => color.new(#389E61 , 20)
        19 => color.new(#3AA758 , 20)
        20 => color.new(#3CB04F , 20)
        21 => color.new(#3EB946 , 20)
        22 => color.new(#3FC13E , 20)
        23 => color.new(#41CA35 , 20)
        24 => color.new(#43D32C , 20)
        25 => color.new(#45DC23 , 20)
        26 => color.new(#47E51A , 20)
        27 => color.new(#49ED12 , 20)
        28 => color.new(#4BF609 , 20)
        29 => color.new(#4DFF00 , 20)
        30 => color.new(#53FF00 , 20)
        31 => color.new(#59FF00 , 20)
        32 => color.new(#5FFE00 , 20)
        33 => color.new(#65FE00 , 20)
        34 => color.new(#6BFE00 , 20)
        35 => color.new(#71FE00 , 20)
        36 => color.new(#77FD00 , 20)
        37 => color.new(#7DFD00 , 20)
        38 => color.new(#82FD00 , 20)
        39 => color.new(#88FD00 , 20)
        40 => color.new(#8EFC00 , 20)
        41 => color.new(#94FC00 , 20)
        42 => color.new(#9AFC00 , 20)
        43 => color.new(#A0FB00 , 20)
        44 => color.new(#A6FB00 , 20)
        45 => color.new(#ACFB00 , 20)
        46 => color.new(#B2FB00 , 20)
        47 => color.new(#B8FA00 , 20)
        48 => color.new(#BEFA00 , 20)
        49 => color.new(#C4FA00 , 20)
        50 => color.new(#CAF900 , 20)
        51 => color.new(#D0F900 , 20)
        52 => color.new(#D5F900 , 20)
        53 => color.new(#DBF900 , 20)
        54 => color.new(#E1F800 , 20)
        55 => color.new(#E7F800 , 20)
        56 => color.new(#EDF800 , 20)
        57 => color.new(#F3F800 , 20)
        58 => color.new(#F9F700 , 20)
        59 => color.new(#FFF700 , 20)
        60 => color.new(#FFEE00 , 20)
        61 => color.new(#FFE600 , 20)
        62 => color.new(#FFDE00 , 20)
        63 => color.new(#FFD500 , 20)
        64 => color.new(#FFCD00 , 20)
        65 => color.new(#FFC500 , 20)
        66 => color.new(#FFBD00 , 20)
        67 => color.new(#FFB500 , 20)
        68 => color.new(#FFAC00 , 20)
        69 => color.new(#FFA400 , 20)
        70 => color.new(#FF9C00 , 20)
        71 => color.new(#FF9400 , 20)
        72 => color.new(#FF8C00 , 20)
        73 => color.new(#FF8300 , 20)
        74 => color.new(#FF7B00 , 20)
        75 => color.new(#FF7300 , 20)
        76 => color.new(#FF6B00 , 20)
        77 => color.new(#FF6200 , 20)
        78 => color.new(#FF5A00 , 20)
        79 => color.new(#FF5200 , 20)
        80 => color.new(#FF4A00 , 20)
        81 => color.new(#FF4200 , 20)
        82 => color.new(#FF3900 , 20)
        83 => color.new(#FF3100 , 20)
        84 => color.new(#FF2900 , 20)
        85 => color.new(#FF2100 , 20)
        86 => color.new(#FF1900 , 20)
        87 => color.new(#FF1000 , 20)
        88 => color.new(#FF0800 , 20)
        89 => color.new(#FF0000 , 20)
        90 => color.new(#F60000 , 20)
        91 => color.new(#DF0505 , 20)
        92 => color.new(#C90909 , 20)
        93 => color.new(#B20E0E , 20)
        94 => color.new(#9B1313 , 20)
        95 => color.new(#851717 , 20)
        96 => color.new(#6E1C1C , 20)
        97 => color.new(#572121 , 20)
        98 => color.new(#412525 , 20)
        99 => color.new(#2A2A2A , 20)
        100 => color.new(#220027 , 20)
    out

filter(float src, int len) =>
    var float filter = na
    filter := ta.cum((src + (src[1] * 2) + (src[2] * 2) + src[3])/6)
    (filter - filter[len])/len

rsi(src, len) =>
    RSI = ta.rsi(filter(src, 1), len)
    f = -math.pow(math.abs(math.abs(RSI - 50) - 50), 1 + math.pow(len / 14, 0.618) - 1) / math.pow(50, math.pow(len / 14, 0.618) - 1) + 50
    rsia = if RSI > 50
        f + 50
    else
        -f + 50
    rsia
length = input.int(8)
RSI = rsi(close, length)

barcolor(grad(RSI))

//// Indicators

// Blue Wave
n1 = input.int(9, 'Channel Length', group='Wave Length')
n2 = input.int(12, 'Average Length', group='Wave Length')


ap = ohlc4
esa = ta.ema(ap, n1)
D = ta.ema(math.abs(ap - esa), n1)
ci = (ap - esa) / (0.015 * D)
tci = ta.ema(ci, n2)
wt1 = tci
wt2 = ta.sma(wt1, 3)

// MFI
mfi_upper = math.sum(volume * (ta.change(hlc3) <= 0 ? 0 : hlc3), 58)
mfi_lower = math.sum(volume * (ta.change(hlc3) >= 0 ? 0 : hlc3), 58)
_mfi_rsi(mfi_upper, mfi_lower) =>
    if mfi_lower == 0
        100
    if mfi_upper == 0
        0
    100.0 - 100.0 / (1.0 + mfi_upper / mfi_lower)
mf = _mfi_rsi(mfi_upper, mfi_lower)
mfi = (mf - 50) * 2.5

//// Plots
mfi_color = mfi > 0 ? #009688 : #9c27b0
dot_color = wt1 < wt2 ? #FF0000 : #4CAF50

plot(wt1, style=plot.style_area, color=color.new(#90caf9, 50), title='Light Wave')
plot(wt2, style=plot.style_area, color=color.new(#2a2e39, 60), title='Dark Wave')
plot(mfi, 'MFI Area', style=plot.style_area, color=color.new(mfi_color, 80), linewidth=2)
plot(mfi, 'MFI Line', style=plot.style_line, color=color.new(#000000, 1.5), linewidth=1)
plot(ta.crossunder(wt1, wt2) or ta.crossover(wt1, wt2) ? wt2 : na, title='Dots', style=plot.style_circles, color=color.new(dot_color, 50), linewidth=2)
bgcolor(color.new(#000000, 80))

//plot
itvm = input(36, 'middle interval')
itvl = input(44, 'long interval')
source = input(close, 'source')


ord(seq, idx, itv) =>
    p = seq[idx]
    o = 1
    s = 0
    for i = 0 to itv - 1 by 1
        if p < seq[i]
            o += 1
            o
        else
            if p == seq[i]
                s += 1
                o + (s - 1) / 2.0
    o

c(itv) =>
    sum = 0.0
    for i = 0 to itv - 1 by 1
        sum += math.pow(i + 1 - ord(source, i, itv), 2)
        sum
    sum

rci(itv) =>
    (1.0 - 6.0 * c(itv) / (itv * (itv * itv - 1.0))) * 100.0

plot(rci(itvm), title='RCI middle', color=color.new(#0000FF, 0))
plot(rci(itvl), title='RCI long', color=color.new(#008000, 0))

src = close
len = input.int(14, title='RSI Length')
up = ta.rma(math.max(ta.change(src), 0), len)
down = ta.rma(-math.min(ta.change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 150 - 320 / (1 + up / down)



sma_f_p = input(defval=9, title='RSI SMA Period')
BBperiod=input(31,title="Bollinger Bands period")
BBpar=input(1.6,title="Bolliger Bands parameter")


h1 = hline(54, color=color.new(#ff0000,50), linestyle=hline.style_dotted, linewidth=1)
h2 = hline(23, color=#434651,  linestyle=hline.style_dotted, linewidth=1)
fill (h1, h2, color=color.new(#ff0000, transp=90))
h3 = hline(-10, color=#2a2e39, linestyle=hline.style_dotted, linewidth=1)
h4 = hline(-42, color=color.new(#00e600,50), linestyle=hline.style_dotted, linewidth=1)
fill (h3, h4, color=color.new(#0e6000, transp=90))
h5 = hline(80, color=#434651, linestyle=hline.style_dotted, linewidth=1)

sma_rsi = ta.sma(rsi, sma_f_p)
dev =ta.stdev(rsi,BBperiod)
cband=ta.sma(rsi,BBperiod)
uband=cband+BBpar*dev
lband=cband-BBpar*dev
psma = plot(sma_rsi, title='RSI-SMA', color=color.new(#ff9800, 15), style=plot.style_line, linewidth=2)

plot(uband, "Upper Band", color=color.new(#2a2e39,0),linewidth=1)
plot(cband, "Middle Band", color=color.new(#363a55,0),linewidth=1)
plot(lband, "Lower Band", color=color.new(#2a2e39,0),linewidth=1)

plot(rsi, color=color.new(#d1d4dc, 35), linewidth=2, title='RSI')

smoothK = input.int(3, "K", minval=1)
smoothD = input.int(3, "D", minval=1)
lengthRSI = input.int(14, "RSI Length", minval=1)
lengthStoch = input.int(14, "Stochastic Length", minval=1)
Src = input(close, title="RSI Source")
rsi1 = ta.rsi(Src, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)
plot(k, "K", color=color.new(#2962FF,40))
plot(d, "D", color=color.new(#FF6D00,40))


