Smart QQE Mod
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © traderharikrishna

//@version=5
indicator("Smart QQE",overlay=true)
// Credits to Glaz
// Rescaled for overlay by @traderharikrishna
// Code By Glaz
// Modifications:
// Added Columns to show when signal is outside of Thresh Hold Channnel.
// Set default Parameters to match QQE Cross Alert indicator.
//


RSI_Period = input(14, title='RSI Length')
SF = input(5, title='RSI Smoothing')
QQE = input(4.238, title='Fast QQE Factor')
ThreshHold = input(10, title='Thresh-hold')

//
sQQEx = input(false, title='Show Smooth RSI, QQE Signal crosses')
sQQEz = input(false, title='Show Smooth RSI Zero crosses')
sQQEc = input(false, title='Show Smooth RSI Thresh Hold Channel Exits')
ma_type = input.string(title='MA Type', defval='EMA', options=['ALMA', 'EMA', 'DEMA', 'TEMA', 'WMA', 'VWMA', 'SMA', 'SMMA', 'HMA', 'LSMA', 'PEMA'])
lsma_offset = input.int(defval=0, title='* Least Squares (LSMA) Only - Offset Value', minval=0)
alma_offset = input.float(defval=0.85, title='* Arnaud Legoux (ALMA) Only - Offset Value', minval=0, step=0.01)
alma_sigma = input.int(defval=6, title='* Arnaud Legoux (ALMA) Only - Sigma Value', minval=0)
inpDrawBars = input(true, title='color bars?')


ma(type, src, len) =>
    float result = 0
    if type == 'SMA'  // Simple
        result := ta.sma(src, len)
        result
    if type == 'EMA'  // Exponential
        result := ta.ema(src, len)
        result
    if type == 'DEMA'  // Double Exponential
        e = ta.ema(src, len)
        result := 2 * e - ta.ema(e, len)
        result
    if type == 'TEMA'  // Triple Exponential
        e = ta.ema(src, len)
        result := 3 * (e - ta.ema(e, len)) + ta.ema(ta.ema(e, len), len)
        result
    if type == 'WMA'  // Weighted
        result := ta.wma(src, len)
        result
    if type == 'VWMA'  // Volume Weighted
        result := ta.vwma(src, len)
        result
    if type == 'SMMA'  // Smoothed
        w = ta.wma(src, len)
        result := na(w[1]) ? ta.sma(src, len) : (w[1] * (len - 1) + src) / len
        result
    if type == 'HMA'  // Hull
        result := ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len)))
        result
    if type == 'LSMA'  // Least Squares
        result := ta.linreg(src, len, lsma_offset)
        result
    if type == 'ALMA'  // Arnaud Legoux
        result := ta.alma(src, len, alma_offset, alma_sigma)
        result
    if type == 'PEMA'
        // Copyright (c) 2010-present, Bruno Pio
        // Copyright (c) 2019-present, Alex Orekhov (everget)
        // Pentuple Exponential Moving Average script may be freely distributed under the MIT license.
        ema1 = ta.ema(src, len)
        ema2 = ta.ema(ema1, len)
        ema3 = ta.ema(ema2, len)
        ema4 = ta.ema(ema3, len)
        ema5 = ta.ema(ema4, len)
        ema6 = ta.ema(ema5, len)
        ema7 = ta.ema(ema6, len)
        ema8 = ta.ema(ema7, len)
        pema = 8 * ema1 - 28 * ema2 + 56 * ema3 - 70 * ema4 + 56 * ema5 - 28 * ema6 + 8 * ema7 - ema8
        result := pema
        result
    result

src = input(close, title='RSI Source')
//

//
Wilders_Period = RSI_Period * 2 - 1


Rsi = ta.rsi(src, RSI_Period)
RsiMa = ma(ma_type, Rsi, SF)
AtrRsi = math.abs(RsiMa[1] - RsiMa)
MaAtrRsi = ma(ma_type, AtrRsi, Wilders_Period)
dar = ma(ma_type, MaAtrRsi, Wilders_Period) * QQE

longband = 0.0
shortband = 0.0
trendx = 0

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

//
// Find all the QQE Crosses
QQExlong = 0
QQExlong := nz(QQExlong[1])
QQExshort = 0
QQExshort := nz(QQExshort[1])
QQExlong := FastAtrRsiTL < RSIndex ? QQExlong + 1 : 0
QQExshort := FastAtrRsiTL > RSIndex ? QQExshort + 1 : 0
// Zero cross
QQEzlong = 0
QQEzlong := nz(QQEzlong[1])
QQEzshort = 0
QQEzshort := nz(QQEzshort[1])
QQEzlong := sQQEz and RSIndex >= 50 ? QQEzlong + 1 : 0
QQEzshort := sQQEz and RSIndex < 50 ? QQEzshort + 1 : 0
//  
// Thresh Hold channel Crosses give the BUY/SELL alerts.
QQEclong = 0
QQEclong := nz(QQEclong[1])
QQEcshort = 0
QQEcshort := nz(QQEcshort[1])
QQEclong := sQQEc and RSIndex > 50 + ThreshHold ? QQEclong + 1 : 0
QQEcshort := sQQEc and RSIndex < 50 - ThreshHold ? QQEcshort + 1 : 0


hcolor = RsiMa - 50 > ThreshHold ? color.green : RsiMa - 50 < 0 - ThreshHold ? color.red : color.orange

//EOF

bgc = RsiMa - 50 > ThreshHold ? color.rgb(7, 196, 243) : Rsi - 50 < 0 - ThreshHold ? color.rgb(247, 4, 117) : color.orange
barcolor(inpDrawBars ? bgc : na)



rsima=input.int(50,'RSI 100 level' )
orsi=ta.rsi(close,RSI_Period)
adjrsi=close+ta.atr(100)*orsi/100
//rma=ta.ema(adjrsi,rsima)
rsimatype=input.string('SMA',options=['EMA','HMA','SMA','WMA','RMA','VWMA'],title='RSI MA Type')
rma=rsimatype=='RMA'?ta.rma(adjrsi,rsima):rsimatype=='HMA'?ta.hma(adjrsi,rsima):rsimatype=='SMA'?ta.sma(adjrsi,rsima):rsimatype=='WMA'?ta.wma(adjrsi,rsima):rsimatype=='VWMA'?ta.vwma(adjrsi,rsima):ta.ema(adjrsi,rsima)

rsi100=rma+ta.atr(100)*(50/10)
rsi0=rma-ta.atr(100)*(50/10)
r100=plot(rsi100,linewidth=4,color=color.red,title='RSI100')
r0=plot(rsi0,linewidth=4,color=color.rgb(1, 165, 70),title='RSI0')


rsi90=rma+ta.atr(100)*(30/10)
rsi10=rma-ta.atr(100)*(30/10)
r90=plot(rsi90,linewidth=2,color=color.red,title='RSI90')
r10=plot(rsi10,linewidth=2,color=color.rgb(1, 165, 70),title='RSI10')

fill(r100,r90,color=color.rgb(255, 82, 82, 79),title='Overbought')
fill(r10,r0,color=color.rgb(44, 248, 3, 79),title='Oversold')

r2=plot(rma,color=open>rma?#00ff00:open<rma?#ff0000:#f1e20e,title='RSI-50 Level',linewidth=2) //mid band
rsim=rma+ta.atr(100)*(RsiMa - 50)/10
frsim=rma+ta.atr(100)*(FastAtrRsiTL - 50)/10
o8=plot(rsim,  color=#62f70bf5, title='QQE RSI',linewidth=2) //rsima
o2=plot(frsim, title='Fast RSI',color=#f30303,linewidth=2) //fast rsima
bgc2 = RsiMa - 50 > FastAtrRsiTL - 50 ? #144e055e :   #8603035b 
fill(o8,o2,color=bgc2,title='QQE Trend')

//thu=rma+ta.atr(100)*10/10
//thl=rma-ta.atr(100)*10/10
//thup=plot(thu, title='Threshold+',color=#000000)
//thlo=plot(thl, title='Threshold-',color=#000000)
//fill(thup,thlo,color=color.rgb(14, 17, 46, 87),title='Threshold Band') //threshold fill


qqeLong = QQExlong == 1 ? frsim: na
qqeShort = QQExshort == 1 ? frsim: na

plotshape(qqeLong, title="QQE long", text="Long", textcolor=color.new(color.white,0), style=shape.labelup, location=location.belowbar, color=color.new(#216d1f, 0),   size=size.auto)
plotshape(qqeShort, title="QQE short", text="Short", textcolor=color.new(color.white,0), style=shape.labeldown, location=location.abovebar, color=#ff0000,  size=size.auto)


// QQE exit from Thresh Hold Channel
plotshape(sQQEc and QQEclong == 1 ? rsim : na, title='QQE XC Over Channel', style=shape.diamond, location=location.absolute, color=color.new(color.olive, 0), size=size.auto, offset=0)
plotshape(sQQEc and QQEcshort == 1 ? rsim : na, title='QQE XC Under Channel', style=shape.diamond, location=location.absolute, color=color.new(color.red, 0), size=size.auto, offset=0)
// QQE crosses
plotshape(sQQEx and QQExlong == 1 ? frsim : na, title='QQE XQ Cross Over', style=shape.circle, location=location.absolute, color=color.new(color.lime, 0), size=size.auto, offset=-1)
plotshape(sQQEx and QQExshort == 1 ? frsim : na, title='QQE XQ Cross Under', style=shape.circle, location=location.absolute, color=color.new(color.blue, 0), size=size.auto, offset=-1)
// Signal crosses zero line
plotshape(sQQEz and QQEzlong == 1 ? rsim : na, title='QQE XZ Zero Cross Over', style=shape.square, location=location.absolute, color=color.new(color.aqua, 0), size=size.auto, offset=0)
plotshape(sQQEz and QQEzshort == 1 ? rsim : na, title='QQE XZ Zero Cross Under', style=shape.square, location=location.absolute, color=color.new(color.fuchsia, 0), size=size.auto, offset=0)

alertcondition(qqeLong,title = 'SMART QQE LONG',message='SMART QQE LONG')
alertcondition(qqeShort,title = 'SMART QQE SHORT',message='SMART QQE SHORT')

show200ma=input.bool(false,'Show 200MA')
ma200=input.int(200,title='MovingAverage 200')
matype200=input.string('SMA',options=['EMA','HMA','SMA','WMA','RMA','VWMA'],title='MA 200 Type')
mao200=matype200=='RMA'?ta.rma(close,ma200):matype200=='HMA'?ta.hma(close,ma200):matype200=='SMA'?ta.sma(close,ma200):matype200=='WMA'?ta.wma(close,ma200):matype200=='VWMA'?ta.vwma(close,ma200):ta.ema(close,ma200)
plot(show200ma?mao200:na,title='MovingAverage 200',linewidth=3)


///////////////////////MA RIBBON   
showma=input.bool(false,'Show MA ribbon')
macoloro=input.color(color.new(#ff0000, 0),'MA Down Color')
macolorc=input.color(color.new(#62ff42, 0),'MA Up Color')
malen=input(60,'Moving Averge Length')
matype=input.string('HMA',options=['EMA','HMA','SMA','WMA','RMA','VWMA'],title='MA Type')
mao=matype=='RMA'?ta.rma(open,malen):matype=='HMA'?ta.hma(open,malen):matype=='SMA'?ta.sma(open,malen):matype=='WMA'?ta.wma(open,malen):matype=='VWMA'?ta.vwma(open,malen):ta.ema(open,malen)
mac=matype=='RMA'?ta.rma(close,malen):matype=='HMA'?ta.hma(close,malen):matype=='SMA'?ta.sma(close,malen):matype=='WMA'?ta.wma(close,malen):matype=='VWMA'?ta.vwma(close,malen):ta.ema(close,malen)
v=plot(showma?mao:na,color=macoloro,linewidth=1,display=display.none)
y=plot(showma?mac:na,color=macolorc,linewidth=1,display=display.none)
fill(v,y,color=mao>mac?macoloro:macolorc,title='MA Ribbon')
