Repaints Trendlines With Breaks [LUX] For ThinkOrSwim

Repaints
fY5xIMu.png


finally managed to convert it with additional options. pls check
Updated - 08/2024
  • Added zone option.
  • Added angle limit

CODE:

CSS:
#// Indicator for TOS
#// © LuxAlgo
# //indicator("Trendlines With Breaks [LUX]",overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 09/2022
# added alerts, Loockback Period, Smoothing and HH/LL bubbles - Sam4Cok@Samer800 - 11/2022
# Update: Sam4Cok@Samer800    - 07/2024
# (1) adjusted the signals to be fully real-time. There is an option within the settings to disable backpainting for the trendlines, in which this does not affect the signal labels as they are 100% non-repaint or backpainting.
# (2) Added MTF option.
# added zone option and angle limit - by Sam4Cok@Samer800    - 08/2024
input ShowTodayOnly = no;
input DisplayTrendlineAngle = yes;
input minimumAngle  = 0.1; #  ,'Only Trendlines between:'
input maximumAngle  = 90;
input predectiveMode = {Default "Disabled", "ATR", "Stdev"};
input trendlinesDisplay = {Default "Zones", "Lines"};
input zoneWidth = 0.4; # , 'Trendline Thickness')
input Timeframe = {Default "Chart", "Higher Timeframe"};
input higherTimeframe = AggregationPeriod.FIFTEEN_MIN;
input repaint = yes;
input showTrendlines = {default "Upper & Lower", "Lower Only", "Upper Only"}; #  'Display'
input SourceForSlopeCalc = FundamentalType.CLOSE; # Source for slope calculation
input SwingDetectionLookback = 14;
input trendlineCalcSource = {default "Wicks", "Candle Close"};
input SlopeCalcMethod = {default ATR, Stdev, LinReg};     # 'Slope Calculation Method'
input SlopeMultiplier = 1.0;
input ShowBrakouts = {default "Show All", "Only On HH/LL", "Don't Show"};
input ShowHighLowBubbles = no;
input horizontalLine = no;
input ShowExtendedLines = no;
input alerts   = yes;
input alertType = Alert.BAR;
input sound    = Sound.NoSound;

#----------------------------
def na = Double.NaN;
def last = IsNaN(close);
def zone = trendlinesDisplay==trendlinesDisplay."Zones";
def current = GetAggregationPeriod();
def tf = Max(current, higherTimeframe);
def length = SwingDetectionLookback;
def n = BarNumber() + 1;
def offset = if repaint then length else 0;
def today = GetDay() == GetLastDay();
def both = showTrendlines == showTrendlines."Upper & Lower";

#-- Color
DefineGlobalColor("up", CreateColor(38, 166, 154));
DefineGlobalColor("dn", CreateColor(239, 83, 80));

def c1; def o1; def h1; def l1;def src1;
Switch (Timeframe) {
Case "Higher Timeframe" :
    src1 = Fundamental(FundamentalType = SourceForSlopeCalc, Period = higherTimeframe);
    c1 = close(Period = tf);
    o1 = open(Period = tf);
    h1 = high(Period = tf);
    l1 = low(Period = tf);
Default :
    src1 = Fundamental(FundamentalType = SourceForSlopeCalc);
    c1 = close;
    o1 = open;
    h1 = high;
    l1 = low;
}
def src = if isNaN(src1) then src[1] else src1;
def c = if isNaN(c1) then c[1] else c1;
def o = if isNaN(o1) then o[1] else o1;
def h = if isNaN(h1) then h[1] else h1;
def l = if isNaN(l1) then l[1] else l1;
def wickHi;
def wickLo;
switch (trendlineCalcSource) {
case "Candle Close" :
    wickHi = Max(c, o);
    wickLo = Min(c, o);
default:
    wickHi = h;
    wickLo = l;
}
script StDevTS {
    input data = close;
    input pivot_index = 14;
    def sumData = fold j = 0 to pivot_index with p do
                  p + GetValue(data, j);
    def avgData = sumData / pivot_index;
    def sumSqrt = fold i = 0 to pivot_index with SD do
                  SD + Sqr(GetValue(data, i) - avgData);
    plot StDevTS1 = Sqrt(sumSqrt / pivot_index);
}
script variance {
    input src = close;
    input length = 20;
    def mean = Average(src, length);
    def diff = Sum(Sqr(src - mean), length);
    def variance = diff / length;
    plot out = variance;
}
#//----
script Pivots {
    input series    = close;
    input leftBars  = 10;
    input rightBars = 10;
    input isHigh = yes;
    def na = Double.NaN;
    def HH = series == Highest(series, leftBars + 1);
    def LL = series == Lowest(series, leftBars + 1);
    def pivotRange = (leftBars + rightBars + 1);
    def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
    def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
    def barIndexH = if pvtCond then
                    fold i = 1 to rightBars + 1 with p=1 while p do
                    series > GetValue(series, - i) else na;
    def barIndexL = if pvtCond then
                    fold j = 1 to rightBars + 1 with q=1 while q do
                    series < GetValue(series, - j) else na;
    def PivotPoint;
    if isHigh {
        PivotPoint = if HH and barIndexH then series else na;
    } else {
        PivotPoint = if LL and barIndexL then series else na;
    }
    plot pvt = PivotPoint;
}

def tr = TrueRange(h, c, l);
def nATR = WildersAverage(tr, length);
def stdv = StDev(src, length);
def AvgSlpN = Average(n, length);
def AvgSlpSrc = Average(src, length);
def avgSlpSrcN = Average(src * n, length);
def linAbs = AbsValue(avgSlpSrcN - AvgSlpSrc * AvgSlpN);
def varian = variance(n, length);
def lenMulti =  length * SlopeMultiplier;
def slope;
switch (SlopeCalcMethod) {
case Stdev:
    slope = stdv / lenMulti;
case LinReg:
    slope = linAbs / varian / 2 * SlopeMultiplier;
default:
    slope = nATR / lenMulti;
}
def pvtPh = pivots(wickHi[length], length, length, yes);
def pvtpL = pivots(wickLo[length], length, length, no);
def Ph = if !IsNaN(pvtPh) then pvtPh else 0;
def Pl = if !IsNaN(pvtpL) then pvtpL else 0;

def pot_ph = if Ph then 0 else pot_ph[1] + 1;
def pot_pl = if Pl then 0 else pot_pl[1] + 1;
def pvtH_idx  = Max(pot_ph, 1);
def pvtL_idx  = Max(pot_pl, 1);

def sumTRH = fold i = 0 to pvtH_idx with p do
             p + GetValue(tr, i);
def sumTRL = fold j = 0 to pvtL_idx with q do
             q + GetValue(tr, j);
def atrPH = sumTRH / pvtH_idx / length;
def atrPL = sumTRL / pvtL_idx / length;
def StDevPH = StDevTS(src, pvtH_idx) / length;
def StDevPL = StDevTS(src, pvtL_idx) / length;

def slopeAD;
Switch (predectiveMode) {
Case "ATR" :
    slopeAD = if Ph then slope * (1 - (pvtH_idx/length)) + atrPH * (pvtH_idx / length) else
              if Pl then slope * (1 - (pvtL_idx/length)) + atrPL * (pvtL_idx / length) else slope;
Case "Stdev" :
    slopeAD = if Ph then slope * (1 - (pvtH_idx/length)) + StDevPH * (pvtH_idx / length) else
              if Pl then slope * (1 - (pvtL_idx/length)) + StDevPL * (pvtL_idx / length) else slope;
Default :
    slopeAD = slope;
}

#//----
def angleAD = atan(slopeAD) * 180 / Double.Pi;
def angLimitAD = AbsValue(angleAD) > minimumAngle and AbsValue(angleAD) < maximumAngle;


def slope_ph;
def upper;
def upos;
def plotUp;
def widthUp;
def zoneUp;
def cntUp;
def plotExtUp;
def slopeExtUp;
def zoneExtUp;
def angUp; 
def angDn;
def bullCond = Ph and angLimitAD and (showTrendlines == showTrendlines."Upper Only" or both);
if bullCond {
    cntUp    = 0;
    plotExtUp = plotUp[1];
    zoneExtUp = zoneUp[1];
    slopeExtUp = slope_ph[1];
    slope_ph = slopeAD;
    upper    = Ph;
    upos     = no;
    plotUp   = na;
    widthUp  = zoneWidth * slopeAD * lenMulti; #atr(SwingDetectionLookback);
    zoneUp   = na;
    angUp    = angleAD;
} else {
    widthUp  = widthUp[1];
    slope_ph = slope_ph[1];
    upper    = upper[1] - slope_ph;
    upos     = if close > ((upper + if(zone,widthUp,0)) - slope_ph * length) then 1 else upos[1];
    plotUp   = if repaint then upper else upper - slope_ph * length;
    zoneUp   = if repaint then upper + widthUp else (upper + widthUp) - slope_ph * length;
    cntUp    = cntUp[1] + 1;
    slopeExtUp = slopeExtUp[1];
    plotExtUp = if cntUp > SwingDetectionLookback then na else plotExtUp[1] - slopeExtUp;
    zoneExtUp = if cntUp > SwingDetectionLookback then na else zoneExtUp[1] - slopeExtUp;
    angUp     = na;
}
def slope_pl;
def lower;
def dnos;
def plotDn;
def widthDn;
def zoneDn;
def cntDn;
def plotExtDn;
def slopeExtDn;
def zoneExtDn;
def bearCond = pl and angLimitAD and (showTrendlines == showTrendlines."Lower Only" or both);
if bearCond {
    cntDn      = 0;
    plotExtDn  = plotDn[1];
    zoneExtDn  = zoneDn[1];
    slopeExtDn = slope_pl[1];
    slope_pl = slopeAD;
    lower    = Pl;
    dnos     = no;
    widthDn  = zoneWidth * slopeAD * lenMulti;
    plotDn   = na;
    zoneDn   = na;
    angDn    = angleAD;
} else {
    slope_pl = slope_pl[1];
    lower    = lower[1] + slope_pl;
    dnos     = if close < ((lower -if(zone,widthUp,0)) + slope_pl * length) then 1 else dnos[1];
    plotDn   = if repaint then lower else lower + slope_pl * length;
    widthDn  = widthDn[1];
    zoneDn   = if repaint then lower - widthDn else (lower - widthDn) + slope_pl * length;
    cntDn    = cntDn[1] + 1;
    slopeExtDn = slopeExtDn[1];
    plotExtDn  = if cntDn > SwingDetectionLookback then na else plotExtDn[1] + slopeExtDn;
    zoneExtDn  = if cntDn > SwingDetectionLookback then na else zoneExtDn[1] + slopeExtDn;
    angDn      = na;
}
def condUp = if plotUp[-offset] then plotUp[-offset] else na;
def condDn = if plotDn[-offset] then plotDn[-offset] else na;
def condExtUp = if slope_ph==slope_ph[1] and plotExtUp[-offset-1] then plotExtUp[-offset-1] else na;
def condExtDn = if slope_pl==slope_pl[1] and plotExtDn[-offset-1] then plotExtDn[-offset-1] else na;
def ZcondUp = if zone and zoneUp[-offset] then zoneUp[-offset] else na;
def ZcondDn = if zone and zoneDn[-offset] then zoneDn[-offset] else na;
def condExtzUp = if zone and zoneExtUp[-offset-1] then zoneExtUp[-offset-1] else na;
def condExtzDn = if zone and zoneExtDn[-offset-1] then zoneExtDn[-offset-1] else na;

plot lineUp = if !last then if !ShowTodayOnly then condUp else
              if !today then na else condUp else na;
def linezUp = if !last then if !ShowTodayOnly then ZcondUp else
              if !today then na else ZcondUp else na;
plot lineExtUp = if !last then if !ShowTodayOnly then condExtUp else
                if !today then na else condExtUp else na;
def linezExtUp = if !last then if !ShowTodayOnly then condExtzUp else
                 if !today then na else condExtzUp else na;


plot lineDn = if !last then if !ShowTodayOnly then condDn else
              if !today then na else condDn else na;
def linezDn = if !last then if !ShowTodayOnly then ZcondDn else
               if !today then na else ZcondDn else na;
plot lineExtDn = if !last then if !ShowTodayOnly then condExtDn else
                if !today then na else condExtDn else na;
def linezExtDn = if !last then if !ShowTodayOnly then condExtzDn else
                if !today then na else condExtzDn else na;

plot ExtndUp = if last[-1] and ShowExtendedLines  and repaint then condUp else na;
plot ExtndDn = if last[-1] and ShowExtendedLines  and repaint then condDn else na;
plot ExtndzUp = if last[-1] and ShowExtendedLines  and repaint then ZcondUp else na;
plot ExtndzDn = if last[-1] and ShowExtendedLines  and repaint then ZcondDn else na;

lineUp.SetDefaultColor(GlobalColor("up"));
lineDn.SetDefaultColor(GlobalColor("dn"));
lineExtUp.SetDefaultColor(GlobalColor("up"));
lineExtDn.SetDefaultColor(GlobalColor("dn"));

ExtndUp.SetDefaultColor(GlobalColor("up"));
ExtndDn.SetDefaultColor(GlobalColor("dn"));
ExtndUp.SetStyle(Curve.SHORT_DASH);
ExtndDn.SetStyle(Curve.SHORT_DASH);
ExtndzUp.SetDefaultColor(GlobalColor("up"));
ExtndzDn.SetDefaultColor(GlobalColor("dn"));
ExtndzUp.SetStyle(Curve.SHORT_DASH);
ExtndzDn.SetStyle(Curve.SHORT_DASH);


AddCloud(linezExtUp, lineExtUp, GlobalColor("up"));
AddCloud(lineExtDn, linezExtDn, GlobalColor("dn"));
AddCloud(linezUp, lineUp, GlobalColor("up"));
AddCloud(lineDn, linezDn, GlobalColor("dn"));
#-------- HHLL Bubbles

def ph_1 = if Ph[-length] then ph[-length] else ph_1[1];
def pl_1 = if Pl[-length] then pl[-length] else pl_1[1];

def hh = if Ph[-length] and ph[-length] > ph_1[1] then ph[-length] else na;
def ll = if Pl[-length] and pl[-length] < pl_1[1] then pl[-length] else na;
def hl = if Pl[-length] and pl[-length] > pl_1[1] then pl[-length] else na;
def lh = if Ph[-length] and ph[-length] < ph_1[1] then ph[-length] else na;
def llBubbles = if ShowHighLowBubbles then if ShowTodayOnly then today and ll else ll else na;
def hhBubbles = if ShowHighLowBubbles then if ShowTodayOnly then today and hh else hh else na;
def hlBubbles = if ShowHighLowBubbles then if ShowTodayOnly then today and hl else hl else na;
def lhBubbles = if ShowHighLowBubbles then if ShowTodayOnly then today and lh else lh else na;

AddChartBubble(llBubbles, ll, "LL", Color.RED, no);
AddChartBubble(hhBubbles, hh, "HH", Color.GREEN, yes);

AddChartBubble(hlBubbles, pl_1, "HL", Color.DARK_RED, no);
AddChartBubble(lhBubbles, ph_1, "LH", Color.DARK_GREEN, yes);

#----------Horizontal Line---------------------------------
def LastPH = CompoundValue(1, if !Ph then LastPH[1] else ph, na);
def LastPL = CompoundValue(1, if !Pl then LastPL[1] else pl, na);
#----
def colUp = if !isNaN(hh) then 1 else if !isNaN(lh) then 0 else colUp[1];
def colDn = if !isNaN(ll) then 1 else if !isNaN(hl) then 0 else colDn[1];

def UpPHline   = if last then na else if !Ph then LastPH else na;
def LoLHline   = if last then na else if !Pl then LastPL else na;

plot upperPHline = if !ShowTodayOnly then UpPHline[-offset] else if !today then na else UpPHline[-offset];
upperPHline.SetHiding(!horizontalLine);
upperPHline.SetStyle(Curve.POINTS);
upperPHline.AssignValueColor(if colUp>0 then Color.GREEN else Color.DARK_GREEN);

plot upperDashline = if IsNaN(UpPHline[-offset]) then LastPH[-offset] else na;
upperDashline.SetHiding(!horizontalLine);
upperDashline.SetStyle(Curve.MEDIUM_DASH);
upperDashline.AssignValueColor(if colUp>0 then Color.GREEN else Color.DARK_GREEN);

plot LowerLHline = if !ShowTodayOnly then LoLHline[-offset] else if !today then na else LoLHline[-offset];
LowerLHline.SetHiding(!horizontalLine);
LowerLHline.SetStyle(Curve.POINTS);
LowerLHline.AssignValueColor(if colDn>0 then Color.RED else Color.DARK_RED);

plot LowerDashline = if IsNaN(LoLHline[-offset]) then LastPL[-offset] else na;
LowerDashline.SetHiding(!horizontalLine);
LowerDashline.SetStyle(Curve.MEDIUM_DASH);
LowerDashline.AssignValueColor(if colDn>0 then Color.RED else Color.DARK_RED);

#//Breakouts
def filtUp; def filtDn;
Switch (ShowBrakouts) {
Case "Only On HH/LL" :
    filtUp = colUp>0;
    filtDn = colDn>0;
Case "Don't Show" :
    filtUp = na;
    filtDn = na;
Default :
    filtUp = yes;
    filtDn = yes;
}
def uposExt = if isNaN(lineExtUp) then 0 else if close > if(zone,linezExtUp,lineExtUp) and !upos then 1 else uposExt[1];
def dnosExt = if isNaN(lineExtDn) then 0 else if close < if(zone,linezExtDn,lineExtDn) and !dnos then 1 else dnosExt[1];
def UpBreak = (upos > upos[1] or uposExt > uposExt[1]) and filtUp;
def LoBreak = (dnos > dnos[1] or dnosExt > dnosExt[1]) and filtDn;
def Labelup = if !ShowTodayOnly then UpBreak else
              if !today then na else UpBreak;
def Labeldn = if !ShowTodayOnly then LoBreak else
              if !today then na else LoBreak;

AddChartBubble(Labelup, low, "B", Color.CYAN, no);     # "Upper Break"
AddChartBubble(Labeldn, high, "B", Color.MAGENTA);     # "Lower Break"

#-- angel

plot angleUp = if DisplayTrendlineAngle and !isNaN(angUp[-offset]) then Round(angUp[-offset], 2) else na;
plot angleDn = if DisplayTrendlineAngle and !isNaN(angDn[-offset]) then Round(angDn[-offset], 2) else na;
angleUp.SetDefaultColor(Color.WHITE);
angleDn.SetDefaultColor(Color.WHITE);
angleUp.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
angleDn.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
#---- Alerts
Alert(alerts and Labelup, "Break Up!", alertType, sound);
Alert(alerts and Labeldn, "Break Down!", alertType, sound);


#### END--------

UPDATED - 07/2024
(1) Adjusted the signals to be fully real-time. There is an option within the settings to disable backpainting for the trendlines, in which this does not affect the signal labels as they are 100% non-repaint or backpainting.
(2) Added MTF option.

CODE:
CSS:
#// Indicator for TOS
#// © LuxAlgo
# //indicator("Trendlines With Breaks [LUX]",overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 09/2022
# added alerts, Loockback Period, Smoothing and HH/LL bubbles - Sam4Cok@Samer800 - 11/2022
# Update: Sam4Cok@Samer800    - 07/2024
# (1) adjusted the signals to be fully real-time. There is an option within the settings to disable backpainting for the trendlines, in which this does not affect the signal labels as they are 100% non-repaint or backpainting.
# (2) Added MTF option.
input ShowTodayOnly = no;
input Timeframe = {Default "Chart", "Higher Timeframe"};
input higherTimeframe = AggregationPeriod.FIFTEEN_MIN;
input repaint = yes;
input showTrendlines = {default "Upper & Lower", "Lower Only", "Upper Only"}; #  'Display'
input SourceForSlopeCalc = FundamentalType.CLOSE; # Source for slope calculation
input SwingDetectionLookback = 14;
input trendlineCalcSource = {default "Wicks", "Candle Close"};
input SlopeCalcMethod = {default ATR, Stdev, Linreg};     # 'Slope Calculation Method'
input SlopeMultiplier = 1.0;
input ShowBrakouts = {default "Show All", "Only On HH/LL", "Don't Show"};
input ShowHighLowBubbles = no;
input horizontalLine = no;
input ShowExtendedLines = yes;
input alerts   = yes;
input alertType = Alert.BAR;
input sound    = Sound.NoSound;

#----------------------------
def na = Double.NaN;
def last = IsNaN(close);
def length = SwingDetectionLookback;
def len = length;
def n = BarNumber() + 1;
def offset = if repaint then length else 0;
def today = GetDay() == GetLastDay();
def both = showTrendlines == showTrendlines."Upper & Lower";
def bullCond = showTrendlines == showTrendlines."Upper Only" or both;
def bearCond = showTrendlines == showTrendlines."Lower Only" or both;

#-- Color
DefineGlobalColor("up", CreateColor(38, 166, 154));
DefineGlobalColor("dn", CreateColor(239, 83, 80));

def c1; def o1; def h1; def l1;def src1;
Switch (Timeframe) {
Case "Higher Timeframe" :
    src1 = Fundamental(FundamentalType = SourceForSlopeCalc, Period = higherTimeframe);
    c1 = close(Period = higherTimeframe);
    o1 = open(Period = higherTimeframe);
    h1 = high(Period = higherTimeframe);
    l1 = low(Period = higherTimeframe);
Default :
    src1 = Fundamental(FundamentalType = SourceForSlopeCalc);
    c1 = close;
    o1 = open;
    h1 = high;
    l1 = low;
}
def src = if isNaN(src1) then 0 else src1;
def c = if isNaN(c1) then 0 else c1;
def o = if isNaN(o1) then 0 else o1;
def h = if isNaN(h1) then 0 else h1;
def l = if isNaN(l1) then 0 else l1;
def wickHi;
def wickLo;
switch (trendlineCalcSource) {
case "Candle Close" :
    wickHi = Max(c, o);
    wickLo = Min(c, o);
default:
    wickHi = h;
    wickLo = l;
}
script variance {
    input src = close;
    input length = 20;
    def mean = Average(src, length);
    def diff = Sum(Sqr(src - mean), length);
    def variance = diff / length;
    plot out = variance;
}
#//----
script Pivots {
    input series    = close;
    input leftBars  = 10;
    input rightBars = 10;
    input isHigh = yes;
    def na = Double.NaN;
    def HH = series == Highest(series, leftBars + 1);
    def LL = series == Lowest(series, leftBars + 1);
    def pivotRange = (leftBars + rightBars + 1);
    def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
    def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
    def barIndexH = if pvtCond then
                    fold i = 1 to rightBars + 1 with p=1 while p do
                    series > GetValue(series, - i) else na;
    def barIndexL = if pvtCond then
                    fold j = 1 to rightBars + 1 with q=1 while q do
                    series < GetValue(series, - j) else na;
    def PivotPoint;
    if isHigh {
        PivotPoint = if HH and barIndexH then series else na;
    } else {
        PivotPoint = if LL and barIndexL then series else na;
    }
    plot pvt = PivotPoint;
}
def tr = TrueRange(h, c, l);
def nATR = WildersAverage(tr, length);
def stdv = StDev(src, length);
def AvgSlpN = Average(n, length);
def AvgSlpSrc = Average(src, length);
def avgSlpSrcN = Average(src * n, length);
def linAbs = AbsValue(avgSlpSrcN - AvgSlpSrc * AvgSlpN);
def varian = variance(n, length);
def lenMulti =  length * SlopeMultiplier;
def slope;
switch (SlopeCalcMethod) {
case Stdev:
    slope = stdv / lenMulti;
case Linreg:
    slope = linAbs / varian / 2 * SlopeMultiplier;
default:
    slope = nATR / lenMulti;
}
#//----
def pvtPh = pivots(wickHi[len], length, length, yes);
def pvtpL = pivots(wickLo[len], length, length, no);
def ph1 = if !ShowTodayOnly then !IsNaN(pvtPh) else if !today then na else !IsNaN(pvtPh);
def pl1 = if !ShowTodayOnly then !IsNaN(pvtpL) else if !today then na else !IsNaN(pvtpL);
def Ph = if !IsNaN(pvtPh) then pvtPh else 0;
def Pl = if !IsNaN(pvtpL) then pvtpL else 0;

def slope_ph;
def upper;
def upos;
def plotUp;
def slope_pl;
def lower;
def dnos;
def plotDn;

if Ph and bullCond {
    slope_ph = slope;
    upper    = Ph;
    upos     = no;
    plotUp   = na;
} else {
    slope_ph = slope_ph[1];
    upper    = upper[1] - slope_ph;
    upos     = if close > upper - slope_ph * length then yes else upos[1];
    plotUp   = if repaint then upper else upper - slope_ph * length;
}
if Pl and bearCond{
    slope_pl = slope;
    lower    = Pl;
    dnos     = no;
    plotDn   = na;
} else {
    slope_pl = slope_pl[1];
    lower    = lower[1] + slope_pl;
    dnos     = if close < lower + slope_pl * length then 1 else dnos[1];
    plotDn   = if repaint then lower else lower + slope_pl * length;
}

def condUp = if plotUp[-offset] then plotUp[-offset] else na;
def condDn = if plotDn[-offset] then plotDn[-offset] else na;

plot lineUp = if !last then if !ShowTodayOnly then condUp else
              if !today then na else condUp else na;
plot lineDn = if !last then if !ShowTodayOnly then condDn else
              if !today then na else condDn else na;
plot ExtndUp = if last and ShowExtendedLines then condUp else na;
plot ExtndDn = if last and ShowExtendedLines then condDn else na;
lineUp.SetDefaultColor(GlobalColor("up"));
ExtndUp.SetDefaultColor(GlobalColor("up"));
lineDn.SetDefaultColor(GlobalColor("dn"));
ExtndDn.SetDefaultColor(GlobalColor("dn"));
ExtndUp.SetStyle(Curve.SHORT_DASH);
ExtndDn.SetStyle(Curve.SHORT_DASH);

#-------- HHLL Bubbles

def ph_1 = if Ph[-len] then ph[-len] else ph_1[1];
def pl_1 = if Pl[-len] then pl[-len] else pl_1[1];

def hh = if Ph[-len] and ph[-len] > ph_1[1] then ph[-len] else na;
def ll = if Pl[-len] and pl[-len] < pl_1[1] then pl[-len] else na;
def hl = if Pl[-len] and pl[-len] > pl_1[1] then pl[-len] else na;
def lh = if Ph[-len] and ph[-len] < ph_1[1] then ph[-len] else na;
def llBubbles = if ShowHighLowBubbles then if ShowTodayOnly then today and ll else ll else na;
def hhBubbles = if ShowHighLowBubbles then if ShowTodayOnly then today and hh else hh else na;
def hlBubbles = if ShowHighLowBubbles then if ShowTodayOnly then today and hl else hl else na;
def lhBubbles = if ShowHighLowBubbles then if ShowTodayOnly then today and lh else lh else na;

AddChartBubble(llBubbles, ll, "LL", Color.RED, no);
AddChartBubble(hhBubbles, hh, "HH", Color.GREEN, yes);

AddChartBubble(hlBubbles, pl_1, "HL", Color.DARK_RED, no);
AddChartBubble(lhBubbles, ph_1, "LH", Color.DARK_GREEN, yes);

#----------Horizontal Line---------------------------------
def LastPH = CompoundValue(1, if !Ph then LastPH[1] else ph, na);
def LastPL = CompoundValue(1, if !Pl then LastPL[1] else pl, na);
#----
def colUp = if !isNaN(hh) then 1 else if !isNaN(lh) then 0 else colUp[1];
def colDn = if !isNaN(ll) then 1 else if !isNaN(hl) then 0 else colDn[1];

def UpPHline   = if last then na else if !Ph then LastPH else na;
def LoLHline   = if last then na else if !Pl then LastPL else na;

plot upperPHline = if !ShowTodayOnly then UpPHline[-offset] else if !today then na else UpPHline[-offset];
upperPHline.SetHiding(!horizontalLine);
upperPHline.SetStyle(Curve.POINTS);
upperPHline.AssignValueColor(if colUp>0 then Color.GREEN else Color.DARK_GREEN);

plot upperDashline = if IsNaN(UpPHline[-offset]) then LastPH[-offset] else na;
upperDashline.SetHiding(!horizontalLine);
upperDashline.SetStyle(Curve.MEDIUM_DASH);
upperDashline.AssignValueColor(if colUp>0 then Color.GREEN else Color.DARK_GREEN);

plot LowerLHline = if !ShowTodayOnly then LoLHline[-offset] else if !today then na else LoLHline[-offset];
LowerLHline.SetHiding(!horizontalLine);
LowerLHline.SetStyle(Curve.POINTS);
LowerLHline.AssignValueColor(if colDn>0 then Color.RED else Color.DARK_RED);

plot LowerDashline = if IsNaN(LoLHline[-offset]) then LastPL[-offset] else na;
LowerDashline.SetHiding(!horizontalLine);
LowerDashline.SetStyle(Curve.MEDIUM_DASH);
LowerDashline.AssignValueColor(if colDn>0 then Color.RED else Color.DARK_RED);

#//Breakouts
def filtUp; def filtDn;
Switch (ShowBrakouts) {
Case "Only On HH/LL" :
    filtUp = colUp>0;
    filtDn = colDn>0;
Case "Don't Show" :
    filtUp = na;
    filtDn = na;
Default :
    filtUp = yes;
    filtDn = yes;
}
def UpBreak = upos > upos[1] and filtUp;
def LoBreak = dnos > dnos[1] and filtDn;
def Labelup = if !ShowTodayOnly then UpBreak else
              if !today then na else UpBreak;
def Labeldn = if !ShowTodayOnly then LoBreak else
              if !today then na else LoBreak;

AddChartBubble(Labelup, low, "B", Color.CYAN, no);     # "Upper Break"
AddChartBubble(Labeldn, high, "B", Color.MAGENTA);     # "Lower Break"

#---- Alerts
Alert(alerts and Labelup, "Break Up!", alertType, sound);
Alert(alerts and Labeldn, "Break Down!", alertType, sound);


#### END--------

CODE:

CSS:
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
#// © LuxAlgo
#indicator("Trendlines With Breaks [LUX]",overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 09/2022

input src = close; # Saource for slope calculation
input Showubble = yes;
input showConfirmedOnly = no;#(false,'Show Only Confirmed Breakouts')
input horizontalLine = no;
input wick = yes;
input method = {default ATR, Stdev, Linreg}; #('Atr','Slope Calculation Method'
input length = 14;
input SlopeStep = 1.0;#(1.,'Slope',minval=0,step=.1)

#//----
def upper;
def lower;
def slope;
def slope_ph;
def slope_pl;
def n = BarNumber();
def na = Double.NaN;

#==========================
#       PIVOT SCRIPT
#==========================
script FindPivots {
    input dat = high; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input PF  = 14;    # default pivot forward period
    input PB  = 14;    # default pivot backward period
    ##############
    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
    def _VBar;   # the bar number at the pivot point
    def _PV;     # the previous pivot Value
    def _PVBar;  # the previous pivot bar number
    def _VDiff;  # the difference in values between last two pivot points
    def _VDist;  # the diffence in barnumbers between last two pivot points
    def _VSlope; # the Slope calculated using value and distance changes
    def _VPivot; # used for the pivot point connector line only
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop =
        fold a = 1 to PF + 1
        with b = 1 while b
        do if HL > 0 then
            dat > GetValue(dat, -a) else
            dat < GetValue(dat, -a) ;
    if (HL > 0) {
        _V = if _BN > PB and dat == Highest(dat, PB) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > PB and dat == Lowest(dat, PB) and _VStop
            then dat else _nan;
    }
    _VBar   = if !IsNaN(_V) then _BN else _VBar[1];
    _PV     = if !IsNaN(_V) then GetValue(dat, _BN - _VBar[1]) else _PV[1];
    _PVBar  = if   _VBar != _VBar[1]
            then _PVBar[1] else _VBar;
    _VDiff  = AbsValue(_V) - AbsValue(_PV);
    _VDist  = _BN - _PVBar;
    _VSlope = if _V > _PV  then 1 else
              if _V < _PV  then -1 else 0;
    if (HL > 0) {
        _VPivot = _BN >= HighestAll(_PVBar);
    } else {
        _VPivot = _BN >= LowestAll(_PVBar);
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan; #return the final _dat value at the most
}
switch (method) {
case ATR:
    slope = ATR(length) / length * SlopeStep;
case Stdev:
    slope = StDev(src, length) / length * SlopeStep;
case Linreg:
    slope = AbsValue(Average(src*n, length) - Average(src,length) * Average(n, length)) /
                                                sqr(StDev(n,length)) / 2 * SlopeStep;
}
#//----
def wickHi = if wick then high else if open>=close then open else close;
def wickLo = if wick then low else if open>=close then close else open;
def ph =  findpivots(wickHi, 1, length, length);
def pl =  findpivots(wickLo, -1, length, length);

slope_ph = if !IsNaN(ph) then slope else slope_ph[1];
slope_pl = if !IsNaN(pl) then slope else slope_pl[1];

upper = if(n<0) then na else if !IsNaN(ph) then ph else upper[1] - slope_ph;
lower = if(n<0) then na else if !IsNaN(pl) then pl else lower[1] + slope_pl;
#//----
def single_upper;
def single_lower;
single_upper = if (src[1] > upper) then 0 else
               if !isNaN(ph) then 1 else single_upper[1];
single_lower = if (src[1] < lower) then 0 else
               if !isNaN(pl) then 1 else single_lower[1];

def upper_breakout = single_upper[1] and src[1] > upper
                and (if showConfirmedOnly then src > src[1] else 1);
def lower_breakout = single_lower[1] and src[1] < lower
                and (if showConfirmedOnly then src < src[1] else 1);

def UpBreak = If(upper_breakout, low[1], na);
def LoBreak = If(lower_breakout, high[1], na);

def Labelup = UpBreak[-1];
def Labeldn = LoBreak[-1];

#//----

def UpPH   = if IsNaN(close) then na else if IsNaN(ph[-1]) then upper else na;
def LoLH   = if IsNaN(close) then na else if IsNaN(pl[-1]) then lower else na;

plot upperPH =  if UpPH == 0 then na else UpPH;
upperPH.SetStyle(Curve.FIRM);
upperPH.SetDefaultColor(CreateColor(38, 166, 154));

plot upperDash =  if IsNaN(UpPH) then upper else na;;
upperDash.SetStyle(Curve.MEDIUM_DASH);
upperDash.SetDefaultColor(CreateColor(38, 166, 154));

plot LowerLH =  if LoLH == 0 then na else LoLH;
LowerLH.SetStyle(Curve.FIRM);
LowerLH.SetDefaultColor(CreateColor(239, 83, 80));

plot LowerDash =  if IsNaN(LoLH) then lower else na;
LowerDash.SetStyle(Curve.MEDIUM_DASH);
LowerDash.SetDefaultColor(CreateColor(239, 83, 80));

#----------Horizontal Line---------------------------------
def LastPH = CompoundValue(1, if isNaN(ph) then LastPH[1] else ph, ph);
def LastPL = CompoundValue(1, if isNan(pl) then LastPL[1] else pl, pl);
#----
def UpPHline   = if IsNaN(close) then na else if IsNaN(ph[-1]) then LastPH else na;
def LoLHline   = if IsNaN(close) then na else if IsNaN(pl[-1]) then LastPL else na;

plot upperPHline =  UpPHline;
upperPhline.SetHiding(!horizontalLine);
upperPHline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
upperPHline.SetDefaultColor(CreateColor(38, 166, 154));

plot upperDashline = if IsNaN(UpPHline) then LastPH else na;
upperDashline.SetHiding(!horizontalLine);
upperDashline.SetStyle(Curve.MEDIUM_DASH);
upperDashline.SetDefaultColor(CreateColor(38, 166, 154));

plot LowerLHline =  LoLHline;
LowerLHline.SetHiding(!horizontalLine);
LowerLHline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LowerLHline.SetDefaultColor(CreateColor(239, 83, 80));

plot LowerDashline = if IsNaN(LoLHline) then LastPL else na;
LowerDashline.SetHiding(!horizontalLine);
LowerDashline.SetStyle(Curve.MEDIUM_DASH);
LowerDashline.SetDefaultColor(CreateColor(239, 83, 80));

#-------- Bubbles
AddChartBubble(Showubble and Labelup, Labelup, "Break", CreateColor(38, 166, 154), no);
AddChartBubble(Showubble and Labeldn, Labeldn, "Break", CreateColor(239, 83, 80), yes);


#### END

V2.0
I added alerts, smoothing, HH/LL label and option to adjust the lookback period so you may get better results.

CSS:
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
#// © LuxAlgo
#indicator("Trendlines With Breaks [LUX]",overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 09/2022
# added alerts, Loockback Period, Smoothing and HH/LL bubbles - Sam4Cok@Samer800 - 11/2022

input alerts   = yes;
input alertType = alert.BAR;
input sound    = {default "NoSound", "Ding", "Bell", "Chimes", "Ring"};
input LookbackPeriod = 14;
input SlopeSaource = close; # Saource for slope calculation
input ShowTodayOnly = yes;
input ShowBrakouts = {"Don't Show Breakouts", default "Only Confirmed Breakouts", "Show All Breakouts"};
input ShowHighLowBubbles = no;
input horizontalLine = no;
input SaourceCalcMethod = {default "Wicks", "Candle Close"};
input SlopeCalcMethod = {default ATR, Stdev, Linreg}; #('Atr','Slope Calculation Method'
input SlopeLength = 14;
input SlopeStep = 1.0;
input AtrSmoothing = yes;

#----------------------------
def c = close;
def o = open;
def h = high;
def l = low;
def n = BarNumber();
def na = Double.NaN;
def today =  GetDay() == GetLastDay();
def wick = If(SaourceCalcMethod == SaourceCalcMethod."Wicks", 1, 0);
def ShowSignal = if ShowBrakouts == ShowBrakouts."Only Confirmed Breakouts" then 1 else
                 if ShowBrakouts == ShowBrakouts."Show All Breakouts" then 2 else 0;
#//----
def upper;
def lower;
def slope;
def slope_ph;
def slope_pl;

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 nATR = If(AtrSmoothing, ExpAverage(ATR(Slopelength), 3), ATR(Slopelength));
def LinSlope = WMA(SlopeSaource * n, Slopelength) -
               WMA(SlopeSaource, Slopelength) *
               WMA(n, Slopelength);
switch (SlopeCalcMethod) {
case ATR:
    slope = nATR / Slopelength * SlopeStep;
case Stdev:
    slope = StDev(SlopeSaource, Slopelength) / Slopelength * SlopeStep;
case Linreg:
    slope = AbsValue(LinSlope) / Sqr(StDev(n, Slopelength)) / 2 * SlopeStep;
}
#//----
def wickHi = if wick then h else Max(c, o);
def wickLo = if wick then l else Min(c, o);
def ph = findpivots(wickHi, 1, LookbackPeriod, LookbackPeriod);
def pl = findpivots(wickLo, -1, LookbackPeriod, LookbackPeriod);

slope_ph = if !IsNaN(ph) then slope else slope_ph[1];
slope_pl = if !IsNaN(pl) then slope else slope_pl[1];

upper = if (n < 0) then na else if !IsNaN(ph) then ph else upper[1] - slope_ph;
lower = if (n < 0) then na else if !IsNaN(pl) then pl else lower[1] + slope_pl;
#//----
def single_upper;
def single_lower;
single_upper = if (SlopeSaource[1] > upper) then 0 else
               if !IsNaN(ph) then 1 else single_upper[1];
single_lower = if (SlopeSaource[1] < lower) then 0 else
               if !IsNaN(pl) then 1 else single_lower[1];

def upper_breakout = single_upper[1] and SlopeSaource[1] > upper
                and (if ShowSignal == 1 then SlopeSaource > SlopeSaource[1] else 1);
def lower_breakout = single_lower[1] and SlopeSaource[1] < lower
                and (if ShowSignal == 1 then SlopeSaource < SlopeSaource[1] else 1);

def UpBreak = If(upper_breakout, l[1], na);
def LoBreak = If(lower_breakout, h[1], na);

def Labelup = if ShowSignal>0 then if ShowTodayOnly then
              if !today then na else UpBreak[-1] else UpBreak[-1] else na;
def Labeldn = if ShowSignal>0 then if ShowTodayOnly then
              if !today then na else LoBreak[-1] else LoBreak[-1] else na;

#//----

def UpPH   = if IsNaN(c) then na else if IsNaN(ph[-1]) then upper else na;
def LoLH   = if IsNaN(c) then na else if IsNaN(pl[-1]) then lower else na;

plot upperPH = if ShowTodayOnly then if UpPH == 0 or !today then na else UpPH else
               if UpPH == 0 then na else UpPH ;
upperPH.SetStyle(Curve.FIRM);
upperPH.SetDefaultColor(CreateColor(38, 166, 154));

plot upperDash =  if IsNaN(UpPH) then upper else na;
;
upperDash.SetStyle(Curve.MEDIUM_DASH);
upperDash.SetDefaultColor(CreateColor(38, 166, 154));

plot LowerLH = if ShowTodayOnly then if LoLH == 0 or !today then na else LoLH else
                if LoLH == 0 then na else LoLH ;
LowerLH.SetStyle(Curve.FIRM);
LowerLH.SetDefaultColor(CreateColor(239, 83, 80));

plot LowerDash =  if IsNaN(LoLH) then lower else na;
LowerDash.SetStyle(Curve.MEDIUM_DASH);
LowerDash.SetDefaultColor(CreateColor(239, 83, 80));

#----------Horizontal Line---------------------------------
def LastPH = CompoundValue(1, if IsNaN(ph) then LastPH[1] else ph, ph);
def LastPL = CompoundValue(1, if IsNaN(pl) then LastPL[1] else pl, pl);
#----
def UpPHline   = if IsNaN(c) then na else if IsNaN(ph[-1]) then LastPH else na;
def LoLHline   = if IsNaN(c) then na else if IsNaN(pl[-1]) then LastPL else na;

plot upperPHline = if ShowTodayOnly then if today then UpPHline else na else UpPHline;
upperPHline.SetHiding(!horizontalLine);
upperPHline.SetStyle(Curve.POINTS);
upperPHline.SetDefaultColor(CreateColor(38, 166, 154));

plot upperDashline = if IsNaN(UpPHline) then LastPH else na;
upperDashline.SetHiding(!horizontalLine);
upperDashline.SetStyle(Curve.MEDIUM_DASH);
upperDashline.SetDefaultColor(CreateColor(38, 166, 154));

plot LowerLHline = if UpPHline then if today then LoLHline else na else LoLHline ;
LowerLHline.SetHiding(!horizontalLine);
LowerLHline.SetStyle(Curve.POINTS);
LowerLHline.SetDefaultColor(CreateColor(239, 83, 80));

plot LowerDashline = if IsNaN(LoLHline) then LastPL else na;
LowerDashline.SetHiding(!horizontalLine);
LowerDashline.SetStyle(Curve.MEDIUM_DASH);
LowerDashline.SetDefaultColor(CreateColor(239, 83, 80));

#-------- Breakout Bubbles
AddChartBubble(ShowSignal==1 and Labelup, Labelup, "Break", Color.CYAN, no);
AddChartBubble(ShowSignal==1 and Labeldn, Labeldn, "Break", Color.MAGENTA, yes);

AddChartBubble(ShowSignal==2 and Labelup , Labelup, "Break", CreateColor(38, 166, 154), no);
AddChartBubble(ShowSignal==2 and Labeldn , Labeldn, "Break", CreateColor(239, 83, 80), yes);
#-------- HHLL Bubbles

def ph_1 = if !isnan(ph) then ph else ph_1[1];
def pl_1 = if !isnan(pl) then pl else pl_1[1];

def hh = if !isnan(ph) and ph > ph_1[1] then ph else na;
def ll = if !isnan(pl) and pl < pl_1[1] then pl else na;
def hl = pl > pl_1[1];
def lh = ph < ph_1[1];
def llBubbles = if ShowHighLowBubbles then if ShowTodayOnly then today and ll else ll else na;
def hhBubbles = if ShowHighLowBubbles then if ShowTodayOnly then today and hh else hh else na;
def hlBubbles = if ShowHighLowBubbles then if ShowTodayOnly then today and hl else hl else na;
def lhBubbles = if ShowHighLowBubbles then if ShowTodayOnly then today and lh else lh else na;

AddChartBubble(llBubbles, ll, "LL", Color.RED, no);
AddChartBubble(hhBubbles, hh, "HH", Color.GREEN, yes);

AddChartBubble(hlBubbles, pl_1, "HL", Color.DARK_RED, no);
AddChartBubble(lhBubbles, ph_1, "LH", Color.DARK_GREEN, yes);

#---- Alerts
Alert(alerts and Labelup, "Break Up!", alertType, sound);
Alert(alerts and Labeldn, "Break Down!", alertType, sound);

#### END--------
 
Last edited:
this my first post lol so i have to get use to how all of this works! thanks for the advice

and oh ****. thanks a bunch!

you're welcome.
look around , ask questions. lots of helpful people here
 
Last edited:
I really like this study as it fits my main trading style. How do i remove the bubble with Break in it? I don't need that aspect and think it would be cleaner for me to remove.

Any help is appreciated!
 
I really like this study as it fits my main trading style. How do i remove the bubble with Break in it? I don't need that aspect and think it would be cleaner for me to remove.

Any help is appreciated!
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
#// © LuxAlgo
#indicator("Trendlines With Breaks [LUX]",overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 09/2022

input src = close; # Saource for slope calculation
input Showubble = yes;
input showConfirmedOnly = no;#(false,'Show Only Confirmed Breakouts')
input horizontalLine = no;
input wick = yes;
input method = {default ATR, Stdev, Linreg}; #('Atr','Slope Calculation Method'
input length = 14;
input SlopeStep = 1.0;#(1.,'Slope',minval=0,step=.1)

#//----
def upper;
def lower;
def slope;
def slope_ph;
def slope_pl;
def n = BarNumber();
def na = Double.NaN;

#==========================
# PIVOT SCRIPT
#==========================
script FindPivots {
input dat = high; # default data or study being evaluated
input HL = 0; # default high or low pivot designation, -1 low, +1 high
input PF = 14; # default pivot forward period
input PB = 14; # default pivot backward period
##############
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
def _VBar; # the bar number at the pivot point
def _PV; # the previous pivot Value
def _PVBar; # the previous pivot bar number
def _VDiff; # the difference in values between last two pivot points
def _VDist; # the diffence in barnumbers between last two pivot points
def _VSlope; # the Slope calculated using value and distance changes
def _VPivot; # used for the pivot point connector line only
##############
_BN = BarNumber();
_nan = Double.NaN;
_VStop =
fold a = 1 to PF + 1
with b = 1 while b
do if HL > 0 then
dat > GetValue(dat, -a) else
dat < GetValue(dat, -a) ;
if (HL > 0) {
_V = if _BN > PB and dat == Highest(dat, PB) and _VStop
then dat else _nan;
} else {
_V = if _BN > PB and dat == Lowest(dat, PB) and _VStop
then dat else _nan;
}
_VBar = if !IsNaN(_V) then _BN else _VBar[1];
_PV = if !IsNaN(_V) then GetValue(dat, _BN - _VBar[1]) else _PV[1];
_PVBar = if _VBar != _VBar[1]
then _PVBar[1] else _VBar;
_VDiff = AbsValue(_V) - AbsValue(_PV);
_VDist = _BN - _PVBar;
_VSlope = if _V > _PV then 1 else
if _V < _PV then -1 else 0;
if (HL > 0) {
_VPivot = _BN >= HighestAll(_PVBar);
} else {
_VPivot = _BN >= LowestAll(_PVBar);
}
plot result = if !IsNaN(_V) and _VStop then _V else _nan; #return the final _dat value at the most
}
switch (method) {
case ATR:
slope = ATR(length) / length * SlopeStep;
case Stdev:
slope = StDev(src, length) / length * SlopeStep;
case Linreg:
slope = AbsValue(Average(src*n, length) - Average(src,length) * Average(n, length)) /
sqr(StDev(n,length)) / 2 * SlopeStep;
}
#//----
def wickHi = if wick then high else if open>=close then open else close;
def wickLo = if wick then low else if open>=close then close else open;
def ph = findpivots(wickHi, 1, length, length);
def pl = findpivots(wickLo, -1, length, length);

slope_ph = if !IsNaN(ph) then slope else slope_ph[1];
slope_pl = if !IsNaN(pl) then slope else slope_pl[1];

upper = if(n<0) then na else if !IsNaN(ph) then ph else upper[1] - slope_ph;
lower = if(n<0) then na else if !IsNaN(pl) then pl else lower[1] + slope_pl;
#//----
def single_upper;
def single_lower;
single_upper = if (src[1] > upper) then 0 else
if !isNaN(ph) then 1 else single_upper[1];
single_lower = if (src[1] < lower) then 0 else
if !isNaN(pl) then 1 else single_lower[1];

def upper_breakout = single_upper[1] and src[1] > upper
and (if showConfirmedOnly then src > src[1] else 1);
def lower_breakout = single_lower[1] and src[1] < lower
and (if showConfirmedOnly then src < src[1] else 1);

def UpBreak = If(upper_breakout, low[1], na);
def LoBreak = If(lower_breakout, high[1], na);

def Labelup = UpBreak[-1];
def Labeldn = LoBreak[-1];

#//----

def UpPH = if IsNaN(close) then na else if IsNaN(ph[-1]) then upper else na;
def LoLH = if IsNaN(close) then na else if IsNaN(pl[-1]) then lower else na;

plot upperPH = if UpPH == 0 then na else UpPH;
upperPH.SetStyle(Curve.FIRM);
upperPH.SetDefaultColor(CreateColor(38, 166, 154));

plot upperDash = if IsNaN(UpPH) then upper else na;;
upperDash.SetStyle(Curve.MEDIUM_DASH);
upperDash.SetDefaultColor(CreateColor(38, 166, 154));

plot LowerLH = if LoLH == 0 then na else LoLH;
LowerLH.SetStyle(Curve.FIRM);
LowerLH.SetDefaultColor(CreateColor(239, 83, 80));

plot LowerDash = if IsNaN(LoLH) then lower else na;
LowerDash.SetStyle(Curve.MEDIUM_DASH);
LowerDash.SetDefaultColor(CreateColor(239, 83, 80));

#----------Horizontal Line---------------------------------
def LastPH = CompoundValue(1, if isNaN(ph) then LastPH[1] else ph, ph);
def LastPL = CompoundValue(1, if isNan(pl) then LastPL[1] else pl, pl);
#----
def UpPHline = if IsNaN(close) then na else if IsNaN(ph[-1]) then LastPH else na;
def LoLHline = if IsNaN(close) then na else if IsNaN(pl[-1]) then LastPL else na;

plot upperPHline = UpPHline;
upperPhline.SetHiding(!horizontalLine);
upperPHline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
upperPHline.SetDefaultColor(CreateColor(38, 166, 154));

plot upperDashline = if IsNaN(UpPHline) then LastPH else na;
upperDashline.SetHiding(!horizontalLine);
upperDashline.SetStyle(Curve.MEDIUM_DASH);
upperDashline.SetDefaultColor(CreateColor(38, 166, 154));

plot LowerLHline = LoLHline;
LowerLHline.SetHiding(!horizontalLine);
LowerLHline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LowerLHline.SetDefaultColor(CreateColor(239, 83, 80));

plot LowerDashline = if IsNaN(LoLHline) then LastPL else na;
LowerDashline.SetHiding(!horizontalLine);
LowerDashline.SetStyle(Curve.MEDIUM_DASH);
LowerDashline.SetDefaultColor(CreateColor(239, 83, 80));
 
Last edited by a moderator:
Amazing indicator. Could someone please add Alerts to this study when a "break" bubble occurs on the chart. Much appreciated
Thank you
 
Anyone noticed a deference in "break" signals when Show Extended Hours are on or off?
 
I am really enjoying this indicator! It does a lot of the work quickly. Does it repaint? Goodness, I did a strategy on 5 days 100t ES contract $7500... Could it be the golden goose lol? Thanks for the conversion @samer800
 
Last edited:
Hi @samer800 thank you so much for your update. I'm still learning the inner workings of indicators, and have been messing around with the settings for a 1m:5d SPY chart with a lookback of 5, length of 2, and step of 0.5. That's the closest I can get it to my hand-drawn trendines. Is there a way to better learn how to adjust each setting for each timeframe instead of just doing trial and error with each setting?
 
Is it me or the alerts dont work? This indicator is awesome! Part of my main strategy too. Anyone else having issues with the alerts? If not, what am I doing wrong? I have all the settings set to YES and ON....saw a bubble pop up passing the trendline right in front of my eyes and no alerts, either in the BAR or audio. I have other alerts on using other indicators and they sound off, so I know my speakers are working hahahahahah

Any help or even confirmation would be highly appreciated....
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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