#// 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