Goingdark365
Member
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
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
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: