You are on page 1of 7

//@version=5

indicator(title='Kve TDI', shorttitle='Kve TDI',max_bars_back=1500)


sp_rsi = input(false, title='⎻⎻⎻⎻⎻⎻ RSI Settings ⎻⎻⎻⎻⎻⎻⎻')
rsiLength = input.int(10, minval=1, title='RSI Length')
rsiOverbought = input.int(80, title='RSI Overbought - Level 2', minval=0,
maxval=100)
rsiOverbought1 = input.int(70, title='RSI Overbought - Level 1', minval=0,
maxval=100)
rsiOversold1 = input.int(30, title='RSI Oversold - Level 1', minval=0,
maxval=100)
rsiOversold = input.int(20, title='RSI Oversold - Level 2', minval=0, maxval=100)
rsiSource = input(close, title='RSI Source')

sp_bb = input(false, title='⎻⎻⎻⎻⎻⎻ TDI BB Settings ⎻⎻⎻⎻⎻⎻⎻')


bb_length = input.int(34, minval=1, title='BB Length')
bb_type = input.string('SMA', title='BB Type', options=['SMA', 'EMA', 'DEMA',
'TEMA', 'WMA', 'VWMA', 'SMMA', 'Hull', 'LSMA', 'ALMA', 'KAMA', 'MAMA', 'ZEMA',
'HWMA', 'AHMA'])
lsmaOffset = input.int(defval=1, title='Least Squares (LSMA) Only - Offset Value',
minval=0)
almaOffset = input.float(defval=0.85, title='Arnaud Legoux (ALMA) Only - Offset
Value', minval=0, step=0.01)
almaSigma = input.int(defval=6, title='Arnaud Legoux (ALMA) Only - Sigma Value',
minval=0)
bb_std = input.float(1.6185, minval=0, title='BB std.')
bb_plus = input.float(0.0, minval=0, title='BB +Constant')

sp_ma = input(false, title='⎻⎻⎻⎻⎻⎻ TDI Signal Setting ⎻⎻⎻⎻⎻⎻⎻')


rsiSignalLen = input.int(34, minval=1, title='TDI Signal Length ')
rsiSignalType = input.string('TEMA', title='BB Type', options=['SMA', 'EMA',
'DEMA', 'TEMA', 'WMA', 'VWMA', 'SMMA', 'Hull', 'LSMA', 'ALMA', 'KAMA', 'MAMA',
'ZEMA', 'HWMA', 'AHMA'])
lsmaOffset2 = input.int(defval=1, title='Least Squares (LSMA) Only - Offset Value',
minval=0)
almaOffset2 = input.float(defval=0.85, title='Arnaud Legoux (ALMA) Only - Offset
Value', minval=0, step=0.01)
almaSigma2 = input.int(defval=6, title='Arnaud Legoux (ALMA) Only - Sigma Value',
minval=0)

sp_sh = input(false, title='⎻⎻⎻⎻⎻⎻Show/Hide Signals ⎻⎻⎻⎻⎻⎻⎻')

//------ Functions ----- //


kama(src, len) =>
xvnoise = math.abs(src - src[1])
nfastend = 0.666
nslowend = 0.0645
nsignal = math.abs(src - src[len])
nnoise = math.sum(xvnoise, len)
nefratio = nnoise != 0 ? nsignal / nnoise : 0
nsmooth = math.pow(nefratio * (nfastend - nslowend) + nslowend, 2)
nAMA = 0.0
nAMA := nz(nAMA[1]) + nsmooth * (src - nz(nAMA[1]))
nAMA

mama(src, len) =>


fl = 0.5
sl = 0.05
pi = 3.1415926
sp = (4 * src + 3 * src[1] + 2 * src[2] + src[3]) / 10.0
p = 0.0
i2 = 0.0
q2 = 0.0
dt = (.0962 * sp + .5769 * nz(sp[2]) - .5769 * nz(sp[4]) - .0962 * nz(sp[6])) *
(.075 * nz(p[1]) + .54)
q1 = (.0962 * dt + .5769 * nz(dt[2]) - .5769 * nz(dt[4]) - .0962 * nz(dt[6])) *
(.075 * nz(p[1]) + .54)
i1 = nz(dt[3])
jI = (.0962 * i1 + .5769 * nz(i1[2]) - .5769 * nz(i1[4]) - .0962 * nz(i1[6])) *
(.075 * nz(p[1]) + .54)
jq = (.0962 * q1 + .5769 * nz(q1[2]) - .5769 * nz(q1[4]) - .0962 * nz(q1[6])) *
(.075 * nz(p[1]) + .54)
i2_ = i1 - jq
q2_ = q1 + jI
i2 := .2 * i2_ + .8 * nz(i2[1])
q2 := .2 * q2_ + .8 * nz(q2[1])
re_ = i2 * nz(i2[1]) + q2 * nz(q2[1])
im_ = i2 * nz(q2[1]) - q2 * nz(i2[1])
re = 0.0
im = 0.0
re := .2 * re_ + .8 * nz(re[1])
im := .2 * im_ + .8 * nz(im[1])
p1 = im != 0 and re != 0 ? 2 * pi / math.atan(im / re) : nz(p[1])
iff_1 = p1 < 0.67 * nz(p1[1]) ? 0.67 * nz(p1[1]) : p1
p2 = p1 > 1.5 * nz(p1[1]) ? 1.5 * nz(p1[1]) : iff_1
iff_2 = p2 > 50 ? 50 : p2
p3 = p2 < 6 ? 6 : iff_2
p := .2 * p3 + .8 * nz(p3[1])
phase = 180 / pi * math.atan(q1 / i1)
dphase_ = nz(phase[1]) - phase
dphase = dphase_ < 1 ? 1 : dphase_
alpha_ = fl / dphase
iff_3 = alpha_ > fl ? fl : alpha_
alpha = alpha_ < sl ? sl : iff_3
mama = 0.0
mama := alpha * src + (1 - alpha) * nz(mama[1])
mama

t3(src, len) =>


xe1_1 = ta.ema(src, len)
xe2_1 = ta.ema(xe1_1, len)
xe3_1 = ta.ema(xe2_1, len)
xe4_1 = ta.ema(xe3_1, len)
xe5_1 = ta.ema(xe4_1, len)
xe6_1 = ta.ema(xe5_1, len)
b_1 = 0.7
c1_1 = -b_1 * b_1 * b_1
c2_1 = 3 * b_1 * b_1 + 3 * b_1 * b_1 * b_1
c3_1 = -6 * b_1 * b_1 - 3 * b_1 - 3 * b_1 * b_1 * b_1
c4_1 = 1 + 3 * b_1 + b_1 * b_1 * b_1 + 3 * b_1 * b_1
nT3Average_1 = c1_1 * xe6_1 + c2_1 * xe5_1 + c3_1 * xe4_1 + c4_1 * xe3_1
nT3Average_1

// The general form of the weights of the (2m + 1)-term Henderson Weighted Moving
Average
getWeight(m, j) =>
numerator = 315 * (math.pow(m + 1, 2) - math.pow(j, 2)) * (math.pow(m + 2, 2) -
math.pow(j, 2)) * (math.pow(m + 3, 2) - math.pow(j, 2)) * (3 * math.pow(m + 2, 2) -
11 * math.pow(j, 2) - 16)
denominator = 8 * (m + 2) * (math.pow(m + 2, 2) - 1) * (4 * math.pow(m + 2, 2)
- 1) * (4 * math.pow(m + 2, 2) - 9) * (4 * math.pow(m + 2, 2) - 25)

denominator != 0 ? numerator / denominator : 0

hwma(src, termsNumber) =>


sum = 0.0
weightSum = 0.0

termMult = (termsNumber - 1) / 2

for i = 0 to termsNumber - 1 by 1
weight = getWeight(termMult, i - termMult)
sum += nz(src[i]) * weight
weightSum += weight
weightSum

sum / weightSum

//Functions from @JayRogers / CryptoNTez / Me :)


variant(type, src, len, lsmaOffset, almaOffset, almaSigma) =>
v1 = ta.sma(src, len) // Simple
v2 = ta.ema(src, len) // Exponential
v3 = 2 * v2 - ta.ema(v2, len) // Double Exponential
v4 = 3 * (v2 - ta.ema(v2, len)) + ta.ema(ta.ema(v2, len), len) // Triple
Exponential
v5 = ta.wma(src, len) // Weighted
v6 = ta.vwma(src, len) // Volume Weighted
sma_1 = ta.sma(src, len) // Smoothed
v7 = na(v5[1]) ? sma_1 : (v5[1] * (len - 1) + src) / len
v8 = ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len),
math.round(math.sqrt(len))) // Hull
v9 = ta.linreg(src, len, lsmaOffset) // Least Squares
v10 = ta.alma(src, len, almaOffset, almaSigma) // Arnaud Legoux
v11 = kama(src, len) // KAMA
v12 = mama(src, len) // MAMA
ema1 = ta.ema(src, len)
ema2 = ta.ema(ema1, len)
v13 = t3(src, len) // T3
v14 = ema1 + ema1 - ema2 // Zero Lag Exponential
v15 = hwma(src, len) // Henderson Moving average thanks to @everget
ahma = 0.0
ahma := nz(ahma[1]) + (src - (nz(ahma[1]) + nz(ahma[len])) / 2) / len //Ahrens
Moving Average
v16 = ahma

type == 'EMA' ? v2 : type == 'DEMA' ? v3 : type == 'TEMA' ? v4 : type ==


'WMA' ? v5 : type == 'VWMA' ? v6 : type == 'SMMA' ? v7 : type == 'Hull' ? v8 : type
== 'LSMA' ? v9 : type == 'ALMA' ? v10 : type == 'KAMA' ? v11 : type == 'MAMA' ? v12
: type == 'T3' ? v13 : type == 'ZEMA' ? v14 : type == 'HWMA' ? v15 : type == 'AHMA'
? v16 : v1

// ----- Main Logic ----- //


// ----- RSI
rsi = ta.rsi(rsiSource, rsiLength)
lineOverbought = hline(rsiOverbought, color=color.red,linestyle=hline.style_solid,
linewidth=2, title='Overbought Level 2')
lineOversold = hline(rsiOversold, color=color.red,
linestyle=hline.style_solid,linewidth=2, title='Oversold Level 2')
lineBullish = hline(rsiOverbought1, color=color.red, linestyle=hline.style_dashed,
title='Overbought Level 1')
lineBearish = hline(rsiOversold1, color=color.red, linestyle=hline.style_dashed,
title='Oversold Level 1')
lineMid = hline(50, color=color.black, title='Mid Line')
fill(lineOversold, lineBearish, color = #a5a9ab86, transp = 60, editable = false)
fill(lineOverbought, lineBullish, color = #a5a9ab86, transp = 60, editable = false)
// ----- RSI/EMA
ema_base = variant(rsiSignalType, rsi, rsiSignalLen, lsmaOffset2, almaOffset2,
almaSigma2)

// ----- BB Calculation
bb_basis = variant(bb_type, rsi, bb_length, lsmaOffset, almaOffset, almaSigma)
bb_dev = bb_std * ta.stdev(rsi, bb_length)
bb_const = rsi * bb_plus / 100
bb_upper = bb_basis + bb_dev + bb_const
bb_lower = bb_basis - bb_dev - bb_const

bbUpPlot = plot(bb_upper, 'Upper Band', color=color.new(color.teal, 0),


linewidth=1,editable = false)
bbLowPlot = plot(bb_lower, 'Lower Band', color=color.new(color.teal, 0),
linewidth=1,editable = false)
midColor = bb_basis < bb_basis[1] ? color.red : color.lime
bbMidPlot = plot(bb_basis, 'Mid Band', color=midColor, linewidth=4,
transp=45,editable = false)

//fillBandsTop = fill(bbUpPlot, bbMidPlot, color=#C0C0C0, title="Fill Bands Top",


transp=70)
//fillBandsBot = fill(bbLowPlot, bbMidPlot, color=#C0C0C0, title="Fill Bands
Bottom", transp=70)

// ----- Sharkfins / Hoooks


rsiUp = rsi + 7
rsiDown = rsi - 7
shark = input(false, 'Show Sharkfins?')

// Sharkfin Breakout out of the Bands


plotshape(shark ? bb_upper < rsi and rsiOverbought < rsi ? rsiUp : na : na,
title='SharkFin/Hook Short+', color=color.new(color.red, 15),
style=shape.triangledown, location=location.top, size=size.tiny,editable = false)
plotshape(shark ? bb_upper < rsi and rsiOverbought > rsi ? rsiUp : na : na,
title='SharkFin/Hook Short+', color=color.new(color.orange, 15),
style=shape.triangledown, location=location.top, size=size.tiny,editable = false)

plotshape(shark ? bb_lower > rsi and rsiOversold > rsi ? rsiDown : na : na,
title='SharkFin/Hook Long+', color=color.new(color.lime, 15),
style=shape.triangleup, location=location.bottom, size=size.tiny,editable = false)
plotshape(shark ? bb_lower > rsi and rsiOversold < rsi ? rsiDown : na : na,
title='SharkFin/Hook Long+', color=color.new(color.orange, 15),
style=shape.triangleup, location=location.bottom, size=size.tiny,editable = false)

sharkCross = input(true, 'Show Sharkfin Cross Backs ?')


// BackIntoTheBand
crossover_1 = ta.crossover(bb_upper, rsi)
plotshape(sharkCross ? crossover_1 and rsiOverbought < rsi ? bb_upper : na : na,
title='SharkFin/Hook Cross Short+ (Oversold)', color=color.new(color.red, 15),
style=shape.xcross, location=location.absolute, size=size.small,editable = false)
crossover_2 = ta.crossover(bb_upper, rsi)
plotshape(sharkCross ? crossover_2 and rsiOverbought > rsi ? bb_upper : na : na,
title='SharkFin/Hook Cross Short+ (Not Oversold)', color=color.new(color.orange,
15), style=shape.xcross, location=location.absolute, size=size.small,editable =
false)

crossunder_1 = ta.crossunder(bb_lower, rsi)


plotshape(sharkCross ? crossunder_1 and rsiOversold > rsi ? bb_lower : na : na,
title='SharkFin/Hook Cross Long+ (Overbought) ', color=color.new(color.lime, 15),
style=shape.xcross, location=location.absolute, size=size.small,editable = false)
crossunder_2 = ta.crossunder(bb_lower, rsi)
plotshape(sharkCross ? crossunder_2 and rsiOversold < rsi ? bb_lower : na : na,
title='SharkFin/Hook Cross Long+ (Not Overbought)', color=color.new(color.orange,
15), style=shape.xcross, location=location.absolute, size=size.small,editable =
false)

//MA Cross
maCross = input(true, 'Show RSI / MA Cross?')
crossunder_3 = ta.crossunder(rsi, ema_base)
plotshape(maCross ? crossunder_3 and rsi < rsiOversold1 ? rsi : na : na,
title='Signal MA Cross Long+', color=color.new(color.lime, 15), style=shape.circle,
location=location.absolute, size=size.tiny,editable = false)
crossunder_4 = ta.crossunder(rsi, ema_base)
plotshape(maCross ? crossunder_4 and rsi > rsiOverbought1 ? rsi : na : na,
title='Signal MA Cross Short+', color=color.new(color.red, 15), style=shape.circle,
location=location.absolute, size=size.tiny,editable = false)

//Mid Cross
senCross = input(true, 'Show MA / Sentiment Cross?')
crossover_3 = ta.crossover(ema_base, bb_basis)
plotshape(senCross ? crossover_3 ? bb_basis : na : na, title='Sentiment Cross
Long+', color=color.new(color.lime, 15), style=shape.circle,
location=location.absolute, size=size.small,editable = false)
crossunder_5 = ta.crossunder(ema_base, bb_basis)
plotshape(senCross ? crossunder_5 ? bb_basis : na : na, title='Sentiment Cross
Short+', color=color.new(color.red, 15), style=shape.circle,
location=location.absolute, size=size.small,editable = false)

plot(ema_base, color=color.new(#fe0101, 3), linewidth=1, title='TDI Signal


Line',editable = false)

//RSI PLOT
rsiColor = rsi > rsiOverbought ? color.lime : rsi < rsiOversold ? color.red :
color.black
rsiPlot = plot(rsi, color=rsiColor, linewidth=3, title='RSI', transp=0)
plot(ta.ema(rsi, 2), color=rsiColor, linewidth=1, title='RSI Shadow 2',
transp=35,editable = false)
plot(ta.ema(rsi, 3), color=rsiColor, linewidth=1, title='RSI Shadow 3',
transp=55,editable = false)

/////////////////////////////////////////////////////////////

// Input parameters
relativeStrengthIndexLength = input.int(14, title="RSI Length", group="RSI
Settings")
smoothingLength = input.int(21, title="RSI Smoothing Length", group="RSI Settings")
rsiInputSource = input.source(close, title="RSI Source", group="RSI Settings")
isSmoothed = input.bool(false, "Smooth RSI?", group="RSI Settings")

movingAverageLength = input.int(14, title="MA Length", group="MA Settings", display


= display.data_window)
movingAverageType = input.string("HMA", title="MA Type", options=["SMA", "HMA",
"EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings", display =
display.data_window)
showMovingAverage = input.bool(true, "Show RSI MA?", group="MA Settings")

trendFactor = input.float(0.8, title="Factor", group="Super Trend Settings")


averageTrueRangeLength = input.int(10, title="ATR Length", group="Super Trend
Settings")

colorUp = input.color(#211afc, title="Up Color")


colorDown = input.color(#ff0505, title="Down Color")

// MA Selection
calculateMA(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"HMA" => ta.hma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)

// Function to calculate ATR


calculateATR(source, atrLength) =>
highestHigh = ta.highest(source, atrLength)
lowestLow = ta.lowest(source, atrLength)
trueRange = na(highestHigh[1]) ? highestHigh - lowestLow : math.max(highestHigh
- lowestLow, math.abs(highestHigh - source[1]), math.abs(lowestLow - source[1]))
ta.rma(trueRange, atrLength)

// Function to calculate Supertrend


calculateSupertrend(factor, atrPeriod, source) =>
priceSource = source
atr = calculateATR(source, atrPeriod)
upperBand = priceSource + factor * atr
lowerBand = priceSource - factor * atr
prevLowerBand = nz(lowerBand[1])
prevUpperBand = nz(upperBand[1])
lowerBand := lowerBand > prevLowerBand or source[1] < prevLowerBand ? lowerBand
: prevLowerBand
upperBand := upperBand < prevUpperBand or source[1] > prevUpperBand ? upperBand
: prevUpperBand
int trendDirection = na
float supertrendValue = na
prevSupertrend = supertrendValue[1]
if na(atr[1])
trendDirection := 1
else if prevSupertrend == prevUpperBand
trendDirection := source > upperBand ? -1 : 1
else
trendDirection := source < lowerBand ? 1 : -1
supertrendValue := trendDirection == -1 ? lowerBand : upperBand
[supertrendValue, trendDirection]

// Calculating RSI
rsiValue = isSmoothed ? ta.hma(ta.rsi(rsiInputSource, relativeStrengthIndexLength),
smoothingLength) : ta.rsi(rsiInputSource, relativeStrengthIndexLength)
rsiMovingAverage = calculateMA(rsiValue, movingAverageLength, movingAverageType)

// Calculating Supertrend based on RSI values


[rsiSupertrend, trendDirection] = calculateSupertrend(trendFactor,
averageTrueRangeLength, rsiValue)

// Plotting
supertrendPlot = plot(rsiSupertrend, color = trendDirection == -1 ? colorUp :
colorDown, title="Supertrend",display = display.none,editable = false)
rsiPlota = plot(rsiValue, color = color.new(color.gray, 100), title="RSI",display =
display.none,editable = false)
overboughtLine = plot(70, color = color.from_gradient(rsiValue, 50, 70,
color.new(color.gray, 70), color.new(colorDown, 0)), title="Overbought Line",
linewidth = 3,display = display.none,editable = false)
oversoldLine = plot(30, color = color.from_gradient(rsiValue, 30, 50,
color.new(colorUp, 0), color.new(color.gray, 70)), title="Oversold Line", linewidth
= 3,display = display.none,editable = false)
plot(rsiMovingAverage, color = color.gray, title = "RSI MA", display =
showMovingAverage ? display.all : display.none, linewidth = 1,display =
display.none,editable = false)
plot(50, color = color.gray, title="Mid Line",display = display.none,editable =
false)

// Filling
fill(overboughtLine, oversoldLine, color = color.new(color.gray, 90),
title="Overbought/Oversold Fill",display = display.none,editable = false)
fill(supertrendPlot, rsiPlota, color = trendDirection == 1 ? color.new(colorDown,
90) : color.new(colorUp, 90), title="Trend Fill",display = display.none,editable =
false)
fill(supertrendPlot, rsiPlota, rsiValue, rsiSupertrend, color.new(chart.bg_color,
1000), trendDirection == 1 ? colorDown : colorUp,display = display.none,editable =
false)

// Char plotting for crossover and crossunder


plotshape(ta.crossover(rsiSupertrend, rsiValue) and rsiSupertrend > 70 ? 85 : na,
style=shape.triangledown, location=location.absolute, color=colorDown,
size=size.tiny, title="Crossover Down",editable = false)
plotshape(ta.crossunder(rsiSupertrend, rsiValue) and rsiSupertrend < 30 ? 15 : na,
style=shape.triangleup, location=location.absolute, color=colorUp, size=size.tiny,
title="Crossunder Up",editable = false)

You might also like