The Namib Flow
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © greenmask9

//@version=4
study("The Namib Flow", overlay = true)


src = input(close, title="RSI Source", group = "Global")

//▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ Hull Suite

//INPUT
modeSwitch = input("Hma", title="Hull Variation", options=["Hma", "Thma", "Ehma"], group = "Hull Suite")
length = input(60, title="Length(180-200 for floating S/R , 55 for swing entry)", group = "Hull Suite")
lengthMult = input(6.0, title="Length multiplier (Used to view higher timeframes with straight band)", group = "Hull Suite")

useHtf = input(false, title="Show Hull MA from X timeframe? (good for scalping)", group = "Hull Suite")
htf = input("240", title="Higher timeframe", type=input.resolution, group = "Hull Suite")

switchColor = input(true, "Color Hull according to trend?", group = "Hull Suite")
candleCol = input(false,title="Color candles based on Hull's Trend?", group = "Hull Suite")
visualSwitch  = input(true, title="Show as a Band?", group = "Hull Suite")
thicknesSwitch = input(15, title="Line Thickness", group = "Hull Suite")
transpSwitch = input(40, title="Band Transparency",step=5, group = "Hull Suite")

//FUNCTIONS
//HMA
HMA(_src, _length) =>  wma(2 * wma(_src, _length / 2) - wma(_src, _length), round(sqrt(_length)))
//EHMA    
EHMA(_src, _length) =>  ema(2 * ema(_src, _length / 2) - ema(_src, _length), round(sqrt(_length)))
//THMA    
THMA(_src, _length) =>  wma(wma(_src,_length / 3) * 3 - wma(_src, _length / 2) - wma(_src, _length), _length)
    
//SWITCH
Mode(modeSwitch, src, len) =>
      modeSwitch == "Hma"  ? HMA(src, len) :
      modeSwitch == "Ehma" ? EHMA(src, len) : 
      modeSwitch == "Thma" ? THMA(src, len/2) : na

//OUT
_hull = Mode(modeSwitch, src, int(length * lengthMult))
HULL = useHtf ? security(syminfo.ticker, htf, _hull) : _hull
MHULL = HULL[0]
SHULL = HULL[2]


//COLOR
hullColor = switchColor ? (HULL > HULL[2] ? #00ff00 : #ff0000) : #ff9800

Fi1 = plot(MHULL, title="MHULL", color=hullColor, linewidth=thicknesSwitch, transp=50)
Fi2 = plot(visualSwitch ? SHULL : na, title="SHULL", color=hullColor, linewidth=thicknesSwitch, transp=50)
fill(Fi1, Fi2, title="Band Filler", color=hullColor, transp=transpSwitch)



//▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ Follow Line Indicator

BBperiod      = input(defval = 6,     title = "BB Period",    type = input.integer, minval = 1, group = "Follow Line")
BBdeviations  = input(defval = 1.00,     title = "BB Deviations",    type = input.float, minval = 0.1, step=0.05, group = "Follow Line")
UseATRfilter  = input(defval = true, title = "ATR Filter",  type = input.bool, group = "Follow Line")
ATRperiod     = input(defval = 5,     title = "ATR Period",    type = input.integer, minval = 1, group = "Follow Line")
hl            = input(defval = false, title = "Hide Labels",  type = input.bool, group = "Follow Line")
//
BBUpper=sma (close,BBperiod)+stdev(close, BBperiod)*BBdeviations
BBLower=sma (close,BBperiod)-stdev(close, BBperiod)*BBdeviations
//
TrendLine = 0.0
iTrend = 0.0
buy = 0.0
sell = 0.0
//
BBSignal = close>BBUpper? 1 : close<BBLower? -1 : 0
// 
if BBSignal == 1 and UseATRfilter == 1
    TrendLine:=low-atr(ATRperiod)
    if TrendLine<TrendLine[1] 
        TrendLine:=TrendLine[1]
if BBSignal == -1 and UseATRfilter == 1
    TrendLine:=high+atr(ATRperiod)
    if TrendLine>TrendLine[1]
        TrendLine:=TrendLine[1]
if BBSignal == 0 and UseATRfilter == 1
    TrendLine:=TrendLine[1]
//
if BBSignal == 1 and UseATRfilter == 0
    TrendLine:=low
    if TrendLine<TrendLine[1] 
        TrendLine:=TrendLine[1]
if BBSignal == -1 and UseATRfilter == 0
    TrendLine:=high
    if TrendLine>TrendLine[1]
        TrendLine:=TrendLine[1]
if BBSignal == 0 and UseATRfilter == 0
    TrendLine:=TrendLine[1]
//
iTrend:=iTrend[1]
if TrendLine>TrendLine[1] 
    iTrend:=1
if TrendLine<TrendLine[1] 
    iTrend:=-1
//
buy:=iTrend[1]==-1 and iTrend==1 ? 1 : na
sell:=iTrend[1]==1 and iTrend==-1? 1 : na
//
plot(TrendLine, color=iTrend > 0?color.blue:color.red ,style=plot.style_line,linewidth=2,transp=0,title="Trend Line") 
plotshape(buy == 1 and hl == false? TrendLine-atr(8) :na, text='💣', style= shape.labelup, location=location.absolute, color=color.blue, textcolor=color.white, offset=0, transp=0,size=size.auto)
plotshape(sell == 1 and hl == false ?TrendLine+atr(8):na, text='🔨', style=shape.labeldown, location=location.absolute, color=color.red, textcolor=color.white, offset=0, transp=0,size=size.auto)


//▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ QQE Mod

RSI_Period = input(6, title='RSI Length', group = "QQE Mod")
SF = input(5, title='RSI Smoothing', group = "QQE Mod")
QQE = input(3, title='Fast QQE Factor', group = "QQE Mod")
ThreshHold = input(3, title="Thresh-hold", group = "QQE Mod")
//


//

//
Wilders_Period = RSI_Period * 2 - 1


Rsi = rsi(src, RSI_Period)
RsiMa = ema(Rsi, SF)
AtrRsi = abs(RsiMa[1] - RsiMa)
MaAtrRsi = ema(AtrRsi, Wilders_Period)
dar = ema(MaAtrRsi, Wilders_Period) * QQE

longband = 0.0
shortband = 0.0
trend = 0

DeltaFastAtrRsi = dar
RSIndex = RsiMa
newshortband = RSIndex + DeltaFastAtrRsi
newlongband = RSIndex - DeltaFastAtrRsi
longband := RSIndex[1] > longband[1] and RSIndex > longband[1] ? 
   max(longband[1], newlongband) : newlongband
shortband := RSIndex[1] < shortband[1] and RSIndex < shortband[1] ? 
   min(shortband[1], newshortband) : newshortband
cross_1 = cross(longband[1], RSIndex)
trend := cross(RSIndex, shortband[1]) ? 1 : cross_1 ? -1 : nz(trend[1], 1)
FastAtrRsiTL = trend == 1 ? longband : shortband
////////////////////


q_length = input(50, minval=1, title="Bollinger Length", group = "QQE Mod")
mult = input(0.35, minval=0.001, maxval=5, step=0.1, title="BB Multiplier", group = "QQE Mod")
basis = sma(FastAtrRsiTL - 50, q_length)
dev = mult * stdev(FastAtrRsiTL - 50, q_length)
upper = basis + dev
lower = basis - dev
color_bar = RsiMa - 50 > upper ? #00c3ff : RsiMa - 50 < lower ? #ff0062 : color.gray


//
// Zero cross
QQEzlong = 0
QQEzlong := nz(QQEzlong[1])
QQEzshort = 0
QQEzshort := nz(QQEzshort[1])
QQEzlong := RSIndex >= 50 ? QQEzlong + 1 : 0
QQEzshort := RSIndex < 50 ? QQEzshort + 1 : 0
//  

Zero = hline(0, color=color.white, linestyle=hline.style_dotted, linewidth=1)

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

RSI_Period2 = input(6, title='RSI Length', group = "QQE Mod")
SF2 = input(5, title='RSI Smoothing', group = "QQE Mod")
QQE2 = input(1.61, title='Fast QQE2 Factor', group = "QQE Mod")
ThreshHold2 = input(3, title="Thresh-hold", group = "QQE Mod")

src2 = input(close, title="RSI Source")
//

//
Wilders_Period2 = RSI_Period2 * 2 - 1


Rsi2 = rsi(src2, RSI_Period2)
RsiMa2 = ema(Rsi2, SF2)
AtrRsi2 = abs(RsiMa2[1] - RsiMa2)
MaAtrRsi2 = ema(AtrRsi2, Wilders_Period2)
dar2 = ema(MaAtrRsi2, Wilders_Period2) * QQE2
longband2 = 0.0
shortband2 = 0.0
trend2 = 0

DeltaFastAtrRsi2 = dar2
RSIndex2 = RsiMa2
newshortband2 = RSIndex2 + DeltaFastAtrRsi2
newlongband2 = RSIndex2 - DeltaFastAtrRsi2
longband2 := RSIndex2[1] > longband2[1] and RSIndex2 > longband2[1] ? 
   max(longband2[1], newlongband2) : newlongband2
shortband2 := RSIndex2[1] < shortband2[1] and RSIndex2 < shortband2[1] ? 
   min(shortband2[1], newshortband2) : newshortband2
cross_2 = cross(longband2[1], RSIndex2)
trend2 := cross(RSIndex2, shortband2[1]) ? 1 : cross_2 ? -1 : nz(trend2[1], 1)
FastAtrRsi2TL = trend2 == 1 ? longband2 : shortband2


//
// Zero cross
QQE2zlong = 0
QQE2zlong := nz(QQE2zlong[1])
QQE2zshort = 0
QQE2zshort := nz(QQE2zshort[1])
QQE2zlong := RSIndex2 >= 50 ? QQE2zlong + 1 : 0
QQE2zshort := RSIndex2 < 50 ? QQE2zshort + 1 : 0
//  

Greenbar1 = RsiMa2 - 50 > ThreshHold2
Greenbar2 = RsiMa - 50 > upper

Redbar1 = RsiMa2 - 50 < 0 - ThreshHold2
Redbar2 = RsiMa - 50 < lower

//
// Bar Color

//▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬conditions

hull_green = HULL > HULL[2]
hull_red = HULL < HULL[2]

follow_green = iTrend > 0
follow_red = not follow_green

QQE_green = Greenbar1 and Greenbar2 == 1
QQE_red = Redbar1 and Redbar2 == 1

barcolor_green = close > open
barcolor_red = close < open

full_condition_long = hull_green and follow_green and QQE_green and barcolor_green
full_condition_short = hull_red and follow_red and QQE_red and barcolor_red
fcl = full_condition_long and not full_condition_long[1]
fcs = full_condition_short and not full_condition_short[1]

//filtering first three occurences

varip long_counter = 0, varip short_counter = 0

if fcl
    short_counter := 0
    if long_counter[1] == 0
        long_counter := 1
    if long_counter[1] == 1
        long_counter := 2
    if long_counter[1] == 2
        long_counter := 3
    if long_counter[1] == 3
        long_counter := 4
if fcs
    long_counter := 0
    if short_counter[1] == 0
        short_counter := 1
    if short_counter[1] == 1
        short_counter := 2
    if short_counter[1] == 2
        short_counter := 3
    if short_counter[1] == 3
        short_counter := 4

//plotshape(long_counter < 4, location = location.bottom)


plotshape(fcl and long_counter < 4, title = "Long", location = location.belowbar, size = size.small, color = color.yellow, style = shape.triangleup)
plotshape(fcs and short_counter < 4, title = "Short", location = location.abovebar, size = size.small, color = color.yellow, style = shape.triangledown)

alertcondition(fcl and long_counter < 4 , title = "Bullish Alert")
alertcondition(fcs and short_counter < 4 , title = "Bearish Alert")
alertcondition((fcl and long_counter < 4) or (fcs and short_counter < 4), title = "Any Alert")



