Repaints SMT Divergences [LuxAlgo] For ThinkOrSwim

Repaints

serendipity2020

Member
Plus
SMT Divergence is the concept used by ICT trader to find divergence between ES/NQ or other corresponding futures.
This indicator may work on other equities too but broadly used across futures by SMT traders.
h4kR6oK.png


Hello @samer800
Appreciate if you can convert this trading view indicator https://www.tradingview.com/script/ecEI56ff-SMT-Divergences-LuxAlgo/
Thanks
 
Last edited by a moderator:
Hello @samer800

Appreciate if you can convert this trading view indicator https://www.tradingview.com/script/ecEI56ff-SMT-Divergences-LuxAlgo/

SMT Divergence is the concept used by ICT trader to find divergence between ES/NQ or other corresponding futures.

This indicator may work on other equities too but broadly used across futures by SMT traders.

Thanks
check the below:

CSS:
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
#// © LuxAlgo
#indicator("SMT Divergences [LuxAlgo]", "LuxAlgo - SMT Divergences", overlay = true, max_lines_count = 500,
#//Settings
# Converted by Sam4Cok@Samer800    - 02/2024
input PivotLookback = 3;#, 'Pivot Lookback', minval = 2)
input useSymbol1 = yes;#, 'Comparison Symbol', inline = 'symA')
input ComparisonSymbol1    = "/ES";#('CME_MINI_DL:ES1!', '', inline = 'symA')
input useSymbol2 = yes; # 'Comparison Symbol', inline = 'symB')
input ComparisonSymbol2   = "/YM";#('CBOT_MINI_DL:YM1!', '', inline = 'symB')
input showDashboard = no;#(false, 'Show Dashboard'

def na = Double.NaN;
def n = BarNumber();

script Pivots {
    input series    = close;
    input leftBars  = 10;
    input rightBars = 10;
    input isHigh = yes;
    def na = Double.NaN;
    def HH = series == Highest(series, leftBars + 1);
    def LL = series == Lowest(series, leftBars + 1);
    def pivotRange = (leftBars + rightBars + 1);
    def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
    def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
    def barIndexH = if pvtCond then
                    fold i = 1 to rightBars + 1 with p=1 while p do
                    series > GetValue(series, - i) else na;
    def barIndexL = if pvtCond then
                    fold j = 1 to rightBars + 1 with q=1 while q do
                    series < GetValue(series, - j) else na;
    def PivotPoint;
    if isHigh {
        PivotPoint = if HH and barIndexH then series else na;
    } else {
        PivotPoint = if LL and barIndexL then series else na;
    }
    plot pvt = PivotPoint;
}
#//Swing highs divergences
script get_divergence {
    input ph = yes;
    input y2 = close;
    input sym_y2 = close;
    def n = BarNumber();
    def y1;
    def sym_y1;
    def x1;
    def smt;
#    def loc;
    def stVal;
    def enVal;
    def stBar;
    def enBar;
    if y2 != y2[1] and sym_y2 != sym_y2[1] {
        if (y2 - y1[1]) * (sym_y2 - sym_y1[1]) < 0 {
        stVal = y2;
        enVal = y1[1];
        stBar = n;
        enBar = x1[1];
        smt = smt[1] + 1;
        } else {
        stVal = stVal[1];
        enVal = enVal[1];
        stBar = 0;
        enBar = 0;
        smt = smt[1];
       }
        sym_y1 = sym_y2;
        y1 = y2;
        x1 = n;
    } else if (ph and y2 > y2[1]) or (!ph and y2 < y2[1]) {
        sym_y1 = Double.NaN;
        y1 = y2;
        x1 = n;
        stVal = stVal[1];
        enVal = enVal[1];
        stBar = 0;
        enBar = 0;
        smt = smt[1];
    } else {
        sym_y1 = sym_y1[1];
        y1 = y1[1];
        x1 = x1[1];
        smt = smt[1];
        stVal = stVal[1];
        enVal = enVal[1];
        stBar = 0;
        enBar = 0;
    }
    plot startVal = stVal;
    plot endVal   = enVal;
    plot startBar = stBar;
    plot endBar = enBar;
    plot cnt = smt;
}
#//Detect swing highs/lows and divergences
Script fixnan {
input data = close;
    def fixnan = if !IsNaN(data) then data else fixnan[1];
    plot out = if fixnan then fixnan else Double.NaN;
}
def ph = fixnan(Pivots(high, PivotLookback, PivotLookback, yes));
def pl = fixnan(Pivots(low, PivotLookback, PivotLookback, no));
def phN = if ph != ph[1] then phN[1] + 1 else phN[1];
def plN = if pl != pl[1] then plN[1] + 1 else plN[1];

#//Comparison symbol pivots
def h1 = high(Symbol = ComparisonSymbol1);
def l1 = low(Symbol = ComparisonSymbol1);
def c1 = close(Symbol = ComparisonSymbol1);

def h2 = high(Symbol = ComparisonSymbol2);
def l2 = low(Symbol = ComparisonSymbol2);
def c2 = close(Symbol = ComparisonSymbol2);

#//Detect swing high divergences
def sym_ph1;def sym_pl1;
def ph_smt1;
def pl_smt1;
def ph_smt1stVal;
def ph_smt1enVal;
def ph_smt1stBar;
def ph_smt1enBar;
def pl_smt1stVal;
def pl_smt1enVal;
def pl_smt1stBar;
def pl_smt1enBar;
if useSymbol1 {
    sym_ph1 = fixnan(Pivots(h1, PivotLookback, PivotLookback, yes));
    sym_pl1 = fixnan(Pivots(l1, PivotLookback, PivotLookback, no));
    ph_smt1 = get_divergence(yes, ph, sym_ph1).cnt;
    ph_smt1stVal = get_divergence(yes, ph, sym_ph1).startVal;
    ph_smt1enVal = get_divergence(yes, ph, sym_ph1).endVal;
    ph_smt1stBar = get_divergence(yes, ph, sym_ph1).startBar;
    ph_smt1enBar = get_divergence(yes, ph, sym_ph1).endBar;
    pl_smt1stVal = get_divergence(no, pl, sym_pl1).startVal;
    pl_smt1 = get_divergence(no, pl, sym_pl1).cnt;
    pl_smt1enVal = get_divergence(no, pl, sym_pl1).endVal;
    pl_smt1stBar = get_divergence(no, pl, sym_pl1).startBar;
    pl_smt1enBar = get_divergence(no, pl, sym_pl1).endBar;
    } else {
    sym_ph1 = na;
    sym_pl1 = na;
    ph_smt1 = na;
    ph_smt1stVal = na;
    ph_smt1enVal = na;
    ph_smt1stBar = na;
    ph_smt1enBar = na;
    pl_smt1 = na;
    pl_smt1stVal = na;
    pl_smt1enVal = na;
    pl_smt1stBar = na;
    pl_smt1enBar = na;
}
def sym_ph2;def sym_pl2;
def ph_smt2;
def pl_smt2;
def ph_smt2stVal;
def ph_smt2enVal;
def ph_smt2stBar;
def ph_smt2enBar;
def pl_smt2stVal;
def pl_smt2enVal;
def pl_smt2stBar;
def pl_smt2enBar;
if useSymbol2 {
    sym_ph2 = fixnan(Pivots(h2, PivotLookback, PivotLookback, yes));
    sym_pl2 = fixnan(Pivots(l2, PivotLookback, PivotLookback, no));
    ph_smt2 = get_divergence(yes, ph, sym_ph2).cnt;
    ph_smt2stVal = get_divergence(yes, ph, sym_ph2).startVal;
    ph_smt2enVal = get_divergence(yes, ph, sym_ph2).endVal;
    ph_smt2stBar = get_divergence(yes, ph, sym_ph2).startBar;
    ph_smt2enBar = get_divergence(yes, ph, sym_ph2).endBar;
    pl_smt2 = get_divergence(no, pl, sym_pl2).cnt;
    pl_smt2stVal = get_divergence(no, pl, sym_pl2).startVal;
    pl_smt2enVal = get_divergence(no, pl, sym_pl2).endVal;
    pl_smt2stBar = get_divergence(no, pl, sym_pl2).startBar;
    pl_smt2enBar = get_divergence(no, pl, sym_pl2).endBar;
    } else {
    sym_ph2 = na;
    sym_pl2 = na;
    ph_smt2 = na;
    pl_smt2 = na;
    ph_smt2stVal = na;
    ph_smt2enVal = na;
    ph_smt2stBar = na;
    ph_smt2enBar = na;
    pl_smt2stVal = na;
    pl_smt2enVal = na;
    pl_smt2stBar = na;
    pl_smt2enBar = na;
}

AddChartBubble(ph_smt1enVal!=ph_smt1enVal[1], high, ComparisonSymbol1, Color.RED);
AddChartBubble(pl_smt1enVal!=pl_smt1enVal[1], low , ComparisonSymbol1, Color.CYAN, no);
AddChartBubble(ph_smt2enVal!=ph_smt2enVal[1], high, ComparisonSymbol2, Color.RED);
AddChartBubble(pl_smt2enVal!=pl_smt2enVal[1], low , ComparisonSymbol2, Color.CYAN, no);

plot sym1DivPh = if n == highestAll(ph_smt1stBar) then high else
                 if n == highestAll(ph_smt1enBar) then high else na;
sym1DivPh.SetDefaultColor(Color.RED);
sym1DivPh.EnableApproximation();
plot sym1DivPl = if n == highestAll(pl_smt1stBar) then low else
                 if n == highestAll(pl_smt1enBar) then low else na;
sym1DivPl.SetDefaultColor(Color.CYAN);
sym1DivPl.EnableApproximation();

plot sym2DivPh = if n == highestAll(ph_smt2stBar) then high else
                 if n == highestAll(ph_smt2enBar) then high else na;
sym2DivPh.SetDefaultColor(Color.RED);
sym2DivPh.EnableApproximation();
plot sym2DivPl = if n == highestAll(pl_smt2stBar) then low else
                 if n == highestAll(pl_smt2enBar) then low else na;
sym2DivPl.SetDefaultColor(Color.CYAN);
sym2DivPl.EnableApproximation();

#-- Label
def ph1R = Round(ph_smt1 / phN, 2);
def pl1R = Round(pl_smt1 / plN, 2);
def ph2R = Round(ph_smt2 / phN, 2);
def pl2R = Round(pl_smt2 / plN, 2);

AddLabel(showDashboard,"Swing High: [" + ComparisonSymbol1 + "] " + ph_smt1 + " (" + ph1R + "%) - [" +
                                         ComparisonSymbol2 + "] " + ph_smt2 + " (" + ph2R + "%)", color.RED);

AddLabel(showDashboard,"Swing Low: [" + ComparisonSymbol1 + "] " + pl_smt1 + " (" + pl1R + "%) - [" +
                                        ComparisonSymbol2 + "] " + pl_smt2 + " (" + pl2R + "%)", color.CYAN);

#-- END of CODE
 
check the below:

CSS:
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
#// © LuxAlgo
#indicator("SMT Divergences [LuxAlgo]", "LuxAlgo - SMT Divergences", overlay = true, max_lines_count = 500,
#//Settings
# Converted by Sam4Cok@Samer800    - 02/2024
input PivotLookback = 3;#, 'Pivot Lookback', minval = 2)
input useSymbol1 = yes;#, 'Comparison Symbol', inline = 'symA')
input ComparisonSymbol1    = "/ES";#('CME_MINI_DL:ES1!', '', inline = 'symA')
input useSymbol2 = yes; # 'Comparison Symbol', inline = 'symB')
input ComparisonSymbol2   = "/YM";#('CBOT_MINI_DL:YM1!', '', inline = 'symB')
input showDashboard = no;#(false, 'Show Dashboard'

def na = Double.NaN;
def n = BarNumber();

script Pivots {
    input series    = close;
    input leftBars  = 10;
    input rightBars = 10;
    input isHigh = yes;
    def na = Double.NaN;
    def HH = series == Highest(series, leftBars + 1);
    def LL = series == Lowest(series, leftBars + 1);
    def pivotRange = (leftBars + rightBars + 1);
    def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
    def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
    def barIndexH = if pvtCond then
                    fold i = 1 to rightBars + 1 with p=1 while p do
                    series > GetValue(series, - i) else na;
    def barIndexL = if pvtCond then
                    fold j = 1 to rightBars + 1 with q=1 while q do
                    series < GetValue(series, - j) else na;
    def PivotPoint;
    if isHigh {
        PivotPoint = if HH and barIndexH then series else na;
    } else {
        PivotPoint = if LL and barIndexL then series else na;
    }
    plot pvt = PivotPoint;
}
#//Swing highs divergences
script get_divergence {
    input ph = yes;
    input y2 = close;
    input sym_y2 = close;
    def n = BarNumber();
    def y1;
    def sym_y1;
    def x1;
    def smt;
#    def loc;
    def stVal;
    def enVal;
    def stBar;
    def enBar;
    if y2 != y2[1] and sym_y2 != sym_y2[1] {
        if (y2 - y1[1]) * (sym_y2 - sym_y1[1]) < 0 {
        stVal = y2;
        enVal = y1[1];
        stBar = n;
        enBar = x1[1];
        smt = smt[1] + 1;
        } else {
        stVal = stVal[1];
        enVal = enVal[1];
        stBar = 0;
        enBar = 0;
        smt = smt[1];
       }
        sym_y1 = sym_y2;
        y1 = y2;
        x1 = n;
    } else if (ph and y2 > y2[1]) or (!ph and y2 < y2[1]) {
        sym_y1 = Double.NaN;
        y1 = y2;
        x1 = n;
        stVal = stVal[1];
        enVal = enVal[1];
        stBar = 0;
        enBar = 0;
        smt = smt[1];
    } else {
        sym_y1 = sym_y1[1];
        y1 = y1[1];
        x1 = x1[1];
        smt = smt[1];
        stVal = stVal[1];
        enVal = enVal[1];
        stBar = 0;
        enBar = 0;
    }
    plot startVal = stVal;
    plot endVal   = enVal;
    plot startBar = stBar;
    plot endBar = enBar;
    plot cnt = smt;
}
#//Detect swing highs/lows and divergences
Script fixnan {
input data = close;
    def fixnan = if !IsNaN(data) then data else fixnan[1];
    plot out = if fixnan then fixnan else Double.NaN;
}
def ph = fixnan(Pivots(high, PivotLookback, PivotLookback, yes));
def pl = fixnan(Pivots(low, PivotLookback, PivotLookback, no));
def phN = if ph != ph[1] then phN[1] + 1 else phN[1];
def plN = if pl != pl[1] then plN[1] + 1 else plN[1];

#//Comparison symbol pivots
def h1 = high(Symbol = ComparisonSymbol1);
def l1 = low(Symbol = ComparisonSymbol1);
def c1 = close(Symbol = ComparisonSymbol1);

def h2 = high(Symbol = ComparisonSymbol2);
def l2 = low(Symbol = ComparisonSymbol2);
def c2 = close(Symbol = ComparisonSymbol2);

#//Detect swing high divergences
def sym_ph1;def sym_pl1;
def ph_smt1;
def pl_smt1;
def ph_smt1stVal;
def ph_smt1enVal;
def ph_smt1stBar;
def ph_smt1enBar;
def pl_smt1stVal;
def pl_smt1enVal;
def pl_smt1stBar;
def pl_smt1enBar;
if useSymbol1 {
    sym_ph1 = fixnan(Pivots(h1, PivotLookback, PivotLookback, yes));
    sym_pl1 = fixnan(Pivots(l1, PivotLookback, PivotLookback, no));
    ph_smt1 = get_divergence(yes, ph, sym_ph1).cnt;
    ph_smt1stVal = get_divergence(yes, ph, sym_ph1).startVal;
    ph_smt1enVal = get_divergence(yes, ph, sym_ph1).endVal;
    ph_smt1stBar = get_divergence(yes, ph, sym_ph1).startBar;
    ph_smt1enBar = get_divergence(yes, ph, sym_ph1).endBar;
    pl_smt1stVal = get_divergence(no, pl, sym_pl1).startVal;
    pl_smt1 = get_divergence(no, pl, sym_pl1).cnt;
    pl_smt1enVal = get_divergence(no, pl, sym_pl1).endVal;
    pl_smt1stBar = get_divergence(no, pl, sym_pl1).startBar;
    pl_smt1enBar = get_divergence(no, pl, sym_pl1).endBar;
    } else {
    sym_ph1 = na;
    sym_pl1 = na;
    ph_smt1 = na;
    ph_smt1stVal = na;
    ph_smt1enVal = na;
    ph_smt1stBar = na;
    ph_smt1enBar = na;
    pl_smt1 = na;
    pl_smt1stVal = na;
    pl_smt1enVal = na;
    pl_smt1stBar = na;
    pl_smt1enBar = na;
}
def sym_ph2;def sym_pl2;
def ph_smt2;
def pl_smt2;
def ph_smt2stVal;
def ph_smt2enVal;
def ph_smt2stBar;
def ph_smt2enBar;
def pl_smt2stVal;
def pl_smt2enVal;
def pl_smt2stBar;
def pl_smt2enBar;
if useSymbol2 {
    sym_ph2 = fixnan(Pivots(h2, PivotLookback, PivotLookback, yes));
    sym_pl2 = fixnan(Pivots(l2, PivotLookback, PivotLookback, no));
    ph_smt2 = get_divergence(yes, ph, sym_ph2).cnt;
    ph_smt2stVal = get_divergence(yes, ph, sym_ph2).startVal;
    ph_smt2enVal = get_divergence(yes, ph, sym_ph2).endVal;
    ph_smt2stBar = get_divergence(yes, ph, sym_ph2).startBar;
    ph_smt2enBar = get_divergence(yes, ph, sym_ph2).endBar;
    pl_smt2 = get_divergence(no, pl, sym_pl2).cnt;
    pl_smt2stVal = get_divergence(no, pl, sym_pl2).startVal;
    pl_smt2enVal = get_divergence(no, pl, sym_pl2).endVal;
    pl_smt2stBar = get_divergence(no, pl, sym_pl2).startBar;
    pl_smt2enBar = get_divergence(no, pl, sym_pl2).endBar;
    } else {
    sym_ph2 = na;
    sym_pl2 = na;
    ph_smt2 = na;
    pl_smt2 = na;
    ph_smt2stVal = na;
    ph_smt2enVal = na;
    ph_smt2stBar = na;
    ph_smt2enBar = na;
    pl_smt2stVal = na;
    pl_smt2enVal = na;
    pl_smt2stBar = na;
    pl_smt2enBar = na;
}

AddChartBubble(ph_smt1enVal!=ph_smt1enVal[1], high, ComparisonSymbol1, Color.RED);
AddChartBubble(pl_smt1enVal!=pl_smt1enVal[1], low , ComparisonSymbol1, Color.CYAN, no);
AddChartBubble(ph_smt2enVal!=ph_smt2enVal[1], high, ComparisonSymbol2, Color.RED);
AddChartBubble(pl_smt2enVal!=pl_smt2enVal[1], low , ComparisonSymbol2, Color.CYAN, no);

plot sym1DivPh = if n == highestAll(ph_smt1stBar) then high else
                 if n == highestAll(ph_smt1enBar) then high else na;
sym1DivPh.SetDefaultColor(Color.RED);
sym1DivPh.EnableApproximation();
plot sym1DivPl = if n == highestAll(pl_smt1stBar) then low else
                 if n == highestAll(pl_smt1enBar) then low else na;
sym1DivPl.SetDefaultColor(Color.CYAN);
sym1DivPl.EnableApproximation();

plot sym2DivPh = if n == highestAll(ph_smt2stBar) then high else
                 if n == highestAll(ph_smt2enBar) then high else na;
sym2DivPh.SetDefaultColor(Color.RED);
sym2DivPh.EnableApproximation();
plot sym2DivPl = if n == highestAll(pl_smt2stBar) then low else
                 if n == highestAll(pl_smt2enBar) then low else na;
sym2DivPl.SetDefaultColor(Color.CYAN);
sym2DivPl.EnableApproximation();

#-- Label
def ph1R = Round(ph_smt1 / phN, 2);
def pl1R = Round(pl_smt1 / plN, 2);
def ph2R = Round(ph_smt2 / phN, 2);
def pl2R = Round(pl_smt2 / plN, 2);

AddLabel(showDashboard,"Swing High: [" + ComparisonSymbol1 + "] " + ph_smt1 + " (" + ph1R + "%) - [" +
                                         ComparisonSymbol2 + "] " + ph_smt2 + " (" + ph2R + "%)", color.RED);

AddLabel(showDashboard,"Swing Low: [" + ComparisonSymbol1 + "] " + pl_smt1 + " (" + pl1R + "%) - [" +
                                        ComparisonSymbol2 + "] " + pl_smt2 + " (" + pl2R + "%)", color.CYAN);

#-- END of CODE
Thanks @samer800
Can this be considered non-repainting indicator?
Would it be possible to add alerts in this whenever divergence is formed completely?
I also noticed its showing lines only on last 2 divergences but not on all other prior divergences. Can that be fixed?
 
check the below:

CSS:
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
#// © LuxAlgo
#indicator("SMT Divergences [LuxAlgo]", "LuxAlgo - SMT Divergences", overlay = true, max_lines_count = 500,
#//Settings
# Converted by Sam4Cok@Samer800    - 02/2024
input PivotLookback = 3;#, 'Pivot Lookback', minval = 2)
input useSymbol1 = yes;#, 'Comparison Symbol', inline = 'symA')
input ComparisonSymbol1    = "/ES";#('CME_MINI_DL:ES1!', '', inline = 'symA')
input useSymbol2 = yes; # 'Comparison Symbol', inline = 'symB')
input ComparisonSymbol2   = "/YM";#('CBOT_MINI_DL:YM1!', '', inline = 'symB')
input showDashboard = no;#(false, 'Show Dashboard'

def na = Double.NaN;
def n = BarNumber();

script Pivots {
    input series    = close;
    input leftBars  = 10;
    input rightBars = 10;
    input isHigh = yes;
    def na = Double.NaN;
    def HH = series == Highest(series, leftBars + 1);
    def LL = series == Lowest(series, leftBars + 1);
    def pivotRange = (leftBars + rightBars + 1);
    def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
    def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
    def barIndexH = if pvtCond then
                    fold i = 1 to rightBars + 1 with p=1 while p do
                    series > GetValue(series, - i) else na;
    def barIndexL = if pvtCond then
                    fold j = 1 to rightBars + 1 with q=1 while q do
                    series < GetValue(series, - j) else na;
    def PivotPoint;
    if isHigh {
        PivotPoint = if HH and barIndexH then series else na;
    } else {
        PivotPoint = if LL and barIndexL then series else na;
    }
    plot pvt = PivotPoint;
}
#//Swing highs divergences
script get_divergence {
    input ph = yes;
    input y2 = close;
    input sym_y2 = close;
    def n = BarNumber();
    def y1;
    def sym_y1;
    def x1;
    def smt;
#    def loc;
    def stVal;
    def enVal;
    def stBar;
    def enBar;
    if y2 != y2[1] and sym_y2 != sym_y2[1] {
        if (y2 - y1[1]) * (sym_y2 - sym_y1[1]) < 0 {
        stVal = y2;
        enVal = y1[1];
        stBar = n;
        enBar = x1[1];
        smt = smt[1] + 1;
        } else {
        stVal = stVal[1];
        enVal = enVal[1];
        stBar = 0;
        enBar = 0;
        smt = smt[1];
       }
        sym_y1 = sym_y2;
        y1 = y2;
        x1 = n;
    } else if (ph and y2 > y2[1]) or (!ph and y2 < y2[1]) {
        sym_y1 = Double.NaN;
        y1 = y2;
        x1 = n;
        stVal = stVal[1];
        enVal = enVal[1];
        stBar = 0;
        enBar = 0;
        smt = smt[1];
    } else {
        sym_y1 = sym_y1[1];
        y1 = y1[1];
        x1 = x1[1];
        smt = smt[1];
        stVal = stVal[1];
        enVal = enVal[1];
        stBar = 0;
        enBar = 0;
    }
    plot startVal = stVal;
    plot endVal   = enVal;
    plot startBar = stBar;
    plot endBar = enBar;
    plot cnt = smt;
}
#//Detect swing highs/lows and divergences
Script fixnan {
input data = close;
    def fixnan = if !IsNaN(data) then data else fixnan[1];
    plot out = if fixnan then fixnan else Double.NaN;
}
def ph = fixnan(Pivots(high, PivotLookback, PivotLookback, yes));
def pl = fixnan(Pivots(low, PivotLookback, PivotLookback, no));
def phN = if ph != ph[1] then phN[1] + 1 else phN[1];
def plN = if pl != pl[1] then plN[1] + 1 else plN[1];

#//Comparison symbol pivots
def h1 = high(Symbol = ComparisonSymbol1);
def l1 = low(Symbol = ComparisonSymbol1);
def c1 = close(Symbol = ComparisonSymbol1);

def h2 = high(Symbol = ComparisonSymbol2);
def l2 = low(Symbol = ComparisonSymbol2);
def c2 = close(Symbol = ComparisonSymbol2);

#//Detect swing high divergences
def sym_ph1;def sym_pl1;
def ph_smt1;
def pl_smt1;
def ph_smt1stVal;
def ph_smt1enVal;
def ph_smt1stBar;
def ph_smt1enBar;
def pl_smt1stVal;
def pl_smt1enVal;
def pl_smt1stBar;
def pl_smt1enBar;
if useSymbol1 {
    sym_ph1 = fixnan(Pivots(h1, PivotLookback, PivotLookback, yes));
    sym_pl1 = fixnan(Pivots(l1, PivotLookback, PivotLookback, no));
    ph_smt1 = get_divergence(yes, ph, sym_ph1).cnt;
    ph_smt1stVal = get_divergence(yes, ph, sym_ph1).startVal;
    ph_smt1enVal = get_divergence(yes, ph, sym_ph1).endVal;
    ph_smt1stBar = get_divergence(yes, ph, sym_ph1).startBar;
    ph_smt1enBar = get_divergence(yes, ph, sym_ph1).endBar;
    pl_smt1stVal = get_divergence(no, pl, sym_pl1).startVal;
    pl_smt1 = get_divergence(no, pl, sym_pl1).cnt;
    pl_smt1enVal = get_divergence(no, pl, sym_pl1).endVal;
    pl_smt1stBar = get_divergence(no, pl, sym_pl1).startBar;
    pl_smt1enBar = get_divergence(no, pl, sym_pl1).endBar;
    } else {
    sym_ph1 = na;
    sym_pl1 = na;
    ph_smt1 = na;
    ph_smt1stVal = na;
    ph_smt1enVal = na;
    ph_smt1stBar = na;
    ph_smt1enBar = na;
    pl_smt1 = na;
    pl_smt1stVal = na;
    pl_smt1enVal = na;
    pl_smt1stBar = na;
    pl_smt1enBar = na;
}
def sym_ph2;def sym_pl2;
def ph_smt2;
def pl_smt2;
def ph_smt2stVal;
def ph_smt2enVal;
def ph_smt2stBar;
def ph_smt2enBar;
def pl_smt2stVal;
def pl_smt2enVal;
def pl_smt2stBar;
def pl_smt2enBar;
if useSymbol2 {
    sym_ph2 = fixnan(Pivots(h2, PivotLookback, PivotLookback, yes));
    sym_pl2 = fixnan(Pivots(l2, PivotLookback, PivotLookback, no));
    ph_smt2 = get_divergence(yes, ph, sym_ph2).cnt;
    ph_smt2stVal = get_divergence(yes, ph, sym_ph2).startVal;
    ph_smt2enVal = get_divergence(yes, ph, sym_ph2).endVal;
    ph_smt2stBar = get_divergence(yes, ph, sym_ph2).startBar;
    ph_smt2enBar = get_divergence(yes, ph, sym_ph2).endBar;
    pl_smt2 = get_divergence(no, pl, sym_pl2).cnt;
    pl_smt2stVal = get_divergence(no, pl, sym_pl2).startVal;
    pl_smt2enVal = get_divergence(no, pl, sym_pl2).endVal;
    pl_smt2stBar = get_divergence(no, pl, sym_pl2).startBar;
    pl_smt2enBar = get_divergence(no, pl, sym_pl2).endBar;
    } else {
    sym_ph2 = na;
    sym_pl2 = na;
    ph_smt2 = na;
    pl_smt2 = na;
    ph_smt2stVal = na;
    ph_smt2enVal = na;
    ph_smt2stBar = na;
    ph_smt2enBar = na;
    pl_smt2stVal = na;
    pl_smt2enVal = na;
    pl_smt2stBar = na;
    pl_smt2enBar = na;
}

AddChartBubble(ph_smt1enVal!=ph_smt1enVal[1], high, ComparisonSymbol1, Color.RED);
AddChartBubble(pl_smt1enVal!=pl_smt1enVal[1], low , ComparisonSymbol1, Color.CYAN, no);
AddChartBubble(ph_smt2enVal!=ph_smt2enVal[1], high, ComparisonSymbol2, Color.RED);
AddChartBubble(pl_smt2enVal!=pl_smt2enVal[1], low , ComparisonSymbol2, Color.CYAN, no);

plot sym1DivPh = if n == highestAll(ph_smt1stBar) then high else
                 if n == highestAll(ph_smt1enBar) then high else na;
sym1DivPh.SetDefaultColor(Color.RED);
sym1DivPh.EnableApproximation();
plot sym1DivPl = if n == highestAll(pl_smt1stBar) then low else
                 if n == highestAll(pl_smt1enBar) then low else na;
sym1DivPl.SetDefaultColor(Color.CYAN);
sym1DivPl.EnableApproximation();

plot sym2DivPh = if n == highestAll(ph_smt2stBar) then high else
                 if n == highestAll(ph_smt2enBar) then high else na;
sym2DivPh.SetDefaultColor(Color.RED);
sym2DivPh.EnableApproximation();
plot sym2DivPl = if n == highestAll(pl_smt2stBar) then low else
                 if n == highestAll(pl_smt2enBar) then low else na;
sym2DivPl.SetDefaultColor(Color.CYAN);
sym2DivPl.EnableApproximation();

#-- Label
def ph1R = Round(ph_smt1 / phN, 2);
def pl1R = Round(pl_smt1 / plN, 2);
def ph2R = Round(ph_smt2 / phN, 2);
def pl2R = Round(pl_smt2 / plN, 2);

AddLabel(showDashboard,"Swing High: [" + ComparisonSymbol1 + "] " + ph_smt1 + " (" + ph1R + "%) - [" +
                                         ComparisonSymbol2 + "] " + ph_smt2 + " (" + ph2R + "%)", color.RED);

AddLabel(showDashboard,"Swing Low: [" + ComparisonSymbol1 + "] " + pl_smt1 + " (" + pl1R + "%) - [" +
                                        ComparisonSymbol2 + "] " + pl_smt2 + " (" + pl2R + "%)", color.CYAN);

#-- END of CODE
Is it possible to use this for ticks?
 
Is it possible to use this for ticks?

The majority of studies, on this forum, are designed for time-based charts unless stated otherwise.
Unfortunately, there are no modifications available to seamlessly transition time-based studies to tick charts. However, some members have reported limited success by adjusting their chart settings to include extended hours.
 
Hello all,
SMT divergence is a term coined by the trader known as ICT.
He compares the price action of ES and NQ primarily and notes if one symbol is making a higher high or lower low and the other is not.
Can someone help me code a divergence indicator?
My initial thought is two have two charts open, side by side:


-When BOTH symbols are making new highs or lows to any of the (X) amount of bars before the current bar, nothing is plotted.
__
-When [S1] (Symbol 1 - ES) is making a new high/low and [S2] (Symbol 2 - NQ) is not, I would like a horizontal plot on S2's high or low corresponding to the high or low and bar number of S1.
-Inversely, When S2 makes a HH or LL and S1 does not, I would like a horizontal plot from the corresponding bar number and High or low on S1.
This will show us that a high or low needs to be broken in order for a divergence to be avoided. If the plot appears and price does not cross the high or low and effectively remove the plot, then a divergence has occurred and liquidity may have been taken by one or the other for an upcoming move.


These divergences happen very often and so it is somewhat difficult to narrow in which ones are significant. I expect a cluttered chart and so some way of mitigating the noise would be ideal. ____
Here is a drawn example of a divergence plotted.
Screenshot_2023-02-11_140254.png


Div_1.jpg
Div_2.jpg
Div_3.jpg
Div_4.jpg


Div_examples.jpg

Here are some example divergences
 
Last edited:
Great indicator and thank you! is it possible to replace YM with NQ? I tried in both the code and the menu and both won't work so /ES will only show divergence with YM and not NQ.
 
Great indicator and thank you! is it possible to replace YM with NQ? I tried in both the code and the menu and both won't work so /ES will only show divergence with YM and not NQ.
change the required symbol from the indicator settings.
 

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
169 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