Need help getting avrage of pivot high/low

adapted from: https://usethinkscript.com/threads/swing-high-low-indicator-for-thinkorswim.3109/page-5#post-96805

Ive tried to get the average of last 5 pivot change from low to high and high to low, but haven’t had luck
looking at the last 5 average should be 9.5

not sure if there is a way to do this but any help would be great

QqD84qc.png


Code:
def h = high;
def l = low;
def c = close;
def o = open;
def v = volume;

# Simple Fractal Recon
input length = 5;

def HH1 = h >= Highest(h, length) and h >= Highest(h, length)[-length];
def  High1 = if HH1 then h else Double.NaN;
def LL1 = l <= Lowest(l, length) and l <= Lowest(l, length)[-length];
def Low1 = if LL1 then l else Double.NaN;
def PointCount = if BarNumber() == 1 then 0 else
                 if IsNaN(c) then PointCount[1] else
                 if !IsNaN(High1) then Max(1, PointCount[1] + 1) else
                 if !IsNaN(Low1) then Min(-1, PointCount[1] - 1)
                 else PointCount[1];

input showhorizontals = no;

def RangeHI = if !IsNaN(High1) and PointCount == 1
then h else RangeHI[1];
plot Rhi = RangeHI;
Rhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rhi.SetLineWeight(1);
Rhi.SetDefaultColor(Color.RED);
Rhi.SetHiding(!showhorizontals);

def RangeLO = if !IsNaN(Low1) and PointCount == -1
then l else RangeLO[1];
plot Rlo =  RangeLO;
Rlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rlo.SetLineWeight(1);
Rlo.SetDefaultColor(Color.GREEN);
Rlo.SetHiding(!showhorizontals);


def rh1 = if !IsNaN(Rhi) then Rhi else rh1[1];
def rh2 = if rh1 != rh1[1] then rh1[1] else rh2[1];

def rl1 = if !IsNaN(Rlo) then Rlo else rl1[1];
def rl2 = if rl1 != rl1[1] then rl1[1] else rl2[1];

#ZigZags
def zHI;
def zLO;

zHI = if !IsNaN(High1) and PointCount == 1 then h else Double.NaN;
zLO = if !IsNaN(Low1) and PointCount == -1 then l else Double.NaN;

def bn = BarNumber();
def lastzhibar = if !IsNaN(zHI) then bn else lastzhibar[1];
def lastzlobar = if !IsNaN(zLO) then bn else lastzlobar[1];
def lastzhi    = if bn==highestall(lastzhibar) then h else lastzhi[1];
def lastzlo    = if bn==highestall(lastzlobar) then l else lastzlo[1];

plot hilowline = if !IsNaN(zHI) then zHI else zLO;
hilowline.EnableApproximation();
hilowline.SetLineWeight(3);
hilowline.SetDefaultColor(Color.WHITE);

def last = if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN;
def lastbar   = if BarNumber() == HighestAll(last) then 1 else 0;
def lastvalue = if lastbar then (lastzhi + lastzlo) / 2 else Double.NaN;

plot hilowline1 = if bn < Max(lastzhibar, lastzlobar)
                  then Double.NaN
                  else if lastzlobar > lastzhibar and !IsNaN(zLO)
                  then zLO
                  else if lastzlobar < lastzhibar and !IsNaN(zHI)
                  then zHI else lastvalue;
hilowline1.EnableApproximation();
hilowline1.SetLineWeight(3);
hilowline1.SetDefaultColor(Color.WHITE);

def xxhigh  = if rh1 == high then high else xxhigh[1];
def xxlow   = if rl1 == low then low else xxlow[1];
def chghigh = (rh1 - xxlow[1]); # / xxlow[1] * 100;
def chglow  = (rl1 - xxhigh[1]);# / xxhigh[1] * 100;

def chghigh1 = (rh1[1] - xxlow[2]);
def chglow1  = (rl1[1] - xxhigh[2]);
def chghigh2 = (rh1[2] - xxlow[3]);
def chglow2  = (rl1[2] - xxhigh[3]);
def chghigh3 = (rh1[3] - xxlow[4]);
def chglow3  = (rl1[3] - xxhigh[4]);
def chghigh4 = (rh1[4] - xxlow[5]);
def chglow4  = (rl1[4] - xxhigh[5]);
def chghigh5 = (rh1[5] - xxlow[6]);
def chglow5  = (rl1[5] - xxhigh[6]);
def chghigh6 = (rh1[6] - xxlow[7]);
def chglow6  = (rl1[6] - xxhigh[7]);
def chghigh7 = (rh1[7] - xxlow[8]);
def chglow7  = (rl1[7] - xxhigh[8]);
def chghigh8 = (rh1[8] - xxlow[9]);
def chglow8  = (rl1[8] - xxhigh[9]);
def chghigh9 = (rh1[9] - xxlow[10]);
def chglow9  = (rl1[9] - xxhigh[10]);
def chghigh10 = (rh1[10] - xxlow[11]);
def chglow10  = (rl1[10] - xxhigh[11]);

def avg_length = 5;
def chg_avghigh = ((
chghigh + chghigh1 + chghigh2 + chghigh3 + chghigh4 + chghigh5
 #       + chghigh6 + chghigh7 + chghigh8 + chghigh9 + chghigh10
)/avg_length);

def chg_avglow = (((
chglow + chglow1 + chglow2 + chglow3 + chglow4 + chglow5
 # + chglow6 + chglow7 + chglow8 + chglow9 + chglow10
) /avg_length)  );
AddLabel(yes, "avg-high " + chg_avghigh, color.light_green);
AddLabel(yes, "avg-low " + chg_avglow, color.light_red);

#def avghi = average(zhi,avg_length);
#def avglow = average(zlo,avg_length);

AddChartBubble(!IsNaN(High1) and PointCount == 1, high, AsText(high) + " \n" +  Round(chghigh) + " \n" + "avg "+ (chg_avghigh) , Color.light_GREEN);
AddChartBubble(!IsNaN(Low1) and PointCount == -1, low, AsText(low) + " \n" +  Round(chglow) + " \n" + "avg "+ ((chg_avglow)) ,  Color.light_RED, no);
 
Last edited by a moderator:
Solution
adapted from: https://usethinkscript.com/threads/swing-high-low-indicator-for-thinkorswim.3109/page-5#post-96805

Ive tried to get the average of last 5 pivot change from low to high and high to low, but haven’t had luck
looking at the last 5 average should be 9.5

not sure if there is a way to do this but any help would be great

QqD84qc.png


Code:
def h = high;
def l = low;
def c = close;
def o = open;
def v = volume;

# Simple Fractal Recon
input length = 5;

def HH1 = h >= Highest(h, length) and h >= Highest(h, length)[-length];
def  High1 = if HH1 then h else Double.NaN;
def LL1 = l <= Lowest(l, length) and l <= Lowest(l, length)[-length];
def Low1 = if LL1 then l else Double.NaN;
def PointCount = if BarNumber() == 1 then...
adapted from: https://usethinkscript.com/threads/swing-high-low-indicator-for-thinkorswim.3109/page-5#post-96805

Ive tried to get the average of last 5 pivot change from low to high and high to low, but haven’t had luck
looking at the last 5 average should be 9.5

not sure if there is a way to do this but any help would be great

QqD84qc.png


Code:
def h = high;
def l = low;
def c = close;
def o = open;
def v = volume;

# Simple Fractal Recon
input length = 5;

def HH1 = h >= Highest(h, length) and h >= Highest(h, length)[-length];
def  High1 = if HH1 then h else Double.NaN;
def LL1 = l <= Lowest(l, length) and l <= Lowest(l, length)[-length];
def Low1 = if LL1 then l else Double.NaN;
def PointCount = if BarNumber() == 1 then 0 else
                 if IsNaN(c) then PointCount[1] else
                 if !IsNaN(High1) then Max(1, PointCount[1] + 1) else
                 if !IsNaN(Low1) then Min(-1, PointCount[1] - 1)
                 else PointCount[1];

input showhorizontals = no;

def RangeHI = if !IsNaN(High1) and PointCount == 1
then h else RangeHI[1];
plot Rhi = RangeHI;
Rhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rhi.SetLineWeight(1);
Rhi.SetDefaultColor(Color.RED);
Rhi.SetHiding(!showhorizontals);

def RangeLO = if !IsNaN(Low1) and PointCount == -1
then l else RangeLO[1];
plot Rlo =  RangeLO;
Rlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rlo.SetLineWeight(1);
Rlo.SetDefaultColor(Color.GREEN);
Rlo.SetHiding(!showhorizontals);


def rh1 = if !IsNaN(Rhi) then Rhi else rh1[1];
def rh2 = if rh1 != rh1[1] then rh1[1] else rh2[1];

def rl1 = if !IsNaN(Rlo) then Rlo else rl1[1];
def rl2 = if rl1 != rl1[1] then rl1[1] else rl2[1];

#ZigZags
def zHI;
def zLO;

zHI = if !IsNaN(High1) and PointCount == 1 then h else Double.NaN;
zLO = if !IsNaN(Low1) and PointCount == -1 then l else Double.NaN;

def bn = BarNumber();
def lastzhibar = if !IsNaN(zHI) then bn else lastzhibar[1];
def lastzlobar = if !IsNaN(zLO) then bn else lastzlobar[1];
def lastzhi    = if bn==highestall(lastzhibar) then h else lastzhi[1];
def lastzlo    = if bn==highestall(lastzlobar) then l else lastzlo[1];

plot hilowline = if !IsNaN(zHI) then zHI else zLO;
hilowline.EnableApproximation();
hilowline.SetLineWeight(3);
hilowline.SetDefaultColor(Color.WHITE);

def last = if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN;
def lastbar   = if BarNumber() == HighestAll(last) then 1 else 0;
def lastvalue = if lastbar then (lastzhi + lastzlo) / 2 else Double.NaN;

plot hilowline1 = if bn < Max(lastzhibar, lastzlobar)
                  then Double.NaN
                  else if lastzlobar > lastzhibar and !IsNaN(zLO)
                  then zLO
                  else if lastzlobar < lastzhibar and !IsNaN(zHI)
                  then zHI else lastvalue;
hilowline1.EnableApproximation();
hilowline1.SetLineWeight(3);
hilowline1.SetDefaultColor(Color.WHITE);

def xxhigh  = if rh1 == high then high else xxhigh[1];
def xxlow   = if rl1 == low then low else xxlow[1];
def chghigh = (rh1 - xxlow[1]); # / xxlow[1] * 100;
def chglow  = (rl1 - xxhigh[1]);# / xxhigh[1] * 100;

def chghigh1 = (rh1[1] - xxlow[2]);
def chglow1  = (rl1[1] - xxhigh[2]);
def chghigh2 = (rh1[2] - xxlow[3]);
def chglow2  = (rl1[2] - xxhigh[3]);
def chghigh3 = (rh1[3] - xxlow[4]);
def chglow3  = (rl1[3] - xxhigh[4]);
def chghigh4 = (rh1[4] - xxlow[5]);
def chglow4  = (rl1[4] - xxhigh[5]);
def chghigh5 = (rh1[5] - xxlow[6]);
def chglow5  = (rl1[5] - xxhigh[6]);
def chghigh6 = (rh1[6] - xxlow[7]);
def chglow6  = (rl1[6] - xxhigh[7]);
def chghigh7 = (rh1[7] - xxlow[8]);
def chglow7  = (rl1[7] - xxhigh[8]);
def chghigh8 = (rh1[8] - xxlow[9]);
def chglow8  = (rl1[8] - xxhigh[9]);
def chghigh9 = (rh1[9] - xxlow[10]);
def chglow9  = (rl1[9] - xxhigh[10]);
def chghigh10 = (rh1[10] - xxlow[11]);
def chglow10  = (rl1[10] - xxhigh[11]);

def avg_length = 5;
def chg_avghigh = ((
chghigh + chghigh1 + chghigh2 + chghigh3 + chghigh4 + chghigh5
 #       + chghigh6 + chghigh7 + chghigh8 + chghigh9 + chghigh10
)/avg_length);

def chg_avglow = (((
chglow + chglow1 + chglow2 + chglow3 + chglow4 + chglow5
 # + chglow6 + chglow7 + chglow8 + chglow9 + chglow10
) /avg_length)  );
AddLabel(yes, "avg-high " + chg_avghigh, color.light_green);
AddLabel(yes, "avg-low " + chg_avglow, color.light_red);

#def avghi = average(zhi,avg_length);
#def avglow = average(zlo,avg_length);

AddChartBubble(!IsNaN(High1) and PointCount == 1, high, AsText(high) + " \n" +  Round(chghigh) + " \n" + "avg "+ (chg_avghigh) , Color.light_GREEN);
AddChartBubble(!IsNaN(Low1) and PointCount == -1, low, AsText(low) + " \n" +  Round(chglow) + " \n" + "avg "+ ((chg_avglow)) ,  Color.light_RED, no);

i think does what you asked for


find the average of, the past x differences of, peak - valley
a label displays the average

can pick a quantity of peaks/valleys to average, near the last bar. default is 5
find the difference of those peaks - valleys and calculate an average.

adjust the valid valleys, so valley-peak pairs start with a valley. (if the last pivot is a valley, then adjust the valleys used.)

draw vertical lines on the valid peaks and valleys
show bubbles on the valid peaks and valleys
last peak bubble includes the average


Code:
#peaks_last_xdiffs_avg_00

#https://usethinkscript.com/threads/need-help-getting-avrage-of-pivot-high-low.14406/
# Need help getting average of x pivot high/low differences

def h = high;
def l = low;
def c = close;
def o = open;
def v = volume;

# Simple Fractal Recon
input length = 5;

def HH1 = h >= Highest(h, length) and h >= Highest(h, length)[-length];
def High1 = if HH1 then h else Double.NaN;
def LL1 = l <= Lowest(l, length) and l <= Lowest(l, length)[-length];
def Low1 = if LL1 then l else Double.NaN;
def PointCount = if BarNumber() == 1 then 0 else
                 if IsNaN(c) then PointCount[1] else
                 if !IsNaN(High1) then Max(1, PointCount[1] + 1) else
                 if !IsNaN(Low1) then Min(-1, PointCount[1] - 1)
                 else PointCount[1];

input showhorizontals = no;

def RangeHI = if !IsNaN(High1) and PointCount == 1
then h else RangeHI[1];
plot Rhi = RangeHI;
Rhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rhi.SetLineWeight(1);
Rhi.SetDefaultColor(Color.RED);
Rhi.SetHiding(!showhorizontals);

def RangeLO = if !IsNaN(Low1) and PointCount == -1
then l else RangeLO[1];
plot Rlo =  RangeLO;
Rlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rlo.SetLineWeight(1);
Rlo.SetDefaultColor(Color.GREEN);
Rlo.SetHiding(!showhorizontals);


def rh1 = if !IsNaN(Rhi) then Rhi else rh1[1];
def rh2 = if rh1 != rh1[1] then rh1[1] else rh2[1];

def rl1 = if !IsNaN(Rlo) then Rlo else rl1[1];
def rl2 = if rl1 != rl1[1] then rl1[1] else rl2[1];

#ZigZags
def zHI;
def zLO;

zHI = if !IsNaN(High1) and PointCount == 1 then h else Double.NaN;
zLO = if !IsNaN(Low1) and PointCount == -1 then l else Double.NaN;

def bn = BarNumber();
def lastzhibar = if !IsNaN(zHI) then bn else lastzhibar[1];
def lastzlobar = if !IsNaN(zLO) then bn else lastzlobar[1];
def lastzhi    = if bn==highestall(lastzhibar) then h else lastzhi[1];
def lastzlo    = if bn==highestall(lastzlobar) then l else lastzlo[1];

plot hilowline = if !IsNaN(zHI) then zHI else zLO;
hilowline.EnableApproximation();
hilowline.SetLineWeight(3);
hilowline.SetDefaultColor(Color.WHITE);

def last = if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN;
def lastbar   = if BarNumber() == HighestAll(last) then 1 else 0;
def lastvalue = if lastbar then (lastzhi + lastzlo) / 2 else Double.NaN;

plot hilowline1 = if bn < Max(lastzhibar, lastzlobar)
                  then Double.NaN
                  else if lastzlobar > lastzhibar and !IsNaN(zLO)
                  then zLO
                  else if lastzlobar < lastzhibar and !IsNaN(zHI)
                  then zHI else lastvalue;
hilowline1.EnableApproximation();
hilowline1.SetLineWeight(3);
hilowline1.SetDefaultColor(Color.WHITE);

def xxhigh  = if rh1 == high then high else xxhigh[1];
def xxlow   = if rl1 == low then low else xxlow[1];
def chghigh = (rh1 - xxlow[1]); # / xxlow[1] * 100;
def chglow  = (rl1 - xxhigh[1]);# / xxhigh[1] * 100;

def chghigh1 = (rh1[1] - xxlow[2]);
def chglow1  = (rl1[1] - xxhigh[2]);
def chghigh2 = (rh1[2] - xxlow[3]);
def chglow2  = (rl1[2] - xxhigh[3]);
def chghigh3 = (rh1[3] - xxlow[4]);
def chglow3  = (rl1[3] - xxhigh[4]);
def chghigh4 = (rh1[4] - xxlow[5]);
def chglow4  = (rl1[4] - xxhigh[5]);
def chghigh5 = (rh1[5] - xxlow[6]);
def chglow5  = (rl1[5] - xxhigh[6]);
def chghigh6 = (rh1[6] - xxlow[7]);
def chglow6  = (rl1[6] - xxhigh[7]);
def chghigh7 = (rh1[7] - xxlow[8]);
def chglow7  = (rl1[7] - xxhigh[8]);
def chghigh8 = (rh1[8] - xxlow[9]);
def chglow8  = (rl1[8] - xxhigh[9]);
def chghigh9 = (rh1[9] - xxlow[10]);
def chglow9  = (rl1[9] - xxhigh[10]);
def chghigh10 = (rh1[10] - xxlow[11]);
def chglow10  = (rl1[10] - xxhigh[11]);

def avg_length = 5;
def chg_avghigh = ((
chghigh + chghigh1 + chghigh2 + chghigh3 + chghigh4 + chghigh5
 #       + chghigh6 + chghigh7 + chghigh8 + chghigh9 + chghigh10
)/avg_length);

def chg_avglow = (((
chglow + chglow1 + chglow2 + chglow3 + chglow4 + chglow5
 # + chglow6 + chglow7 + chglow8 + chglow9 + chglow10
) /avg_length)  );
AddLabel(yes, "avg-high " + chg_avghigh, color.light_green);
AddLabel(yes, "avg-low " + chg_avglow, color.light_red);

#def avghi = average(zhi,avg_length);
#def avglow = average(zlo,avg_length);

input bubbles1 = no;
AddChartBubble(bubbles1 and !IsNaN(High1) and PointCount == 1, high, AsText(high) + " \n" +  Round(chghigh) + " \n" + "avg "+ (chg_avghigh) , Color.light_GREEN);
AddChartBubble(bubbles1 and !IsNaN(Low1) and PointCount == -1, low, AsText(low) + " \n" +  Round(chglow) + " \n" + "avg "+ ((chg_avglow)) ,  Color.light_RED, no);

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

input prev_qty_pv_diff_avg = 5;
def qty =  prev_qty_pv_diff_avg;

def pkcnt = if bn == 1 then 0 else if !isnan(zhi) then pkcnt[1] + 1 else pkcnt[1];
def valcnt = if bn == 1 then 0 else if !isnan(zlo) then valcnt[1] + 1 else valcnt[1];


def pkbn = if bn == 1 then 0 else if !isnan(zhi) then bn else pkbn[1];
def valbn = if bn == 1 then 0 else if !isnan(zlo) then bn else valbn[1];
def valadj = if valbn > pkbn then 1 else 0;

def maxpk = highestall(pkcnt);
def maxval = highestall(valcnt);

def enpk = if pkcnt >= (maxpk - qty + 1) then 1 else 0;
def enval = if valcnt >= (maxval - qty + 1 - valadj) and valcnt <= (maxval - valadj) then 1 else 0;

def pksum = if bn == 1 then 0 else if enpk and !isnan(zhi) then pksum[1] + zhi else pksum[1];
def valsum = if bn == 1 then 0 else if enval and !isnan(zlo) then valsum[1] + zlo else valsum[1];

def diffavg = (pksum-valsum)/qty;

addlabel(1, " ", color.black);
addlabel(1, diffavg + " avg for past " + qty + " valley to peak diffs", color.yellow);

input bubbles2 = yes;
AddChartBubble(bubbles2 and enpk and !isnan(zhi), high, AsText(high) + "\n" + pksum + "\n" + 
( if maxpk == pkcnt then ("avg " + diffavg) else ""), Color.light_GREEN);
AddChartBubble(bubbles2 and enval and !isnan(zlo), low, AsText(low) + "\n" + valsum,  Color.light_RED, no);


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

input test1 = yes;
addverticalline(test1 and enpk and !isnan(zhi), "pk", color.green);
addverticalline(test1 and enval and !isnan(zlo), "val", color.red);

def bub1 = !isnan(zhi) or !isnan(zlo);

input test2 = no;
addchartbubble(test2 and bub1, low,
#hh1 + "\n" +
#high1 + "\n" +
#ll1 + "\n" +
#low1
pkcnt + "\n" +
valcnt + "\n" +
zhi + "\n" +
zlo + "\n" +
pksum + "\n" +
valsum + "\n" +
diffavg
, color.yellow, no);
#

duQzMyP.jpg
 
Solution
@halcyonguy as always thank you. It’s very close I was trying to get the average of low to high on the high and average of high to low on the low


hmmm maybe this ver
it adds up peaks, and valleys and subtracts them. then adds up peaks and the valleys , from next valley over, add and /2.


Code:
#peaks_last_xdiffs_avg_01

#https://usethinkscript.com/threads/need-help-getting-avrage-of-pivot-high-low.14406/
#UnAnswered Need help getting avrage of pivot high/low
# Goingdark365  Feb 8, 2023

#adapted from: https://usethinkscript.com/threads/swing-high-low-indicator-for-thinkorswim.3109/page-5#post-96805

#Ive tried to get the average of last 5 pivot change from low to high and high to low, but haven’t had luck
#looking at the last 5 average should be 9.5

#not sure if there is a way to do this but any help would be great



def h = high;
def l = low;
def c = close;
def o = open;
def v = volume;

# Simple Fractal Recon
input length = 5;

def HH1 = h >= Highest(h, length) and h >= Highest(h, length)[-length];
def High1 = if HH1 then h else Double.NaN;
def LL1 = l <= Lowest(l, length) and l <= Lowest(l, length)[-length];
def Low1 = if LL1 then l else Double.NaN;
def PointCount = if BarNumber() == 1 then 0 else
                 if IsNaN(c) then PointCount[1] else
                 if !IsNaN(High1) then Max(1, PointCount[1] + 1) else
                 if !IsNaN(Low1) then Min(-1, PointCount[1] - 1)
                 else PointCount[1];

input showhorizontals = no;

def RangeHI = if !IsNaN(High1) and PointCount == 1
then h else RangeHI[1];
plot Rhi = RangeHI;
Rhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rhi.SetLineWeight(1);
Rhi.SetDefaultColor(Color.RED);
Rhi.SetHiding(!showhorizontals);

def RangeLO = if !IsNaN(Low1) and PointCount == -1
then l else RangeLO[1];
plot Rlo =  RangeLO;
Rlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rlo.SetLineWeight(1);
Rlo.SetDefaultColor(Color.GREEN);
Rlo.SetHiding(!showhorizontals);


def rh1 = if !IsNaN(Rhi) then Rhi else rh1[1];
def rh2 = if rh1 != rh1[1] then rh1[1] else rh2[1];

def rl1 = if !IsNaN(Rlo) then Rlo else rl1[1];
def rl2 = if rl1 != rl1[1] then rl1[1] else rl2[1];

#ZigZags
def zHI;
def zLO;

zHI = if !IsNaN(High1) and PointCount == 1 then h else Double.NaN;
zLO = if !IsNaN(Low1) and PointCount == -1 then l else Double.NaN;

def bn = BarNumber();
def lastzhibar = if !IsNaN(zHI) then bn else lastzhibar[1];
def lastzlobar = if !IsNaN(zLO) then bn else lastzlobar[1];
def lastzhi    = if bn==highestall(lastzhibar) then h else lastzhi[1];
def lastzlo    = if bn==highestall(lastzlobar) then l else lastzlo[1];

plot hilowline = if !IsNaN(zHI) then zHI else zLO;
hilowline.EnableApproximation();
hilowline.SetLineWeight(3);
hilowline.SetDefaultColor(Color.WHITE);

def last = if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN;
def lastbar   = if BarNumber() == HighestAll(last) then 1 else 0;
def lastvalue = if lastbar then (lastzhi + lastzlo) / 2 else Double.NaN;

plot hilowline1 = if bn < Max(lastzhibar, lastzlobar)
                  then Double.NaN
                  else if lastzlobar > lastzhibar and !IsNaN(zLO)
                  then zLO
                  else if lastzlobar < lastzhibar and !IsNaN(zHI)
                  then zHI else lastvalue;
hilowline1.EnableApproximation();
hilowline1.SetLineWeight(3);
hilowline1.SetDefaultColor(Color.WHITE);

def xxhigh  = if rh1 == high then high else xxhigh[1];
def xxlow   = if rl1 == low then low else xxlow[1];
def chghigh = (rh1 - xxlow[1]); # / xxlow[1] * 100;
def chglow  = (rl1 - xxhigh[1]);# / xxhigh[1] * 100;

def chghigh1 = (rh1[1] - xxlow[2]);
def chglow1  = (rl1[1] - xxhigh[2]);
def chghigh2 = (rh1[2] - xxlow[3]);
def chglow2  = (rl1[2] - xxhigh[3]);
def chghigh3 = (rh1[3] - xxlow[4]);
def chglow3  = (rl1[3] - xxhigh[4]);
def chghigh4 = (rh1[4] - xxlow[5]);
def chglow4  = (rl1[4] - xxhigh[5]);
def chghigh5 = (rh1[5] - xxlow[6]);
def chglow5  = (rl1[5] - xxhigh[6]);
def chghigh6 = (rh1[6] - xxlow[7]);
def chglow6  = (rl1[6] - xxhigh[7]);
def chghigh7 = (rh1[7] - xxlow[8]);
def chglow7  = (rl1[7] - xxhigh[8]);
def chghigh8 = (rh1[8] - xxlow[9]);
def chglow8  = (rl1[8] - xxhigh[9]);
def chghigh9 = (rh1[9] - xxlow[10]);
def chglow9  = (rl1[9] - xxhigh[10]);
def chghigh10 = (rh1[10] - xxlow[11]);
def chglow10  = (rl1[10] - xxhigh[11]);

def avg_length = 5;
def chg_avghigh = ((
chghigh + chghigh1 + chghigh2 + chghigh3 + chghigh4 + chghigh5
 #       + chghigh6 + chghigh7 + chghigh8 + chghigh9 + chghigh10
)/avg_length);

def chg_avglow = (((
chglow + chglow1 + chglow2 + chglow3 + chglow4 + chglow5
 # + chglow6 + chglow7 + chglow8 + chglow9 + chglow10
) /avg_length)  );

#AddLabel(yes, "avg-high " + chg_avghigh, color.light_green);
#AddLabel(yes, "avg-low " + chg_avglow, color.light_red);

#def avghi = average(zhi,avg_length);
#def avglow = average(zlo,avg_length);

input bubbles1 = no;
AddChartBubble(bubbles1 and !IsNaN(High1) and PointCount == 1, high, AsText(high) + " \n" +  Round(chghigh) + " \n" + "avg "+ (chg_avghigh) , Color.light_GREEN);
AddChartBubble(bubbles1 and !IsNaN(Low1) and PointCount == -1, low, AsText(low) + " \n" +  Round(chglow) + " \n" + "avg "+ ((chg_avglow)) ,  Color.light_RED, no);

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

def na = double.nan;

input prev_qty_pv_diff_avg = 5;
def qty =  prev_qty_pv_diff_avg;

def pkcnt = if bn == 1 then 0 else if !isnan(zhi) then pkcnt[1] + 1 else pkcnt[1];
def valcnt = if bn == 1 then 0 else if !isnan(zlo) then valcnt[1] + 1 else valcnt[1];


def pkbn = if bn == 1 then 0 else if !isnan(zhi) then bn else pkbn[1];
def valbn = if bn == 1 then 0 else if !isnan(zlo) then bn else valbn[1];
def valadj = if valbn > pkbn then 1 else 0;

def maxpk = highestall(pkcnt);
def maxval = highestall(valcnt);

def enpk = if pkcnt >= (maxpk - qty + 1) then 1 else 0;
def enval1 = if valcnt >= (maxval - qty + 1 - valadj) and valcnt <= (maxval - valadj) then 1 else 0;
def enval2 = if valcnt >= (maxval - qty + 1 ) and valcnt <= maxval then 1 else 0;

#plot ff = if enval2 then low*0.99 else na;


def pksum = if bn == 1 then 0 else if enpk and !isnan(zhi) then pksum[1] + zhi else pksum[1];
def valsum1 = if bn == 1 then 0 else if enval1 and !isnan(zlo) then valsum1[1] + zlo else valsum1[1];
def valsum2 = if bn == 1 then 0 else if enval2 and !isnan(zlo) then valsum2[1] + zlo else valsum2[1];

# valley to peak diff avg
#def diffavg = (((pksum-valsum1)/qty);
#  avg the diff going up to peak then coming back down to a valley , for x peaks
#  (valley to peak diff avg + peak to valley diff avg ) / 2
def diffavg = (((pksum-valsum1)/qty) + ((pksum-valsum2)/qty))/2;


addlabel(1, " ", color.black);
addlabel(1, diffavg + " avg for past " + qty + " valley to peak diffs", color.yellow);

input bubbles2 = no;
AddChartBubble(bubbles2 and enpk and !isnan(zhi), high, AsText(high) + "\n" + pksum , Color.light_GREEN, yes);

AddChartBubble(bubbles2 and enval1 and !isnan(zlo), low, AsText(low) + "\n" + valsum1,  Color.light_RED, no);

# sums up to last peak, wrong
#AddChartBubble(bubbles2 and enpk and !isnan(zhi), high, ( if maxpk == pkcnt then ("avg " + diffavg) else ""), Color.light_GREEN, yes);

# avg up to last valley
input avg_bubble = no;
AddChartBubble(avg_bubble and  maxval == valcnt and !isnan(zlo), low, 
#AddChartBubble(bubbles2 and enval2, low, 
( if maxval == valcnt then ("avg " + diffavg) else "") + "\n"
# +
#maxval + " mv\n" +
#valcnt + " v"
, Color.light_REd, no);


addchartbubble(0 and enval2 , low,
"v"
, color.yellow, no);


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

input test1 = yes;
addverticalline(test1 and enpk and !isnan(zhi), "pk", color.green);
addverticalline(test1 and enval1 and !isnan(zlo), "val", color.red);

def bub1 = !isnan(zhi) or !isnan(zlo);

input test2 = no;
addchartbubble(test2 and bub1, low,
#hh1 + "\n" +
#high1 + "\n" +
#ll1 + "\n" +
#low1
pkcnt + "\n" +
valcnt + "\n" +
zhi + "\n" +
zlo + "\n" +
pksum + "\n" +
valsum1 + "\n" +
diffavg
, color.yellow, no);

#
 

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