How to Connect Peaks and Breaks on ZigZag

Chemmy

Active member
Hi all, I've been trying to work on something but I've run into a bit of a wall -- I want to draw connections within a ZigZag function based on two criteria:

1) Draw connection from peak to peak and valley to valley

2) If a higher peak is established, draw a line from the previous high and vice versa for lows

Like below, where you can see each peak is connected and so is each valley -- also where horizontal lines are drawn at each new break:
vEYZGtB.png


The main issues as I can tell are that this is an arbitrary length based on each zigzag, and plotting backwards -- would something like this require a fold function in order to access these previous peaks/valleys? Or am I going in the wrong direction with this?

I'm currently working off of a copy of @SleepyZ 's ZigZag Supply and Demand code, which has been one of my favorite studies for a long time. Code below:

Code:
# Archive Name: ZigZag High Low with Supply DemandIndex and Fibs_Linus_Lar
# Archive Section: Scripts
# Suggested Tos Name: ZigZagHighLow_SupplyDemand_FibExtensions_LinusLar
# Archive Date:
# Archive Notes:

#TOS version ZigZagHighLow modified in part by Linus' and Lar's code
input bubbleoffset = .0005;
input percentamount = .01;
input revAmount = .15;
input atrreversal = 3.0;
input atrlength = 5;
def zz = ZigZagHighLow("price h" = high, "price l" = low, "percentage reversal" = percentamount, "absolute reversal" = revAmount, "atr length" = atrlength, "atr reversal" = atrreversal);
def reversalAmount        = if (close * percentamount / 100) > Max(revAmount < atrreversal * atrlength, revAmount) then (close * percentamount / 100) else if revAmount < atrreversal * atrlength then atrreversal * atrlength else revAmount;
rec zzSave = if !IsNaN(zz) then zz else GetValue(zzSave, 1);
def chg = (if zzSave == high then high else low) - GetValue(zzSave, 1);
def isUp = chg >= 0;
rec isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue(zz, 1)) and GetValue(isConf, 1));
def zzd = if isUp then 1 else 0;
plot zzp = if zzd <= 1 then zz else Double.NaN;
zzp.AssignValueColor(if zzd == 1 then Color.GREEN else if zzd == 0 then Color.RED else Color.DARK_ORANGE);
zzp.SetStyle(Curve.FIRM);
zzp.EnableApproximation();

#Price Change between zigzags
def xxhigh = if zzSave == high then  high else xxhigh[1];
def chghigh = high - xxhigh[1];
def xxlow = if zzSave == low then low else xxlow[1];
def chglow = low - xxlow[1];
#input showBubbleschange = yes;
#AddChartBubble(showBubbleschange and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + bubbleoffset)  else low * (1 - bubbleoffset)   , "$" + chg , if isUp and chghigh > 0 then Color.GREEN else if isUp and chghigh < 0 then Color.RED else if isUp then Color.YELLOW else if !isUp and chglow > 0 then Color.GREEN else if !isUp and chglow < 0 then Color.RED else Color.YELLOW, isUp);

#Price at High/Low
#input showBubblesprice = no;
#AddChartBubble(showBubblesprice and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + bubbleoffset)  else low * (1 - #bubbleoffset)   , if isUp then "$" + high else "$" + low , if isUp and chghigh > 0 then Color.GREEN else if isUp and chghigh < 0 #then Color.RED else if isUp then Color.YELLOW else if !isUp and chglow > 0 then Color.GREEN else if !isUp and chglow < 0 then #Color.RED else Color.YELLOW, isUp);

#Label for Confirmed/Unconfirmed Status of Current Zigzag
#AddLabel(BarNumber() != 1, (if isConf then "Confirmed " else "Unconfirmed ") + "ZigZag: " + chg, if !isConf then Color.DARK_ORANGE else if isUp then Color.GREEN else Color.RED);


#---------------------------------------------------------------------------------------------------------------------------------
def ATRSave = if  !IsNaN(zz) then zzSave else GetValue(ATRSave, 1);
def ATRSave1 = if  ATRSave != ATRSave[1] then ATRSave[1] else GetValue(ATRSave1, 1);
def ATRSave2 = if  ATRSave1 != ATRSave1[1] then ATRSave1[1] else GetValue(ATRSave2, 1);
def ATRSave3 = if  ATRSave2 != ATRSave2[1] then ATRSave2[1] else GetValue(ATRSave3, 1);
#---------------------------------------------------------------------------------------------------------------------------------
def ATRchg = Round(zzSave - GetValue(ATRSave, 1));
plot ATRisUp = ATRchg >= 0;
ATRisUp.Hide();
def ATRisDown = ATRchg <= 0;
#def Conf = AbsValue(ATRchg) >= reversalamount or (IsNaN(GetValue(ZZ, 1)) and GetValue(isConf, 1));

plot ATRDiff = ATRchg;
ATRDiff.Hide();

#Bubbles and spacers--------------------------------------------------------------------------

input show_atr_change_bubble = yes;
AddChartBubble(show_atr_change_bubble,
                  zz,
                  AsText(ATRSave - ATRSave2),
                  if ATRisUp then Color.GREEN
                  else if ATRisDown then Color.RED
                  else Color.WHITE, ATRisUp);

input showHH_HL_LH_LL_bubble = yes;
AddChartBubble(showHH_HL_LH_LL_bubble,
                  zz,
                  if ATRisUp and (ATRSave - ATRSave2) > 0
                  then "HH"
                  else if ATRisUp
                  then "LH"
                  else if ATRisDown and (ATRSave - ATRSave2) > 0
                  then "HL"
                  else if ATRisDown and (ATRSave - ATRSave2) < 0
                  then "LL"
                  else "",
                  if ATRisUp then Color.GREEN
                  else if ATRisDown then Color.RED
                  else Color.WHITE, ATRisUp);


#AddChartBubble(showHH_HL_LH_LL_bubble,
#                  ZZ,
#                  if ATRisUp and EQH
#                  then "EQH"
#                  else if ATRisDown and EQL
#                  then "EQL"
#                  else "",
#                  if ATRisUp then Color.GREEN
#                  else if ATRisDown then Color.RED
#                  else Color.WHITE);


#Arrows
def zzL = if !IsNaN(zz) and !isUp then low else GetValue(zzL, 1);
def zzH = if !IsNaN(zz) and isUp then high else GetValue(zzH, 1);
def dir = CompoundValue(1, if zzL != zzL[1] or low == zzL[1] and low == zzSave then 1 else if zzH != zzH[1] or high == zzH[1] and high == zzSave then -1 else dir[1], 0);
def signal = CompoundValue(1, if dir > 0 and low > zzL then if signal[1] <= 0 then 1 else signal[1] else if dir < 0 and high < zzH then if signal[1] >= 0 then -1 else signal[1]    else signal[1], 0);
input showarrows = no;
plot U1 = showarrows and signal > 0 and signal[1] <= 0;
U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
U1.SetDefaultColor(Color.GREEN);
U1.SetLineWeight(4);
plot D1 = showarrows and signal < 0 and signal[1] >= 0;
D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
D1.SetDefaultColor(Color.RED);
D1.SetLineWeight(4);

## Double Tops and bottoms
input show_Double_TopsBottoms_bubble = yes;
input EQ_range = 0.10;
def EQH = show_Double_TopsBottoms_bubble and ATRisUp and AbsValue(ATRSave - ATRSave2) <= EQ_range;
def EQL = show_Double_TopsBottoms_bubble and ATRisDown and AbsValue(ATRSave - ATRSave2) <= EQ_range;

AddChartBubble(EQH, zz, "EQH", Color.RED);
AddChartBubble(EQL, zz, "EQL", Color.GREEN);


#Alerts
input usealerts = no;
Alert(usealerts and U1, "ZIG-UP", Alert.BAR, Sound.Bell);
Alert(usealerts and D1, "ZAG-DOWN", Alert.BAR, Sound.Chimes);

#Supply Demand Areas
rec data1 = CompoundValue(1, if (zzSave == high or zzSave == low) then data1[1] + 1 else data1[1], 0);
def datacount1 = (HighestAll(data1) - data1[1]);
input numbersuppdemandtoshow = 2;
input showSupplyDemand = {default Pivot, Arrow, None};
def idx = if showSupplyDemand == showSupplyDemand.Pivot then 1 else 0;
def rLow;
def rHigh;
if signal crosses 0 {
    rLow = low[idx];
    rHigh = high[idx];
} else {
    rLow = rLow[1];
    rHigh = rHigh[1];
}
plot HighLine = if datacount1 <= numbersuppdemandtoshow and showSupplyDemand != showSupplyDemand.None and !IsNaN(close) and rHigh != 0 then rHigh else Double.NaN;
HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HighLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);

plot LowLine = if datacount1 <= numbersuppdemandtoshow and  showSupplyDemand != showSupplyDemand.None and !IsNaN(close) and rLow != 0 then rLow else Double.NaN;
LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LowLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);

def hlUp = if signal > 0 then HighLine else Double.NaN;
def hlDn = if signal < 0 then HighLine else Double.NaN;

input showsupplydemandcloud = yes;
AddCloud(if showsupplydemandcloud then hlUp else Double.NaN, LowLine, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(if showsupplydemandcloud then hlDn else Double.NaN, LowLine, Color.LIGHT_RED, Color.LIGHT_RED);

#Store Previous Data
def zzsave1 = if !IsNaN(zzSave) then zzSave else zzsave1[1];
def zzsave2 = zzsave1;
rec priorzz1 = if zzsave2  != zzsave2[1]  then zzsave2[1]  else priorzz1[1];
rec priorzz2 = if priorzz1 != priorzz1[1] then priorzz1[1] else priorzz2[1];
rec priorzz3 = if priorzz2 != priorzz2[1] then priorzz2[1] else priorzz3[1];

#Fibonacci Extensions
rec data = CompoundValue(1, if (zzSave == high or zzSave == low) then data[1] + 1 else data[1], 0);
def datacount = (HighestAll(data) - data[1]);
input numberextfibstoshow = 2;
rec cpo = if dir[1] != dir then 0 else 1;
input showFibExtLines = yes;
input showtodayonly = no;
def today = if showtodayonly == yes then GetDay() == GetLastDay() else GetDay();
def extfib1 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) * 1
else extfib1[1];
plot extfib100 = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1) and dir < 0 and cpo != 0 then extfib1[1] else Double.NaN;
extfib100.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib100.SetDefaultColor(Color.RED);
extfib100.SetLineWeight(1);
extfib100.HideBubble();
def extfib1a = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) * 0.382
else extfib1a[1];
plot extfib382 = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1a) and dir < 0 and cpo != 0 then extfib1a[1] else Double.NaN;
extfib382.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib382.SetDefaultColor(Color.RED);
extfib382.SetLineWeight(1);
extfib382.HideBubble();
def extfib2 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
0.618 else extfib2[1];
plot extfib618 = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib2) and dir < 0 and cpo != 0  then extfib2[1] else Double.NaN;
extfib618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib618.SetDefaultColor(Color.RED);
extfib618.SetLineWeight(1);
extfib618.HideBubble();
def extfib3 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
1.618 else extfib3[1];
plot extfib1618 = if datacount <= numberextfibstoshow and today and showFibExtLines and  !IsNaN(extfib3) and dir < 0  and cpo != 0  then extfib3[1] else Double.NaN;
extfib1618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib1618.SetDefaultColor(Color.RED);
extfib1618.SetLineWeight(1);
extfib1618.HideBubble();
def extfib3a = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
2.000 else extfib3a[1];
plot extfib2000 = if datacount <= numberextfibstoshow and today and showFibExtLines and  !IsNaN(extfib3a) and dir < 0  and cpo != 0  then extfib3a[1] else Double.NaN;
extfib2000.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2000.SetDefaultColor(Color.RED);
extfib2000.SetLineWeight(1);
extfib2000.HideBubble();
def extfib4 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
2.618 else extfib4[1];
plot extfib2618 = if  datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib4) and dir < 0  and cpo != 0  then extfib4[1] else Double.NaN;
extfib2618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2618.SetDefaultColor(Color.RED);
extfib2618.SetLineWeight(1);
extfib2618.HideBubble();
def extfib5 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
3.618 else extfib5[1];
plot extfib3618 = if  datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib5) and dir < 0  and cpo != 0  then extfib5[1] else Double.NaN;
extfib3618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib3618.SetDefaultColor(Color.RED);
extfib3618.SetLineWeight(1);
extfib3618.HideBubble();
def extfib1_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) * 1
else extfib1_[1];
plot extfib100_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1_) and dir > 0 and cpo != 0 then extfib1_[1] else Double.NaN;
extfib100_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib100_.SetDefaultColor(Color.GREEN);
extfib100_.SetLineWeight(1);
extfib100_.HideBubble();
def extfib1a_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) * 0.382
else extfib1a_[1];
plot extfib382_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1a_) and dir > 0 and cpo != 0 then extfib1a_[1] else Double.NaN;
extfib382_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib382_.SetDefaultColor(Color.GREEN);
extfib382_.SetLineWeight(1);
extfib382_.HideBubble();
def extfib2_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
0.618 else extfib2_[1];
plot extfib618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib2_) and dir > 0  and cpo != 0  then extfib2_[1] else Double.NaN;
extfib618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib618_.SetDefaultColor(Color.GREEN);
extfib618_.SetLineWeight(1);
extfib618_.HideBubble();
def extfib3_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
1.618 else extfib3_[1];
plot extfib1618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib3_) and dir > 0  and cpo != 0  then extfib3_[1] else Double.NaN;
extfib1618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib1618_.SetDefaultColor(Color.GREEN);
extfib1618_.SetLineWeight(1);
extfib1618_.HideBubble();
def extfib3a_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
2.000 else extfib3a_[1];
plot extfib2000_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib3a_) and dir > 0  and cpo != 0  then extfib3a_[1] else Double.NaN;
extfib2000_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2000_.SetDefaultColor(Color.GREEN);
extfib2000_.SetLineWeight(1);
extfib2000_.HideBubble();
def extfib4_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
2.618 else extfib4_[1];
plot extfib2618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib4_) and dir > 0  and cpo != 0  then extfib4_[1]  else Double.NaN;
extfib2618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2618_.SetDefaultColor(Color.GREEN);
extfib2618_.SetLineWeight(1);
extfib2618_.HideBubble();
def extfib5_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
3.618 else extfib5_[1];
plot extfib3618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib5_) and dir > 0  and cpo != 0  then extfib5_[1]  else Double.NaN;
extfib3618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib3618_.SetDefaultColor(Color.GREEN);
extfib3618_.SetLineWeight(1);
extfib3618_.HideBubble();
input fibextbubblespacesinexpansion = 8;
def b = fibextbubblespacesinexpansion;
def direction = if !isUp then 1 else 0;
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1[b + 2], "100%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1a[b + 2], "38.2%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib2[b + 2], "61.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3[b + 2], "161.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3a[b + 2], "200%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib4[b + 2], "261.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib5[b + 2], "361.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1_[b + 2], "100%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1a_[b + 2], "38.2%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib2_[b + 2], "61.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3_[b + 2], "161.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3a_[b + 2], "200%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib4_[b + 2], "261.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib5_[b + 2], "361.8%", Color.GREEN, yes);

#Volume at Reversals
def vol = if BarNumber() == 0 then 0 else volume + vol[1];
def vol1 = if BarNumber() == 1 then volume else vol1[1];
def xxvol = if zzSave == high or zzSave == low then TotalSum(volume) else xxvol[1];
def chgvol =  if xxvol - xxvol[1] + vol1 == vol then vol else xxvol - xxvol[1];
input showBubblesVolume = no;
AddChartBubble(showBubblesVolume and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + bubbleoffset)  else low * (1 - bubbleoffset), chgvol, if isUp and chghigh > 0 then Color.GREEN else if isUp and chghigh < 0 then Color.RED else if isUp then Color.YELLOW else if !isUp and chglow > 0 then Color.GREEN else if !isUp and chglow < 0 then Color.RED else Color.YELLOW, if isUp then yes else no );
 

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

Thanks for the suggestion, but that study doesn't use zigzags nor does it connect two points -- I do love that script but it's not exactly what I'm looking to do here.
 
Hi all, I've been trying to work on something but I've run into a bit of a wall -- I want to draw connections within a ZigZag function based on two criteria:

1) Draw connection from peak to peak and valley to valley

2) If a higher peak is established, draw a line from the previous high and vice versa for lows

Like below, where you can see each peak is connected and so is each valley -- also where horizontal lines are drawn at each new break:
vEYZGtB.png


The main issues as I can tell are that this is an arbitrary length based on each zigzag, and plotting backwards -- would something like this require a fold function in order to access these previous peaks/valleys? Or am I going in the wrong direction with this?

I'm currently working off of a copy of @SleepyZ 's ZigZag Supply and Demand code, which has been one of my favorite studies for a long time. Code below:

Code:
# Archive Name: ZigZag High Low with Supply DemandIndex and Fibs_Linus_Lar
# Archive Section: Scripts
# Suggested Tos Name: ZigZagHighLow_SupplyDemand_FibExtensions_LinusLar
# Archive Date:
# Archive Notes:

#TOS version ZigZagHighLow modified in part by Linus' and Lar's code
input bubbleoffset = .0005;
input percentamount = .01;
input revAmount = .15;
input atrreversal = 3.0;
input atrlength = 5;
def zz = ZigZagHighLow("price h" = high, "price l" = low, "percentage reversal" = percentamount, "absolute reversal" = revAmount, "atr length" = atrlength, "atr reversal" = atrreversal);
def reversalAmount        = if (close * percentamount / 100) > Max(revAmount < atrreversal * atrlength, revAmount) then (close * percentamount / 100) else if revAmount < atrreversal * atrlength then atrreversal * atrlength else revAmount;
rec zzSave = if !IsNaN(zz) then zz else GetValue(zzSave, 1);
def chg = (if zzSave == high then high else low) - GetValue(zzSave, 1);
def isUp = chg >= 0;
rec isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue(zz, 1)) and GetValue(isConf, 1));
def zzd = if isUp then 1 else 0;
plot zzp = if zzd <= 1 then zz else Double.NaN;
zzp.AssignValueColor(if zzd == 1 then Color.GREEN else if zzd == 0 then Color.RED else Color.DARK_ORANGE);
zzp.SetStyle(Curve.FIRM);
zzp.EnableApproximation();

#Price Change between zigzags
def xxhigh = if zzSave == high then  high else xxhigh[1];
def chghigh = high - xxhigh[1];
def xxlow = if zzSave == low then low else xxlow[1];
def chglow = low - xxlow[1];
#input showBubbleschange = yes;
#AddChartBubble(showBubbleschange and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + bubbleoffset)  else low * (1 - bubbleoffset)   , "$" + chg , if isUp and chghigh > 0 then Color.GREEN else if isUp and chghigh < 0 then Color.RED else if isUp then Color.YELLOW else if !isUp and chglow > 0 then Color.GREEN else if !isUp and chglow < 0 then Color.RED else Color.YELLOW, isUp);

#Price at High/Low
#input showBubblesprice = no;
#AddChartBubble(showBubblesprice and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + bubbleoffset)  else low * (1 - #bubbleoffset)   , if isUp then "$" + high else "$" + low , if isUp and chghigh > 0 then Color.GREEN else if isUp and chghigh < 0 #then Color.RED else if isUp then Color.YELLOW else if !isUp and chglow > 0 then Color.GREEN else if !isUp and chglow < 0 then #Color.RED else Color.YELLOW, isUp);

#Label for Confirmed/Unconfirmed Status of Current Zigzag
#AddLabel(BarNumber() != 1, (if isConf then "Confirmed " else "Unconfirmed ") + "ZigZag: " + chg, if !isConf then Color.DARK_ORANGE else if isUp then Color.GREEN else Color.RED);


#---------------------------------------------------------------------------------------------------------------------------------
def ATRSave = if  !IsNaN(zz) then zzSave else GetValue(ATRSave, 1);
def ATRSave1 = if  ATRSave != ATRSave[1] then ATRSave[1] else GetValue(ATRSave1, 1);
def ATRSave2 = if  ATRSave1 != ATRSave1[1] then ATRSave1[1] else GetValue(ATRSave2, 1);
def ATRSave3 = if  ATRSave2 != ATRSave2[1] then ATRSave2[1] else GetValue(ATRSave3, 1);
#---------------------------------------------------------------------------------------------------------------------------------
def ATRchg = Round(zzSave - GetValue(ATRSave, 1));
plot ATRisUp = ATRchg >= 0;
ATRisUp.Hide();
def ATRisDown = ATRchg <= 0;
#def Conf = AbsValue(ATRchg) >= reversalamount or (IsNaN(GetValue(ZZ, 1)) and GetValue(isConf, 1));

plot ATRDiff = ATRchg;
ATRDiff.Hide();

#Bubbles and spacers--------------------------------------------------------------------------

input show_atr_change_bubble = yes;
AddChartBubble(show_atr_change_bubble,
                  zz,
                  AsText(ATRSave - ATRSave2),
                  if ATRisUp then Color.GREEN
                  else if ATRisDown then Color.RED
                  else Color.WHITE, ATRisUp);

input showHH_HL_LH_LL_bubble = yes;
AddChartBubble(showHH_HL_LH_LL_bubble,
                  zz,
                  if ATRisUp and (ATRSave - ATRSave2) > 0
                  then "HH"
                  else if ATRisUp
                  then "LH"
                  else if ATRisDown and (ATRSave - ATRSave2) > 0
                  then "HL"
                  else if ATRisDown and (ATRSave - ATRSave2) < 0
                  then "LL"
                  else "",
                  if ATRisUp then Color.GREEN
                  else if ATRisDown then Color.RED
                  else Color.WHITE, ATRisUp);


#AddChartBubble(showHH_HL_LH_LL_bubble,
#                  ZZ,
#                  if ATRisUp and EQH
#                  then "EQH"
#                  else if ATRisDown and EQL
#                  then "EQL"
#                  else "",
#                  if ATRisUp then Color.GREEN
#                  else if ATRisDown then Color.RED
#                  else Color.WHITE);


#Arrows
def zzL = if !IsNaN(zz) and !isUp then low else GetValue(zzL, 1);
def zzH = if !IsNaN(zz) and isUp then high else GetValue(zzH, 1);
def dir = CompoundValue(1, if zzL != zzL[1] or low == zzL[1] and low == zzSave then 1 else if zzH != zzH[1] or high == zzH[1] and high == zzSave then -1 else dir[1], 0);
def signal = CompoundValue(1, if dir > 0 and low > zzL then if signal[1] <= 0 then 1 else signal[1] else if dir < 0 and high < zzH then if signal[1] >= 0 then -1 else signal[1]    else signal[1], 0);
input showarrows = no;
plot U1 = showarrows and signal > 0 and signal[1] <= 0;
U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
U1.SetDefaultColor(Color.GREEN);
U1.SetLineWeight(4);
plot D1 = showarrows and signal < 0 and signal[1] >= 0;
D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
D1.SetDefaultColor(Color.RED);
D1.SetLineWeight(4);

## Double Tops and bottoms
input show_Double_TopsBottoms_bubble = yes;
input EQ_range = 0.10;
def EQH = show_Double_TopsBottoms_bubble and ATRisUp and AbsValue(ATRSave - ATRSave2) <= EQ_range;
def EQL = show_Double_TopsBottoms_bubble and ATRisDown and AbsValue(ATRSave - ATRSave2) <= EQ_range;

AddChartBubble(EQH, zz, "EQH", Color.RED);
AddChartBubble(EQL, zz, "EQL", Color.GREEN);


#Alerts
input usealerts = no;
Alert(usealerts and U1, "ZIG-UP", Alert.BAR, Sound.Bell);
Alert(usealerts and D1, "ZAG-DOWN", Alert.BAR, Sound.Chimes);

#Supply Demand Areas
rec data1 = CompoundValue(1, if (zzSave == high or zzSave == low) then data1[1] + 1 else data1[1], 0);
def datacount1 = (HighestAll(data1) - data1[1]);
input numbersuppdemandtoshow = 2;
input showSupplyDemand = {default Pivot, Arrow, None};
def idx = if showSupplyDemand == showSupplyDemand.Pivot then 1 else 0;
def rLow;
def rHigh;
if signal crosses 0 {
    rLow = low[idx];
    rHigh = high[idx];
} else {
    rLow = rLow[1];
    rHigh = rHigh[1];
}
plot HighLine = if datacount1 <= numbersuppdemandtoshow and showSupplyDemand != showSupplyDemand.None and !IsNaN(close) and rHigh != 0 then rHigh else Double.NaN;
HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HighLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);

plot LowLine = if datacount1 <= numbersuppdemandtoshow and  showSupplyDemand != showSupplyDemand.None and !IsNaN(close) and rLow != 0 then rLow else Double.NaN;
LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LowLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);

def hlUp = if signal > 0 then HighLine else Double.NaN;
def hlDn = if signal < 0 then HighLine else Double.NaN;

input showsupplydemandcloud = yes;
AddCloud(if showsupplydemandcloud then hlUp else Double.NaN, LowLine, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(if showsupplydemandcloud then hlDn else Double.NaN, LowLine, Color.LIGHT_RED, Color.LIGHT_RED);

#Store Previous Data
def zzsave1 = if !IsNaN(zzSave) then zzSave else zzsave1[1];
def zzsave2 = zzsave1;
rec priorzz1 = if zzsave2  != zzsave2[1]  then zzsave2[1]  else priorzz1[1];
rec priorzz2 = if priorzz1 != priorzz1[1] then priorzz1[1] else priorzz2[1];
rec priorzz3 = if priorzz2 != priorzz2[1] then priorzz2[1] else priorzz3[1];

#Fibonacci Extensions
rec data = CompoundValue(1, if (zzSave == high or zzSave == low) then data[1] + 1 else data[1], 0);
def datacount = (HighestAll(data) - data[1]);
input numberextfibstoshow = 2;
rec cpo = if dir[1] != dir then 0 else 1;
input showFibExtLines = yes;
input showtodayonly = no;
def today = if showtodayonly == yes then GetDay() == GetLastDay() else GetDay();
def extfib1 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) * 1
else extfib1[1];
plot extfib100 = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1) and dir < 0 and cpo != 0 then extfib1[1] else Double.NaN;
extfib100.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib100.SetDefaultColor(Color.RED);
extfib100.SetLineWeight(1);
extfib100.HideBubble();
def extfib1a = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) * 0.382
else extfib1a[1];
plot extfib382 = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1a) and dir < 0 and cpo != 0 then extfib1a[1] else Double.NaN;
extfib382.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib382.SetDefaultColor(Color.RED);
extfib382.SetLineWeight(1);
extfib382.HideBubble();
def extfib2 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
0.618 else extfib2[1];
plot extfib618 = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib2) and dir < 0 and cpo != 0  then extfib2[1] else Double.NaN;
extfib618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib618.SetDefaultColor(Color.RED);
extfib618.SetLineWeight(1);
extfib618.HideBubble();
def extfib3 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
1.618 else extfib3[1];
plot extfib1618 = if datacount <= numberextfibstoshow and today and showFibExtLines and  !IsNaN(extfib3) and dir < 0  and cpo != 0  then extfib3[1] else Double.NaN;
extfib1618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib1618.SetDefaultColor(Color.RED);
extfib1618.SetLineWeight(1);
extfib1618.HideBubble();
def extfib3a = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
2.000 else extfib3a[1];
plot extfib2000 = if datacount <= numberextfibstoshow and today and showFibExtLines and  !IsNaN(extfib3a) and dir < 0  and cpo != 0  then extfib3a[1] else Double.NaN;
extfib2000.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2000.SetDefaultColor(Color.RED);
extfib2000.SetLineWeight(1);
extfib2000.HideBubble();
def extfib4 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
2.618 else extfib4[1];
plot extfib2618 = if  datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib4) and dir < 0  and cpo != 0  then extfib4[1] else Double.NaN;
extfib2618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2618.SetDefaultColor(Color.RED);
extfib2618.SetLineWeight(1);
extfib2618.HideBubble();
def extfib5 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
3.618 else extfib5[1];
plot extfib3618 = if  datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib5) and dir < 0  and cpo != 0  then extfib5[1] else Double.NaN;
extfib3618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib3618.SetDefaultColor(Color.RED);
extfib3618.SetLineWeight(1);
extfib3618.HideBubble();
def extfib1_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) * 1
else extfib1_[1];
plot extfib100_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1_) and dir > 0 and cpo != 0 then extfib1_[1] else Double.NaN;
extfib100_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib100_.SetDefaultColor(Color.GREEN);
extfib100_.SetLineWeight(1);
extfib100_.HideBubble();
def extfib1a_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) * 0.382
else extfib1a_[1];
plot extfib382_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1a_) and dir > 0 and cpo != 0 then extfib1a_[1] else Double.NaN;
extfib382_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib382_.SetDefaultColor(Color.GREEN);
extfib382_.SetLineWeight(1);
extfib382_.HideBubble();
def extfib2_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
0.618 else extfib2_[1];
plot extfib618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib2_) and dir > 0  and cpo != 0  then extfib2_[1] else Double.NaN;
extfib618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib618_.SetDefaultColor(Color.GREEN);
extfib618_.SetLineWeight(1);
extfib618_.HideBubble();
def extfib3_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
1.618 else extfib3_[1];
plot extfib1618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib3_) and dir > 0  and cpo != 0  then extfib3_[1] else Double.NaN;
extfib1618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib1618_.SetDefaultColor(Color.GREEN);
extfib1618_.SetLineWeight(1);
extfib1618_.HideBubble();
def extfib3a_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
2.000 else extfib3a_[1];
plot extfib2000_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib3a_) and dir > 0  and cpo != 0  then extfib3a_[1] else Double.NaN;
extfib2000_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2000_.SetDefaultColor(Color.GREEN);
extfib2000_.SetLineWeight(1);
extfib2000_.HideBubble();
def extfib4_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
2.618 else extfib4_[1];
plot extfib2618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib4_) and dir > 0  and cpo != 0  then extfib4_[1]  else Double.NaN;
extfib2618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2618_.SetDefaultColor(Color.GREEN);
extfib2618_.SetLineWeight(1);
extfib2618_.HideBubble();
def extfib5_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
3.618 else extfib5_[1];
plot extfib3618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib5_) and dir > 0  and cpo != 0  then extfib5_[1]  else Double.NaN;
extfib3618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib3618_.SetDefaultColor(Color.GREEN);
extfib3618_.SetLineWeight(1);
extfib3618_.HideBubble();
input fibextbubblespacesinexpansion = 8;
def b = fibextbubblespacesinexpansion;
def direction = if !isUp then 1 else 0;
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1[b + 2], "100%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1a[b + 2], "38.2%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib2[b + 2], "61.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3[b + 2], "161.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3a[b + 2], "200%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib4[b + 2], "261.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib5[b + 2], "361.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1_[b + 2], "100%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1a_[b + 2], "38.2%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib2_[b + 2], "61.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3_[b + 2], "161.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3a_[b + 2], "200%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib4_[b + 2], "261.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib5_[b + 2], "361.8%", Color.GREEN, yes);

#Volume at Reversals
def vol = if BarNumber() == 0 then 0 else volume + vol[1];
def vol1 = if BarNumber() == 1 then volume else vol1[1];
def xxvol = if zzSave == high or zzSave == low then TotalSum(volume) else xxvol[1];
def chgvol =  if xxvol - xxvol[1] + vol1 == vol then vol else xxvol - xxvol[1];
input showBubblesVolume = no;
AddChartBubble(showBubblesVolume and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + bubbleoffset)  else low * (1 - bubbleoffset), chgvol, if isUp and chghigh > 0 then Color.GREEN else if isUp and chghigh < 0 then Color.RED else if isUp then Color.YELLOW else if !isUp and chglow > 0 then Color.GREEN else if !isUp and chglow < 0 then Color.RED else Color.YELLOW, if isUp then yes else no );

here is the first half, to help you understand using , EnableApproximation()

another day i can look at the horizontal line part

----------------------------------------------

modified code after the test code

------------------------

test code
draw lines, connecting every 15th bar high.


Code:
# test_lines_peak_to_peak_00

# ref
#https://usethinkscript.com/threads/how-to-connect-peaks-and-breaks-on-zigzag.14596/
# How to Connect Peaks and Breaks on ZigZag

# draw lines connecting every 15th bar high.

def na = Double.NaN;
#def peak =  ... ( 1or 0 )
# create some numbers by reading every 15th bar.
def peak = if BarNumber() % 15 == 0 then 1 else 0;
def peakline = if peak then high else na;
plot zpeakline = peakline;
zpeakline.EnableApproximation();
zpeakline.SetDefaultColor(Color.CYAN);

input test1_bubble = yes;
AddChartBubble(test1_bubble, high*1.01,
peak + "\n" +
peakline
, (if peak then Color.YELLOW else color.gray), yes);
#

#https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/EnableApproximation


------------------------


modified main code
. draw lines peak to peak
. draw lines valley to valley

new code at the end of original


Code:
# lines_peak_to_peak_00b

#https://usethinkscript.com/threads/how-to-connect-peaks-and-breaks-on-zigzag.14596/
# How to Connect Peaks and Breaks on ZigZag
# Chemmy  2/23

#Hi all, I've been trying to work on something but I've run into a bit of a wall -- I want to draw connections within a ZigZag function based on two criteria:

#1) Draw connection from peak to peak and valley to valley
#2) If a higher peak is established, draw a line from the previous high and vice versa for lows

#Like below, where you can see each peak is connected and so is each valley -- also where horizontal lines are drawn at each new break:
#vEYZGtB.png

#The main issues as I can tell are that this is an arbitrary length based on each zigzag, and plotting backwards -- would something like this require a fold function in order to access these previous peaks/valleys? Or am I going in the wrong direction with this?

#I'm currently working off of a copy of @SleepyZ 's ZigZag Supply and Demand code, which has been one of my favorite studies for a long time. Code below:

#Code:
# Archive Name: ZigZag High Low with Supply DemandIndex and Fibs_Linus_Lar
# Archive Section: Scripts
# Suggested Tos Name: ZigZagHighLow_SupplyDemand_FibExtensions_LinusLar
# Archive Date:
# Archive Notes:

def na = Double.NaN;

#TOS version ZigZagHighLow modified in part by Linus' and Lar's code
input bubbleoffset = .0005;
input percentamount = .01;
input revAmount = .15;
input atrreversal = 3.0;
input atrlength = 5;
def zz = ZigZagHighLow("price h" = high, "price l" = low, "percentage reversal" = percentamount, "absolute reversal" = revAmount, "atr length" = atrlength, "atr reversal" = atrreversal);
def reversalAmount        = if (close * percentamount / 100) > Max(revAmount < atrreversal * atrlength, revAmount) then (close * percentamount / 100) else if revAmount < atrreversal * atrlength then atrreversal * atrlength else revAmount;
rec zzSave = if !IsNaN(zz) then zz else GetValue(zzSave, 1);
def chg = (if zzSave == high then high else low) - GetValue(zzSave, 1);
def isUp = chg >= 0;
rec isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue(zz, 1)) and GetValue(isConf, 1));
def zzd = if isUp then 1 else 0;
plot zzp = if zzd <= 1 then zz else Double.NaN;
zzp.AssignValueColor(if zzd == 1 then Color.GREEN else if zzd == 0 then Color.RED else Color.DARK_ORANGE);
zzp.SetStyle(Curve.FIRM);
zzp.EnableApproximation();

#Price Change between zigzags
def xxhigh = if zzSave == high then  high else xxhigh[1];
def chghigh = high - xxhigh[1];
def xxlow = if zzSave == low then low else xxlow[1];
def chglow = low - xxlow[1];
#input showBubbleschange = yes;
#AddChartBubble(showBubbleschange and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + bubbleoffset)  else low * (1 - bubbleoffset)   , "$" + chg , if isUp and chghigh > 0 then Color.GREEN else if isUp and chghigh < 0 then Color.RED else if isUp then Color.YELLOW else if !isUp and chglow > 0 then Color.GREEN else if !isUp and chglow < 0 then Color.RED else Color.YELLOW, isUp);

#Price at High/Low
#input showBubblesprice = no;
#AddChartBubble(showBubblesprice and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + bubbleoffset)  else low * (1 - #bubbleoffset)   , if isUp then "$" + high else "$" + low , if isUp and chghigh > 0 then Color.GREEN else if isUp and chghigh < 0 #then Color.RED else if isUp then Color.YELLOW else if !isUp and chglow > 0 then Color.GREEN else if !isUp and chglow < 0 then #Color.RED else Color.YELLOW, isUp);

#Label for Confirmed/Unconfirmed Status of Current Zigzag
#AddLabel(BarNumber() != 1, (if isConf then "Confirmed " else "Unconfirmed ") + "ZigZag: " + chg, if !isConf then Color.DARK_ORANGE else if isUp then Color.GREEN else Color.RED);


#---------------------------------------------------------------------------------------------------------------------------------
def ATRSave = if  !IsNaN(zz) then zzSave else GetValue(ATRSave, 1);
def ATRSave1 = if  ATRSave != ATRSave[1] then ATRSave[1] else GetValue(ATRSave1, 1);
def ATRSave2 = if  ATRSave1 != ATRSave1[1] then ATRSave1[1] else GetValue(ATRSave2, 1);
def ATRSave3 = if  ATRSave2 != ATRSave2[1] then ATRSave2[1] else GetValue(ATRSave3, 1);
#---------------------------------------------------------------------------------------------------------------------------------
def ATRchg = Round(zzSave - GetValue(ATRSave, 1));
plot ATRisUp = ATRchg >= 0;
ATRisUp.Hide();
def ATRisDown = ATRchg <= 0;
#def Conf = AbsValue(ATRchg) >= reversalamount or (IsNaN(GetValue(ZZ, 1)) and GetValue(isConf, 1));

plot ATRDiff = ATRchg;
ATRDiff.Hide();

#Bubbles and spacers--------------------------------------------------------------------------

input show_atr_change_bubble = yes;
AddChartBubble(show_atr_change_bubble,
                  zz,
                  AsText(ATRSave - ATRSave2),
                  if ATRisUp then Color.GREEN
                  else if ATRisDown then Color.RED
                  else Color.WHITE, ATRisUp);

input showHH_HL_LH_LL_bubble = yes;
AddChartBubble(showHH_HL_LH_LL_bubble,
                  zz,
                  if ATRisUp and (ATRSave - ATRSave2) > 0
                  then "HH"
                  else if ATRisUp
                  then "LH"
                  else if ATRisDown and (ATRSave - ATRSave2) > 0
                  then "HL"
                  else if ATRisDown and (ATRSave - ATRSave2) < 0
                  then "LL"
                  else "",
                  if ATRisUp then Color.GREEN
                  else if ATRisDown then Color.RED
                  else Color.WHITE, ATRisUp);


#AddChartBubble(showHH_HL_LH_LL_bubble,
#                  ZZ,
#                  if ATRisUp and EQH
#                  then "EQH"
#                  else if ATRisDown and EQL
#                  then "EQL"
#                  else "",
#                  if ATRisUp then Color.GREEN
#                  else if ATRisDown then Color.RED
#                  else Color.WHITE);


#Arrows
def zzL = if !IsNaN(zz) and !isUp then low else GetValue(zzL, 1);
def zzH = if !IsNaN(zz) and isUp then high else GetValue(zzH, 1);
def dir = CompoundValue(1, if zzL != zzL[1] or low == zzL[1] and low == zzSave then 1 else if zzH != zzH[1] or high == zzH[1] and high == zzSave then -1 else dir[1], 0);
def signal = CompoundValue(1, if dir > 0 and low > zzL then if signal[1] <= 0 then 1 else signal[1] else if dir < 0 and high < zzH then if signal[1] >= 0 then -1 else signal[1]    else signal[1], 0);
input showarrows = no;
plot U1 = showarrows and signal > 0 and signal[1] <= 0;
U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
U1.SetDefaultColor(Color.GREEN);
U1.SetLineWeight(4);
plot D1 = showarrows and signal < 0 and signal[1] >= 0;
D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
D1.SetDefaultColor(Color.RED);
D1.SetLineWeight(4);

## Double Tops and bottoms
input show_Double_TopsBottoms_bubble = yes;
input EQ_range = 0.10;
def EQH = show_Double_TopsBottoms_bubble and ATRisUp and AbsValue(ATRSave - ATRSave2) <= EQ_range;
def EQL = show_Double_TopsBottoms_bubble and ATRisDown and AbsValue(ATRSave - ATRSave2) <= EQ_range;

AddChartBubble(EQH, zz, "EQH", Color.RED);
AddChartBubble(EQL, zz, "EQL", Color.GREEN);


#Alerts
input usealerts = no;
Alert(usealerts and U1, "ZIG-UP", Alert.BAR, Sound.Bell);
Alert(usealerts and D1, "ZAG-DOWN", Alert.BAR, Sound.Chimes);

#Supply Demand Areas
rec data1 = CompoundValue(1, if (zzSave == high or zzSave == low) then data1[1] + 1 else data1[1], 0);
def datacount1 = (HighestAll(data1) - data1[1]);
input numbersuppdemandtoshow = 2;
input showSupplyDemand = {default Pivot, Arrow, None};
def idx = if showSupplyDemand == showSupplyDemand.Pivot then 1 else 0;
def rLow;
def rHigh;
if signal crosses 0 {
    rLow = low[idx];
    rHigh = high[idx];
} else {
    rLow = rLow[1];
    rHigh = rHigh[1];
}
plot HighLine = if datacount1 <= numbersuppdemandtoshow and showSupplyDemand != showSupplyDemand.None and !IsNaN(close) and rHigh != 0 then rHigh else Double.NaN;
HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HighLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);

plot LowLine = if datacount1 <= numbersuppdemandtoshow and  showSupplyDemand != showSupplyDemand.None and !IsNaN(close) and rLow != 0 then rLow else Double.NaN;
LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LowLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);

def hlUp = if signal > 0 then HighLine else Double.NaN;
def hlDn = if signal < 0 then HighLine else Double.NaN;

input showsupplydemandcloud = yes;
AddCloud(if showsupplydemandcloud then hlUp else Double.NaN, LowLine, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(if showsupplydemandcloud then hlDn else Double.NaN, LowLine, Color.LIGHT_RED, Color.LIGHT_RED);

#Store Previous Data
def zzsave1 = if !IsNaN(zzSave) then zzSave else zzsave1[1];
def zzsave2 = zzsave1;
rec priorzz1 = if zzsave2  != zzsave2[1]  then zzsave2[1]  else priorzz1[1];
rec priorzz2 = if priorzz1 != priorzz1[1] then priorzz1[1] else priorzz2[1];
rec priorzz3 = if priorzz2 != priorzz2[1] then priorzz2[1] else priorzz3[1];

#Fibonacci Extensions
rec data = CompoundValue(1, if (zzSave == high or zzSave == low) then data[1] + 1 else data[1], 0);
def datacount = (HighestAll(data) - data[1]);
input numberextfibstoshow = 2;
rec cpo = if dir[1] != dir then 0 else 1;
input showFibExtLines = yes;
input showtodayonly = no;
def today = if showtodayonly == yes then GetDay() == GetLastDay() else GetDay();
def extfib1 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) * 1
else extfib1[1];
plot extfib100 = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1) and dir < 0 and cpo != 0 then extfib1[1] else Double.NaN;
extfib100.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib100.SetDefaultColor(Color.RED);
extfib100.SetLineWeight(1);
extfib100.HideBubble();
def extfib1a = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) * 0.382
else extfib1a[1];
plot extfib382 = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1a) and dir < 0 and cpo != 0 then extfib1a[1] else Double.NaN;
extfib382.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib382.SetDefaultColor(Color.RED);
extfib382.SetLineWeight(1);
extfib382.HideBubble();
def extfib2 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
0.618 else extfib2[1];
plot extfib618 = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib2) and dir < 0 and cpo != 0  then extfib2[1] else Double.NaN;
extfib618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib618.SetDefaultColor(Color.RED);
extfib618.SetLineWeight(1);
extfib618.HideBubble();
def extfib3 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
1.618 else extfib3[1];
plot extfib1618 = if datacount <= numberextfibstoshow and today and showFibExtLines and  !IsNaN(extfib3) and dir < 0  and cpo != 0  then extfib3[1] else Double.NaN;
extfib1618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib1618.SetDefaultColor(Color.RED);
extfib1618.SetLineWeight(1);
extfib1618.HideBubble();
def extfib3a = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
2.000 else extfib3a[1];
plot extfib2000 = if datacount <= numberextfibstoshow and today and showFibExtLines and  !IsNaN(extfib3a) and dir < 0  and cpo != 0  then extfib3a[1] else Double.NaN;
extfib2000.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2000.SetDefaultColor(Color.RED);
extfib2000.SetLineWeight(1);
extfib2000.HideBubble();
def extfib4 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
2.618 else extfib4[1];
plot extfib2618 = if  datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib4) and dir < 0  and cpo != 0  then extfib4[1] else Double.NaN;
extfib2618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2618.SetDefaultColor(Color.RED);
extfib2618.SetLineWeight(1);
extfib2618.HideBubble();
def extfib5 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
3.618 else extfib5[1];
plot extfib3618 = if  datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib5) and dir < 0  and cpo != 0  then extfib5[1] else Double.NaN;
extfib3618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib3618.SetDefaultColor(Color.RED);
extfib3618.SetLineWeight(1);
extfib3618.HideBubble();
def extfib1_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) * 1
else extfib1_[1];
plot extfib100_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1_) and dir > 0 and cpo != 0 then extfib1_[1] else Double.NaN;
extfib100_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib100_.SetDefaultColor(Color.GREEN);
extfib100_.SetLineWeight(1);
extfib100_.HideBubble();
def extfib1a_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) * 0.382
else extfib1a_[1];
plot extfib382_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1a_) and dir > 0 and cpo != 0 then extfib1a_[1] else Double.NaN;
extfib382_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib382_.SetDefaultColor(Color.GREEN);
extfib382_.SetLineWeight(1);
extfib382_.HideBubble();
def extfib2_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
0.618 else extfib2_[1];
plot extfib618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib2_) and dir > 0  and cpo != 0  then extfib2_[1] else Double.NaN;
extfib618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib618_.SetDefaultColor(Color.GREEN);
extfib618_.SetLineWeight(1);
extfib618_.HideBubble();
def extfib3_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
1.618 else extfib3_[1];
plot extfib1618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib3_) and dir > 0  and cpo != 0  then extfib3_[1] else Double.NaN;
extfib1618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib1618_.SetDefaultColor(Color.GREEN);
extfib1618_.SetLineWeight(1);
extfib1618_.HideBubble();
def extfib3a_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
2.000 else extfib3a_[1];
plot extfib2000_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib3a_) and dir > 0  and cpo != 0  then extfib3a_[1] else Double.NaN;
extfib2000_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2000_.SetDefaultColor(Color.GREEN);
extfib2000_.SetLineWeight(1);
extfib2000_.HideBubble();
def extfib4_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
2.618 else extfib4_[1];
plot extfib2618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib4_) and dir > 0  and cpo != 0  then extfib4_[1]  else Double.NaN;
extfib2618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2618_.SetDefaultColor(Color.GREEN);
extfib2618_.SetLineWeight(1);
extfib2618_.HideBubble();
def extfib5_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
3.618 else extfib5_[1];
plot extfib3618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib5_) and dir > 0  and cpo != 0  then extfib5_[1]  else Double.NaN;
extfib3618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib3618_.SetDefaultColor(Color.GREEN);
extfib3618_.SetLineWeight(1);
extfib3618_.HideBubble();
input fibextbubblespacesinexpansion = 8;
def b = fibextbubblespacesinexpansion;
def direction = if !isUp then 1 else 0;
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1[b + 2], "100%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1a[b + 2], "38.2%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib2[b + 2], "61.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3[b + 2], "161.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3a[b + 2], "200%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib4[b + 2], "261.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib5[b + 2], "361.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1_[b + 2], "100%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1a_[b + 2], "38.2%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib2_[b + 2], "61.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3_[b + 2], "161.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3a_[b + 2], "200%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib4_[b + 2], "261.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib5_[b + 2], "361.8%", Color.GREEN, yes);

#Volume at Reversals
def vol = if BarNumber() == 0 then 0 else volume + vol[1];
def vol1 = if BarNumber() == 1 then volume else vol1[1];
def xxvol = if zzSave == high or zzSave == low then TotalSum(volume) else xxvol[1];
def chgvol =  if xxvol - xxvol[1] + vol1 == vol then vol else xxvol - xxvol[1];
input showBubblesVolume = no;
AddChartBubble(showBubblesVolume and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + bubbleoffset)  else low * (1 - bubbleoffset), chgvol, if isUp and chghigh > 0 then Color.GREEN else if isUp and chghigh < 0 then Color.RED else if isUp then Color.YELLOW else if !isUp and chglow > 0 then Color.GREEN else if !isUp and chglow < 0 then Color.RED else Color.YELLOW, if isUp then yes else no );



#add this to the end of that study, to separate peak and valley numbers , into 2 variables.

#def na = Double.NaN;
def peaks = if IsNaN(zz) then zz
else if zz == high then high
else na;
def valleys = if IsNaN(zz) then zz
else if zz == low then low
else na;

plot zpeaks = peaks;
zpeaks.EnableApproximation();
zpeaks.SetDefaultColor(Color.CYAN);
#
plot zvalleys = valleys;
zvalleys.EnableApproximation();
zvalleys.SetDefaultColor(Color.magenta);
#
 
here is the first half, to help you understand using , EnableApproximation()

another day i can look at the horizontal line part

----------------------------------------------

modified code after the test code

------------------------

test code
draw lines, connecting every 15th bar high.


Code:
# test_lines_peak_to_peak_00

# ref
#https://usethinkscript.com/threads/how-to-connect-peaks-and-breaks-on-zigzag.14596/
# How to Connect Peaks and Breaks on ZigZag

# draw lines connecting every 15th bar high.

def na = Double.NaN;
#def peak =  ... ( 1or 0 )
# create some numbers by reading every 15th bar.
def peak = if BarNumber() % 15 == 0 then 1 else 0;
def peakline = if peak then high else na;
plot zpeakline = peakline;
zpeakline.EnableApproximation();
zpeakline.SetDefaultColor(Color.CYAN);

input test1_bubble = yes;
AddChartBubble(test1_bubble, high*1.01,
peak + "\n" +
peakline
, (if peak then Color.YELLOW else color.gray), yes);
#

#https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/EnableApproximation


------------------------


modified main code
. draw lines peak to peak
. draw lines valley to valley

new code at the end of original


Code:
# lines_peak_to_peak_00b

#https://usethinkscript.com/threads/how-to-connect-peaks-and-breaks-on-zigzag.14596/
# How to Connect Peaks and Breaks on ZigZag
# Chemmy  2/23

#Hi all, I've been trying to work on something but I've run into a bit of a wall -- I want to draw connections within a ZigZag function based on two criteria:

#1) Draw connection from peak to peak and valley to valley
#2) If a higher peak is established, draw a line from the previous high and vice versa for lows

#Like below, where you can see each peak is connected and so is each valley -- also where horizontal lines are drawn at each new break:
#vEYZGtB.png

#The main issues as I can tell are that this is an arbitrary length based on each zigzag, and plotting backwards -- would something like this require a fold function in order to access these previous peaks/valleys? Or am I going in the wrong direction with this?

#I'm currently working off of a copy of @SleepyZ 's ZigZag Supply and Demand code, which has been one of my favorite studies for a long time. Code below:

#Code:
# Archive Name: ZigZag High Low with Supply DemandIndex and Fibs_Linus_Lar
# Archive Section: Scripts
# Suggested Tos Name: ZigZagHighLow_SupplyDemand_FibExtensions_LinusLar
# Archive Date:
# Archive Notes:

def na = Double.NaN;

#TOS version ZigZagHighLow modified in part by Linus' and Lar's code
input bubbleoffset = .0005;
input percentamount = .01;
input revAmount = .15;
input atrreversal = 3.0;
input atrlength = 5;
def zz = ZigZagHighLow("price h" = high, "price l" = low, "percentage reversal" = percentamount, "absolute reversal" = revAmount, "atr length" = atrlength, "atr reversal" = atrreversal);
def reversalAmount        = if (close * percentamount / 100) > Max(revAmount < atrreversal * atrlength, revAmount) then (close * percentamount / 100) else if revAmount < atrreversal * atrlength then atrreversal * atrlength else revAmount;
rec zzSave = if !IsNaN(zz) then zz else GetValue(zzSave, 1);
def chg = (if zzSave == high then high else low) - GetValue(zzSave, 1);
def isUp = chg >= 0;
rec isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue(zz, 1)) and GetValue(isConf, 1));
def zzd = if isUp then 1 else 0;
plot zzp = if zzd <= 1 then zz else Double.NaN;
zzp.AssignValueColor(if zzd == 1 then Color.GREEN else if zzd == 0 then Color.RED else Color.DARK_ORANGE);
zzp.SetStyle(Curve.FIRM);
zzp.EnableApproximation();

#Price Change between zigzags
def xxhigh = if zzSave == high then  high else xxhigh[1];
def chghigh = high - xxhigh[1];
def xxlow = if zzSave == low then low else xxlow[1];
def chglow = low - xxlow[1];
#input showBubbleschange = yes;
#AddChartBubble(showBubbleschange and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + bubbleoffset)  else low * (1 - bubbleoffset)   , "$" + chg , if isUp and chghigh > 0 then Color.GREEN else if isUp and chghigh < 0 then Color.RED else if isUp then Color.YELLOW else if !isUp and chglow > 0 then Color.GREEN else if !isUp and chglow < 0 then Color.RED else Color.YELLOW, isUp);

#Price at High/Low
#input showBubblesprice = no;
#AddChartBubble(showBubblesprice and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + bubbleoffset)  else low * (1 - #bubbleoffset)   , if isUp then "$" + high else "$" + low , if isUp and chghigh > 0 then Color.GREEN else if isUp and chghigh < 0 #then Color.RED else if isUp then Color.YELLOW else if !isUp and chglow > 0 then Color.GREEN else if !isUp and chglow < 0 then #Color.RED else Color.YELLOW, isUp);

#Label for Confirmed/Unconfirmed Status of Current Zigzag
#AddLabel(BarNumber() != 1, (if isConf then "Confirmed " else "Unconfirmed ") + "ZigZag: " + chg, if !isConf then Color.DARK_ORANGE else if isUp then Color.GREEN else Color.RED);


#---------------------------------------------------------------------------------------------------------------------------------
def ATRSave = if  !IsNaN(zz) then zzSave else GetValue(ATRSave, 1);
def ATRSave1 = if  ATRSave != ATRSave[1] then ATRSave[1] else GetValue(ATRSave1, 1);
def ATRSave2 = if  ATRSave1 != ATRSave1[1] then ATRSave1[1] else GetValue(ATRSave2, 1);
def ATRSave3 = if  ATRSave2 != ATRSave2[1] then ATRSave2[1] else GetValue(ATRSave3, 1);
#---------------------------------------------------------------------------------------------------------------------------------
def ATRchg = Round(zzSave - GetValue(ATRSave, 1));
plot ATRisUp = ATRchg >= 0;
ATRisUp.Hide();
def ATRisDown = ATRchg <= 0;
#def Conf = AbsValue(ATRchg) >= reversalamount or (IsNaN(GetValue(ZZ, 1)) and GetValue(isConf, 1));

plot ATRDiff = ATRchg;
ATRDiff.Hide();

#Bubbles and spacers--------------------------------------------------------------------------

input show_atr_change_bubble = yes;
AddChartBubble(show_atr_change_bubble,
                  zz,
                  AsText(ATRSave - ATRSave2),
                  if ATRisUp then Color.GREEN
                  else if ATRisDown then Color.RED
                  else Color.WHITE, ATRisUp);

input showHH_HL_LH_LL_bubble = yes;
AddChartBubble(showHH_HL_LH_LL_bubble,
                  zz,
                  if ATRisUp and (ATRSave - ATRSave2) > 0
                  then "HH"
                  else if ATRisUp
                  then "LH"
                  else if ATRisDown and (ATRSave - ATRSave2) > 0
                  then "HL"
                  else if ATRisDown and (ATRSave - ATRSave2) < 0
                  then "LL"
                  else "",
                  if ATRisUp then Color.GREEN
                  else if ATRisDown then Color.RED
                  else Color.WHITE, ATRisUp);


#AddChartBubble(showHH_HL_LH_LL_bubble,
#                  ZZ,
#                  if ATRisUp and EQH
#                  then "EQH"
#                  else if ATRisDown and EQL
#                  then "EQL"
#                  else "",
#                  if ATRisUp then Color.GREEN
#                  else if ATRisDown then Color.RED
#                  else Color.WHITE);


#Arrows
def zzL = if !IsNaN(zz) and !isUp then low else GetValue(zzL, 1);
def zzH = if !IsNaN(zz) and isUp then high else GetValue(zzH, 1);
def dir = CompoundValue(1, if zzL != zzL[1] or low == zzL[1] and low == zzSave then 1 else if zzH != zzH[1] or high == zzH[1] and high == zzSave then -1 else dir[1], 0);
def signal = CompoundValue(1, if dir > 0 and low > zzL then if signal[1] <= 0 then 1 else signal[1] else if dir < 0 and high < zzH then if signal[1] >= 0 then -1 else signal[1]    else signal[1], 0);
input showarrows = no;
plot U1 = showarrows and signal > 0 and signal[1] <= 0;
U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
U1.SetDefaultColor(Color.GREEN);
U1.SetLineWeight(4);
plot D1 = showarrows and signal < 0 and signal[1] >= 0;
D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
D1.SetDefaultColor(Color.RED);
D1.SetLineWeight(4);

## Double Tops and bottoms
input show_Double_TopsBottoms_bubble = yes;
input EQ_range = 0.10;
def EQH = show_Double_TopsBottoms_bubble and ATRisUp and AbsValue(ATRSave - ATRSave2) <= EQ_range;
def EQL = show_Double_TopsBottoms_bubble and ATRisDown and AbsValue(ATRSave - ATRSave2) <= EQ_range;

AddChartBubble(EQH, zz, "EQH", Color.RED);
AddChartBubble(EQL, zz, "EQL", Color.GREEN);


#Alerts
input usealerts = no;
Alert(usealerts and U1, "ZIG-UP", Alert.BAR, Sound.Bell);
Alert(usealerts and D1, "ZAG-DOWN", Alert.BAR, Sound.Chimes);

#Supply Demand Areas
rec data1 = CompoundValue(1, if (zzSave == high or zzSave == low) then data1[1] + 1 else data1[1], 0);
def datacount1 = (HighestAll(data1) - data1[1]);
input numbersuppdemandtoshow = 2;
input showSupplyDemand = {default Pivot, Arrow, None};
def idx = if showSupplyDemand == showSupplyDemand.Pivot then 1 else 0;
def rLow;
def rHigh;
if signal crosses 0 {
    rLow = low[idx];
    rHigh = high[idx];
} else {
    rLow = rLow[1];
    rHigh = rHigh[1];
}
plot HighLine = if datacount1 <= numbersuppdemandtoshow and showSupplyDemand != showSupplyDemand.None and !IsNaN(close) and rHigh != 0 then rHigh else Double.NaN;
HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HighLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);

plot LowLine = if datacount1 <= numbersuppdemandtoshow and  showSupplyDemand != showSupplyDemand.None and !IsNaN(close) and rLow != 0 then rLow else Double.NaN;
LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LowLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);

def hlUp = if signal > 0 then HighLine else Double.NaN;
def hlDn = if signal < 0 then HighLine else Double.NaN;

input showsupplydemandcloud = yes;
AddCloud(if showsupplydemandcloud then hlUp else Double.NaN, LowLine, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(if showsupplydemandcloud then hlDn else Double.NaN, LowLine, Color.LIGHT_RED, Color.LIGHT_RED);

#Store Previous Data
def zzsave1 = if !IsNaN(zzSave) then zzSave else zzsave1[1];
def zzsave2 = zzsave1;
rec priorzz1 = if zzsave2  != zzsave2[1]  then zzsave2[1]  else priorzz1[1];
rec priorzz2 = if priorzz1 != priorzz1[1] then priorzz1[1] else priorzz2[1];
rec priorzz3 = if priorzz2 != priorzz2[1] then priorzz2[1] else priorzz3[1];

#Fibonacci Extensions
rec data = CompoundValue(1, if (zzSave == high or zzSave == low) then data[1] + 1 else data[1], 0);
def datacount = (HighestAll(data) - data[1]);
input numberextfibstoshow = 2;
rec cpo = if dir[1] != dir then 0 else 1;
input showFibExtLines = yes;
input showtodayonly = no;
def today = if showtodayonly == yes then GetDay() == GetLastDay() else GetDay();
def extfib1 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) * 1
else extfib1[1];
plot extfib100 = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1) and dir < 0 and cpo != 0 then extfib1[1] else Double.NaN;
extfib100.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib100.SetDefaultColor(Color.RED);
extfib100.SetLineWeight(1);
extfib100.HideBubble();
def extfib1a = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) * 0.382
else extfib1a[1];
plot extfib382 = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1a) and dir < 0 and cpo != 0 then extfib1a[1] else Double.NaN;
extfib382.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib382.SetDefaultColor(Color.RED);
extfib382.SetLineWeight(1);
extfib382.HideBubble();
def extfib2 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
0.618 else extfib2[1];
plot extfib618 = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib2) and dir < 0 and cpo != 0  then extfib2[1] else Double.NaN;
extfib618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib618.SetDefaultColor(Color.RED);
extfib618.SetLineWeight(1);
extfib618.HideBubble();
def extfib3 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
1.618 else extfib3[1];
plot extfib1618 = if datacount <= numberextfibstoshow and today and showFibExtLines and  !IsNaN(extfib3) and dir < 0  and cpo != 0  then extfib3[1] else Double.NaN;
extfib1618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib1618.SetDefaultColor(Color.RED);
extfib1618.SetLineWeight(1);
extfib1618.HideBubble();
def extfib3a = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
2.000 else extfib3a[1];
plot extfib2000 = if datacount <= numberextfibstoshow and today and showFibExtLines and  !IsNaN(extfib3a) and dir < 0  and cpo != 0  then extfib3a[1] else Double.NaN;
extfib2000.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2000.SetDefaultColor(Color.RED);
extfib2000.SetLineWeight(1);
extfib2000.HideBubble();
def extfib4 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
2.618 else extfib4[1];
plot extfib2618 = if  datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib4) and dir < 0  and cpo != 0  then extfib4[1] else Double.NaN;
extfib2618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2618.SetDefaultColor(Color.RED);
extfib2618.SetLineWeight(1);
extfib2618.HideBubble();
def extfib5 = if zzSave == high then high - AbsValue(priorzz2 - priorzz1) *
3.618 else extfib5[1];
plot extfib3618 = if  datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib5) and dir < 0  and cpo != 0  then extfib5[1] else Double.NaN;
extfib3618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib3618.SetDefaultColor(Color.RED);
extfib3618.SetLineWeight(1);
extfib3618.HideBubble();
def extfib1_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) * 1
else extfib1_[1];
plot extfib100_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1_) and dir > 0 and cpo != 0 then extfib1_[1] else Double.NaN;
extfib100_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib100_.SetDefaultColor(Color.GREEN);
extfib100_.SetLineWeight(1);
extfib100_.HideBubble();
def extfib1a_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) * 0.382
else extfib1a_[1];
plot extfib382_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib1a_) and dir > 0 and cpo != 0 then extfib1a_[1] else Double.NaN;
extfib382_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib382_.SetDefaultColor(Color.GREEN);
extfib382_.SetLineWeight(1);
extfib382_.HideBubble();
def extfib2_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
0.618 else extfib2_[1];
plot extfib618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib2_) and dir > 0  and cpo != 0  then extfib2_[1] else Double.NaN;
extfib618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib618_.SetDefaultColor(Color.GREEN);
extfib618_.SetLineWeight(1);
extfib618_.HideBubble();
def extfib3_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
1.618 else extfib3_[1];
plot extfib1618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib3_) and dir > 0  and cpo != 0  then extfib3_[1] else Double.NaN;
extfib1618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib1618_.SetDefaultColor(Color.GREEN);
extfib1618_.SetLineWeight(1);
extfib1618_.HideBubble();
def extfib3a_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
2.000 else extfib3a_[1];
plot extfib2000_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib3a_) and dir > 0  and cpo != 0  then extfib3a_[1] else Double.NaN;
extfib2000_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2000_.SetDefaultColor(Color.GREEN);
extfib2000_.SetLineWeight(1);
extfib2000_.HideBubble();
def extfib4_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
2.618 else extfib4_[1];
plot extfib2618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib4_) and dir > 0  and cpo != 0  then extfib4_[1]  else Double.NaN;
extfib2618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2618_.SetDefaultColor(Color.GREEN);
extfib2618_.SetLineWeight(1);
extfib2618_.HideBubble();
def extfib5_ = if zzSave == low then low + AbsValue(priorzz2 - priorzz1) *
3.618 else extfib5_[1];
plot extfib3618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and !IsNaN(extfib5_) and dir > 0  and cpo != 0  then extfib5_[1]  else Double.NaN;
extfib3618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib3618_.SetDefaultColor(Color.GREEN);
extfib3618_.SetLineWeight(1);
extfib3618_.HideBubble();
input fibextbubblespacesinexpansion = 8;
def b = fibextbubblespacesinexpansion;
def direction = if !isUp then 1 else 0;
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1[b + 2], "100%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1a[b + 2], "38.2%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib2[b + 2], "61.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3[b + 2], "161.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3a[b + 2], "200%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib4[b + 2], "261.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib5[b + 2], "361.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1_[b + 2], "100%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1a_[b + 2], "38.2%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib2_[b + 2], "61.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3_[b + 2], "161.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3a_[b + 2], "200%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib4_[b + 2], "261.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib5_[b + 2], "361.8%", Color.GREEN, yes);

#Volume at Reversals
def vol = if BarNumber() == 0 then 0 else volume + vol[1];
def vol1 = if BarNumber() == 1 then volume else vol1[1];
def xxvol = if zzSave == high or zzSave == low then TotalSum(volume) else xxvol[1];
def chgvol =  if xxvol - xxvol[1] + vol1 == vol then vol else xxvol - xxvol[1];
input showBubblesVolume = no;
AddChartBubble(showBubblesVolume and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + bubbleoffset)  else low * (1 - bubbleoffset), chgvol, if isUp and chghigh > 0 then Color.GREEN else if isUp and chghigh < 0 then Color.RED else if isUp then Color.YELLOW else if !isUp and chglow > 0 then Color.GREEN else if !isUp and chglow < 0 then Color.RED else Color.YELLOW, if isUp then yes else no );



#add this to the end of that study, to separate peak and valley numbers , into 2 variables.

#def na = Double.NaN;
def peaks = if IsNaN(zz) then zz
else if zz == high then high
else na;
def valleys = if IsNaN(zz) then zz
else if zz == low then low
else na;

plot zpeaks = peaks;
zpeaks.EnableApproximation();
zpeaks.SetDefaultColor(Color.CYAN);
#
plot zvalleys = valleys;
zvalleys.EnableApproximation();
zvalleys.SetDefaultColor(Color.magenta);
#

Thanks for this, @halcyonguy ! This helped a ton -- one follow-up question on this though, is it possible to limit the EnableApproximation plot? One of the things I'm trying to do is plot for double tops and double bottoms in a more real-time fashion than what I've seen in some other studies. I've been able to properly get the labels to show using this bit of code:

Code:
## Double Tops and bottoms

input show_Double_TopsBottoms_bubble = yes;

input EQ_range = 0.10;

def EQH = show_Double_TopsBottoms_bubble and ATRisUp and AbsValue(ATRSave - ATRSave2) <= EQ_range;

def EQL = show_Double_TopsBottoms_bubble and ATRisDown and AbsValue(ATRSave - ATRSave2) <= EQ_range;



AddChartBubble(EQH, zz, "EQH", Color.RED);

AddChartBubble(EQL, zz, "EQL", Color.GREEN);

But being able to plot between the two points has been an issue. Even when I can get the peaks to plot properly on the Double Tops/Double Bottoms and nowhere else, the EnableApproximation function seems to track ALL of them and only connect on those points, rather than separating them:

Y3RTZ0T.png


As you can see above, point 1 has an Equal High right before, but the line is being traced from an Equal High almost a day ago -- it then traces onto Point 2, which is correct. Then, however, Points 3 and 4 are Equal Highs with each other -- I want a line to connect those two, rather than connecting back with Point 2. Does that make sense?

Below is the bit of code change I used to get where I'm currently at with the plotting. Is there any way to suppress the EnableApproximation plot beyond a certain range, so that it only plots back with the recent peak? I couldn't find much at all about it on the ThinkorSwim website. If this isn't possible with the current code, I may just cut my losses with trying to plot the line -- I just figured it would be a helpful addition.

Code:
def peaks = if (AbsValue(ATRSave - ATRSave2) <= EQ_range and IsNaN(zz)) then zz
else if (zz == high and AbsValue(ATRSave - ATRSave2) <= EQ_range) then high
else na;

plot zpeaks = peaks ;
zpeaks.EnableApproximation();
zpeaks.SetDefaultColor(Color.CYAN);
 
Thanks for this, @halcyonguy ! This helped a ton -- one follow-up question on this though, is it possible to limit the EnableApproximation plot? One of the things I'm trying to do is plot for double tops and double bottoms in a more real-time fashion than what I've seen in some other studies. I've been able to properly get the labels to show using this bit of code:

Code:
## Double Tops and bottoms

input show_Double_TopsBottoms_bubble = yes;

input EQ_range = 0.10;

def EQH = show_Double_TopsBottoms_bubble and ATRisUp and AbsValue(ATRSave - ATRSave2) <= EQ_range;

def EQL = show_Double_TopsBottoms_bubble and ATRisDown and AbsValue(ATRSave - ATRSave2) <= EQ_range;



AddChartBubble(EQH, zz, "EQH", Color.RED);

AddChartBubble(EQL, zz, "EQL", Color.GREEN);

But being able to plot between the two points has been an issue. Even when I can get the peaks to plot properly on the Double Tops/Double Bottoms and nowhere else, the EnableApproximation function seems to track ALL of them and only connect on those points, rather than separating them:



As you can see above, point 1 has an Equal High right before, but the line is being traced from an Equal High almost a day ago -- it then traces onto Point 2, which is correct. Then, however, Points 3 and 4 are Equal Highs with each other -- I want a line to connect those two, rather than connecting back with Point 2. Does that make sense?

Below is the bit of code change I used to get where I'm currently at with the plotting. Is there any way to suppress the EnableApproximation plot beyond a certain range, so that it only plots back with the recent peak? I couldn't find much at all about it on the ThinkorSwim website. If this isn't possible with the current code, I may just cut my losses with trying to plot the line -- I just figured it would be a helpful addition.

Code:
def peaks = if (AbsValue(ATRSave - ATRSave2) <= EQ_range and IsNaN(zz)) then zz
else if (zz == high and AbsValue(ATRSave - ATRSave2) <= EQ_range) then high
else na;

plot zpeaks = peaks ;
zpeaks.EnableApproximation();
zpeaks.SetDefaultColor(Color.CYAN);

i think once a line starts plotting with .EnableApproximation() it can't be stopped.

to plot several diagonal lines , i have calculated a slope for each line.

an outline would be something like this,
..on a desired peak,
....run a loop, to look at future bars, to find the next desired peak, and return the quantity of bars to it.
....then use that quantity as an offset and read a price from the next peak
....then calculate the slope, the price change bar to bar,
...... (peak2 $ - peak1 $) / bar quantity
..after a peak, compare barnumbers to (peak1 barnumber + quantity) to current barnumber, and add the slope price to previous price

a test code to experiment with

Code:
# test_slope_lines_on_peaks_00

# draw diagonal lines, between peaks

#---------------------------
# test code to find peaks and valleys

#------------------------
#https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
# original code, bar qty includes current bar
# define swing low points , robert payne
# modifified by halcyonguy to ignore last bar , and bar qty does not include current bar
#------------------------
def bn = BarNumber();
def na = double.nan;
input peak_valley_bars = 9;
def length = peak_valley_bars;
def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastbn - bn);
input ignore_last_bar = yes;
def ignorelast = if (ignore_last_bar and bn == lastbn) then 0 else 1;
# original robert code, bar qty includes current bar
def peak = ignorelast and high > highest(high[1], length - 1) and high == GetValue(highest(high, length), -offset);
def valley = ignorelast and low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);

#addlabel(1, "peak/valley bars " + peak_valley_bars, color.yellow);

input show_arrows = yes;
def vert = 0.001;
plot z1 = if show_arrows and peak then high*(1+vert) else na;
z1.SetPaintingStrategy(PaintingStrategy.arrow_down);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(3);
z1.hidebubble();

plot z2 = if show_arrows and valley then low*(1-vert) else na;
z2.SetPaintingStrategy(PaintingStrategy.arrow_up);
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(3);
z2.hidebubble();

#-------------------------------------


# this draws a diagonal line between each peak
# change  ' if peak then { '   to whatever condition you want


def peak_offset;
def nextpeakbn;
def nextpeakhi;
def slope;

if peak then {
# do stuff on a peak
  peak_offset = fold i = 1 to 500
   with p = 1
   while !getvalue(peak, -i)
   do p + 1;
  nextpeakbn = bn + peak_offset;
  nextpeakhi = getvalue(high, -peak_offset);
  slope = (nextpeakhi - high)/peak_offset;
} else {
# do stuff between peaks
  peak_offset = peak_offset[1];
  nextpeakbn = nextpeakbn[1];
  nextpeakhi = nextpeakhi[1];
  slope = slope[1];
}

def diag_en = (bn <= nextpeakbn);

def diag = if peak then high
 else if diag_en then diag[1] + slope
 else na;

plot zdiag = diag;


#-----------------------------
# test stuff

input test1_bubbles = no;
addchartbubble(test1_bubbles and peak, high*1.01,
bn + "\n" +
peak_offset
, color.yellow, yes);

#
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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