//@version=5
indicator("QQE SMART2 Enhanced", overlay=true)
// Initialize trade variables
var float entryPrice = na
var float exitPrice = na
var float profitLoss = na
var bool inTrade = false
var string tradeSignal = ""
// Input parameters
RSI_Period = input.int(14, title='RSI Length')
SF = input.int(5, title='RSI Smoothing')
QQE = input.float(4.238, title='Fast QQE Factor')
ThreshHold = input.float(10, title="Thresh-hold")
// Plot color options
showColoring = input.bool(true, title="Enable Coloring")
colorAboveRising = input.color(color.green, title="Above Zero Rising
Color")
colorAboveFalling = input.color(color.red, title="Above Zero Falling
Color")
colorBelowRising = input.color(color.blue, title="Below Zero Rising
Color")
colorBelowFalling = input.color(color.orange, title="Below Zero Falling
Color")
// Define real-time color customization inputs
plot1ColorRising = input.color(color.green, title="Plot 1 Rising Color")
plot1ColorFalling = input.color(color.red, title="Plot 1 Falling Color")
plot2ColorRising = input.color(color.blue, title="Plot 2 Rising Color")
plot2ColorFalling = input.color(color.orange, title="Plot 2 Falling
Color")
plot3ColorRising = input.color(color.purple, title="Plot 3 Rising
Color")
plot3ColorFalling = input.color(color.yellow, title="Plot 3 Falling
Color")
// Fill options
enableFillBelow = input.bool(true, title="Enable Fill Below Zero")
colorFillBelowRising = input.color(color.new(color.blue, 90),
title="Fill Below Rising Color")
colorFillBelowFalling = input.color(color.new(color.orange, 90),
title="Fill Below Falling Color")
enableFillAbove = input.bool(true, title="Enable Fill Above Zero")
colorFillAboveRising = input.color(color.new(color.green, 90),
title="Fill Above Rising Color")
colorFillAboveFalling = input.color(color.new(color.red, 90),
title="Fill Above Falling Color")
// Bar Color Option
inpDrawBars = input.bool(true, title="Color Bars Based on Indicator") //
Declare inpDrawBars
// Define MA function
ma(type, src, len) =>
switch type
"SMA" => ta.sma(src, len)
"EMA" => ta.ema(src, len)
"DEMA" =>
e = ta.ema(src, len)
2 * e - ta.ema(e, len)
"TEMA" =>
e = ta.ema(src, len)
3 * (e - ta.ema(e, len)) + ta.ema(ta.ema(e, len), len)
"WMA" => ta.wma(src, len)
"VWMA" => ta.vwma(src, len)
"SMMA" =>
w = ta.wma(src, len)
sma_w = ta.sma(src, len) // Calculate SMA here
na(w[1]) ? sma_w : (w[1] * (len - 1) + src) / len
"HMA" =>
ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len),
math.round(math.sqrt(len)))
"LSMA" => ta.linreg(src, len, 0)
"ALMA" => ta.alma(src, len, 0.85, 6)
"PEMA" =>
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)
8 * ema1 - 28 * ema2 + 56 * ema3 - 70 * ema4 + 56 * ema5 -
28 * ema6 + 8 * ema7 - ema8
// Calculate QQE
src = close
Wilders_Period = RSI_Period * 2 - 1
Rsi = ta.rsi(src, RSI_Period)
RsiMa = ma("EMA", Rsi, SF)
AtrRsi = math.abs(RsiMa[1] - RsiMa)
MaAtrRsi = ma("EMA", AtrRsi, Wilders_Period)
dar = ma("EMA", MaAtrRsi, Wilders_Period) * QQE
// Initialize longband and shortband with type
var float longband = na
var float shortband = na
var int trend = na
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
// Calculate cross values separately
cross_1 = ta.cross(longband[1], RSIndex)
cross_to_shortband = ta.cross(RSIndex, shortband[1])
// Correctly assign values to trend with integer type
trend := na(trend[1]) ? na : (cross_to_shortband ? 1 : (cross_1 ? -1 :
trend[1]))
FastAtrRsiTL = trend == 1 ? longband : shortband
// Define buy/sell conditions
buyCondition = ta.cross(RsiMa, longband) and trend == -1
sellCondition = ta.cross(shortband, RSIndex) and trend == 1
// Track trades and calculate profit/loss
if (buyCondition and not inTrade)
entryPrice := close
inTrade := true
tradeSignal := "BUY"
exitPrice := na
profitLoss := na
if (sellCondition and inTrade)
exitPrice := close
profitLoss := exitPrice - entryPrice
inTrade := false
tradeSignal := "SELL"
// Optionally, handle the trade closure and record results
// Plot buy/sell signals
plotshape(series=buyCondition ? close : na, location=location.belowbar,
color=color.fuchsia, style=shape.triangleup, size=size.small, title="Buy
Signal")
plotshape(series=sellCondition ? close : na, location=location.abovebar,
color=color.red, style=shape.triangledown, size=size.small, title="Sell
Signal")
// Plot lines connecting buy and sell signals
var line buyLine = na
var line sellLine = na
if (buyCondition and not na(entryPrice))
if (not na(buyLine))
line.delete(buyLine)
buyLine := line.new(x1=bar_index, y1=entryPrice, x2=bar_index,
y2=entryPrice, color=color.fuchsia, width=2)
if (sellCondition and not na(entryPrice))
if (not na(sellLine))
line.delete(sellLine)
sellLine := line.new(x1=bar_index, y1=entryPrice, x2=bar_index,
y2=close, color=color.red, width=2)
// Create a small chart for trade results
var table tradeTable = table.new(position.bottom_right, 1, 3,
border_width=1)
// Update table with trade details
if (not na(entryPrice) and not inTrade)
table.cell(tradeTable, 0, 0, text="Buy Price: " +
str.tostring(entryPrice), bgcolor=color.green, text_color=color.white)
table.cell(tradeTable, 0, 1, text="Sell Price: " +
str.tostring(exitPrice), bgcolor=color.red, text_color=color.white)
table.cell(tradeTable, 0, 2, text="Profit/Loss: " +
str.tostring(profitLoss), bgcolor=profitLoss >= 0 ? color.green :
color.red, text_color=color.white)
// Define error handling
if (buyCondition and sellCondition)
var bool codeRedIssued = false
if (not codeRedIssued)
label.new(bar_index, high, "Code Red: Possible Trade Error",
color=color.red, textcolor=color.white, size=size.small)
codeRedIssued := true
// Define color logic function
getColor(val, prevVal, risingColor, fallingColor) =>
if val >= 0
val > prevVal ? risingColor : fallingColor
else
val > prevVal ? risingColor : fallingColor
// Define plot variables
plot1 = RsiMa - 50
plot2 = FastAtrRsiTL - 50
plot3 = RsiMa - 50 // Change this if a different third plot is used
// Plot lines with conditional color
p1 = plot(plot1, color=color.new(getColor(plot1, plot1[1],
plot1ColorRising, plot1ColorFalling), 0), title="Plot 1")
p2 = plot(plot2, color=color.new(getColor(plot2, plot2[1],
plot2ColorRising, plot2ColorFalling), 0), title="Plot 2")
p3 = plot(plot3, color=color.new(getColor(plot3, plot3[1],
plot3ColorRising, plot3ColorFalling), 0), title="Plot 3")
// Define fill colors based on conditions
colorFillBelow = (plot1 > plot2) ? colorFillBelowRising :
colorFillBelowFalling
colorFillAbove = (plot2 > plot3) ? colorFillAboveRising :
colorFillAboveFalling
// Apply fills only when plots are properly defined
fill(p1, p2, color=enableFillBelow ? colorFillBelow : na, title="Fill
Below")
fill(p2, p3, color=enableFillAbove ? colorFillAbove : na, title="Fill
Above")
// Plot Threshold Channels
hZero = hline(0, color=color.black, linestyle=hline.style_dashed,
linewidth=1)
hUpper = hline(ThreshHold, color=color.green,
linestyle=hline.style_dashed, linewidth=2)
hLower = hline(0 - ThreshHold, color=color.red,
linestyle=hline.style_dashed, linewidth=2)
fill(hUpper, hLower, color=color.new(color.gray, 80))
// Bar Color
bgc = RsiMa - 50 > ThreshHold ? color.green : RsiMa - 50 < 0 -
ThreshHold ? color.red : color.orange
barcolor(inpDrawBars ? bgc : na)