Momentum-based-ZigZag-incl-QQE-NON-REPAINTING for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
DEvX8K4.png

I finally able to convert it as best effort. In addition, I added more momentum indicator to select from.
added horizontal lines option instead of Zigzag lines . Trade safe :)

Update - 01/2024
  • CODE 1 : Updated the code with signals and some code fix.
  • CODE 2 : Modified Momentum QQE-ZigZag [Non Repaint During Candle Building].

CODE 1 : Momentum-based-ZigZag-incl-QQE-NON-REPAINTING
CSS:
#// https://www.tradingview.com/v/gY4ilk5N/
#// © Peter_O
#indicator('Momentum-based ZigZag', overlay=true)
# Converted and modified by Sam4Cok@Samer800 - 09/2022 (NON-REPAINTING)
# minor code fixing, bar color, signal and arrows - Sam4Cok@Samer800    - 01/2023
input colorBars = yes;
input ShowLabel = yes;
input ShowBubble = yes;
input showSignals = yes;
input color_zigzag_lines = yes;
input ForceLength = 5;
input rsiOverboughtLimit = 80;
input rsiOversoldLimit = 20;
input source = close;
input LineStyle = {Non,Default ZigZag, Lines, Both};
input momentum_select = {MACD, MovingAverage, default QQE, ImpulseMACD, WaveTrend};
input RSI_Period = 14;    # RSI Length if QQE selected
input qqeSlow = 4.238;    # QQE Factor
input qqeSmooth = 5;      # RSI Smoothing
input qqeThreshHold = 10; # Thresh-hold
input QQE_rsi_or_Stoch = {default RSI, Stoch};
input lengthStoch = 14;    # Stochastic Length
input macdMovAvg = {SMA, default EMA};    # Oscillator MA Type if MACD Selected
input macdSignalMA = {SMA, default EMA};    # Signal Line MA Type if MACD Selected
input macdFastLen = 12;     # MACD Fast Length
input macdSlowLen = 26;     # MACD Slow Length'
input signal_length = 9;    # MACD Signal Smoothing
input ImpulseMacdLen = 34;#
input MovAvgType = {default SMA, EMA, ZLEMA, SMMA, WMA, VWMA, HMA, RMA, DEMA};
input movAvgLen1 = 10;
input movAvgLen2 = 21;
input WTChannelLen = 10;
input WTAvgLen = 13;

AddLabel(ShowLabel, momentum_select, Color.WHITE);

def ZigZagLine = (LineStyle == LineStyle.ZigZag or LineStyle == LineStyle.Both);
def StyleLine = (LineStyle == LineStyle.Lines or LineStyle == LineStyle.Both) ;
def na = Double.NaN;
def mcd = momentum_select == momentum_select.MACD;
def movAvg = momentum_select == momentum_select.MovingAverage;
def impMacd = momentum_select == momentum_select.ImpulseMACD;
def wt = momentum_select == momentum_select.WaveTrend;
def qq = momentum_select == momentum_select.QQE;
### Color
DefineGlobalColor("Undefined", Color.DARK_GRAY);
DefineGlobalColor("Up", CreateColor(91, 156, 246));
DefineGlobalColor("Down", CreateColor(171, 71, 188));
#barssince(Condition) =>
script barssince {
    input Condition = 0;
    def barssince = if Condition then 0 else barssince[1] + 1;
    plot return = barssince;
}
# stoch(source, high, low, length) =>
script stoch {
    input src = close;
    input h = high;
    input l = low;
    input len = 0;
    def hh = Highest(h, len);
    def ll = Lowest(l, len);
    def stoch = 100 * (src - ll) / (hh - ll);
    plot return = stoch;
}
############## VWMA
#vwma(source, length)
script VWMA {
    input x = close;
    input y = 15;
    def nom = Average(x * volume, y);
    def dnom = Average(volume, y);
    def VWMA = nom / dnom;
    plot result = VWMA;
}
#"Impulse MACD [LazyBear]
#calc_smma(src, len) =>
script calc_smma {
input src = hlc3;
input len = 34;
    def smma = if smma[1]==0 then Average(src, len) else (smma[1] * (len - 1) + src) / len;
    plot return = smma;
}
#calc_zlema(src, length) =>
script calc_zlema {
input src = hlc3;
input length = 9;
    def ema1= ExpAverage(src, length);
    def ema2= ExpAverage(ema1, length);
    def d=ema1-ema2;
    def zlema = ema1+d;
    plot return = zlema;
}
#// ZigZag function {
#zigzag(_momentum_direction) =>
script zigzag {
    input _momentum_direction = 0;
    def zz_goingup = _momentum_direction == 1;
    def zz_goingdn = _momentum_direction ==-1;
    def zz_top;def zz_bot;
    def zztop = if isNaN(zz_top[1]) then 0 else zz_top[1];
    def zzbot = if isNaN(zz_bot[1]) then 0 else zz_bot[1];
        zz_top = if (high > zztop and zz_goingup or zz_goingdn[1] and zz_goingup) then high else zztop;
        zz_bot = if (low  < zzbot and zz_goingdn or zz_goingup[1] and zz_goingdn) then low  else zzbot;
    def zigzag = if (zz_goingup and zz_goingdn[1]) then zz_bot[1] else
                 if (zz_goingup[1] and zz_goingdn) then zz_top[1] else Double.NaN;
    plot zg = zigzag;
}
Script qqenew {
input QQE = 4.238;
input SF = 5;
input nrsi = 30;
input ThreshHold = 10;
input RSI_Period = 14;
    def Wilders_Period = RSI_Period * 2 - 1;
    def RsiMa = ExpAverage(nRsi, SF);
    def AtrRsi = AbsValue(RsiMa[1] - RsiMa);
    def MaAtrRsi = ExpAverage(AtrRsi, Wilders_Period);
    def dar = ExpAverage(MaAtrRsi, Wilders_Period) * QQE;
    def DeltaFastAtrRsi = dar;
    def RSIndex = RsiMa;
    def longband;def shortband;
    def newshortband = RSIndex + DeltaFastAtrRsi;
    def newlongband = RSIndex - DeltaFastAtrRsi;
    def longband1 = if longband[1]==0 then newlongband else longband[1];
    def shortband1 = if shortband[1]==0 then newshortband else shortband[1];
        longband = if RSIndex[1] > longband1 and RSIndex > longband1
                   then max(longband1, newlongband) else newlongband;
        shortband = if RSIndex[1] < shortband1 and RSIndex < shortband1
                    then min(shortband1, newshortband) else newshortband;
    def QQExlong;
    def QQExshort;
    def QQExlong1 = if isNaN(QQExlong[1]) then 0 else QQExlong[1];
    def QQExshort1 = if isNaN(QQExshort[1]) then 0 else QQExshort[1];
    def cntExLong = if QQExlong1 == 1 then 0 else cntExLong[1] + 1;
    def cntExShort = if QQExshort1 == 1 then 0 else cntExShort[1] + 1;
    def qqe_goingup = cntExLong < cntExShort;
    def qqe_goingdown = cntExLong > cntExShort;
    def last_qqe_high;
    def last_qqe_low;
    def lastqqehigh = if last_qqe_high[1]==0 then high else last_qqe_high[1];
    def lastqqelow = if last_qqe_low[1]==0 then low else last_qqe_low[1];
    last_qqe_high = if high > lastqqehigh and qqe_goingup or qqe_goingdown[1] and qqe_goingup then high else lastqqehigh;
    last_qqe_low = if low < lastqqelow and qqe_goingdown or qqe_goingup[1] and qqe_goingdown then low else lastqqelow;
    def rsiCrosUp = (RSIndex > shortband1) and (RSIndex[1] <= shortband1[1]);
    def hiCrosUp = (high > last_qqe_high) and (high[1] <= lastqqehigh);
    def rsiCrosDn = (RSIndex < longband1) and (RSIndex[1] >= longband1[1]);
    def loCrosDn = (low < last_qqe_low) and (low[1] >= lastqqelow);
    def trend = if trend[1]==0 then 1 else
                if (rsiCrosUp or hiCrosUp) then 1 else
                if (rsiCrosDn or loCrosDn) then -1 else trend[1];
    QQExlong = if trend == 1 and trend[1] == -1 then QQExlong1 + 1 else 0;
    QQExshort = if trend == -1 and trend[1] == 1 then QQExshort1 + 1 else 0;
    def qqenew = if QQExlong==1 then 1 else if QQExshort==1 then -1 else 0;
    plot out = qqenew;
}
#moving_average(_series, _length, _smoothing) =>
script moving_average {
    input _series = close;
    input _length = 0;
    input _smoothing = "SMA";
    def moving_average = if _smoothing == "EMA" then ExpAverage(_series, _length) else
        if _smoothing == "SMA" then Average(_series, _length) else
        if _smoothing == "WMA" then WMA(_series, _length) else
        if _smoothing == "SMMA" then calc_smma(_series, _length) else
        if _smoothing == "ZLEMA" then calc_zlema(_series, _length) else
        if _smoothing == "VWMA" then vwma(_series, _length) else
        if _smoothing == "HMA" then HullMovingAvg(_series, _length) else
        if _smoothing == "RMA" then WildersAverage(_series, _length) else
        if _smoothing == "DEMA" then DEMA(_series, _length) else ExpAverage(_series, _length);
    plot return = moving_average;
}
#/ MACD  {
def fast_ma = moving_average(source, macdFastLen, macdMovAvg);
def slow_ma = moving_average(source, macdSlowLen, macdMovAvg);
def macd = fast_ma - slow_ma;
def signal = moving_average(macd, signal_length, macdSignalMA);
def macdUP   = macd > signal and macd[1] < signal[1];
def macdDOWN = macd < signal and macd[1] > signal[1];
#// Impulse MACD
def hi = calc_smma(high, ImpulseMacdLen);
def lo = calc_smma(low, ImpulseMacdLen);
def mi = calc_zlema(hlc3, ImpulseMacdLen);
def md = if (mi>hi)then (mi-hi) else if (mi<lo) then (mi - lo) else 0;
def sb = Average(md, signal_length);
def impUP = md > sb and md[1] <= sb[1];
def impDN = md < sb and md[1] >= sb[1];
#// Moving Averages
def movAvg1 = moving_average(source, movAvgLen1, MovAvgType);
def movAvg2 = moving_average(source, movAvgLen2, MovAvgType);

def maUP = movAvg1 > movAvg2 and movAvg1[1] <= movAvg2[1];
def maDN = movAvg1 < movAvg2 and movAvg1[1] >= movAvg2[1];

#// End of Moving Averages
#\\ Wave trend
def ap = hlc3;
def esa = ExpAverage(ap, WTChannelLen);
def d = ExpAverage(AbsValue(ap - esa), WTChannelLen);
def ci = (ap - esa) / (0.015 * d);
def tci = ExpAverage(ci, WTAvgLen);
def wt1 = tci;
def wt2 = Average(wt1, 3);
def wtUP = (wt1 > wt2 and wt1[1] <= wt2[1]) and wt1 > 0;
def wtDN = (wt1 < wt2 and wt1[1] >= wt2[1]) and wt1 < 0;

#// QQE #####################
def nRsi = RSI(PRICE = source, LENGTH = RSI_Period);
def stoch = Average(stoch(nRsi, nRsi, nRsi, lengthStoch), 6);
def oscSrc = if QQE_RSI_or_Stoch == QQE_RSI_or_Stoch.RSI then nRsi else stoch;
def qqenew = qqenew(qqeslow, qqeSmooth, oscSrc, qqeThreshHold, RSI_Period);
def qqeUP = qqenew== 1;#QQElong;
def qqeDN = qqenew==-1;#QQEshort;

#// } End of QQE
def momentumUP = if mcd then macdUP else
                 if movAvg then maUP else
                 if impMacd then impUP else
                 if wt then wtUP else
                 if qq then qqeUP else no;
def momentumDN = if mcd then macdDOWN else
                 if movAvg then maDN else
                 if impMacd then impDN else
                 if wt then wtDN else
                 if qq then qqeDN else no;

def momentum_direction = if momentumUP then  1 else
                         if momentumDN then -1 else momentum_direction[1];

def Zigzag = zigzag(momentum_direction).zg;

#// Force detection
def rsi5 = RSI(Price = close, Length = ForceLength);
def ob = rsiOverboughtLimit;
def os = rsiOversoldLimit;
def barssince_momentumUP = barssince(momentumUP);
def barssince_momentumDN = barssince(momentumDN);
def barssince_rsi5up = barssince(rsi5 > ob);
def barssince_rsi5dn = barssince(rsi5 < os);
def rsi5up = barssince_momentumUP >= barssince_rsi5up;
def rsi5dn = barssince_momentumDN >= barssince_rsi5dn;
def momentum_DN_was_force_up = momentumDN and rsi5up[1];
def momentum_UP_was_force_dn = momentumUP and rsi5dn[1];
def zzcolor_rsi5 = if momentum_DN_was_force_up then 1 else
                   if momentum_UP_was_force_dn then -1 else 0;

def col = if zzcolor_rsi5>0 then  1 else
          if zzcolor_rsi5<0 then -1 else
          if !isNaN(zigzag) then 0 else col[1];

def GoShort = momentumDN and !momentum_DN_was_force_up;
def GoLong  = momentumUP and !momentum_UP_was_force_dn;

#-- Plots
plot zigLine = if Zigzag then Zigzag else na;
zigLine.AssignValueColor(if color_zigzag_lines then
                         if col[1]>0 then GlobalColor("Up") else
                         if col[1]<0 then GlobalColor("Down") else GlobalColor("Undefined") else GlobalColor("Undefined"));
zigLine.SetLineWeight(2);
zigLine.EnableApproximation();
zigLine.SetHiding(!ZigZagLine);

AddChartBubble(ShowBubble and GoShort, zigLine, "$" + Round(Zigzag, 2), GlobalColor("Down"), yes);
AddChartBubble(ShowBubble and GoLong, zigLine, "$" + Round(Zigzag, 2), GlobalColor("Up"), no);

#-- Signal and bar color
AssignPriceColor(if !colorBars then Color.CURRENT else
                 if col[1]>0 then GlobalColor("Up") else
                 if col[1]<0 then GlobalColor("Down") else GlobalColor("Undefined"));

plot long  = if (showSignals and zzcolor_rsi5[1] > 0) then low else na;
plot short = if (showSignals and zzcolor_rsi5[1] < 0) then high else na;

long.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
long.SetDefaultColor(GlobalColor("Up"));
long.SetLineWeight(2);
short.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
short.SetDefaultColor(GlobalColor("down"));
short.SetLineWeight(2);

# extend the current valley line to the right edge of the chart
def bottom = if GoLong then Zigzag else bottom[1];
plot bottomLine = bottom;
bottomLine.SetPaintingStrategy(PaintingStrategy.DASHES);
bottomLine.SetDefaultColor(GlobalColor("Up"));
bottomLine.SetHiding(!StyleLine);
# extend the current valley line to the right edge of the chart
def peak = if GoShort then Zigzag else peak[1];
plot peakLine = peak;
peakLine.SetPaintingStrategy(PaintingStrategy.DASHES);
peakLine.SetDefaultColor(GlobalColor("down"));
peakLine.SetHiding(!StyleLine);

###### END

CODE 2 : Modified Momentum QQE-ZigZag [Non Repaint During Candle Building].

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Hutmacher42
#indicator("Modified Momentum QQE-ZigZag [Non Repaint During Candle Building]", overlay = true, max_labels_count = 500)
# Converted by Sam4Cok@Samer800    - 01/2024
#// Input: RSI-QQE ZigZag
input colorDivConvBar = yes;
input showHiLoLabels = yes;
input showZigzagLine = yes;
input rsiType = {"Cutlers", default "Wilders"};    # "Rsi-Type"
input rsiLength = 14;                              # "Rsi-Len"
input rsiMulti = 2;                                # "Mtf-Mult"
input qqeFactor = 3.0;                                # "QQe-Mult"
input qqeSmoothing = 1;                            # "Smooth"
#// Input: RSI Divergence / Convergence
input rsiSource = close;                           # "Div-Src"
input NoOfPivotPoints = {default "1", "2", "3"};   # "PPs-Back"
input ShowDivergence = yes;                        # "Show-Divergence"
input divBottomLimit = 28;                         # "Div-BotLimit"
input divTopLimit = 70;                            # "Div-TopLimit"
input ShowConvergence = yes;                       # "Show-Convergence"
input convBottomLimit = 52.5;                      # "Conv-BotLimit"
input convTopLimit = 47.5;                         # "Conv-TopLimit"
#// Input: Supertrend
input ShowSupertrend = yes;                        # "Show-Supertrend"
input supertrendLength = 34;                       # "Ma-Len"
input MovAvgType = {"SMA", default "EMA", "HH / LL"};    # "Ma-Type"
input atrLength = 34;                              # "Atr-Len"
input atrFactor = 4.25;                            # "Atr-Mult"
input includeZigZagInSupertrendCalc = yes;         # "Include-ZigZag"

def na = Double.NaN;
def IncludeZigZag = includeZigZagInSupertrendCalc;
#-- Colors
DefineGlobalColor("l1_bull", CreateColor(76, 175, 80));
DefineGlobalColor("l1_bear", CreateColor(255, 82, 82));
DefineGlobalColor("l1_zz", CreateColor(91, 156, 246));
DefineGlobalColor("l1_pos", CreateColor(255, 235, 59));
DefineGlobalColor("l1_neg", Color.MAGENTA);
DefineGlobalColor("l2_pos", Color.CYAN);
DefineGlobalColor("l2_neg", CreateColor(255, 146, 0));

#//- Functions -//
#// Function: Modified MTF Relative Strength Index
script f_rsi {
    input _src = close;
    input _len = 14;
    input _mtf = 1;
    input _type = "Wilders";
    def wilder = _type == "Wilders";
    def _change = _src - _src[_mtf];
    def _up =  Max(_change, 0);
    def _dn = -Min(_change, 0);
    def _up_change = if wilder then WildersAverage(_up, (_len * _mtf)) else Average(_up, (_len * _mtf));
    def _dn_change = if wilder then WildersAverage(_dn, (_len * _mtf)) else Average(_dn, (_len * _mtf));
    def _rsi = if _dn_change == 0 then 100 else
               if _up_change == 0 then 0 else 100 - (100 / (1 + _up_change / _dn_change));
    plot out = _rsi;
}
#// Function: ZigZag
script f_zz {
    input _dir = 0;
    input _high = high;
    input _low = low;
    def _up = _dir == 1;
    def _dn = _dir == -1;
    def _peak;
    def _botm;
    def peak = if _peak[1]==0 then _high else _peak[1];
    def botm = if _botm[1]==0 then _low else _botm[1];
        _peak = if _high > peak and _up or _dn[1] and _up then _high else peak;
        _botm = if _low  < botm and _dn or _up[1] and _dn then _low  else botm;
    def _zz = if _up and _dn[1] then botm else
              if _up[1] and _dn then peak else Double.NaN;
    def _top = if _dn and _up[1] then peak else Double.NaN;
    def _bot = if _up and _dn[1] then botm else Double.NaN;
    plot zz = _zz;
    plot top = _top;
    plot bot = _bot;
}
#// Function: Modified QQE
Script f_qqe {
    input rsiLength = 14;
    input rsiMulti = 2;
    input _up_rsi = 70;
    input _dn_rsi = 30;
    input qqeSmoothing = 1;
    input qqeFactor = 4.25;
    def _wilders = rsiLength * 2 - 1;
    def _up_ema = ExpAverage(_up_rsi, qqeSmoothing);
    def _up_atr = AbsValue(_up_ema[1] - _up_ema);
    def _up_diff = ExpAverage(ExpAverage(_up_atr, _wilders), _wilders) * qqeFactor;
    def _up_ma = _up_ema - _up_diff;
    def _up;
    def up = if _up[1]==0 then _up_ma else _up[1];
       _up = if _up_ema[1] > up and _up_ema > up then Max(_up_ma, up) else _up_ma;
    def _dn_ema = ExpAverage(_dn_rsi, qqeSmoothing);
    def _dn_atr = AbsValue(_dn_ema[1] - _dn_ema);
    def _dn_diff = ExpAverage(ExpAverage(_dn_atr, _wilders), _wilders) * qqeFactor;
    def _dn_ma = _dn_ema + _dn_diff;
    def _dn;
    def dn = if _dn[1]==0 then _dn_ma else _dn[1];
       _dn = if _dn_ema[1] < dn and _dn_ema < dn then Min(_dn_ma, dn) else _dn_ma;
    def crossover  = (_dn_ema > dn) and (_dn_ema[1] <= dn[1]);
    def crossunder = (_up_ema < up) and (_up_ema[1] >= up[1]);
    def qq_trend = if crossover then 1 else
                   if crossunder then -1 else qq_trend[1];
    plot out = qq_trend;
}
#// Function: Supertrend
script f_supertrend {
    input _top = high;
    input _bot = low;
    input _incl = yes;
    input _len = 34;
    input _type = "EMA";
    input _atr_len = 34;
    input _atr_mult = 4.25;
    def _atr = ATR(Length = _atr_len) * _atr_mult;
    def _up_ma = if _type == "EMA" then ExpAverage(low[1], _len) else
                 if _type == "SMA" then Average(low[1], _len) else
                 if _type == "HH / LL" then Lowest(low[1], _len) else Double.NaN;
    def _up_b = if _incl then _bot[1] - _atr else Double.NaN;
    def _up1 = _up_ma - _atr;
    def _up;
    def PreUp = if _up[1]==0 then _up1 else _up[1];
        _up = if low[1] > PreUp then Max(_up1, Max(PreUp, If(IsNaN(_up_b), _up1 - 1, _up_b))) else _up1;
    def _dn_ma = if _type == "EMA" then ExpAverage(high[1], _len) else
                 if _type == "SMA" then Average(high[1], _len) else
                 if _type == "HH / LL" then Highest(high[1], _len) else Double.NaN;
    def _dn_t = if _incl then _top[1] + _atr else Double.NaN;
    def _dn1 = _dn_ma + _atr;
    def _dn;
    def PreDn = if _dn[1]==0 then _dn1 else _dn[1];
        _dn = if high[1] < PreDn then Min(_dn1, Min(PreDn, If(IsNaN(_dn_t), _dn1 + 1, _dn_t))) else _dn1;
    def crossover  = (high[1] crosses above PreDn);
    def crossunder = (low[1] crosses below PreUp);
    def _trend = if crossover  then  1 else
                 if crossunder then -1 else _trend[1];
    plot trend = _trend;
    plot up = _up;
    plot dn = _dn;
}
#//- Calculations -//
#// Calc: Rsi QQE ZigZag
def up_rsi = f_rsi(low, rsiLength, rsiMulti, rsiType);
def dn_rsi = f_rsi(high, rsiLength, rsiMulti, rsiType);
def qqe_trend = f_qqe(rsiLength, rsiMulti, up_rsi,dn_rsi, qqeSmoothing, qqeFactor);
def zz = f_zz(qqe_trend, high, low).zz;
def zztop = f_zz(qqe_trend, high, low).top;
def zzbot = f_zz(qqe_trend, high, low).bot;
def nzztop = !IsNaN(zztop);
def nzzbot = !IsNaN(zzbot);
def zz_top;def prev_top_1;def prev_top_2;def prev_top_3;
def zz_bot;def prev_bot_1;def prev_bot_2;def prev_bot_3;
if nzztop {
    prev_top_3 = prev_top_2[1];
    prev_top_2 = prev_top_1[1];
    prev_top_1 = zz_top[1];
    zz_top     = zztop;
    } else {
    prev_top_3 = prev_top_3[1];
    prev_top_2 = prev_top_2[1];
    prev_top_1 = prev_top_1[1];
    zz_top     = if zz_top[1]==0 then high else zz_top[1];
}
if nzzbot {
    prev_bot_3 = prev_bot_2[1];
    prev_bot_2 = prev_bot_1[1];
    prev_bot_1 = zz_bot[1];
    zz_bot     = zzbot;
    } else {
    prev_bot_3 = prev_bot_3[1];
    prev_bot_2 = prev_bot_2[1];
    prev_bot_1 = prev_bot_1[1];
    zz_bot     = if zz_bot[1]==0 then low else zz_bot[1];
}
#// Calc: RSI QQE Divergence
def rsi = f_rsi(rsiSource, rsiLength, rsiMulti, rsiType);
def rsi_zz = f_zz(qqe_trend, rsi, rsi).zz;
def rsitop = f_zz(qqe_trend, rsi, rsi).top;
def rsibot = f_zz(qqe_trend, rsi, rsi).bot;
def nrsitop = !IsNaN(rsitop);
def nrsibot = !IsNaN(rsibot);
def rsi_top;def rsi_prev_top_1;def rsi_prev_top_2;def rsi_prev_top_3;
def rsi_bot;def rsi_prev_bot_1;def rsi_prev_bot_2;def rsi_prev_bot_3;
if nrsitop {
    rsi_prev_top_3 = rsi_prev_top_2[1];
    rsi_prev_top_2 = rsi_prev_top_1[1];
    rsi_prev_top_1 = rsi_top[1];
    rsi_top        = rsitop;
    } else {
    rsi_prev_top_3 = rsi_prev_top_3[1];
    rsi_prev_top_2 = rsi_prev_top_2[1];
    rsi_prev_top_1 = rsi_prev_top_1[1];
    rsi_top        = if rsi_top[1]==0 then rsi else rsi_top[1];
}
if nrsibot {
    rsi_prev_bot_3 = rsi_prev_bot_2[1];
    rsi_prev_bot_2 = rsi_prev_bot_1[1];
    rsi_prev_bot_1 = rsi_bot[1];
    rsi_bot        = rsibot;
    } else {
    rsi_prev_bot_3 = rsi_prev_bot_3[1];
    rsi_prev_bot_2 = rsi_prev_bot_2[1];
    rsi_prev_bot_1 = rsi_prev_bot_1[1];
    rsi_bot        = if rsi_bot[1]==0 then rsi else rsi_bot[1];
}
#// Calc: Supertrend
def s_trend = f_supertrend(zz_top, zz_bot, IncludeZigZag, supertrendLength, MovAvgType, atrLength, atrFactor).trend;
def s_up = f_supertrend(zz_top, zz_bot, IncludeZigZag, supertrendLength, MovAvgType, atrLength, atrFactor).up;
def s_dn = f_supertrend(zz_top, zz_bot, IncludeZigZag, supertrendLength, MovAvgType, atrLength, atrFactor).dn;
def st_up = if s_trend == 1 then s_up else na;
def st_dn = if s_trend == -1 then s_dn else na;

#-- Cal Div
def pos_limit_1 = nrsibot and rsi_bot < divBottomLimit and rsi_prev_bot_1 < divBottomLimit;
def pos_limit_2 = nrsibot and rsi_bot < divBottomLimit and rsi_prev_bot_2 < divBottomLimit;
def pos_limit_3 = nrsibot and rsi_bot < divBottomLimit and rsi_prev_bot_3 < divBottomLimit;
def pos_limit_4 = nrsibot and rsi_bot < convBottomLimit and rsi_prev_bot_1 < convBottomLimit;
def pos_limit_5 = nrsibot and rsi_bot < convBottomLimit and rsi_prev_bot_2 < convBottomLimit;
def pos_limit_6 = nrsibot and rsi_bot < convBottomLimit and rsi_prev_bot_3 < convBottomLimit;

def neg_limit_1 = nrsitop and rsi_top > divTopLimit and rsi_prev_top_1 > divTopLimit;
def neg_limit_2 = nrsitop and rsi_top > divTopLimit and rsi_prev_top_2 > divTopLimit;
def neg_limit_3 = nrsitop and rsi_top > divTopLimit and rsi_prev_top_3 > divTopLimit;
def neg_limit_4 = nrsitop and rsi_top > convTopLimit and rsi_prev_top_1 > convTopLimit;
def neg_limit_5 = nrsitop and rsi_top > convTopLimit and rsi_prev_top_2 > convTopLimit;
def neg_limit_6 = nrsitop and rsi_top > convTopLimit and rsi_prev_top_3 > convTopLimit;

def pos_div_1 = nzzbot and zz_bot < prev_bot_1 and rsi_bot > rsi_prev_bot_1 and pos_limit_1;
def pos_div_2 = nzzbot and zz_bot < prev_bot_2 and rsi_bot > rsi_prev_bot_2 and pos_limit_2;
def pos_div_3 = nzzbot and zz_bot < prev_bot_3 and rsi_bot > rsi_prev_bot_3 and pos_limit_3;
def pos_con_1 = nzzbot and zz_bot > prev_bot_1 and rsi_bot < rsi_prev_bot_1 and pos_limit_4;
def pos_con_2 = nzzbot and zz_bot > prev_bot_2 and rsi_bot < rsi_prev_bot_2 and pos_limit_5;
def pos_con_3 = nzzbot and zz_bot > prev_bot_3 and rsi_bot < rsi_prev_bot_3 and pos_limit_6;

def neg_div_1 = nzztop and zz_top > prev_top_1 and rsi_top < rsi_prev_top_1 and neg_limit_1;
def neg_div_2 = nzztop and zz_top > prev_top_2 and rsi_top < rsi_prev_top_2 and neg_limit_2;
def neg_div_3 = nzztop and zz_top > prev_top_3 and rsi_top < rsi_prev_top_3 and neg_limit_3;
def neg_con_1 = nzztop and zz_top < prev_top_1 and rsi_top > rsi_prev_top_1 and neg_limit_4;
def neg_con_2 = nzztop and zz_top < prev_top_2 and rsi_top > rsi_prev_top_2 and neg_limit_5;
def neg_con_3 = nzztop and zz_top < prev_top_3 and rsi_top > rsi_prev_top_3 and neg_limit_6;

def pos_div_add;
def pos_con_add;
def neg_div_add;
def neg_con_add;
switch (NoOfPivotPoints) {
case "2" :
    pos_div_add = pos_div_1 or pos_div_2;
    pos_con_add = pos_con_1 or pos_con_2;
    neg_div_add = neg_div_1 or neg_div_2;
    neg_con_add = neg_con_1 or neg_con_2;
case "3" :
    pos_div_add = pos_div_1 or pos_div_2 or pos_div_3;
    pos_con_add = pos_con_1 or pos_con_2 or pos_con_3;
    neg_div_add = neg_div_1 or neg_div_2 or neg_div_3;
    neg_con_add = neg_con_1 or neg_con_2 or neg_con_3;
default :
    pos_div_add = pos_div_1;
    pos_con_add = pos_con_1;
    neg_div_add = neg_div_1;
    neg_con_add = neg_con_1;
}
def pos_div = if ShowDivergence then pos_div_add else no;
def pos_con = if ShowConvergence then pos_con_add else no;
def neg_div = if ShowDivergence then neg_div_add else no;
def neg_con = if ShowConvergence then neg_con_add else no;
def div_col = if pos_div then  2 else
              if neg_div then -2 else
              if pos_con then  1 else
              if neg_con then -1 else na;
#// Calc: ZigZag Continuation Labels
def incr_bull = nzzbot and zz_bot > prev_bot_1;#zz_bot > prev_bot_1;# ? true : false
def decr_bull = nzzbot and zz_bot < prev_bot_1;#? true : false
def incr_bear = nzztop and zz_top < prev_top_1;#zz_top < prev_top_1;# ? true : false
def decr_bear = nzztop and zz_top > prev_top_1;#zz_top > prev_top_1;# ? true : false

def zz_col = if decr_bull and pos_div then  2 else
             if decr_bear and neg_div then -2 else
             if incr_bull and pos_div then  2 else
             if incr_bear and neg_div then -2 else
             if decr_bull and pos_con then  1 else
             if decr_bear and neg_con then -1 else
             if incr_bull and pos_con then  1 else
             if incr_bear and neg_con then -1 else
             if decr_bull and pos_div==no and !pos_con then -3 else
             if decr_bear and neg_div==no and !neg_con then  3 else
             if incr_bull and pos_div==no and !pos_con then  3 else
             if incr_bear and neg_div==no and !neg_con then -3 else 0;

def zz_txt = if decr_bull and pos_div then 1 else
             if decr_bear and neg_div then 2 else
             if incr_bull and pos_div then 3 else
             if incr_bear and neg_div then 4 else
             if decr_bull and pos_con then 5 else
             if decr_bear and neg_con then 6 else
             if incr_bull and pos_con then 7 else
             if incr_bear and neg_con then 8 else
             if decr_bull and pos_div==no and pos_con==no then 9 else
             if decr_bear and neg_div==no and neg_con==no then 10 else
             if incr_bull and pos_div==no and pos_con==no then 11 else
             if incr_bear and neg_div==no and neg_con==no then 12 else 0;

#// Draw: Divergence Labels
AssignPriceColor(if !colorDivConvBar then Color.CURRENT else
                 if div_col == 2 then GlobalColor("l1_pos") else
                 if div_col ==-2 then GlobalColor("l1_neg") else
                 if div_col == 1 then GlobalColor("l2_pos") else
                 if div_col ==-1 then GlobalColor("l2_neg") else Color.CURRENT);


AddChartBubble(zz_txt == 1, zz, "LL\nDiv", GlobalColor("l1_pos"), no);
AddChartBubble(zz_txt == 2, zz, "HH\nDiv", GlobalColor("l1_neg"));
AddChartBubble(zz_txt == 3, zz, "HL\nDiv", GlobalColor("l1_pos"), no);
AddChartBubble(zz_txt == 4, zz, "LH\nDiv", GlobalColor("l1_neg"));
AddChartBubble(zz_txt == 5, zz, "LL\nConv", GlobalColor("l2_pos"), no);
AddChartBubble(zz_txt == 6, zz, "HH\nConv", GlobalColor("l2_neg"));
AddChartBubble(zz_txt == 7, zz, "HL\nConv", GlobalColor("l2_pos"), no);
AddChartBubble(zz_txt == 8, zz, "LH\nConv", GlobalColor("l2_neg"));

AddChartBubble(showHiLoLabels and zz_txt == 9,  zz, "LL", GlobalColor("l1_bear"), no);
AddChartBubble(showHiLoLabels and zz_txt == 10, zz, "HH", GlobalColor("l1_bull"));
AddChartBubble(showHiLoLabels and zz_txt == 11, zz, "HL", GlobalColor("l1_bull"), no);
AddChartBubble(showHiLoLabels and zz_txt == 12, zz, "LH", GlobalColor("l1_bear"));

#//- Drawings -//
#// Draw: ZigZag

plot zzLine = if showZigzagLine and !isNaN(zz) then zz else na;
zzLine.SetDefaultColor(GlobalColor("l1_zz"));
zzLine.EnableApproximation();

plot zzPt = if !IsNaN(zz) and (showZigzagLine or showHiLoLabels) then zz else na;
zzPt.SetPaintingStrategy(PaintingStrategy.POINTS);
zzPt.AssignValueColor(if zz_col == 2 then GlobalColor("l1_pos") else
                      if zz_col ==-2 then GlobalColor("l1_neg") else
                      if zz_col == 1 then GlobalColor("l2_pos") else
                      if zz_col ==-1 then GlobalColor("l2_neg") else
                      if zz_col == 3 then GlobalColor("l1_bull") else
                      if zz_col ==-3 then GlobalColor("l1_bear") else Color.GRAY);
#// Draw: Supertrend
def ohlc = ohlc4;
plot STup = if ShowSupertrend then st_up else na;    # "Up-Trend"
plot STdn = if ShowSupertrend then st_dn else na;    # "Down-Trend"
STup.SetDefaultColor(GlobalColor("l1_bull"));
STdn.SetDefaultColor(GlobalColor("l1_bear"));
AddCloud(ohlc, STup, Color.DARK_GREEN);
AddCloud(STdn, ohlc, Color.DARK_RED);

#-- END of CODE
 
Last edited:
Hi @samer800, This is a great indicator and helps with identifying higher lows and is rarely wrong on longer time frames(I have been using this from last few months and used it on hundreds of stocks). I am struggling to use this as a scan where the recent low is higher than previous low. Is there a way to save previous low and current low as variables so we can scan using those variables ? Thank you.
 
Amazing indicator and great work @samer800! I’ve done some research on it and have seen high win rates and wanted to learn more about it. I found this video on YouTube for the indicator for trading view and it has labels to go long or short without the pricing labels.

Here is the video: https://eightify.app/summary/tradin...curacy-with-moment-based-zigzag-qqe-indicator

Is there anyway we can add arrows or labels like the original code on trading view to let us know to go long or short?
 
Amazing indicator and great work @samer800! I’ve done some research on it and have seen high win rates and wanted to learn more about it. I found this video on YouTube for the indicator for trading view and it has labels to go long or short without the pricing labels.

Here is the video: https://eightify.app/summary/tradin...curacy-with-moment-based-zigzag-qqe-indicator

Is there anyway we can add arrows or labels like the original code on trading view to let us know to go long or short?
I added arrows and the second indicators in the original post. pls check.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
392 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top