Trend Surfers - Momentum + ADX + EMA
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TrendSurfersSignals

// This script is a mix between the Lazybear Momentum indicator, ADX indicator and EMA.

// Histogram meaning:
// Green = The momentum is growing and the ADX is growing or above your set value
// Red = The momentum is growing on the down side and the ADX is growing or above your set value
// Orange = The market doesn't have enough momentum or the ADX is not growing or above your value (no trend)

// Background meaning:
// Blue = The price is above the EMA
// Purple = The price is under the EMA

// Cross color on 0 line:
// Dark = The market might be sideway still
// Light = The market is in a bigger move

//@version=5
indicator('Trend Surfers - Momentum + ADX + EMA', 'TS - MOMADXEMA',
     overlay=false)

// Momentum
length = input(20, title="BB Length", group = "Momentum")
mult = input(2.0,title="BB MultFactor", group = "Momentum")
lengthKC=input(20, title="KC Length", group = "Momentum")
multKC = input(1.5, title="KC MultFactor", group = "Momentum")

useTrueRange = input.bool(true, title="Use TrueRange (KC)", group = "Momentum")

// ADX
adxminval = input.int(20, minval=1, title="ADX min. value", group = "ADX", tooltip = "Determine if market is still trending. Higher number = bigger trend")
len = input.int(14, minval=1, title="DI Length", group = "ADX")
lensig = input.int(14, title="ADX Smoothing", minval=1, maxval=50, group = "ADX")
[diplus, diminus, adx] = ta.dmi(len, lensig)

//EMA 
emalen = input.int(5, minval=1, title="Length", group = "EMA")
src = input(close, title="Source", group = "EMA")
offset = input.int(title="Offset", defval=0, minval=-500, maxval=500, group = "EMA")
i_res = input.timeframe('D', "Resolution", group = "EMA")
out = ta.ema(src, emalen)
emaval = request.security(syminfo.tickerid, i_res, out)

// Calculate BB
source = close
basis = ta.sma(source, length)
dev = multKC * ta.stdev(source, length)
upperBB = basis + dev
lowerBB = basis - dev

// Calculate KC
ma = ta.sma(source, lengthKC)
range1 = useTrueRange ? ta.tr : (high - low)
rangema = ta.sma(range1, lengthKC)
upperKC = ma + rangema * multKC
lowerKC = ma - rangema * multKC

sqzOn  = (lowerBB > lowerKC) and (upperBB < upperKC)
sqzOff = (lowerBB < lowerKC) and (upperBB > upperKC)
noSqz  = (sqzOn == false) and (sqzOff == false)

val = ta.linreg(source  -  math.avg(math.avg(ta.highest(high, lengthKC), ta.lowest(low, lengthKC)),ta.sma(close,lengthKC)), lengthKC,0)

momupadxup = val > 0 and val > nz(val[1]) and (adx >= adx[1] or adx > adxminval)
momdownadxup = val < 0 and val < nz(val[1]) and (adx >= adx[1] or adx > adxminval)

// Plot
scolor = noSqz ? color.blue : sqzOn ? color.black : color.gray 
bcolor = momupadxup ? color.rgb(25, 255, 25) : momdownadxup ? color.rgb(255, 25, 25) : color.rgb(255, 140, 25)
bgcolor = close > emaval ? color.rgb(25, 25, 255, transp = 80) : close < emaval ? color.rgb(255, 25, 140, transp = 80) : color.yellow
bgcolor(bgcolor)
plot(val, color=bcolor, style=plot.style_histogram, linewidth=4)
plot(0, color=scolor, style=plot.style_cross, linewidth=2)

