Code: Select all
//@version=4
//Credits to Glaz and Shizaru for their QQE indicator code.
study("Didi Index Improved with QQE | jh")
src = input(title="Source", type=input.source, defval=close)
curtaLen = input(title="Curta (Short) Length", type=input.integer, defval=3)
mediaLen = input(title="Media (Medium) Length", type=input.integer, defval=8)
longaLen = input(title="Longa (Long) Length", type=input.integer, defval=20) //20
ma_type = input(title="Didi Index MA", type=input.string, defval="SMA", options=["ALMA", "EMA", "WMA", "SMA", "SMMA", "HMA"])
alma_offset = input(defval=0.85,type=input.float, title="* Arnaud Legoux (ALMA) Only - Offset Value", minval=0, step=0.01)
alma_sigma = input(defval=6, title="* Arnaud Legoux (ALMA) Only - Sigma Value", minval=0)
show_filling = input(true, title="Show Filling", type=input.bool)
//QQE Trend Line
useQQE = input(title="QQE 1 or 2", type=input.integer, defval=1, options=[1,2])
signal_len = input(defval=14,type=input.integer, title="Signal Length")
smoothening = input(defval=5,type=input.integer, title="Smoothening Length")
qqe_factor = input(defval=4.236,type=input.float, title="QQE Factor")
ma(type, src, len) =>
float result = 0
if type=="SMA" // Simple
result := sma(src, len)
if type=="EMA" // Exponential
result := ema(src, len)
if type=="WMA" // Weighted
result := wma(src, len)
if type=="SMMA" // Smoothed
w = wma(src, len)
result := na(w[1]) ? sma(src, len) : (w[1] * (len - 1) + src) / len
if type=="HMA" // Hull
result := wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len)))
if type=="ALMA" // Arnaud Legoux
result := alma(src, len, alma_offset, alma_sigma)
result
//Credits to Shizaru
qqeLine(src, len, sf, qqe) =>
wildersper = len*2-1
rsima = ema(src,sf)
atr = abs(rsima[1]-rsima)
maatrrsi = ema(atr,wildersper)
dar = ema(maatrrsi,wildersper)*qqe
trr = 0.0
trr := rsima>nz(trr[1])?((rsima-dar)<trr[1]?trr[1]:(rsima-dar)):((rsima+dar)>trr[1]?trr[1]:(rsima+dar))
//Credits to Glaz
qqeLine2(src, len, sf, qqe) =>
Wilders_Period = len*2-1
RsiMa = ema(src,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
trend := cross(RSIndex, shortband[1])?1:cross(longband[1], RSIndex)?-1:nz(trend[1],1)
FastAtrRsiTL = trend==1? longband: shortband
FastAtrRsiTL_plot = FastAtrRsiTL
media = ma(ma_type, src, mediaLen)
curta = (ma(ma_type,src, curtaLen) - media)
longa = (ma(ma_type, src, longaLen) - media)
plot(0, title="Media", color=color.gray, transp=0)
A = plot(curta, title="Curta", color=color.green, transp=0, linewidth=1)
B = plot(longa, title="Longa", color=color.red, transp=0, linewidth=1)
trendColor = show_filling ? (curta > longa ? color.lime : color.orange) : na
fill(A, B, color=trendColor, transp=80)
qqe_line = useQQE == 1 ? qqeLine (curta, signal_len, smoothening, qqe_factor) : qqeLine2 (curta, signal_len, smoothening, qqe_factor)
qqeColor = qqe_line > 0 ? color.lime : color.orange
plot(qqe_line, color=qqeColor, linewidth=2, transp=20, style=plot.style_cross)
c_qqeline_cross_Long = crossover(curta, qqe_line)
c_qqeline_cross_Short = crossover(qqe_line, curta)
//Signals based on crossover
c_cross_Long = crossover(curta, longa)
c_cross_Short = crossover(longa, curta)
//Signals based on signal position
c_trend_Long = curta > longa ? 1 : 0
c_trend_Short = longa > curta ? 1 : 0
confirm_Long = c_cross_Long
confirm_Short = c_cross_Short
long_direction_check = (confirm_Long and (curta > 0 and longa > 0) and (curta > longa))
plotshape(long_direction_check, color = color.green, style=shape.triangleup, location=location.top, transp=65)
false_long_direction_check = (confirm_Long and ( not (curta > 0 and longa > 0) ) and (curta > longa))
plotshape(false_long_direction_check, color = color.green, style=shape.xcross, location=location.top, transp=65)
short_direction_check = (confirm_Short and (curta < 0 and longa < 0) and (curta < longa))
plotshape(short_direction_check, color = color.red, style=shape.triangledown, location=location.top, transp=65)
false_short_direction_check = (confirm_Short and ( not (curta < 0 and longa < 0)) and (curta < longa))
plotshape(false_short_direction_check, color = color.red, style=shape.xcross, location=location.top, transp=65)
plotshape(c_qqeline_cross_Long and c_trend_Long, color = color.green, style=shape.triangleup, location=location.bottom, transp=65)
plotshape(c_qqeline_cross_Short and c_trend_Short, color = color.red, style=shape.triangledown, location=location.bottom, transp=65)
plotshape(c_qqeline_cross_Long and c_trend_Short, color = color.green, style=shape.xcross, location=location.bottom, transp=65)
plotshape(c_qqeline_cross_Short and c_trend_Long, color = color.red, style=shape.xcross, location=location.bottom, transp=65)