Support Resistance with Breaks and Retests for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
jRjZbsy.png


* Not Typical converting.

Creator Message: https://www.tradingview.com/script/Xeeko6TV-Support-Resistance-with-Breaks-and-Retests/

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © nsadeghi
# https://www.tradingview.com/v/Xeeko6TV/
#indicator("Support Resistance with Breaks and Retests",
# Converted and mod by Sam4Cok@Samer800     - 03/2023

input lookback  = 20;              # 'Lookback Range''How many bars for a pivot event to occur.'
input BarsSinceBreakout  = 2;      # 'Bars Since Breakout''How many bars since breakout in order to detect a retest.'
input RetestDetectionLimiter  = 2; # 'Retest Detection Limiter'
input breakoutBubbles = yes;   # 'Breakouts'
input retestWedges    = yes;   # 'Retests'
input repType   = {default "On", "Candle Confirmation", "High & Low"};    # 'Repainting'

def na = Double.NaN;
def retest = retestWedges;
def Breakout = breakoutBubbles;
def retSince = BarsSinceBreakout;
def retValid = RetestDetectionLimiter;
def bb              = lookback;
def rTon            = repType == repType."On";
def rTcc            = repType == repType."Candle Confirmation";
def rThv            = repType == repType."High & Low";

def srcHi; def srcLo;

if rTon {
    srcHi = close;
    srcLo = close;
    } else
if rTcc {
    srcHi = close[1];
    srcLo = close[1];
    } else
if rThv {
    srcHi = high;
    srcLo = low;
    } else {
    srcHi = srcHi[1];
    srcLo = srcLo[1];
}
#---- Colors
DefineGlobalColor("green" , CreateColor(8, 153, 129));
DefineGlobalColor("red"   , Color.PINK);#CreateColor(255, 82, 82));
#// Pivot Instance

script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else fix[1];
    plot result = fix;
}
#barssince(Condition) =>
script barssince {
input Condition = 0;
def barssince = if Condition then 1 else barssince[1] + 1;
plot return = barssince;
}
#valuewhen (cond, source, occurrence) =>
script valuewhen {
    input cond = 0;
    input source = 0;
    def offset = fold j = 0 to 200 with p while p < 2 do
                 p + ( if p == 1 then j - 2 else if GetValue(cond, j) then 1 else 0 );
    plot price = GetValue(source, offset - 1);
}
script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL = 5;    # default Pivot Lookback Left
    input lbR = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def pl_1 = fixnan(FindPivots(low, -1, bb, bb));
def ph_1 = fixnan(FindPivots(high, 1, bb, bb));
def hh = if high==highest(high, bb) then high else hh[1];
def ll = if low==lowest(low, bb) then low else ll[1];
def ph = if ph_1==0 then hh else ph_1;
def pl = if pl_1==0 then ll else pl_1;

#// Box Height
def s_yLoc = if low[1]  > low[-1]  then low[-1] else low[1];
def r_yLoc = if high[1] > high[-1] then high[1] else high[-1];
#// Functions
#drawBox(condition, y1, y2, color) =>
script drawBox {
    input condition = yes;
    input y1        = high;
    input y2        = low;
    def boxHi = if condition then y1 else boxHi[1];
    def boxLo = if condition then y2 else boxLo[1];
    plot ph = if !IsNaN(close[5]) and !IsNaN(close) then boxHi else Double.NaN;
    plot pl = if !IsNaN(close[5]) and !IsNaN(close) then boxLo else Double.NaN;
}
#repaint(c1, c2, c3) =>
script repaint {
    input type = "On";
    input c1 = no;
    input c2 = no;
    input c3 = no;
    def rTon = type == "On";
    def rTcc = type == "Candle Confirmation";
    def rThv = type == "High & Low";
    def repaint = if rTon then c1 else if rThv then c2 else if rTcc then c3 else repaint[1];
    plot out = repaint;
}
#retestCondition(breakout, condition) =>
Script retestCondition {
input breakout = no;
input condition = no;
input retSince = 2;
    def retestCondition = barssince(!breakout) > retSince and condition;
    plot out = retestCondition;
}
#// Draw and Update Boxes
def sBoxH = drawBox((pl != pl[1]), s_yLoc, pl).ph;
def sBoxL = drawBox((pl != pl[1]), s_yLoc, pl).pl;
def rBoxH = drawBox((ph != ph[1]), ph, r_yLoc).ph;
def rBoxL = drawBox((ph != ph[1]), ph, r_yLoc).pl;

def sTop = sBoxH;
def sBot = sBoxL;
def rTop = rBoxH;
def rBot = rBoxL;

#// Breakout Event
def sBreak;
def rBreak;
def sLabel;
def rLabel;

def c1 = if close  < sBoxL then c1[1] + 1 else 0;
def c2 = if low    < sBoxL then c2[1] + 1 else 0;
def c3 = if close[1] < sBoxL[1] then c3[1] + 1 else 0;
def d1 = if close  > rBoxH then d1[1] + 1 else 0;
def d2 = if high   > rBoxH then d2[1] + 1 else 0;
def d3 = if close[1] > rBoxH[1] then d3[1] + 1 else 0;

def cu = repaint(repType, c1==1, c2==1, c3==1);
def co = repaint(repType, d1==1, d2==1, d3==1);

def sBreak1; def rBreak1;def sBreak11; def rBreak11;
if (pl-pl[1]) {
    sBreak1 =  if !sBreak[1] then no else sBreak[1];
    rBreak1 = rBreak[1];
    } else
if (ph-ph[1]) {
    sBreak1 = sBreak[1];
    rBreak1 = if !rBreak[1] then no else sBreak[1];
    } else {
    sBreak1 =  yes;#sBreak[1];
    rBreak1 =  yes;#rBreak[1];
}
if cu and !sBreak1 {
        sBreak = yes;
        rBreak = rBreak1;
    } else
if co and !rBreak1 {
        sBreak = sBreak1;
        rBreak = yes;
    } else {
        sBreak = sBreak1;
        rBreak = rBreak1;
}
if (pl-pl[1]) {
    sBreak11 = if !sBreak11[1] then 0 else sBreak;
    rBreak11 = rBreak11[1];
    } else
if (ph-ph[1]) {
    sBreak11 = sBreak11[1];
    rBreak11 = if !rBreak11[1] then 0 else sBreak;
    } else {
    sBreak11 =  sBreak11[1];
    rBreak11 =  rBreak11[1];
}
if cu and !sBreak11 {
        sLabel = if breakout then sBoxL else na;
        rLabel = na;
    } else
if co and !rBreak11 {
        sLabel = na;
        rLabel = if breakout then rBoxH else na;
    } else {
        sLabel = na;
        rLabel = na;
}
def changePh = srcHi > highest(srcHi[1], bb);
def changePl = srcLo < lowest(srcLo[1] , bb);


AddChartBubble(changePl and !isNaN(sLabel), sLabel, "break", Color.RED, yes);
AddChartBubble(changePh and !isNaN(rLabel), rLabel, "break", Color.GREEN, no);

#// Retest Event
def s1_ = retestCondition(sBreak, high >= sTop and close <= sBot, retSince);
def s2_ = retestCondition(sBreak, high >= sTop and close >= sBot and close <= sTop, retSince);
def s3_ = retestCondition(sBreak, high >= sBot and high <= sTop, retSince);
def s4_ = retestCondition(sBreak, high >= sBot and high <= sTop and close < sBot, retSince);
def r1_ = retestCondition(rBreak, low <= rBot and close >= rTop, retSince);
def r2_ = retestCondition(rBreak, low <= rBot and close <= rTop and close >= rBot, retSince);
def r3_ = retestCondition(rBreak, low <= rTop and low >= rBot, retSince);
def r4_ = retestCondition(rBreak, low <= rTop and low >= rBot and close > rTop, retSince);

def s1 = s1_;
def s2 = s2_;
def s3 = s3_;
def s4 = s4_;
def r1 = r1_;
def r2 = r2_;
def r3 = r3_;
def r4 = r4_;

#retestEvent(c1, c2, c3, c4, y1, y2, col, style, pType) =>
Script retestEvent {
input c1 = 1;
input c2 = 1;
input c3 = 1;
input c4 = 1;
input y1 = high;
input y2 = low;
input pType = 1;
input input_retValid = 2;
input ph = high;
input pl = low;
input repType = "On";
     def retOccurred; def retEvent;
     def retActive   = c1 or c2 or c3 or c4;
     def retEvent1   = retActive and !retActive[1];
     def retEventVal = if retEvent1 then y2 else retEventVal[1];
        if (if pType == 1 then y2 < retEventVal else y2 > retEventVal) {
            retEvent = retActive;
        } else {
            retEvent = retEvent1;
}
#    def retValue = valuewhen(retEvent, y1);
    def retValue = if retEvent then y1 else retValue[1];
    def retSince = barssince(retEvent);
    def hc1 = close >= retValue;
    def hc2 = high >= retValue;
    def hc3 = close[1] >= retValue[1];
    def lc1 = close <= retValue;
    def lc2 = high <= retValue;
    def lc3 = close[1] <= retValue[1];
    def rTon = repType == "On";
    def rTcc = repType == "Candle Confirmation";
    def rThv = repType == "High & Low";
    def reph = if rTon then hc1 else if rThv then hc2 else if rTcc then hc3 else reph[1];
    def repl = if rTon then lc1 else if rThv then lc2 else if rTcc then lc3 else repl[1];
    def Potential = if retEvent then y2 else 0;
    def retConditions = if pType ==  1 then reph else repl;
    def retValid = retSince > 0 and retSince <= input_retValid and retConditions and !retOccurred[1];
        retOccurred = if retEvent then no else
                      if retValid then yes else
                      if pType == 1  and (ph!=ph[1]) and retOccurred[1] then no else
                      if pType == -1 and (pl!=pl[1]) and retOccurred[1] then no else retOccurred[1];
    def retest = if retValid then y2 else 0;
    def labelCount;
    if retEvent {
        labelCount = 0;
     } else {
        labelCount = labelCount[1] + 1;
}
    plot retVal = retest;
    plot retPot = Potential;
    plot count = labelCount;
}
def retH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retVal;
def PotH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retPot;
def cntH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).count;
def retL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retVal;
def PotL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retPot;
def cntL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).count;
#--
def retH = retH_;
def PotH = PotH_;
def cntH = cntH_;
def retL = retL_;
def PotL = PotL_;
def cntL = cntL_;

def retLCol = if retL then retL else retLCol[1];
def retHCol = if retH then retH else retHCol[1];
def breakUp  = srcHi>retHCol;#srcHi > rBot;
def breakDn  = srcLo<retLCol;#srcLo < sTop;

plot PotBreak = if retest then if PotH and cntH<2 then PotH else na else na;
PotBreak.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
PotBreak.SetDefaultColor(color.CYAN);

plot PotBreakDn = if retest then if PotL and cntL<2 then PotL else na else na;
PotBreakDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
PotBreakDn.SetDefaultColor(color.MAGENTA);

plot sTop_ = if sBoxH==0 then na else sBoxH;
plot sBot_ = if sBoxL==0 then na else sBoxL;
plot rTop_ = if rBoxH==0 then na else rBoxH;
plot rBot_ = if rBoxL==0 then na else rBoxL;

    sTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    sBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

    sTOP_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    sBot_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    rTop_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));
    rBot_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));


AddCloud(if breakDn then sTop else na, sBot, Color.DARK_RED);
AddCloud(if breakUp then rTop else na, rBot, Color.DARK_GREEN);

#--- END Code
 
Last edited by a moderator:

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

jRjZbsy.png


* Not Typical converting.

Creator Message: https://www.tradingview.com/script/Xeeko6TV-Support-Resistance-with-Breaks-and-Retests/

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © nsadeghi
# https://www.tradingview.com/v/Xeeko6TV/
#indicator("Support Resistance with Breaks and Retests",
# Converted and mod by Sam4Cok@Samer800     - 03/2023

input lookback  = 20;              # 'Lookback Range''How many bars for a pivot event to occur.'
input BarsSinceBreakout  = 2;      # 'Bars Since Breakout''How many bars since breakout in order to detect a retest.'
input RetestDetectionLimiter  = 2; # 'Retest Detection Limiter'
input breakoutBubbles = yes;   # 'Breakouts'
input retestWedges    = yes;   # 'Retests'
input repType   = {default "On", "Candle Confirmation", "High & Low"};    # 'Repainting'

def na = Double.NaN;
def retest = retestWedges;
def Breakout = breakoutBubbles;
def retSince = BarsSinceBreakout;
def retValid = RetestDetectionLimiter;
def bb              = lookback;
def rTon            = repType == repType."On";
def rTcc            = repType == repType."Candle Confirmation";
def rThv            = repType == repType."High & Low";

def srcHi; def srcLo;

if rTon {
    srcHi = close;
    srcLo = close;
    } else
if rTcc {
    srcHi = close[1];
    srcLo = close[1];
    } else
if rThv {
    srcHi = high;
    srcLo = low;
    } else {
    srcHi = srcHi[1];
    srcLo = srcLo[1];
}
#---- Colors
DefineGlobalColor("green" , CreateColor(8, 153, 129));
DefineGlobalColor("red"   , Color.PINK);#CreateColor(255, 82, 82));
#// Pivot Instance

script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else fix[1];
    plot result = fix;
}
#barssince(Condition) =>
script barssince {
input Condition = 0;
def barssince = if Condition then 1 else barssince[1] + 1;
plot return = barssince;
}
#valuewhen (cond, source, occurrence) =>
script valuewhen {
    input cond = 0;
    input source = 0;
    def offset = fold j = 0 to 200 with p while p < 2 do
                 p + ( if p == 1 then j - 2 else if GetValue(cond, j) then 1 else 0 );
    plot price = GetValue(source, offset - 1);
}
script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL = 5;    # default Pivot Lookback Left
    input lbR = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def pl_1 = fixnan(FindPivots(low, -1, bb, bb));
def ph_1 = fixnan(FindPivots(high, 1, bb, bb));
def hh = if high==highest(high, bb) then high else hh[1];
def ll = if low==lowest(low, bb) then low else ll[1];
def ph = if ph_1==0 then hh else ph_1;
def pl = if pl_1==0 then ll else pl_1;

#// Box Height
def s_yLoc = if low[1]  > low[-1]  then low[-1] else low[1];
def r_yLoc = if high[1] > high[-1] then high[1] else high[-1];
#// Functions
#drawBox(condition, y1, y2, color) =>
script drawBox {
    input condition = yes;
    input y1        = high;
    input y2        = low;
    def boxHi = if condition then y1 else boxHi[1];
    def boxLo = if condition then y2 else boxLo[1];
    plot ph = if !IsNaN(close[5]) and !IsNaN(close) then boxHi else Double.NaN;
    plot pl = if !IsNaN(close[5]) and !IsNaN(close) then boxLo else Double.NaN;
}
#repaint(c1, c2, c3) =>
script repaint {
    input type = "On";
    input c1 = no;
    input c2 = no;
    input c3 = no;
    def rTon = type == "On";
    def rTcc = type == "Candle Confirmation";
    def rThv = type == "High & Low";
    def repaint = if rTon then c1 else if rThv then c2 else if rTcc then c3 else repaint[1];
    plot out = repaint;
}
#retestCondition(breakout, condition) =>
Script retestCondition {
input breakout = no;
input condition = no;
input retSince = 2;
    def retestCondition = barssince(!breakout) > retSince and condition;
    plot out = retestCondition;
}
#// Draw and Update Boxes
def sBoxH = drawBox((pl != pl[1]), s_yLoc, pl).ph;
def sBoxL = drawBox((pl != pl[1]), s_yLoc, pl).pl;
def rBoxH = drawBox((ph != ph[1]), ph, r_yLoc).ph;
def rBoxL = drawBox((ph != ph[1]), ph, r_yLoc).pl;

def sTop = sBoxH;
def sBot = sBoxL;
def rTop = rBoxH;
def rBot = rBoxL;

#// Breakout Event
def sBreak;
def rBreak;
def sLabel;
def rLabel;

def c1 = if close  < sBoxL then c1[1] + 1 else 0;
def c2 = if low    < sBoxL then c2[1] + 1 else 0;
def c3 = if close[1] < sBoxL[1] then c3[1] + 1 else 0;
def d1 = if close  > rBoxH then d1[1] + 1 else 0;
def d2 = if high   > rBoxH then d2[1] + 1 else 0;
def d3 = if close[1] > rBoxH[1] then d3[1] + 1 else 0;

def cu = repaint(repType, c1==1, c2==1, c3==1);
def co = repaint(repType, d1==1, d2==1, d3==1);

def sBreak1; def rBreak1;def sBreak11; def rBreak11;
if (pl-pl[1]) {
    sBreak1 =  if !sBreak[1] then no else sBreak[1];
    rBreak1 = rBreak[1];
    } else
if (ph-ph[1]) {
    sBreak1 = sBreak[1];
    rBreak1 = if !rBreak[1] then no else sBreak[1];
    } else {
    sBreak1 =  yes;#sBreak[1];
    rBreak1 =  yes;#rBreak[1];
}
if cu and !sBreak1 {
        sBreak = yes;
        rBreak = rBreak1;
    } else
if co and !rBreak1 {
        sBreak = sBreak1;
        rBreak = yes;
    } else {
        sBreak = sBreak1;
        rBreak = rBreak1;
}
if (pl-pl[1]) {
    sBreak11 = if !sBreak11[1] then 0 else sBreak;
    rBreak11 = rBreak11[1];
    } else
if (ph-ph[1]) {
    sBreak11 = sBreak11[1];
    rBreak11 = if !rBreak11[1] then 0 else sBreak;
    } else {
    sBreak11 =  sBreak11[1];
    rBreak11 =  rBreak11[1];
}
if cu and !sBreak11 {
        sLabel = if breakout then sBoxL else na;
        rLabel = na;
    } else
if co and !rBreak11 {
        sLabel = na;
        rLabel = if breakout then rBoxH else na;
    } else {
        sLabel = na;
        rLabel = na;
}
def changePh = srcHi > highest(srcHi[1], bb);
def changePl = srcLo < lowest(srcLo[1] , bb);


AddChartBubble(changePl and !isNaN(sLabel), sLabel, "break", Color.RED, yes);
AddChartBubble(changePh and !isNaN(rLabel), rLabel, "break", Color.GREEN, no);

#// Retest Event
def s1_ = retestCondition(sBreak, high >= sTop and close <= sBot, retSince);
def s2_ = retestCondition(sBreak, high >= sTop and close >= sBot and close <= sTop, retSince);
def s3_ = retestCondition(sBreak, high >= sBot and high <= sTop, retSince);
def s4_ = retestCondition(sBreak, high >= sBot and high <= sTop and close < sBot, retSince);
def r1_ = retestCondition(rBreak, low <= rBot and close >= rTop, retSince);
def r2_ = retestCondition(rBreak, low <= rBot and close <= rTop and close >= rBot, retSince);
def r3_ = retestCondition(rBreak, low <= rTop and low >= rBot, retSince);
def r4_ = retestCondition(rBreak, low <= rTop and low >= rBot and close > rTop, retSince);

def s1 = s1_;
def s2 = s2_;
def s3 = s3_;
def s4 = s4_;
def r1 = r1_;
def r2 = r2_;
def r3 = r3_;
def r4 = r4_;

#retestEvent(c1, c2, c3, c4, y1, y2, col, style, pType) =>
Script retestEvent {
input c1 = 1;
input c2 = 1;
input c3 = 1;
input c4 = 1;
input y1 = high;
input y2 = low;
input pType = 1;
input input_retValid = 2;
input ph = high;
input pl = low;
input repType = "On";
     def retOccurred; def retEvent;
     def retActive   = c1 or c2 or c3 or c4;
     def retEvent1   = retActive and !retActive[1];
     def retEventVal = if retEvent1 then y2 else retEventVal[1];
        if (if pType == 1 then y2 < retEventVal else y2 > retEventVal) {
            retEvent = retActive;
        } else {
            retEvent = retEvent1;
}
#    def retValue = valuewhen(retEvent, y1);
    def retValue = if retEvent then y1 else retValue[1];
    def retSince = barssince(retEvent);
    def hc1 = close >= retValue;
    def hc2 = high >= retValue;
    def hc3 = close[1] >= retValue[1];
    def lc1 = close <= retValue;
    def lc2 = high <= retValue;
    def lc3 = close[1] <= retValue[1];
    def rTon = repType == "On";
    def rTcc = repType == "Candle Confirmation";
    def rThv = repType == "High & Low";
    def reph = if rTon then hc1 else if rThv then hc2 else if rTcc then hc3 else reph[1];
    def repl = if rTon then lc1 else if rThv then lc2 else if rTcc then lc3 else repl[1];
    def Potential = if retEvent then y2 else 0;
    def retConditions = if pType ==  1 then reph else repl;
    def retValid = retSince > 0 and retSince <= input_retValid and retConditions and !retOccurred[1];
        retOccurred = if retEvent then no else
                      if retValid then yes else
                      if pType == 1  and (ph!=ph[1]) and retOccurred[1] then no else
                      if pType == -1 and (pl!=pl[1]) and retOccurred[1] then no else retOccurred[1];
    def retest = if retValid then y2 else 0;
    def labelCount;
    if retEvent {
        labelCount = 0;
     } else {
        labelCount = labelCount[1] + 1;
}
    plot retVal = retest;
    plot retPot = Potential;
    plot count = labelCount;
}
def retH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retVal;
def PotH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retPot;
def cntH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).count;
def retL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retVal;
def PotL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retPot;
def cntL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).count;
#--
def retH = retH_;
def PotH = PotH_;
def cntH = cntH_;
def retL = retL_;
def PotL = PotL_;
def cntL = cntL_;

def retLCol = if retL then retL else retLCol[1];
def retHCol = if retH then retH else retHCol[1];
def breakUp  = srcHi>retHCol;#srcHi > rBot;
def breakDn  = srcLo<retLCol;#srcLo < sTop;

plot PotBreak = if retest then if PotH and cntH<2 then PotH else na else na;
PotBreak.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
PotBreak.SetDefaultColor(color.CYAN);

plot PotBreakDn = if retest then if PotL and cntL<2 then PotL else na else na;
PotBreakDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
PotBreakDn.SetDefaultColor(color.MAGENTA);

plot sTop_ = if sBoxH==0 then na else sBoxH;
plot sBot_ = if sBoxL==0 then na else sBoxL;
plot rTop_ = if rBoxH==0 then na else rBoxH;
plot rBot_ = if rBoxL==0 then na else rBoxL;

    sTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    sBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

    sTOP_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    sBot_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    rTop_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));
    rBot_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));


AddCloud(if breakDn then sTop else na, sBot, Color.DARK_RED);
AddCloud(if breakUp then rTop else na, rBot, Color.DARK_GREEN);

#--- END Code

Would it be possible to generate a scanner for this ? i know it wouldnt be possible with real time but looking at daily chart with previous data. Thanks
 
Would it be possible to generate a scanner for this ? i know it wouldnt be possible with real time but looking at daily chart with previous data. Thanks
This script is too complex to use in the scanner, watchlists, and conditional orders.
 
jRjZbsy.png


* Not Typical converting.

Creator Message: https://www.tradingview.com/script/Xeeko6TV-Support-Resistance-with-Breaks-and-Retests/

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © nsadeghi
# https://www.tradingview.com/v/Xeeko6TV/
#indicator("Support Resistance with Breaks and Retests",
# Converted and mod by Sam4Cok@Samer800     - 03/2023

input lookback  = 20;              # 'Lookback Range''How many bars for a pivot event to occur.'
input BarsSinceBreakout  = 2;      # 'Bars Since Breakout''How many bars since breakout in order to detect a retest.'
input RetestDetectionLimiter  = 2; # 'Retest Detection Limiter'
input breakoutBubbles = yes;   # 'Breakouts'
input retestWedges    = yes;   # 'Retests'
input repType   = {default "On", "Candle Confirmation", "High & Low"};    # 'Repainting'

def na = Double.NaN;
def retest = retestWedges;
def Breakout = breakoutBubbles;
def retSince = BarsSinceBreakout;
def retValid = RetestDetectionLimiter;
def bb              = lookback;
def rTon            = repType == repType."On";
def rTcc            = repType == repType."Candle Confirmation";
def rThv            = repType == repType."High & Low";

def srcHi; def srcLo;

if rTon {
    srcHi = close;
    srcLo = close;
    } else
if rTcc {
    srcHi = close[1];
    srcLo = close[1];
    } else
if rThv {
    srcHi = high;
    srcLo = low;
    } else {
    srcHi = srcHi[1];
    srcLo = srcLo[1];
}
#---- Colors
DefineGlobalColor("green" , CreateColor(8, 153, 129));
DefineGlobalColor("red"   , Color.PINK);#CreateColor(255, 82, 82));
#// Pivot Instance

script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else fix[1];
    plot result = fix;
}
#barssince(Condition) =>
script barssince {
input Condition = 0;
def barssince = if Condition then 1 else barssince[1] + 1;
plot return = barssince;
}
#valuewhen (cond, source, occurrence) =>
script valuewhen {
    input cond = 0;
    input source = 0;
    def offset = fold j = 0 to 200 with p while p < 2 do
                 p + ( if p == 1 then j - 2 else if GetValue(cond, j) then 1 else 0 );
    plot price = GetValue(source, offset - 1);
}
script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL = 5;    # default Pivot Lookback Left
    input lbR = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def pl_1 = fixnan(FindPivots(low, -1, bb, bb));
def ph_1 = fixnan(FindPivots(high, 1, bb, bb));
def hh = if high==highest(high, bb) then high else hh[1];
def ll = if low==lowest(low, bb) then low else ll[1];
def ph = if ph_1==0 then hh else ph_1;
def pl = if pl_1==0 then ll else pl_1;

#// Box Height
def s_yLoc = if low[1]  > low[-1]  then low[-1] else low[1];
def r_yLoc = if high[1] > high[-1] then high[1] else high[-1];
#// Functions
#drawBox(condition, y1, y2, color) =>
script drawBox {
    input condition = yes;
    input y1        = high;
    input y2        = low;
    def boxHi = if condition then y1 else boxHi[1];
    def boxLo = if condition then y2 else boxLo[1];
    plot ph = if !IsNaN(close[5]) and !IsNaN(close) then boxHi else Double.NaN;
    plot pl = if !IsNaN(close[5]) and !IsNaN(close) then boxLo else Double.NaN;
}
#repaint(c1, c2, c3) =>
script repaint {
    input type = "On";
    input c1 = no;
    input c2 = no;
    input c3 = no;
    def rTon = type == "On";
    def rTcc = type == "Candle Confirmation";
    def rThv = type == "High & Low";
    def repaint = if rTon then c1 else if rThv then c2 else if rTcc then c3 else repaint[1];
    plot out = repaint;
}
#retestCondition(breakout, condition) =>
Script retestCondition {
input breakout = no;
input condition = no;
input retSince = 2;
    def retestCondition = barssince(!breakout) > retSince and condition;
    plot out = retestCondition;
}
#// Draw and Update Boxes
def sBoxH = drawBox((pl != pl[1]), s_yLoc, pl).ph;
def sBoxL = drawBox((pl != pl[1]), s_yLoc, pl).pl;
def rBoxH = drawBox((ph != ph[1]), ph, r_yLoc).ph;
def rBoxL = drawBox((ph != ph[1]), ph, r_yLoc).pl;

def sTop = sBoxH;
def sBot = sBoxL;
def rTop = rBoxH;
def rBot = rBoxL;

#// Breakout Event
def sBreak;
def rBreak;
def sLabel;
def rLabel;

def c1 = if close  < sBoxL then c1[1] + 1 else 0;
def c2 = if low    < sBoxL then c2[1] + 1 else 0;
def c3 = if close[1] < sBoxL[1] then c3[1] + 1 else 0;
def d1 = if close  > rBoxH then d1[1] + 1 else 0;
def d2 = if high   > rBoxH then d2[1] + 1 else 0;
def d3 = if close[1] > rBoxH[1] then d3[1] + 1 else 0;

def cu = repaint(repType, c1==1, c2==1, c3==1);
def co = repaint(repType, d1==1, d2==1, d3==1);

def sBreak1; def rBreak1;def sBreak11; def rBreak11;
if (pl-pl[1]) {
    sBreak1 =  if !sBreak[1] then no else sBreak[1];
    rBreak1 = rBreak[1];
    } else
if (ph-ph[1]) {
    sBreak1 = sBreak[1];
    rBreak1 = if !rBreak[1] then no else sBreak[1];
    } else {
    sBreak1 =  yes;#sBreak[1];
    rBreak1 =  yes;#rBreak[1];
}
if cu and !sBreak1 {
        sBreak = yes;
        rBreak = rBreak1;
    } else
if co and !rBreak1 {
        sBreak = sBreak1;
        rBreak = yes;
    } else {
        sBreak = sBreak1;
        rBreak = rBreak1;
}
if (pl-pl[1]) {
    sBreak11 = if !sBreak11[1] then 0 else sBreak;
    rBreak11 = rBreak11[1];
    } else
if (ph-ph[1]) {
    sBreak11 = sBreak11[1];
    rBreak11 = if !rBreak11[1] then 0 else sBreak;
    } else {
    sBreak11 =  sBreak11[1];
    rBreak11 =  rBreak11[1];
}
if cu and !sBreak11 {
        sLabel = if breakout then sBoxL else na;
        rLabel = na;
    } else
if co and !rBreak11 {
        sLabel = na;
        rLabel = if breakout then rBoxH else na;
    } else {
        sLabel = na;
        rLabel = na;
}
def changePh = srcHi > highest(srcHi[1], bb);
def changePl = srcLo < lowest(srcLo[1] , bb);


AddChartBubble(changePl and !isNaN(sLabel), sLabel, "break", Color.RED, yes);
AddChartBubble(changePh and !isNaN(rLabel), rLabel, "break", Color.GREEN, no);

#// Retest Event
def s1_ = retestCondition(sBreak, high >= sTop and close <= sBot, retSince);
def s2_ = retestCondition(sBreak, high >= sTop and close >= sBot and close <= sTop, retSince);
def s3_ = retestCondition(sBreak, high >= sBot and high <= sTop, retSince);
def s4_ = retestCondition(sBreak, high >= sBot and high <= sTop and close < sBot, retSince);
def r1_ = retestCondition(rBreak, low <= rBot and close >= rTop, retSince);
def r2_ = retestCondition(rBreak, low <= rBot and close <= rTop and close >= rBot, retSince);
def r3_ = retestCondition(rBreak, low <= rTop and low >= rBot, retSince);
def r4_ = retestCondition(rBreak, low <= rTop and low >= rBot and close > rTop, retSince);

def s1 = s1_;
def s2 = s2_;
def s3 = s3_;
def s4 = s4_;
def r1 = r1_;
def r2 = r2_;
def r3 = r3_;
def r4 = r4_;

#retestEvent(c1, c2, c3, c4, y1, y2, col, style, pType) =>
Script retestEvent {
input c1 = 1;
input c2 = 1;
input c3 = 1;
input c4 = 1;
input y1 = high;
input y2 = low;
input pType = 1;
input input_retValid = 2;
input ph = high;
input pl = low;
input repType = "On";
     def retOccurred; def retEvent;
     def retActive   = c1 or c2 or c3 or c4;
     def retEvent1   = retActive and !retActive[1];
     def retEventVal = if retEvent1 then y2 else retEventVal[1];
        if (if pType == 1 then y2 < retEventVal else y2 > retEventVal) {
            retEvent = retActive;
        } else {
            retEvent = retEvent1;
}
#    def retValue = valuewhen(retEvent, y1);
    def retValue = if retEvent then y1 else retValue[1];
    def retSince = barssince(retEvent);
    def hc1 = close >= retValue;
    def hc2 = high >= retValue;
    def hc3 = close[1] >= retValue[1];
    def lc1 = close <= retValue;
    def lc2 = high <= retValue;
    def lc3 = close[1] <= retValue[1];
    def rTon = repType == "On";
    def rTcc = repType == "Candle Confirmation";
    def rThv = repType == "High & Low";
    def reph = if rTon then hc1 else if rThv then hc2 else if rTcc then hc3 else reph[1];
    def repl = if rTon then lc1 else if rThv then lc2 else if rTcc then lc3 else repl[1];
    def Potential = if retEvent then y2 else 0;
    def retConditions = if pType ==  1 then reph else repl;
    def retValid = retSince > 0 and retSince <= input_retValid and retConditions and !retOccurred[1];
        retOccurred = if retEvent then no else
                      if retValid then yes else
                      if pType == 1  and (ph!=ph[1]) and retOccurred[1] then no else
                      if pType == -1 and (pl!=pl[1]) and retOccurred[1] then no else retOccurred[1];
    def retest = if retValid then y2 else 0;
    def labelCount;
    if retEvent {
        labelCount = 0;
     } else {
        labelCount = labelCount[1] + 1;
}
    plot retVal = retest;
    plot retPot = Potential;
    plot count = labelCount;
}
def retH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retVal;
def PotH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retPot;
def cntH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).count;
def retL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retVal;
def PotL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retPot;
def cntL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).count;
#--
def retH = retH_;
def PotH = PotH_;
def cntH = cntH_;
def retL = retL_;
def PotL = PotL_;
def cntL = cntL_;

def retLCol = if retL then retL else retLCol[1];
def retHCol = if retH then retH else retHCol[1];
def breakUp  = srcHi>retHCol;#srcHi > rBot;
def breakDn  = srcLo<retLCol;#srcLo < sTop;

plot PotBreak = if retest then if PotH and cntH<2 then PotH else na else na;
PotBreak.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
PotBreak.SetDefaultColor(color.CYAN);

plot PotBreakDn = if retest then if PotL and cntL<2 then PotL else na else na;
PotBreakDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
PotBreakDn.SetDefaultColor(color.MAGENTA);

plot sTop_ = if sBoxH==0 then na else sBoxH;
plot sBot_ = if sBoxL==0 then na else sBoxL;
plot rTop_ = if rBoxH==0 then na else rBoxH;
plot rBot_ = if rBoxL==0 then na else rBoxL;

    sTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    sBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

    sTOP_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    sBot_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    rTop_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));
    rBot_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));


AddCloud(if breakDn then sTop else na, sBot, Color.DARK_RED);
AddCloud(if breakUp then rTop else na, rBot, Color.DARK_GREEN);

#--- END Code
Thank You. This was my request a few days ago. I was informed it couldn't be done.
 
Thank You. This was my request a few days ago. I was informed it couldn't be done.
the code it self can't be converted, I just rebuilt it to work in TOS as most of the indicator I convert.
Still, many indicators still I couldn't convert.
 
Is it possible too see only current day? or like 2 days rather then automatically seeing multiple days. Thank you. Great script
check the below

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © nsadeghi
# https://www.tradingview.com/v/Xeeko6TV/
#indicator("Support Resistance with Breaks and Retests",
# Converted and mod by Sam4Cok@Samer800     - 03/2023
# update - added option to show today plot only - 05/2023

input lookback  = 20;              # 'Lookback Range''How many bars for a pivot event to occur.'
input ShowTodayOnly = no;
input BarsSinceBreakout  = 2;      # 'Bars Since Breakout''How many bars since breakout in order to detect a retest.'
input RetestDetectionLimiter  = 2; # 'Retest Detection Limiter'
input breakoutBubbles = no;   # 'Breakouts'
input retestWedges    = no;   # 'Retests'
input repType   = {default "On", "Candle Confirmation", "High & Low"};    # 'Repainting'

def na = Double.NaN;
def retest = retestWedges;
def Breakout = breakoutBubbles;
def retSince = BarsSinceBreakout;
def retValid = RetestDetectionLimiter;
def bb              = lookback;
def today = if !ShowTodayOnly then 1 else GetDay()== GetLastDay();
def rTon            = repType == repType."On";
def rTcc            = repType == repType."Candle Confirmation";
def rThv            = repType == repType."High & Low";

def srcHi; def srcLo;

if rTon {
    srcHi = close;
    srcLo = close;
    } else
if rTcc {
    srcHi = close[1];
    srcLo = close[1];
    } else
if rThv {
    srcHi = high;
    srcLo = low;
    } else {
    srcHi = srcHi[1];
    srcLo = srcLo[1];
}
#---- Colors
DefineGlobalColor("green" , CreateColor(8, 153, 129));
DefineGlobalColor("red"   , Color.PINK);#CreateColor(255, 82, 82));
#// Pivot Instance

script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else fix[1];
    plot result = fix;
}
#barssince(Condition) =>
script barssince {
input Condition = 0;
def barssince = if Condition then 1 else barssince[1] + 1;
plot return = barssince;
}
#valuewhen (cond, source, occurrence) =>
script valuewhen {
    input cond = 0;
    input source = 0;
    def offset = fold j = 0 to 200 with p while p < 2 do
                 p + ( if p == 1 then j - 2 else if GetValue(cond, j) then 1 else 0 );
    plot price = GetValue(source, offset - 1);
}
script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL = 5;    # default Pivot Lookback Left
    input lbR = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def pl_1 = fixnan(FindPivots(low, -1, bb, bb));
def ph_1 = fixnan(FindPivots(high, 1, bb, bb));
def hh = if high==highest(high, bb) then high else hh[1];
def ll = if low==lowest(low, bb) then low else ll[1];
def ph = if ph_1==0 then hh else ph_1;
def pl = if pl_1==0 then ll else pl_1;

#// Box Height
def s_yLoc = if low[1]  > low[-1]  then low[-1] else low[1];
def r_yLoc = if high[1] > high[-1] then high[1] else high[-1];
#// Functions
#drawBox(condition, y1, y2, color) =>
script drawBox {
    input condition = yes;
    input y1        = high;
    input y2        = low;
    def bar = AbsValue(CompoundValue(1, BarNumber(), 0));
    def boxHi = if condition then y1 else boxHi[1];
    def boxLo = if condition then y2 else boxLo[1];
    plot ph = if bar < 1 then Double.NaN else if !IsNaN(close[5]) and !IsNaN(close) then boxHi else Double.NaN;
    plot pl = if bar < 1 then Double.NaN else if !IsNaN(close[5]) and !IsNaN(close) then boxLo else Double.NaN;
}
#repaint(c1, c2, c3) =>
script repaint {
    input type = "On";
    input c1 = no;
    input c2 = no;
    input c3 = no;
    def rTon = type == "On";
    def rTcc = type == "Candle Confirmation";
    def rThv = type == "High & Low";
    def repaint = if rTon then c1 else if rThv then c2 else if rTcc then c3 else repaint[1];
    plot out = repaint;
}
#retestCondition(breakout, condition) =>
Script retestCondition {
input breakout = no;
input condition = no;
input retSince = 2;
    def retestCondition = barssince(!breakout) > retSince and condition;
    plot out = retestCondition;
}
#// Draw and Update Boxes
def sBoxH = drawBox((pl != pl[1]), s_yLoc, pl).ph;
def sBoxL = drawBox((pl != pl[1]), s_yLoc, pl).pl;
def rBoxH = drawBox((ph != ph[1]), ph, r_yLoc).ph;
def rBoxL = drawBox((ph != ph[1]), ph, r_yLoc).pl;

def sTop = sBoxH;
def sBot = sBoxL;
def rTop = rBoxH;
def rBot = rBoxL;

#// Breakout Event
def sBreak;
def rBreak;
def sLabel;
def rLabel;

def c1 = if close  < sBoxL then c1[1] + 1 else 0;
def c2 = if low    < sBoxL then c2[1] + 1 else 0;
def c3 = if close[1] < sBoxL[1] then c3[1] + 1 else 0;
def d1 = if close  > rBoxH then d1[1] + 1 else 0;
def d2 = if high   > rBoxH then d2[1] + 1 else 0;
def d3 = if close[1] > rBoxH[1] then d3[1] + 1 else 0;

def cu = repaint(repType, c1==1, c2==1, c3==1);
def co = repaint(repType, d1==1, d2==1, d3==1);

def sBreak1; def rBreak1;def sBreak11; def rBreak11;
if (pl-pl[1]) {
    sBreak1 =  if !sBreak[1] then no else sBreak[1];
    rBreak1 = rBreak[1];
    } else
if (ph-ph[1]) {
    sBreak1 = sBreak[1];
    rBreak1 = if !rBreak[1] then no else sBreak[1];
    } else {
    sBreak1 =  yes;#sBreak[1];
    rBreak1 =  yes;#rBreak[1];
}
if cu and !sBreak1 {
        sBreak = yes;
        rBreak = rBreak1;
    } else
if co and !rBreak1 {
        sBreak = sBreak1;
        rBreak = yes;
    } else {
        sBreak = sBreak1;
        rBreak = rBreak1;
}
if (pl-pl[1]) {
    sBreak11 = if !sBreak11[1] then 0 else sBreak;
    rBreak11 = rBreak11[1];
    } else
if (ph-ph[1]) {
    sBreak11 = sBreak11[1];
    rBreak11 = if !rBreak11[1] then 0 else sBreak;
    } else {
    sBreak11 =  sBreak11[1];
    rBreak11 =  rBreak11[1];
}
if cu and !sBreak11 {
        sLabel = if breakout then sBoxL else na;
        rLabel = na;
    } else
if co and !rBreak11 {
        sLabel = na;
        rLabel = if breakout then rBoxH else na;
    } else {
        sLabel = na;
        rLabel = na;
}
def changePh = srcHi > highest(srcHi[1], bb);
def changePl = srcLo < lowest(srcLo[1] , bb);


AddChartBubble(changePl and !isNaN(sLabel), sLabel, "break", Color.RED, yes);
AddChartBubble(changePh and !isNaN(rLabel), rLabel, "break", Color.GREEN, no);

#// Retest Event
def s1_ = retestCondition(sBreak, high >= sTop and close <= sBot, retSince);
def s2_ = retestCondition(sBreak, high >= sTop and close >= sBot and close <= sTop, retSince);
def s3_ = retestCondition(sBreak, high >= sBot and high <= sTop, retSince);
def s4_ = retestCondition(sBreak, high >= sBot and high <= sTop and close < sBot, retSince);
def r1_ = retestCondition(rBreak, low <= rBot and close >= rTop, retSince);
def r2_ = retestCondition(rBreak, low <= rBot and close <= rTop and close >= rBot, retSince);
def r3_ = retestCondition(rBreak, low <= rTop and low >= rBot, retSince);
def r4_ = retestCondition(rBreak, low <= rTop and low >= rBot and close > rTop, retSince);

def s1 = s1_;
def s2 = s2_;
def s3 = s3_;
def s4 = s4_;
def r1 = r1_;
def r2 = r2_;
def r3 = r3_;
def r4 = r4_;

#retestEvent(c1, c2, c3, c4, y1, y2, col, style, pType) =>
Script retestEvent {
input c1 = 1;
input c2 = 1;
input c3 = 1;
input c4 = 1;
input y1 = high;
input y2 = low;
input pType = 1;
input input_retValid = 2;
input ph = high;
input pl = low;
input repType = "On";
     def retOccurred; def retEvent;
     def retActive   = c1 or c2 or c3 or c4;
     def retEvent1   = retActive and !retActive[1];
     def retEventVal = if retEvent1 then y2 else retEventVal[1];
        if (if pType == 1 then y2 < retEventVal else y2 > retEventVal) {
            retEvent = retActive;
        } else {
            retEvent = retEvent1;
}
#    def retValue = valuewhen(retEvent, y1);
    def retValue = if retEvent then y1 else retValue[1];
    def retSince = barssince(retEvent);
    def hc1 = close >= retValue;
    def hc2 = high >= retValue;
    def hc3 = close[1] >= retValue[1];
    def lc1 = close <= retValue;
    def lc2 = high <= retValue;
    def lc3 = close[1] <= retValue[1];
    def rTon = repType == "On";
    def rTcc = repType == "Candle Confirmation";
    def rThv = repType == "High & Low";
    def reph = if rTon then hc1 else if rThv then hc2 else if rTcc then hc3 else reph[1];
    def repl = if rTon then lc1 else if rThv then lc2 else if rTcc then lc3 else repl[1];
    def Potential = if retEvent then y2 else 0;
    def retConditions = if pType ==  1 then reph else repl;
    def retValid = retSince > 0 and retSince <= input_retValid and retConditions and !retOccurred[1];
        retOccurred = if retEvent then no else
                      if retValid then yes else
                      if pType == 1  and (ph!=ph[1]) and retOccurred[1] then no else
                      if pType == -1 and (pl!=pl[1]) and retOccurred[1] then no else retOccurred[1];
    def retest = if retValid then y2 else 0;
    def labelCount;
    if retEvent {
        labelCount = 0;
     } else {
        labelCount = labelCount[1] + 1;
}
    plot retVal = retest;
    plot retPot = Potential;
    plot count = labelCount;
}
def retH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retVal;
def PotH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retPot;
def cntH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).count;
def retL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retVal;
def PotL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retPot;
def cntL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).count;
#--
def retH = retH_;
def PotH = PotH_;
def cntH = cntH_;
def retL = retL_;
def PotL = PotL_;
def cntL = cntL_;

def retLCol = if retL then retL else retLCol[1];
def retHCol = if retH then retH else retHCol[1];
def breakUp  = srcHi>retHCol;#srcHi > rBot;
def breakDn  = srcLo<retLCol;#srcLo < sTop;

plot PotBreak = if today then if retest then if PotH and cntH<2 then PotH else na else na else na;
PotBreak.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
PotBreak.SetDefaultColor(color.CYAN);

plot PotBreakDn = if today then if retest then if PotL and cntL<2 then PotL else na else na else na;
PotBreakDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
PotBreakDn.SetDefaultColor(color.MAGENTA);

plot sTop_ = if today then if sBoxH==0 then na else sBoxH else na;
plot sBot_ = if today then if sBoxL==0 then na else sBoxL else na;
plot rTop_ = if today then if rBoxH==0 then na else rBoxH else na;
plot rBot_ = if today then if rBoxL==0 then na else rBoxL else na;

    sTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    sBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

    sTOP_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    sBot_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    rTop_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));
    rBot_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));


AddCloud(if !today then na else if breakDn then sTop else na, sBot, Color.DARK_RED);
AddCloud(if !today then na else if breakUp then rTop else na, rBot, Color.DARK_GREEN);

#--- END Code
 
check the below

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © nsadeghi
# https://www.tradingview.com/v/Xeeko6TV/
#indicator("Support Resistance with Breaks and Retests",
# Converted and mod by Sam4Cok@Samer800     - 03/2023
# update - added option to show today plot only - 05/2023

input lookback  = 20;              # 'Lookback Range''How many bars for a pivot event to occur.'
input ShowTodayOnly = no;
input BarsSinceBreakout  = 2;      # 'Bars Since Breakout''How many bars since breakout in order to detect a retest.'
input RetestDetectionLimiter  = 2; # 'Retest Detection Limiter'
input breakoutBubbles = no;   # 'Breakouts'
input retestWedges    = no;   # 'Retests'
input repType   = {default "On", "Candle Confirmation", "High & Low"};    # 'Repainting'

def na = Double.NaN;
def retest = retestWedges;
def Breakout = breakoutBubbles;
def retSince = BarsSinceBreakout;
def retValid = RetestDetectionLimiter;
def bb              = lookback;
def today = if !ShowTodayOnly then 1 else GetDay()== GetLastDay();
def rTon            = repType == repType."On";
def rTcc            = repType == repType."Candle Confirmation";
def rThv            = repType == repType."High & Low";

def srcHi; def srcLo;

if rTon {
    srcHi = close;
    srcLo = close;
    } else
if rTcc {
    srcHi = close[1];
    srcLo = close[1];
    } else
if rThv {
    srcHi = high;
    srcLo = low;
    } else {
    srcHi = srcHi[1];
    srcLo = srcLo[1];
}
#---- Colors
DefineGlobalColor("green" , CreateColor(8, 153, 129));
DefineGlobalColor("red"   , Color.PINK);#CreateColor(255, 82, 82));
#// Pivot Instance

script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else fix[1];
    plot result = fix;
}
#barssince(Condition) =>
script barssince {
input Condition = 0;
def barssince = if Condition then 1 else barssince[1] + 1;
plot return = barssince;
}
#valuewhen (cond, source, occurrence) =>
script valuewhen {
    input cond = 0;
    input source = 0;
    def offset = fold j = 0 to 200 with p while p < 2 do
                 p + ( if p == 1 then j - 2 else if GetValue(cond, j) then 1 else 0 );
    plot price = GetValue(source, offset - 1);
}
script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL = 5;    # default Pivot Lookback Left
    input lbR = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def pl_1 = fixnan(FindPivots(low, -1, bb, bb));
def ph_1 = fixnan(FindPivots(high, 1, bb, bb));
def hh = if high==highest(high, bb) then high else hh[1];
def ll = if low==lowest(low, bb) then low else ll[1];
def ph = if ph_1==0 then hh else ph_1;
def pl = if pl_1==0 then ll else pl_1;

#// Box Height
def s_yLoc = if low[1]  > low[-1]  then low[-1] else low[1];
def r_yLoc = if high[1] > high[-1] then high[1] else high[-1];
#// Functions
#drawBox(condition, y1, y2, color) =>
script drawBox {
    input condition = yes;
    input y1        = high;
    input y2        = low;
    def bar = AbsValue(CompoundValue(1, BarNumber(), 0));
    def boxHi = if condition then y1 else boxHi[1];
    def boxLo = if condition then y2 else boxLo[1];
    plot ph = if bar < 1 then Double.NaN else if !IsNaN(close[5]) and !IsNaN(close) then boxHi else Double.NaN;
    plot pl = if bar < 1 then Double.NaN else if !IsNaN(close[5]) and !IsNaN(close) then boxLo else Double.NaN;
}
#repaint(c1, c2, c3) =>
script repaint {
    input type = "On";
    input c1 = no;
    input c2 = no;
    input c3 = no;
    def rTon = type == "On";
    def rTcc = type == "Candle Confirmation";
    def rThv = type == "High & Low";
    def repaint = if rTon then c1 else if rThv then c2 else if rTcc then c3 else repaint[1];
    plot out = repaint;
}
#retestCondition(breakout, condition) =>
Script retestCondition {
input breakout = no;
input condition = no;
input retSince = 2;
    def retestCondition = barssince(!breakout) > retSince and condition;
    plot out = retestCondition;
}
#// Draw and Update Boxes
def sBoxH = drawBox((pl != pl[1]), s_yLoc, pl).ph;
def sBoxL = drawBox((pl != pl[1]), s_yLoc, pl).pl;
def rBoxH = drawBox((ph != ph[1]), ph, r_yLoc).ph;
def rBoxL = drawBox((ph != ph[1]), ph, r_yLoc).pl;

def sTop = sBoxH;
def sBot = sBoxL;
def rTop = rBoxH;
def rBot = rBoxL;

#// Breakout Event
def sBreak;
def rBreak;
def sLabel;
def rLabel;

def c1 = if close  < sBoxL then c1[1] + 1 else 0;
def c2 = if low    < sBoxL then c2[1] + 1 else 0;
def c3 = if close[1] < sBoxL[1] then c3[1] + 1 else 0;
def d1 = if close  > rBoxH then d1[1] + 1 else 0;
def d2 = if high   > rBoxH then d2[1] + 1 else 0;
def d3 = if close[1] > rBoxH[1] then d3[1] + 1 else 0;

def cu = repaint(repType, c1==1, c2==1, c3==1);
def co = repaint(repType, d1==1, d2==1, d3==1);

def sBreak1; def rBreak1;def sBreak11; def rBreak11;
if (pl-pl[1]) {
    sBreak1 =  if !sBreak[1] then no else sBreak[1];
    rBreak1 = rBreak[1];
    } else
if (ph-ph[1]) {
    sBreak1 = sBreak[1];
    rBreak1 = if !rBreak[1] then no else sBreak[1];
    } else {
    sBreak1 =  yes;#sBreak[1];
    rBreak1 =  yes;#rBreak[1];
}
if cu and !sBreak1 {
        sBreak = yes;
        rBreak = rBreak1;
    } else
if co and !rBreak1 {
        sBreak = sBreak1;
        rBreak = yes;
    } else {
        sBreak = sBreak1;
        rBreak = rBreak1;
}
if (pl-pl[1]) {
    sBreak11 = if !sBreak11[1] then 0 else sBreak;
    rBreak11 = rBreak11[1];
    } else
if (ph-ph[1]) {
    sBreak11 = sBreak11[1];
    rBreak11 = if !rBreak11[1] then 0 else sBreak;
    } else {
    sBreak11 =  sBreak11[1];
    rBreak11 =  rBreak11[1];
}
if cu and !sBreak11 {
        sLabel = if breakout then sBoxL else na;
        rLabel = na;
    } else
if co and !rBreak11 {
        sLabel = na;
        rLabel = if breakout then rBoxH else na;
    } else {
        sLabel = na;
        rLabel = na;
}
def changePh = srcHi > highest(srcHi[1], bb);
def changePl = srcLo < lowest(srcLo[1] , bb);


AddChartBubble(changePl and !isNaN(sLabel), sLabel, "break", Color.RED, yes);
AddChartBubble(changePh and !isNaN(rLabel), rLabel, "break", Color.GREEN, no);

#// Retest Event
def s1_ = retestCondition(sBreak, high >= sTop and close <= sBot, retSince);
def s2_ = retestCondition(sBreak, high >= sTop and close >= sBot and close <= sTop, retSince);
def s3_ = retestCondition(sBreak, high >= sBot and high <= sTop, retSince);
def s4_ = retestCondition(sBreak, high >= sBot and high <= sTop and close < sBot, retSince);
def r1_ = retestCondition(rBreak, low <= rBot and close >= rTop, retSince);
def r2_ = retestCondition(rBreak, low <= rBot and close <= rTop and close >= rBot, retSince);
def r3_ = retestCondition(rBreak, low <= rTop and low >= rBot, retSince);
def r4_ = retestCondition(rBreak, low <= rTop and low >= rBot and close > rTop, retSince);

def s1 = s1_;
def s2 = s2_;
def s3 = s3_;
def s4 = s4_;
def r1 = r1_;
def r2 = r2_;
def r3 = r3_;
def r4 = r4_;

#retestEvent(c1, c2, c3, c4, y1, y2, col, style, pType) =>
Script retestEvent {
input c1 = 1;
input c2 = 1;
input c3 = 1;
input c4 = 1;
input y1 = high;
input y2 = low;
input pType = 1;
input input_retValid = 2;
input ph = high;
input pl = low;
input repType = "On";
     def retOccurred; def retEvent;
     def retActive   = c1 or c2 or c3 or c4;
     def retEvent1   = retActive and !retActive[1];
     def retEventVal = if retEvent1 then y2 else retEventVal[1];
        if (if pType == 1 then y2 < retEventVal else y2 > retEventVal) {
            retEvent = retActive;
        } else {
            retEvent = retEvent1;
}
#    def retValue = valuewhen(retEvent, y1);
    def retValue = if retEvent then y1 else retValue[1];
    def retSince = barssince(retEvent);
    def hc1 = close >= retValue;
    def hc2 = high >= retValue;
    def hc3 = close[1] >= retValue[1];
    def lc1 = close <= retValue;
    def lc2 = high <= retValue;
    def lc3 = close[1] <= retValue[1];
    def rTon = repType == "On";
    def rTcc = repType == "Candle Confirmation";
    def rThv = repType == "High & Low";
    def reph = if rTon then hc1 else if rThv then hc2 else if rTcc then hc3 else reph[1];
    def repl = if rTon then lc1 else if rThv then lc2 else if rTcc then lc3 else repl[1];
    def Potential = if retEvent then y2 else 0;
    def retConditions = if pType ==  1 then reph else repl;
    def retValid = retSince > 0 and retSince <= input_retValid and retConditions and !retOccurred[1];
        retOccurred = if retEvent then no else
                      if retValid then yes else
                      if pType == 1  and (ph!=ph[1]) and retOccurred[1] then no else
                      if pType == -1 and (pl!=pl[1]) and retOccurred[1] then no else retOccurred[1];
    def retest = if retValid then y2 else 0;
    def labelCount;
    if retEvent {
        labelCount = 0;
     } else {
        labelCount = labelCount[1] + 1;
}
    plot retVal = retest;
    plot retPot = Potential;
    plot count = labelCount;
}
def retH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retVal;
def PotH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retPot;
def cntH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).count;
def retL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retVal;
def PotL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retPot;
def cntL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).count;
#--
def retH = retH_;
def PotH = PotH_;
def cntH = cntH_;
def retL = retL_;
def PotL = PotL_;
def cntL = cntL_;

def retLCol = if retL then retL else retLCol[1];
def retHCol = if retH then retH else retHCol[1];
def breakUp  = srcHi>retHCol;#srcHi > rBot;
def breakDn  = srcLo<retLCol;#srcLo < sTop;

plot PotBreak = if today then if retest then if PotH and cntH<2 then PotH else na else na else na;
PotBreak.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
PotBreak.SetDefaultColor(color.CYAN);

plot PotBreakDn = if today then if retest then if PotL and cntL<2 then PotL else na else na else na;
PotBreakDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
PotBreakDn.SetDefaultColor(color.MAGENTA);

plot sTop_ = if today then if sBoxH==0 then na else sBoxH else na;
plot sBot_ = if today then if sBoxL==0 then na else sBoxL else na;
plot rTop_ = if today then if rBoxH==0 then na else rBoxH else na;
plot rBot_ = if today then if rBoxL==0 then na else rBoxL else na;

    sTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    sBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

    sTOP_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    sBot_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    rTop_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));
    rBot_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));


AddCloud(if !today then na else if breakDn then sTop else na, sBot, Color.DARK_RED);
AddCloud(if !today then na else if breakUp then rTop else na, rBot, Color.DARK_GREEN);

#--- END Code
Thank you. Learned how to code "current day" using your script as a base.
 
Hey @samer800, thanks for creating the TOS version for this indicator, is there a way to make it plot faster? It becomes useless in real time because it's displayed after five candles are closed. So if you want to enter a trade, it will be too late.
 
Hey @samer800, thanks for creating the TOS version for this indicator, is there a way to make it plot faster? It becomes useless in real time because it's displayed after five candles are closed. So if you want to enter a trade, it will be too late.
i did some code change. pls test.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © nsadeghi
# https://www.tradingview.com/v/Xeeko6TV/
#indicator("Support Resistance with Breaks and Retests",
# Converted and mod by Sam4Cok@Samer800     - 03/2023
# update - added option to show today plot only - 05/2023
# some Code fix - Sam4Cok@Samer800     - 07/2023

input lookback  = 20;         # 'Lookback Range''How many bars for a pivot event to occur.'
input ShowTodayOnly = no;
input BarsSinceBreakout  = 2; # 'Bars Since Breakout''How many bars since breakout in order to detect a retest.'
input RetestDetectionLimiter  = 2; # 'Retest Detection Limiter'
input breakoutBubbles = no;   # 'Breakouts'
input retestWedges    = no;   # 'Retests'
input repType   = {"On",default "Candle Confirmation", "High & Low"};    # 'Repainting'

def na = Double.NaN;
def last = isNaN(close);
def bar = if last then bar[1] else bar[1]+1;
def retest = retestWedges;
def Breakout = breakoutBubbles;
def retSince = BarsSinceBreakout;
def retValid = RetestDetectionLimiter;
def bb       = lookback;
def today = if !ShowTodayOnly then yes else GetDay()== GetLastDay();

def srcHi; def srcLo;
Switch (repType) {
Case "On" :
    srcHi = close;
    srcLo = close;
Case "Candle Confirmation" :
    srcHi = close[1];
    srcLo = close[1];
Case "High & Low" :
    srcHi = high;
    srcLo = low;
}
#---- Colors
DefineGlobalColor("green" , CreateColor(8, 153, 129));
DefineGlobalColor("red"   , Color.PINK);#CreateColor(255, 82, 82));
#// Pivot Instance

script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else fix[1];
    plot result = fix;
}
#barssince(Condition) =>
script barssince {
input Condition = 0;
def barssince = if Condition then 0 else barssince[1] + 1;
plot return = barssince;
}

script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL = 5;    # default Pivot Lookback Left
    input lbR = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def pl_1 = fixnan(FindPivots(low, -1, bb, bb));
def ph_1 = fixnan(FindPivots(high, 1, bb, bb));
def hh = if high==highest(high, bb) then high else hh[1];
def ll = if low==lowest(low, bb) then low else ll[1];
def ph = if ph_1==0 then hh else ph_1;
def pl = if pl_1==0 then ll else pl_1;

#// Box Height
def s_yLoc = if low[1]  > low[-1]  then low[-1] else low[1];
def r_yLoc = if high[1] > high[-1] then high[1] else high[-1];
#// Functions
#repaint(c1, c2, c3) =>
script repaint {
    input type = "On";
    input c1 = no;
    input c2 = no;
    input c3 = no;
    def rTon = type == "On";
    def rTcc = type == "Candle Confirmation";
    def rThv = type == "High & Low";
    def repaint = if rTon then c1 else if rThv then c2 else if rTcc then c3 else Double.NaN;
    plot out = repaint;
}
#retestCondition(breakout, condition) =>
Script retestCondition {
input breakout = no;
input condition = no;
input retSince = 2;
    def retestCondition = barssince(isNaN(breakout)) > retSince and condition;
    plot out = retestCondition;
}
#// Draw and Update Boxes
def sBoxH = if last then na else if pl != pl[1] then s_yLoc else sBoxH[1];#  pl).ph;
def sBoxL = if last then na else if pl != pl[1] then pl else sBoxL[1];
def rBoxH = if last then na else if ph != ph[1] then ph else rBoxH[1];
def rBoxL = if last then na else if ph != ph[1] then r_yLoc else rBoxL[1];

def sTop  = sBoxH;
plot sBot = sBoxL;
def rTop  = rBoxH;
plot rBot = rBoxL;
sBot.SetHiding(yes);
rBot.SetHiding(yes);
#// Breakout Event

def c1 = (close < sBoxL) and (close[1] >= sBoxL[1]);
def c2 = (low   < sBoxL) and (low[1]   >= sBoxL[1]);
def d1 = (close > rBoxH) and (close[1] <= rBoxH[1]);
def d2 = (high  > rBoxH) and (high[1]  <= rBoxH[1]);

def cu = repaint(repType, c1, c2, c1 and !last[-1]);
def co = repaint(repType, d1, d2, d1 and !last[-1]);
def plChange = pl-pl[1];
def phChange = ph-ph[1];

def sBreak1; def rBreak1;
#def sBreak11; def rBreak11;
def sBreak; def rBreak;
def sLabel; def rLabel;

if cu and isNaN(sBreak1[1]) {
        sBreak = yes;
        rBreak = rBreak[1];
        sLabel = if breakout then sBoxL else na;
        rLabel = na;
    } else
if co and isNaN(rBreak1[1]) {
        sBreak = sBreak[1];
        rBreak = yes;
        sLabel = na;
        rLabel = if breakout then rBoxH else na;
    } else {
        sBreak = sBreak[1];
        rBreak = rBreak[1];
        sLabel = na;
        rLabel = na;
}
if plChange {
    sBreak1 =  if isNaN(sBreak[1]) then sBreak[1] else na;
    rBreak1 = rBreak[1];
    } else
if phChange {
    sBreak1 = sBreak[1];
    rBreak1 = if isNaN(rBreak[1]) then rBreak[1] else na;
    } else {
    sBreak1 = na;
    rBreak1 = na;
}
def changePh = srcHi > highest(srcHi[1], bb);
def changePl = srcLo < lowest(srcLo[1] , bb);

AddChartBubble(today and changePl and sLabel, sLabel, "break", Color.RED, yes);
AddChartBubble(today and changePh and rLabel, rLabel, "break", Color.GREEN, no);

#// Retest Event
def s1_ = retestCondition(sBreak, high >= sTop and close <= sBot, retSince);
def s2_ = retestCondition(sBreak, high >= sTop and close >= sBot and close <= sTop, retSince);
def s3_ = retestCondition(sBreak, high >= sBot and high <= sTop, retSince);
def s4_ = retestCondition(sBreak, high >= sBot and high <= sTop and close < sBot, retSince);
def r1_ = retestCondition(rBreak, low <= rBot and close >= rTop, retSince);
def r2_ = retestCondition(rBreak, low <= rBot and close <= rTop and close >= rBot, retSince);
def r3_ = retestCondition(rBreak, low <= rTop and low >= rBot, retSince);
def r4_ = retestCondition(rBreak, low <= rTop and low >= rBot and close > rTop, retSince);

def s1 = s1_;
def s2 = s2_;
def s3 = s3_;
def s4 = s4_;
def r1 = r1_;
def r2 = r2_;
def r3 = r3_;
def r4 = r4_;

#retestEvent(c1, c2, c3, c4, y1, y2, col, style, pType) =>
Script retestEvent {
input c1 = 1;
input c2 = 1;
input c3 = 1;
input c4 = 1;
input y1 = high;
input y2 = low;
input pType = 1;
input input_retValid = 2;
input ph = high;
input pl = low;
input repType = "On";
     def isconfirmed = !isNaN(close[-1]);
     def retOccurred; def retEvent;
     def retActive   = c1 or c2 or c3 or c4;
     def retEvent1   = retActive and !retActive[1];
     def retEventVal = if retEvent1 then y2 else retEventVal[1];
     def cond        = if pType == 1 then y2 < retEventVal else y2 > retEventVal;
        if cond {
            retEvent = retActive;
        } else {
            retEvent = retEvent1;
}
    def retValue = if retEvent then y1 else retValue[1];
    def retSince = barssince(!isNaN(retEvent) and retEvent);
    def hc1 = close >= retValue;
    def hc2 = high  >= retValue;
    def lc1 = close <= retValue;
    def lc2 = high  <= retValue;
    def rTon = repType == "On";
    def rTcc = repType == "Candle Confirmation";
    def rThv = repType == "High & Low";
    def reph = if rTon then hc1 else if rThv then hc2 else if rTcc then (hc1 and isconfirmed) else reph[1];
    def repl = if rTon then lc1 else if rThv then lc2 else if rTcc then (lc1 and isconfirmed) else repl[1];
    def Potential = if retEvent then y2 else 0;
    def retConditions = if pType ==  1 then reph else repl;
    def retValid = retSince > 0 and retSince <= input_retValid and retConditions and !retOccurred[1];
        retOccurred = if retEvent then no else
                      if retValid then yes else
                      if pType == 1  and (ph!=ph[1]) and retOccurred[1] then no else
                      if pType == -1 and (pl!=pl[1]) and retOccurred[1] then no else retOccurred[1];
    def retest = if retValid then y2 else 0;
    def labelCount;
    if retEvent {
        labelCount = 0;
     } else {
        labelCount = labelCount[1] + 1;
}
    plot retVal = retest;
    plot retPot = Potential;
    plot count = labelCount;
}
def retH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retVal;
def PotH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retPot;
def cntH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).count;
def retL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retVal;
def PotL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retPot;
def cntL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).count;
#--
def retH = retH_;
def PotH = PotH_;
def cntH = cntH_;
def retL = retL_;
def PotL = PotL_;
def cntL = cntL_;

def retLCol = if retL then retL else retLCol[1];
def retHCol = if retH then retH else retHCol[1];
def breakUp  = srcHi>retHCol;#srcHi > rBot;
def breakDn  = srcLo<retLCol;#srcLo < sTop;

plot PotBreak = if today then if retest then if PotH and cntH<2 then PotH else na else na else na;
PotBreak.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
PotBreak.SetDefaultColor(color.CYAN);

plot PotBreakDn = if today then if retest then if PotL and cntL<2 then PotL else na else na else na;
PotBreakDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
PotBreakDn.SetDefaultColor(color.MAGENTA);

plot sTop_ = if today then if sBoxH==0 then na else sBoxH else na;
plot sBot_ = if today then if sBoxL==0 then na else sBoxL else na;
plot rTop_ = if today then if rBoxH==0 then na else rBoxH else na;
plot rBot_ = if today then if rBoxL==0 then na else rBoxL else na;

    sTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    sBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

    sTOP_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    sBot_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    rTop_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));
    rBot_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));


AddCloud(if !today then na else if breakDn then sTop else na, sBot, Color.DARK_RED);
AddCloud(if !today then na else if breakUp then rTop else na, rBot, Color.DARK_GREEN);

#--- END Code
 
Thank you, may I know how faster is now? or what did change?
i did some code change. pls test.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © nsadeghi
# https://www.tradingview.com/v/Xeeko6TV/
#indicator("Support Resistance with Breaks and Retests",
# Converted and mod by Sam4Cok@Samer800     - 03/2023
# update - added option to show today plot only - 05/2023
# some Code fix - Sam4Cok@Samer800     - 07/2023

input lookback  = 20;         # 'Lookback Range''How many bars for a pivot event to occur.'
input ShowTodayOnly = no;
input BarsSinceBreakout  = 2; # 'Bars Since Breakout''How many bars since breakout in order to detect a retest.'
input RetestDetectionLimiter  = 2; # 'Retest Detection Limiter'
input breakoutBubbles = no;   # 'Breakouts'
input retestWedges    = no;   # 'Retests'
input repType   = {"On",default "Candle Confirmation", "High & Low"};    # 'Repainting'

def na = Double.NaN;
def last = isNaN(close);
def bar = if last then bar[1] else bar[1]+1;
def retest = retestWedges;
def Breakout = breakoutBubbles;
def retSince = BarsSinceBreakout;
def retValid = RetestDetectionLimiter;
def bb       = lookback;
def today = if !ShowTodayOnly then yes else GetDay()== GetLastDay();

def srcHi; def srcLo;
Switch (repType) {
Case "On" :
    srcHi = close;
    srcLo = close;
Case "Candle Confirmation" :
    srcHi = close[1];
    srcLo = close[1];
Case "High & Low" :
    srcHi = high;
    srcLo = low;
}
#---- Colors
DefineGlobalColor("green" , CreateColor(8, 153, 129));
DefineGlobalColor("red"   , Color.PINK);#CreateColor(255, 82, 82));
#// Pivot Instance

script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else fix[1];
    plot result = fix;
}
#barssince(Condition) =>
script barssince {
input Condition = 0;
def barssince = if Condition then 0 else barssince[1] + 1;
plot return = barssince;
}

script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL = 5;    # default Pivot Lookback Left
    input lbR = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def pl_1 = fixnan(FindPivots(low, -1, bb, bb));
def ph_1 = fixnan(FindPivots(high, 1, bb, bb));
def hh = if high==highest(high, bb) then high else hh[1];
def ll = if low==lowest(low, bb) then low else ll[1];
def ph = if ph_1==0 then hh else ph_1;
def pl = if pl_1==0 then ll else pl_1;

#// Box Height
def s_yLoc = if low[1]  > low[-1]  then low[-1] else low[1];
def r_yLoc = if high[1] > high[-1] then high[1] else high[-1];
#// Functions
#repaint(c1, c2, c3) =>
script repaint {
    input type = "On";
    input c1 = no;
    input c2 = no;
    input c3 = no;
    def rTon = type == "On";
    def rTcc = type == "Candle Confirmation";
    def rThv = type == "High & Low";
    def repaint = if rTon then c1 else if rThv then c2 else if rTcc then c3 else Double.NaN;
    plot out = repaint;
}
#retestCondition(breakout, condition) =>
Script retestCondition {
input breakout = no;
input condition = no;
input retSince = 2;
    def retestCondition = barssince(isNaN(breakout)) > retSince and condition;
    plot out = retestCondition;
}
#// Draw and Update Boxes
def sBoxH = if last then na else if pl != pl[1] then s_yLoc else sBoxH[1];#  pl).ph;
def sBoxL = if last then na else if pl != pl[1] then pl else sBoxL[1];
def rBoxH = if last then na else if ph != ph[1] then ph else rBoxH[1];
def rBoxL = if last then na else if ph != ph[1] then r_yLoc else rBoxL[1];

def sTop  = sBoxH;
plot sBot = sBoxL;
def rTop  = rBoxH;
plot rBot = rBoxL;
sBot.SetHiding(yes);
rBot.SetHiding(yes);
#// Breakout Event

def c1 = (close < sBoxL) and (close[1] >= sBoxL[1]);
def c2 = (low   < sBoxL) and (low[1]   >= sBoxL[1]);
def d1 = (close > rBoxH) and (close[1] <= rBoxH[1]);
def d2 = (high  > rBoxH) and (high[1]  <= rBoxH[1]);

def cu = repaint(repType, c1, c2, c1 and !last[-1]);
def co = repaint(repType, d1, d2, d1 and !last[-1]);
def plChange = pl-pl[1];
def phChange = ph-ph[1];

def sBreak1; def rBreak1;
#def sBreak11; def rBreak11;
def sBreak; def rBreak;
def sLabel; def rLabel;

if cu and isNaN(sBreak1[1]) {
        sBreak = yes;
        rBreak = rBreak[1];
        sLabel = if breakout then sBoxL else na;
        rLabel = na;
    } else
if co and isNaN(rBreak1[1]) {
        sBreak = sBreak[1];
        rBreak = yes;
        sLabel = na;
        rLabel = if breakout then rBoxH else na;
    } else {
        sBreak = sBreak[1];
        rBreak = rBreak[1];
        sLabel = na;
        rLabel = na;
}
if plChange {
    sBreak1 =  if isNaN(sBreak[1]) then sBreak[1] else na;
    rBreak1 = rBreak[1];
    } else
if phChange {
    sBreak1 = sBreak[1];
    rBreak1 = if isNaN(rBreak[1]) then rBreak[1] else na;
    } else {
    sBreak1 = na;
    rBreak1 = na;
}
def changePh = srcHi > highest(srcHi[1], bb);
def changePl = srcLo < lowest(srcLo[1] , bb);

AddChartBubble(today and changePl and sLabel, sLabel, "break", Color.RED, yes);
AddChartBubble(today and changePh and rLabel, rLabel, "break", Color.GREEN, no);

#// Retest Event
def s1_ = retestCondition(sBreak, high >= sTop and close <= sBot, retSince);
def s2_ = retestCondition(sBreak, high >= sTop and close >= sBot and close <= sTop, retSince);
def s3_ = retestCondition(sBreak, high >= sBot and high <= sTop, retSince);
def s4_ = retestCondition(sBreak, high >= sBot and high <= sTop and close < sBot, retSince);
def r1_ = retestCondition(rBreak, low <= rBot and close >= rTop, retSince);
def r2_ = retestCondition(rBreak, low <= rBot and close <= rTop and close >= rBot, retSince);
def r3_ = retestCondition(rBreak, low <= rTop and low >= rBot, retSince);
def r4_ = retestCondition(rBreak, low <= rTop and low >= rBot and close > rTop, retSince);

def s1 = s1_;
def s2 = s2_;
def s3 = s3_;
def s4 = s4_;
def r1 = r1_;
def r2 = r2_;
def r3 = r3_;
def r4 = r4_;

#retestEvent(c1, c2, c3, c4, y1, y2, col, style, pType) =>
Script retestEvent {
input c1 = 1;
input c2 = 1;
input c3 = 1;
input c4 = 1;
input y1 = high;
input y2 = low;
input pType = 1;
input input_retValid = 2;
input ph = high;
input pl = low;
input repType = "On";
     def isconfirmed = !isNaN(close[-1]);
     def retOccurred; def retEvent;
     def retActive   = c1 or c2 or c3 or c4;
     def retEvent1   = retActive and !retActive[1];
     def retEventVal = if retEvent1 then y2 else retEventVal[1];
     def cond        = if pType == 1 then y2 < retEventVal else y2 > retEventVal;
        if cond {
            retEvent = retActive;
        } else {
            retEvent = retEvent1;
}
    def retValue = if retEvent then y1 else retValue[1];
    def retSince = barssince(!isNaN(retEvent) and retEvent);
    def hc1 = close >= retValue;
    def hc2 = high  >= retValue;
    def lc1 = close <= retValue;
    def lc2 = high  <= retValue;
    def rTon = repType == "On";
    def rTcc = repType == "Candle Confirmation";
    def rThv = repType == "High & Low";
    def reph = if rTon then hc1 else if rThv then hc2 else if rTcc then (hc1 and isconfirmed) else reph[1];
    def repl = if rTon then lc1 else if rThv then lc2 else if rTcc then (lc1 and isconfirmed) else repl[1];
    def Potential = if retEvent then y2 else 0;
    def retConditions = if pType ==  1 then reph else repl;
    def retValid = retSince > 0 and retSince <= input_retValid and retConditions and !retOccurred[1];
        retOccurred = if retEvent then no else
                      if retValid then yes else
                      if pType == 1  and (ph!=ph[1]) and retOccurred[1] then no else
                      if pType == -1 and (pl!=pl[1]) and retOccurred[1] then no else retOccurred[1];
    def retest = if retValid then y2 else 0;
    def labelCount;
    if retEvent {
        labelCount = 0;
     } else {
        labelCount = labelCount[1] + 1;
}
    plot retVal = retest;
    plot retPot = Potential;
    plot count = labelCount;
}
def retH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retVal;
def PotH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retPot;
def cntH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).count;
def retL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retVal;
def PotL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retPot;
def cntL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).count;
#--
def retH = retH_;
def PotH = PotH_;
def cntH = cntH_;
def retL = retL_;
def PotL = PotL_;
def cntL = cntL_;

def retLCol = if retL then retL else retLCol[1];
def retHCol = if retH then retH else retHCol[1];
def breakUp  = srcHi>retHCol;#srcHi > rBot;
def breakDn  = srcLo<retLCol;#srcLo < sTop;

plot PotBreak = if today then if retest then if PotH and cntH<2 then PotH else na else na else na;
PotBreak.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
PotBreak.SetDefaultColor(color.CYAN);

plot PotBreakDn = if today then if retest then if PotL and cntL<2 then PotL else na else na else na;
PotBreakDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
PotBreakDn.SetDefaultColor(color.MAGENTA);

plot sTop_ = if today then if sBoxH==0 then na else sBoxH else na;
plot sBot_ = if today then if sBoxL==0 then na else sBoxL else na;
plot rTop_ = if today then if rBoxH==0 then na else rBoxH else na;
plot rBot_ = if today then if rBoxL==0 then na else rBoxL else na;

    sTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    sBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

    sTOP_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    sBot_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    rTop_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));
    rBot_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));


AddCloud(if !today then na else if breakDn then sTop else na, sBot, Color.DARK_RED);
AddCloud(if !today then na else if breakUp then rTop else na, rBot, Color.DARK_GREEN);

#--- END Code
 
Would it be possible to generate a scanner for this ? i know it wouldnt be possible with real time but looking at daily chart with previous data. Thanks
View attachment 18026

* Not Typical converting.

Creator Message: https://www.tradingview.com/script/Xeeko6TV-Support-Resistance-with-Breaks-and-Retests/

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © nsadeghi
# https://www.tradingview.com/v/Xeeko6TV/
#indicator("Support Resistance with Breaks and Retests",
# Converted and mod by Sam4Cok@Samer800     - 03/2023

input lookback  = 20;              # 'Lookback Range''How many bars for a pivot event to occur.'
input BarsSinceBreakout  = 2;      # 'Bars Since Breakout''How many bars since breakout in order to detect a retest.'
input RetestDetectionLimiter  = 2; # 'Retest Detection Limiter'
input breakoutBubbles = yes;   # 'Breakouts'
input retestWedges    = yes;   # 'Retests'
input repType   = {default "On", "Candle Confirmation", "High & Low"};    # 'Repainting'

def na = Double.NaN;
def retest = retestWedges;
def Breakout = breakoutBubbles;
def retSince = BarsSinceBreakout;
def retValid = RetestDetectionLimiter;
def bb              = lookback;
def rTon            = repType == repType."On";
def rTcc            = repType == repType."Candle Confirmation";
def rThv            = repType == repType."High & Low";

def srcHi; def srcLo;

if rTon {
    srcHi = close;
    srcLo = close;
    } else
if rTcc {
    srcHi = close[1];
    srcLo = close[1];
    } else
if rThv {
    srcHi = high;
    srcLo = low;
    } else {
    srcHi = srcHi[1];
    srcLo = srcLo[1];
}
#---- Colors
DefineGlobalColor("green" , CreateColor(8, 153, 129));
DefineGlobalColor("red"   , Color.PINK);#CreateColor(255, 82, 82));
#// Pivot Instance

script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else fix[1];
    plot result = fix;
}
#barssince(Condition) =>
script barssince {
input Condition = 0;
def barssince = if Condition then 1 else barssince[1] + 1;
plot return = barssince;
}
#valuewhen (cond, source, occurrence) =>
script valuewhen {
    input cond = 0;
    input source = 0;
    def offset = fold j = 0 to 200 with p while p < 2 do
                 p + ( if p == 1 then j - 2 else if GetValue(cond, j) then 1 else 0 );
    plot price = GetValue(source, offset - 1);
}
script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL = 5;    # default Pivot Lookback Left
    input lbR = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def pl_1 = fixnan(FindPivots(low, -1, bb, bb));
def ph_1 = fixnan(FindPivots(high, 1, bb, bb));
def hh = if high==highest(high, bb) then high else hh[1];
def ll = if low==lowest(low, bb) then low else ll[1];
def ph = if ph_1==0 then hh else ph_1;
def pl = if pl_1==0 then ll else pl_1;

#// Box Height
def s_yLoc = if low[1]  > low[-1]  then low[-1] else low[1];
def r_yLoc = if high[1] > high[-1] then high[1] else high[-1];
#// Functions
#drawBox(condition, y1, y2, color) =>
script drawBox {
    input condition = yes;
    input y1        = high;
    input y2        = low;
    def boxHi = if condition then y1 else boxHi[1];
    def boxLo = if condition then y2 else boxLo[1];
    plot ph = if !IsNaN(close[5]) and !IsNaN(close) then boxHi else Double.NaN;
    plot pl = if !IsNaN(close[5]) and !IsNaN(close) then boxLo else Double.NaN;
}
#repaint(c1, c2, c3) =>
script repaint {
    input type = "On";
    input c1 = no;
    input c2 = no;
    input c3 = no;
    def rTon = type == "On";
    def rTcc = type == "Candle Confirmation";
    def rThv = type == "High & Low";
    def repaint = if rTon then c1 else if rThv then c2 else if rTcc then c3 else repaint[1];
    plot out = repaint;
}
#retestCondition(breakout, condition) =>
Script retestCondition {
input breakout = no;
input condition = no;
input retSince = 2;
    def retestCondition = barssince(!breakout) > retSince and condition;
    plot out = retestCondition;
}
#// Draw and Update Boxes
def sBoxH = drawBox((pl != pl[1]), s_yLoc, pl).ph;
def sBoxL = drawBox((pl != pl[1]), s_yLoc, pl).pl;
def rBoxH = drawBox((ph != ph[1]), ph, r_yLoc).ph;
def rBoxL = drawBox((ph != ph[1]), ph, r_yLoc).pl;

def sTop = sBoxH;
def sBot = sBoxL;
def rTop = rBoxH;
def rBot = rBoxL;

#// Breakout Event
def sBreak;
def rBreak;
def sLabel;
def rLabel;

def c1 = if close  < sBoxL then c1[1] + 1 else 0;
def c2 = if low    < sBoxL then c2[1] + 1 else 0;
def c3 = if close[1] < sBoxL[1] then c3[1] + 1 else 0;
def d1 = if close  > rBoxH then d1[1] + 1 else 0;
def d2 = if high   > rBoxH then d2[1] + 1 else 0;
def d3 = if close[1] > rBoxH[1] then d3[1] + 1 else 0;

def cu = repaint(repType, c1==1, c2==1, c3==1);
def co = repaint(repType, d1==1, d2==1, d3==1);

def sBreak1; def rBreak1;def sBreak11; def rBreak11;
if (pl-pl[1]) {
    sBreak1 =  if !sBreak[1] then no else sBreak[1];
    rBreak1 = rBreak[1];
    } else
if (ph-ph[1]) {
    sBreak1 = sBreak[1];
    rBreak1 = if !rBreak[1] then no else sBreak[1];
    } else {
    sBreak1 =  yes;#sBreak[1];
    rBreak1 =  yes;#rBreak[1];
}
if cu and !sBreak1 {
        sBreak = yes;
        rBreak = rBreak1;
    } else
if co and !rBreak1 {
        sBreak = sBreak1;
        rBreak = yes;
    } else {
        sBreak = sBreak1;
        rBreak = rBreak1;
}
if (pl-pl[1]) {
    sBreak11 = if !sBreak11[1] then 0 else sBreak;
    rBreak11 = rBreak11[1];
    } else
if (ph-ph[1]) {
    sBreak11 = sBreak11[1];
    rBreak11 = if !rBreak11[1] then 0 else sBreak;
    } else {
    sBreak11 =  sBreak11[1];
    rBreak11 =  rBreak11[1];
}
if cu and !sBreak11 {
        sLabel = if breakout then sBoxL else na;
        rLabel = na;
    } else
if co and !rBreak11 {
        sLabel = na;
        rLabel = if breakout then rBoxH else na;
    } else {
        sLabel = na;
        rLabel = na;
}
def changePh = srcHi > highest(srcHi[1], bb);
def changePl = srcLo < lowest(srcLo[1] , bb);


AddChartBubble(changePl and !isNaN(sLabel), sLabel, "break", Color.RED, yes);
AddChartBubble(changePh and !isNaN(rLabel), rLabel, "break", Color.GREEN, no);

#// Retest Event
def s1_ = retestCondition(sBreak, high >= sTop and close <= sBot, retSince);
def s2_ = retestCondition(sBreak, high >= sTop and close >= sBot and close <= sTop, retSince);
def s3_ = retestCondition(sBreak, high >= sBot and high <= sTop, retSince);
def s4_ = retestCondition(sBreak, high >= sBot and high <= sTop and close < sBot, retSince);
def r1_ = retestCondition(rBreak, low <= rBot and close >= rTop, retSince);
def r2_ = retestCondition(rBreak, low <= rBot and close <= rTop and close >= rBot, retSince);
def r3_ = retestCondition(rBreak, low <= rTop and low >= rBot, retSince);
def r4_ = retestCondition(rBreak, low <= rTop and low >= rBot and close > rTop, retSince);

def s1 = s1_;
def s2 = s2_;
def s3 = s3_;
def s4 = s4_;
def r1 = r1_;
def r2 = r2_;
def r3 = r3_;
def r4 = r4_;

#retestEvent(c1, c2, c3, c4, y1, y2, col, style, pType) =>
Script retestEvent {
input c1 = 1;
input c2 = 1;
input c3 = 1;
input c4 = 1;
input y1 = high;
input y2 = low;
input pType = 1;
input input_retValid = 2;
input ph = high;
input pl = low;
input repType = "On";
     def retOccurred; def retEvent;
     def retActive   = c1 or c2 or c3 or c4;
     def retEvent1   = retActive and !retActive[1];
     def retEventVal = if retEvent1 then y2 else retEventVal[1];
        if (if pType == 1 then y2 < retEventVal else y2 > retEventVal) {
            retEvent = retActive;
        } else {
            retEvent = retEvent1;
}
#    def retValue = valuewhen(retEvent, y1);
    def retValue = if retEvent then y1 else retValue[1];
    def retSince = barssince(retEvent);
    def hc1 = close >= retValue;
    def hc2 = high >= retValue;
    def hc3 = close[1] >= retValue[1];
    def lc1 = close <= retValue;
    def lc2 = high <= retValue;
    def lc3 = close[1] <= retValue[1];
    def rTon = repType == "On";
    def rTcc = repType == "Candle Confirmation";
    def rThv = repType == "High & Low";
    def reph = if rTon then hc1 else if rThv then hc2 else if rTcc then hc3 else reph[1];
    def repl = if rTon then lc1 else if rThv then lc2 else if rTcc then lc3 else repl[1];
    def Potential = if retEvent then y2 else 0;
    def retConditions = if pType ==  1 then reph else repl;
    def retValid = retSince > 0 and retSince <= input_retValid and retConditions and !retOccurred[1];
        retOccurred = if retEvent then no else
                      if retValid then yes else
                      if pType == 1  and (ph!=ph[1]) and retOccurred[1] then no else
                      if pType == -1 and (pl!=pl[1]) and retOccurred[1] then no else retOccurred[1];
    def retest = if retValid then y2 else 0;
    def labelCount;
    if retEvent {
        labelCount = 0;
     } else {
        labelCount = labelCount[1] + 1;
}
    plot retVal = retest;
    plot retPot = Potential;
    plot count = labelCount;
}
def retH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retVal;
def PotH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).retPot;
def cntH_ = retestEvent(r1, r2, r3, r4, high, low, 1,retValid,ph, pl, repType).count;
def retL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retVal;
def PotL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).retPot;
def cntL_ = retestEvent(s1, s2, s3, s4, low, high, -1,retValid,ph, pl, repType).count;
#--
def retH = retH_;
def PotH = PotH_;
def cntH = cntH_;
def retL = retL_;
def PotL = PotL_;
def cntL = cntL_;

def retLCol = if retL then retL else retLCol[1];
def retHCol = if retH then retH else retHCol[1];
def breakUp  = srcHi>retHCol;#srcHi > rBot;
def breakDn  = srcLo<retLCol;#srcLo < sTop;

plot PotBreak = if retest then if PotH and cntH<2 then PotH else na else na;
PotBreak.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
PotBreak.SetDefaultColor(color.CYAN);

plot PotBreakDn = if retest then if PotL and cntL<2 then PotL else na else na;
PotBreakDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
PotBreakDn.SetDefaultColor(color.MAGENTA);

plot sTop_ = if sBoxH==0 then na else sBoxH;
plot sBot_ = if sBoxL==0 then na else sBoxL;
plot rTop_ = if rBoxH==0 then na else rBoxH;
plot rBot_ = if rBoxL==0 then na else rBoxL;

    sTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    sBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rTop_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    rBot_.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

    sTOP_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    sBot_.AssignValueColor(if breakDn then Color.RED else GlobalColor("red"));
    rTop_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));
    rBot_.AssignValueColor(if breakUp then Color.GREEN else GlobalColor("green"));


AddCloud(if breakDn then sTop else na, sBot, Color.DARK_RED);
AddCloud(if breakUp then rTop else na, rBot, Color.DARK_GREEN);

#--- END Code
This is great. Would it be possible to make it ring when the break and retest bubble shows?
 
Is it possible to create an alert when one appears?
This is great. Would it be possible to make it ring when the break and retest bubble shows?
add the below at the end of the code:

CSS:
input alerts = yes;
input sound    = sound.NoSound;
input alertType = Alert.BAR;

Alert(alerts and breakUp, "Buy Alert", alertType, sound);
Alert(alerts and breakDn, "Sell Alert", alertType, sound);
 
Last edited by a moderator:
samer800 would it be possible to add assignbackground color.green when price breaksup? I love this indicator but my attention is always pulled throughout the day when I have multiple screens up.
 
add the below at the end of the code:

CSS:
input alerts = yes;
input sound    = sound.NoSound;
input alertType = Alert.BAR;

Alert(alerts and breakUp, "Buy Alert", alertType, sound);
Alert(alerts and breakDn, "Sell Alert", alertType, sound);
Do you know how to get a chartbubble at the retest candle at retest?
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
326 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